Skip to main content

Contexts

A context object encapsulates everything related to the current request. A context can take multiple shapes depending on the operation being handled.

Shared Properties

By default, all context objects inherits from these properties;

credentials

The parsed credentials associated with the request through the X-Unito-Credentials header. Will contain the keys for the variables defined in the corresponding configuration's authorization. Note that unauthenticated endpoints don't have access to that property.

signal

An AbortSignal instantiated with the X-Unito-Operation-Deadline header. This header contains the timestamp after which Unito will consider the operation to be timed out. You can use this signal to abort any operation that would exceed this time frame.

logger

The logger pre decorated with the correlation ID and the additionnal metadata provided through the request headers.

params

The params extracted from the provided path. Given a path defined as /users/:userId/notes/:noteId, a call to /users/123/notes/456 will yield the following;

{
userId: '123',
noteId: '456'
}

This is a generic property that can be narrowed down on a per handler basis.

const context: GetItemContext<{ foo: string }, {}>) {
// `context.params.foo` readily available as a string.
}

query

The raw query string parsed as an object. All values are strings. A call to /?page=2 will yield the following;

{
page: '2',
}

This is a generic property that can be narrowed down on a per handler basis.

const context: GetItemContext<{}, { limit?: string, offset?: string }>) {
// `context.query.limit` and `context.query.offset` readily available as potentially undefined strings.
}

Specific Properties

In addition to the above shared properties, some handlers receive some more specialized properties.

filters

Available with the GetCollectionContext. Returns the parsed filter query param, as defined per the specification. Given /items?filter=name=John,department.name=R&D, the filters property would yield the following;

[
{ field: 'name', operator: 'EQUAL', values: ['John'] },
{ field: 'department.name', operator: 'EQUAL', values: ['Engineering'] }
]

selects

Available with the GetCollectionContext. Returns the parsed select query param, as defined per the specification. Given /items?select=name,department.name, the selects property would yield the following;

['name', 'department.name']

body

Available on all contexts associated with handlers expecting an incoming payload, such as CreateItemHandler and UpdateItemHandler, the body property exposes the body of the incoming request. This is a generic property that can be narrowed down on a per handler basis.

export type ItemType = {
id: string;
name: string;
address: {
street: string;
doorNumber: string;
city: string
}
};

const context: CreateItemContext<{}, {}, ItemType>) {
// `context.body` is now available and typed as ItemType
}

Types

Context

Base Context type listing fields available in all the specific contexts. Provides access to the Credentials, an initialized logger, the request's params and the request's query (both of the later can be typed through the context's generics).

type Context<P, Q> = {
credentials: Credentials;
logger: Logger;
params: P;
query: Q;
}

GetItemContext

The GetItemContext has access to the base context properties only.

GetCollectionContext

The GetCollectionContext provides access to the filters and selects that are parse from the queryParams for you, in addition to the base context properties.

export type GetCollectionContext<P, Q> = Context<P, Q> & {
filters: Filter[];
selects: string[];
};

CreateItemContext

The CreateItemContext provides access to the request's body (can be typed through the context's generics).

export type CreateItemContext<P, Q, B extends API.CreateItemRequestPayload>
= Context<P, Q> & { body: B };

UpdateItemContext

The UpdateItemContext also provides access to the request's body (can be typed through the context's generics).

export type UpdateItemContext<P, Q, B extends API.UpdateItemRequestPayload>
= Context<P, Q> & { body: B };

DeleteContext

The DeleteItemContext has access to the base context properties only.

GetCredentialAccountContext

The GetItemContext has access to the base context properties only.

ParseWebhooksContext

The ParseWebhooksContext provides access to the request's body (can be typed through the context's generics), and omit the Credentials since the call is unauthenticated.

export type ParseWebhooksContext<P, Q, B extends API.WebhookParseRequestPayload>
= Omit<Context<P, Q>, 'credentials'> & { body: B };

UpdateWebhookSubscriptionsContext

The UpdateWebhookSubscriptionsContext provides access to the request's body (can be typed through the context's generics).

export type UpdateWebhookSubscriptionsContext<P, Q, B extends API.WebhookSubscriptionRequestPayload>
= Context<P, Q> & { body: B };

AcknowledgeWebhooksContext

The AcknowledgeWebhooksContext provides access to the request's body (can be typed through the context's generics), and omit the Credentials since the call is unauthenticated.

export type AcknowledgeWebhooksContext<P, Q, B extends API.WebhookParseRequestPayload>
= Omit<Context<P, Q>, 'credentials'> & { body: B };