workduck-io / dwindle

!Amplify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

!Amplify


Languages

Language:TypeScript 98.5%Language:HTML 1.5%