7.4.1 • Published 7 months ago

@gasket/request v7.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

@gasket/request

The purpose of this package is to provide a consistent request object for Gasket plugins and apps to use, regardless of the request handling framework.

Installation

Existing apps

npm install @gasket/request

Functions

GasketRequest

The GasketRequest class is a representation of a request object that can be used by Gasket plugins and apps. It is a consistent shape that can be used across different request handling frameworks.

A GasketRequest object has the following properties:

PropertyTypeDescriptionArguments
headersobjectRequest headersrequired
cookiesobjectRequest cookiesdefault: {}
queryobjectQuery parametersdefault: {}
pathstringRequest pathdefault: ''

makeGasketRequest

A GasketRequest can be created from a Node IncomingMessage object, an Express Request object, or a Next.js NextRequest object, amongst others.

import { makeGasketRequest } from '@gasket/request';

export default async function expressHandler(req, res) {
  const gasketRequest = await makeGasketRequest(req);
  // use gasketRequest
}

You can also assemble a GasketRequest object from parts of a request object.

import { makeGasketRequest } from '@gasket/request';

const headers = {
  'x-example': 'example'
};

const staticGasketRequest = await makeGasketRequest({ headers });

withGasketRequest

A higher-order function that can wrap a GasketAction function and provides a GasketRequest object as the first argument. This provides an easy way to normalize the request object for any GasketAction and potential lifecycle hooks.

import { withGasketRequest } from '@gasket/request';

export const myAction = withGasketRequest(
  async function handler(gasket, gasketRequest) {
    // use gasketRequest
  }
);

// example usage
await gasket.actions.myAction(req);

If your action needs additional arguments, you can pass them following the gasketReq.

import { withGasketRequest } from '@gasket/request';

export const myAction = withGasketRequest(
  async function handler(gasket, gasketRequest, arg1, arg2) {
    // use gasketRequest
  }
);

// example usage
await gasket.actions.myAction(req, true, false);

withGasketRequestCache

Much like the previous function, withGasketRequestCache is a higher-order function that wraps an action handler function and provides a GasketRequest. The difference is the handler will only be triggered once per request, and the result will be cached for subsequent calls.

import { withGasketRequestCache } from '@gasket/request';

export const myAction = withGasketRequestCache(
  async function handler(gasket, gasketRequest, arg1, arg2) {
    // use gasketRequest
  }
);

// example usage
await gasket.actions.myAction(req, true, false);

Purpose

Why do we need a request object for Gasket?

Some Gasket plugins and apps need to interact with the request object. Unfortunately, the shape and details of a request object is not consistent across all request handling frameworks.

For example:

  • Node's IncomingMessage docs
  • Express "enhances" this as Request docs
    • Various other engines also do the same
  • Next.js API's (including middleware) use NextRequest docs
  • This extends the browser-compatible Fetch Request docs
  • Next.js App Router does not expose the request object, so we make a representation with the parts available docs

And we are aware of apps using Gasket for Static Pages with Next.js, assembling a request-like object to use with certain actions.

7.4.1

7 months ago

7.4.0

8 months ago

7.3.6

8 months ago

7.3.5

8 months ago

7.3.4

10 months ago

7.3.3

10 months ago

7.3.2

10 months ago

7.3.1

10 months ago

7.3.0

10 months ago

7.2.3

11 months ago

7.2.0

12 months ago

7.1.3

12 months ago

7.1.0

1 year ago

7.1.0-next.7

1 year ago

7.1.0-next.6

1 year ago

7.1.0-next.5

1 year ago

7.1.0-next.4

1 year ago

7.1.0-next.3

1 year ago

7.1.0-next.2

1 year ago

7.1.0-next.1

1 year ago

7.1.0-next.0

1 year ago

7.1.0-canary.3

1 year ago

7.1.0-canary.2

1 year ago

7.1.0-canary.1

1 year ago