@h4ad/serverless-adapter
Version:
Run REST APIs and other web applications using your existing Node.js application framework (NestJS, Express, Koa, Hapi, Fastify and many others), on top of AWS, Azure, Digital Ocean and many other clouds.
366 lines (360 loc) • 10.8 kB
TypeScript
import { B as BothValueHeaders, M as MultiValueHeaders } from '../../headers-DjfGHDmI.js';
import { A as AdapterContract, a as AdapterRequest, G as GetResponseAdapterProps, O as OnErrorProps } from '../../adapter.contract-FnS-TPRj.js';
import 'http';
import 'node:http';
import '../../logger-F8qccesk.js';
/**
* The return value of {@link HuaweiContext} getRequestID
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetRequestIDSecondsReturn = string;
/**
* The return value of {@link HuaweiContext} getRemainingTimeInMilliSeconds
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetRemainingTimeInMilliSecondsReturn = number;
/**
* The return value of {@link HuaweiContext} getAccessKey
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetAccessKeyReturn = string;
/**
* The return value of {@link HuaweiContext} getSecretKey
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetSecretKeyReturn = string;
/**
* The parameters of the method {@link HuaweiContext} getUserData
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetUserDataKeyParameter = string;
/**
* The return value of {@link HuaweiContext} getUserData
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetUserDataReturn = any;
/**
* The return value of {@link HuaweiContext} getFunctionName
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetFunctionNameReturn = string;
/**
* The return value of {@link HuaweiContext} getRunningTimeInSeconds
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetRunningTimeInSecondsReturn = number;
/**
* The return value of {@link HuaweiContext} getVersion
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetVersionReturn = string;
/**
* The return value of {@link HuaweiContext} getMemorySize
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetMemorySizeReturn = number;
/**
* The return value of {@link HuaweiContext} getCPUNumber
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetCPUNumberReturn = number;
/**
* The return value of {@link HuaweiContext} getProjectID
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetProjectIdReturn = number;
/**
* The return value of {@link HuaweiContext} getPackage
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetPackageReturn = string;
/**
* The return value of {@link HuaweiContext} getToken
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetTokenReturn = string;
/**
* The return value of {@link HuaweiContext} getLogger
*
* Is the instance of logger that can be used to send logs to
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
type GetLoggerReturn = {
info(message: string): void;
};
/**
* The interface that represents methods sent by huawei to get information about the function graph.
* See more in {@link https://support.huaweicloud.com/intl/en-us/devg-functiongraph/functiongraph_02_0410.html#section1 | Context Methods}
*
* @public
* @breadcrumb Types / Huawei / HuaweiContext
*/
interface HuaweiContext {
/**
* Obtains a request ID.
*/
getRequestID(): GetRequestIDSecondsReturn;
/**
* Obtains the remaining running time of a function.
*/
getRemainingTimeInMilliSeconds(): GetRemainingTimeInMilliSecondsReturn;
/**
* Obtains the AK (valid for 24 hours) of an agency. If you use this method, you need to configure an agency for the function.
*/
getAccessKey(): GetAccessKeyReturn;
/**
* Obtains the SK (valid for 24 hours) of an agency. If you use this method, you need to configure an agency for the function.
*/
getSecretKey(): GetSecretKeyReturn;
/**
* Uses keys to obtain the values passed by environment variables.
*
* @param key - The key to get environment variables values
*/
getUserData(key: GetUserDataKeyParameter): GetUserDataReturn;
/**
* Obtains the name of a function.
*/
getFunctionName(): GetFunctionNameReturn;
/**
* Obtains the timeout of a function.
*/
getRunningTimeInSeconds(): GetRunningTimeInSecondsReturn;
/**
* Obtains the version of a function.
*/
getVersion(): GetVersionReturn;
/**
* Obtains the allocated memory.
*/
getMemorySize(): GetMemorySizeReturn;
/**
* Number of CPU millicores used by the function (1 core = 1000 millicores).
*
* The value of this field is proportional to that of MemorySize. By default, 100 CPU millicores are required for 128 MB memory. The number of CPU millicores is calculated as follows: Memory/128 x 100 + 200 (basic CPU millicores).
*/
getCPUNumber(): GetCPUNumberReturn;
/**
* Obtains a project ID.
*/
getProjectID(): GetProjectIdReturn;
/**
* Obtains a function group, that is, an app.
*/
getPackage(): GetPackageReturn;
/**
* Obtains the token (valid for 24 hours) of an agency. If you use this method, you need to configure an agency for the function.
*/
getToken(): GetTokenReturn;
/**
* Obtains the logger method provided by the context and returns a log output class. Logs are output in the format of Time-Request ID-Content by using the info method.
*
* For example, use the info method to output logs:
*
* @example
* ```typescript
* logg = context.getLogger()
*
* logg.info("hello")
* ```
*/
getLogger(): GetLoggerReturn;
}
/**
* The interface that represents the Api Gateway Event of Huawei when integrate with Function Graph of Event Type.
* See more in {@link https://support.huaweicloud.com/intl/en-us/devg-functiongraph/functiongraph_02_0102.html#functiongraph_02_0102__li5178638110137 | Reference}.
*
* @public
* @breadcrumb Types / Huawei / HuaweiApiGatewayEvent
*/
interface HuaweiApiGatewayEvent {
/**
* The body value with the content of this event serialized in JSON
*/
body: string;
/**
* The headers of the request which this event represents
*/
headers: BothValueHeaders;
/**
* The HTTP Method of the request which this event represents
*/
httpMethod: string;
/**
* Tells if the body is base64 encoded
*/
isBase64Encoded: boolean;
/**
* The path of the request which this event represents
*/
path: string;
/**
* The path parameters of the request which this event represents
*/
pathParameters: HuaweiRequestPathParameters;
/**
* The query strings of the request which this event represents
*/
queryStringParameters: HuaweiRequestQueryStringParameters;
/**
* The request context with information about the stage, api and requestId
*/
requestContext: HuaweiRequestContext;
/**
* It can have more properties that I could not discover yet
*/
[key: string]: any;
}
/**
* The path parameters of the request, usually is the name of the wildcard you create in FunctionGraph, such as /\{proxy\}.
*
* @public
* @breadcrumb Types / Huawei / HuaweiApiGatewayEvent
*/
type HuaweiRequestPathParameters = Record<string, string>;
/**
* The query strings of the request
*
* @public
* @breadcrumb Types / Huawei / HuaweiApiGatewayEvent
*/
type HuaweiRequestQueryStringParameters = Record<string, string | string[]>;
/**
* The interface that represents the values you can get inside request context.
*
* @public
* @breadcrumb Types / Huawei / HuaweiApiGatewayEvent
*/
interface HuaweiRequestContext {
/**
* The ID of your API inside Api Gateway
*/
apiId: string;
/**
* The ID of this request
*/
requestId: string;
/**
* The name of the stage running this Function Graph
*/
stage: string;
}
/**
* The interface that represents the Api Gateway Response of Huawei when integrate with Function Graph of Event Type.
* See more in {@link https://support.huaweicloud.com/intl/en-us/devg-functiongraph/functiongraph_02_0102.html#functiongraph_02_0102__li5178638110137 | Reference}.
*
* @public
* @breadcrumb Types / Huawei / HuaweiApiGatewayResponse
*/
interface HuaweiApiGatewayResponse {
/**
* Tells if the body was encoded as base64
*/
isBase64Encoded: boolean;
/**
* The HTTP Status code of this response
*/
statusCode: number;
/**
* The headers sent with this response
*/
headers: MultiValueHeaders;
/**
* The body value with the content of this response serialized in JSON
*/
body: string;
}
/**
* The options to customize the {@link HuaweiApiGatewayAdapter}
*
* @breadcrumb Adapters / Huawei / HuaweiApiGatewayAdapter
* @public
*/
interface HuaweiApiGatewayOptions {
/**
* Strip base path for custom domains
*
* @defaultValue ''
*/
stripBasePath?: string;
}
/**
* The adapter to handle requests from Huawei Api Gateway
*
* @example
* ```typescript
* const stripBasePath = '/any/custom/base/path'; // default ''
* const adapter = new ApiGatewayAdapter({ stripBasePath });
* ```
*
* @breadcrumb Adapters / Huawei / HuaweiApiGatewayAdapter
* @public
*
* {@link https://support.huaweicloud.com/intl/en-us/devg-functiongraph/functiongraph_02_0102.html#functiongraph_02_0102__li5178638110137 | Event Reference}
*/
declare class HuaweiApiGatewayAdapter implements AdapterContract<HuaweiApiGatewayEvent, HuaweiContext, HuaweiApiGatewayResponse> {
protected readonly options?: HuaweiApiGatewayOptions | undefined;
/**
* Default constructor
*
* @param options - The options to customize the {@link HuaweiApiGatewayAdapter}
*/
constructor(options?: HuaweiApiGatewayOptions | undefined);
/**
* {@inheritDoc}
*/
getAdapterName(): string;
/**
* {@inheritDoc}
*/
canHandle(event: unknown): event is HuaweiApiGatewayEvent;
/**
* {@inheritDoc}
*/
getRequest(event: HuaweiApiGatewayEvent): AdapterRequest;
/**
* {@inheritDoc}
*/
getResponse({ headers: responseHeaders, body, isBase64Encoded, statusCode, }: GetResponseAdapterProps<HuaweiApiGatewayEvent>): HuaweiApiGatewayResponse;
/**
* {@inheritDoc}
*/
onErrorWhileForwarding({ error, delegatedResolver, respondWithErrors, event, log, }: OnErrorProps<HuaweiApiGatewayEvent, HuaweiApiGatewayResponse>): void;
/**
* Get path from event with query strings
*
* @param event - The event sent by serverless
*/
protected getPathFromEvent(event: HuaweiApiGatewayEvent): string;
}
export { HuaweiApiGatewayAdapter, type HuaweiApiGatewayOptions };