1.1.1 • Published 9 months ago

@askeladden/hasura-utils v1.1.1

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

Hasura Utils

Helper functions to ease the development with Hasura events and actions

  • Validate the hasura client signature (api client-id)
  • Validate the input data
  • Handle the input data
  • Handle errors
  • Return the expected response

Basic usage:

import hasura-utils from '@askeladden/hasura-utils';

Required parameters

const signatureConfig: RequestSignatureConfig = {
  signatureSecret: '<signature-secret>',
  signatureHeader: '<valid-signature-header>',
};

<'signature-secret'> is the client-id secret defined in the Hasura Action
<'valid-signature-header'> is the signature header defined in the Hasura action ('X-Hasura-Signature')

const payloadHandler: GenericRequestHandler<HasuraActionRequest, HasuraActionResponse>

HasuraActionRequest is the type of the body in the Hasura Action request
HasuraActionResponse is the type Hasura Action expects in the response

Optional parameters

The validationHandler is optional but can be used to validate input data.
There's even a helper function for using Zod schemas.

const schema: z.object({
    value: z.string()
})

const validationHandler = createZodSchemaHandler<HasuraActionRequest>(schema);

Basic usage of the action handler

export default (req: HttpRequest, res: HttpResponse): async () => {
await hasuraActionHandler<HasuraActionInput, HasuraActionResponse>({
      config: signatureConfig,
      payloadHandler: payloadHandler,
      validationHandler?: validationHandler
    })(request, response);
}

Basic usage of the event handler

export default (req: HttpRequest, res: HttpResponse): async () => {
await hasuraEventHandler<HasuraEventInput, HasuraEvetResponse>({
      config: signatureConfig,
      payloadHandler: payloadHandler,
      validationHandler?: validationHandler
    })(request, response);
}
1.1.1

9 months ago

1.1.0

11 months ago

1.0.1

1 year ago

1.0.0

1 year ago