Skip to main content

CorsFramework

@h4ad/serverless-adapter > CorsFramework

(class) CorsFramework

The framework that handles cors for your api without relying on internals of the framework

Signature:

export declare class CorsFramework<TApp> implements FrameworkContract<TApp> 

Implements: FrameworkContract <TApp>

Example

import express from 'express';
import \{ ServerlessAdapter } from '@h4ad/serverless-adapter';
import \{ ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
import \{ CorsFramework } from '@h4ad/serverless-adapter/lib/frameworks/cors';

const expressFramework = new ExpressFramework();
const options: CorsOptions = \{}; // customize the options
const framework = new CorsFramework(expressFramework, options);

export const handler = ServerlessAdapter.new(null)
.setFramework(framework)
// set other configurations and then build
.build();

(constructor)

Default Constructor

Signature:

constructor(framework: FrameworkContract<TApp>, options?: CorsFrameworkOptions | undefined);

Parameters

ParameterTypeDescription
frameworkFrameworkContract <TApp>
optionsCorsFrameworkOptions | undefined(Optional)

(property) cachedCorsInstance

The cached instance of cors

Signature:

protected readonly cachedCorsInstance: ReturnType<typeof cors>;

(property) corsHeaders

All cors headers that can be added by cors package

Signature:

protected readonly corsHeaders: string[];

(method) formatHeaderValuesAddedByCorsPackage

Format the headers to be standardized with the rest of the library, such as ApiGatewayV2. Also, some frameworks don't support headers as an array, so we need to format the values.

Signature:

protected formatHeaderValuesAddedByCorsPackage(response: ServerResponse): void;

Parameters

ParameterTypeDescription
responseServerResponse

Returns:

void

(property) framework

Signature:

protected readonly framework: FrameworkContract<TApp>;

(method) isInvalidOriginOrMethodIsNotAllowed

Check if the origin is invalid or if the method is not allowed. Highly inspired by [access-control](https://github.com/primus/access-control/blob/master/index.js\#L95-L115)

Signature:

protected isInvalidOriginOrMethodIsNotAllowed(request: IncomingMessage, allowedOrigin: number | string | string[] | undefined): boolean;

Parameters

ParameterTypeDescription
requestIncomingMessage
allowedOriginnumber | string | string[] | undefined

Returns:

boolean

(method) onCorsNext

Handle next execution called by the cors package

Signature:

protected onCorsNext(app: TApp, request: IncomingMessage, response: ServerResponse): () => void;

Parameters

ParameterTypeDescription
appTApp
requestIncomingMessage
responseServerResponse

Returns:

() => void

(property) options

Signature:

protected readonly options?: CorsFrameworkOptions | undefined;

(method) sendRequest

Signature:

sendRequest(app: TApp, request: IncomingMessage, response: ServerResponse): void;

Parameters

ParameterTypeDescription
appTApp
requestIncomingMessage
responseServerResponse

Returns:

void