Giter Club home page Giter Club logo

dwindle's Introduction

Dwindle

A simple http client for AWS services.

Currently covered services:

  • Cognito http

Planned:

  • S3

Usage

Dwindle exposes 2 main concepts:

The useAuth hook

This is a react hook, that exposes the following methods:

[
    initCognito,
    signIn,
    signUp,
    verifySignUp,
    resendCode,
    forgotPassword,
    verifyForgotPassword,
    getUserDetails,
    changePassword,
    signOut,
    refreshToken,
    userCred,
    getConfig,
    googleSignIn,
    updateUserAttributes,
    userAddWorkspace,
    refreshIdentityPoolCreds,
    uploadImageToS3,
]

These methods call the amazon-cognito-identity-js package that calls cognito APIs internally. It is used to sign-in, sign-out, create a new user etc in the frontend clients. The hook manages state using the useAuthStore zustand store.

It is used normally like a hook. For example, to sign in, we use it as follows:

import { useAuth } from '@workduck-io/dwindle'

const useAuth = () => {
    const { signIn } = useAuth()

    const login = async (email: string, password: string): Promise<LoginResult> => {
        const loginResult = await signIn(email, password)
            .then((d) => {
                return { loginStatus: 'success', loginData: d }
            })
            .catch((e) => {
                console.error({ e })
                return { loginStatus: e.toString(), loginData: {} as UserCred }
            })
        return loginResult
    }

    return { login }
}

The KYClient Class

We need to make API Calls to different services, while setting the Authorization and mex-workspace-id headers since the backend expects them. The KYClient class provides a way of making these calls, and handles authorization, refreshing token on expiry etc.

It can be used as follows:

const kyClient = new KYClient()

const data = await kyClient.get('https://reddit.com/r/aww.json')

// To set API Header, which is needed for Workduck API Calls, use the following:
kyClient.setWorkspaceHeader('WORKSPACE_123')
// This ensures that any request sent will include the `mex-workspace-id` header set to `WORKSPACE_123`

// The KY Client supports HTTP Methods - GET, POST, PUT, PATCH, DELETE
kyClient.post(url, data, options)
kyClient.put(url, data, options)
kyClient.patch(url, data, options)
kyClient.delete(url, data, options)

// Options is of type `Options` from `ky/distribution/types`

// For GET requests, we support an additional parameter: cacheConfig
kyClient.get(url, cacheConfig, options)
// If it is passed, a request is cancelled if it is found to be made again before expiry. The expected type is:

interface CacheConfig {
  enabled: boolean // Whether to check this request against cache or not
  expiry: number // Duration to cache in milliseconds
}

The below docs were generated by ChatGPT so treat with caution

This code defines a KYClient class that provides a wrapper for the ky HTTP client library. The KYClient class has methods for making HTTP requests with the ky client, including GET, POST, PATCH, DELETE, and PUT. Additionally, the class has methods for setting the workspace header and clearing the cache. The class takes in optional kyOptions and kyClient objects, which are used to create an instance of the ky client. The instance of the ky client is stored in the _client property of the KYClient class.

The KYClient class has several private methods that are used as hooks for the ky client. These include a _attachTokenAndRequestIDHook method that adds the user's authorization token and a request ID to the request headers, a _attachWorkspaceIDHook method that adds the workspace ID to the request headers, and a _refreshTokenHook method that refreshes the user's authorization token if it has expired. The KYClient class also has a _urlHash object that is used to store the URLs that have been cached and the time at which they were cached.

For more fun documentation: https://chat.openai.com/chat

Note on React dependency

You may need to link react of the example app to the dwindle package via yarn link or npm link, as React requires only a single instance.

dwindle's People

Contributors

xypnox avatar rishivikramn avatar rppanda avatar dependabot[bot] avatar sahil-shubham avatar vivekrj0 avatar dineshsingh1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.