UNPKG

lemon-core

Version:
248 lines (247 loc) 8.45 kB
import { NextDecoder, NextContext, NextIdentity, NextIdentityJwt } from 'lemon-model'; import { ProtocolParam } from './../core-services'; import { LambdaHandler, WEBHandler, Context, LambdaSubHandler, WEBEvent } from './lambda-handler'; import { APIGatewayProxyResult, APIGatewayEventRequestContext, APIGatewayProxyEvent } from 'aws-lambda'; import { AWSKMSService } from '../aws/aws-kms-service'; /** * class: `WEBController` * - common controller interface. */ export interface CoreWEBController { hello(): string; type(): string; decode: NextDecoder; } interface ProxyParams { param: ProtocolParam; event: WEBEvent; } declare type ProxyResult = APIGatewayProxyResult; declare type ProxyResponser = () => ProxyResult; declare type ProxyChain = ProxyParams | ProxyResponser; /** * build http response body * - if body is string type, then content-type would be text/<some>. * - or default type is json * * @param statusCode http response code like 200 * @param body string or object * @param contentType (optional) the target content-type * @param origin (optional) the allow origin (default *) * @returns http response body */ export declare const buildResponse: (statusCode: number, body: any, contentType?: string, origin?: string) => ProxyResult; export declare const success: (body: any, contentType?: string, origin?: string) => APIGatewayProxyResult; export declare const notfound: (body: any) => APIGatewayProxyResult; export declare const failure: (body: any, status?: number) => APIGatewayProxyResult; export declare const redirect: (location: any, status?: number) => APIGatewayProxyResult; /** * start proxy-chain by event & context. * @param event event * @param $ctx context */ export declare const promised: (event: APIGatewayProxyEvent, $ctx: NextContext) => Promise<ProxyChain>; /** * builder for default handler */ export declare const mxNextHandler: (thiz: LambdaWEBHandler) => (params: ProxyChain) => Promise<ProxyResult>; /** * builder for failure promised. */ export declare const mxNextFailure: (event: APIGatewayProxyEvent, $ctx: NextContext) => (e: any) => APIGatewayProxyResult | Promise<APIGatewayProxyResult>; /** * class: LambdaWEBHandler * - default WEB Handler w/ event-listeners. */ export declare class LambdaWEBHandler extends LambdaSubHandler<WEBHandler> { static REPORT_ERROR: boolean; private _handlers; /** * default constructor w/ registering self. */ constructor(lambda: LambdaHandler, register?: boolean); /** * add web-handlers by `NextDecoder`. * * @param type type of WEB(API) * @param decoder next decorder */ setHandler(type: string, decoder: NextDecoder): void; /** * check if there is handler for type. * @param type type of WEB(API) */ hasHandler(type: string): boolean; /** * registr web-controller. * @param controller the web-controller. */ addController(controller: CoreWEBController): void; /** * get all decoders. */ getHandlerDecoders(): { [key: string]: NextDecoder; }; /** * Default WEB Handler. */ handle: WEBHandler; /** * handle param via protocol-service. * * @param param protocol parameters * @param event (optional) origin event object. */ handleProtocol<TResult = any>(param: ProtocolParam, event?: APIGatewayProxyEvent): Promise<TResult>; /** * builder of tools for http-headers * - extracting header content, and parse. */ tools: (headers: HttpHeaderSet) => HttpHeaderTool; /** * pack the request context for Http request. * * @param event origin Event. * @param orgContext (optional) original lambda.Context */ packContext(event: APIGatewayProxyEvent, orgContext?: Context): Promise<NextContext>; } /** * class: `HttpHeaderSet` * - header has only <string> value (or values) */ export interface HttpHeaderSet { [name: string]: string | string[]; } /** * class: `HttpHeaderTool` * - parse header and extract identity. */ export interface HttpHeaderTool { /** say hello */ hello(): string; /** * get values by name * @param name case-insentive name of header */ getHeaders(name: string): string[]; /** * get the last value in header by name * @param name case-insentive name of header */ getHeader(name: string): string; /** * parse of header[`env(HEADER_LEMON_IDENTITY)`] to get `NextIdentity` * @param name (optional) name of header (default is 'x-lemon-identity') */ parseIdentityHeader(name?: string): Promise<NextIdentity>; /** * parse of header[`env(HEADER_LEMON_LANGUAGE)`] to get language-type. * @param name (optional) name of header (default is 'x-lemon-language') */ parseLanguageHeader(name?: string): string; /** * parse of header[`env(HEADER_LEMON_LANGUAGE)] to get cookie-set. * @param name (optional) name of header (default is 'cookie') */ parseCookiesHeader(name?: string): { [key: string]: string; }; /** * override with AWS request-context * @param $org the current request-context. * @param reqContext (optional) request-context from AWS lambda handler. */ prepareContext($org: NextContext, reqContext?: APIGatewayEventRequestContext): Promise<NextContext>; } /** * class: `MyHttpHeaderTool` * - basic implementation of HttpHeaderTool */ export declare class MyHttpHeaderTool implements HttpHeaderTool { protected handler: LambdaWEBHandler; protected headers: HttpHeaderSet; /** * default constructor. * @param headers */ constructor(handler: LambdaWEBHandler, headers: HttpHeaderSet); hello(): string; /** * get values by name * @param name case-insentive name of field */ getHeaders: (name: string) => string[]; /** * get the last value in header by name */ getHeader: (name: string) => string; /** * check if this request is from externals (like API-GW) * @returns true if in external */ isExternal: () => boolean; /** * parse of header[`x-lemon-identity`] to get the instance of `NextIdentity` * - lambda 호출의 2가지 방법이 있음 (interval vs external) * - internal는 AWS 같은 계정내 호출로 labmda 직접 호출이 가능함. * - external는 API-GW를 통한 호출로 JWT 지원 (since 3.1.2). * * **[FOR INTERNAL CALL BY LAMBDA]** * - `x-lemon-identity` 정보로부터, 계정 정보를 얻음 (for direct call via lambda) * - 외부 호출과 구분하기 위해서 headr[host]가 비어 있어야함 (API-GW에서는 무조건 있으므로) * * **[FOR EXTERNAL CALL BY API-GW]** * - support ONLY JWT Token authentication (verification). * - iat */ parseIdentityHeader: <T extends NextIdentity<any> = NextIdentity<any>>(name?: string) => Promise<T>; /** * parse as identity from json encoded text. */ parseIdentityJson: (val: string) => Promise<NextIdentity<any>>; /** * find(or make) the proper KMSService per key * @param keyId key of KMS * @returns service */ protected findKMSService(keyId: string): AWSKMSService; /** * encode as JWT string. */ encodeIdentityJWT: (identity: NextIdentity, params?: { /** KMS alias to use */ alias?: string; /** current ms */ current?: number; }) => Promise<{ signature: string; message: string; token: string; }>; /** * parse as jwt-token, and validate the signature. */ parseIdentityJWT: <T extends NextIdentityJwt<any> = NextIdentityJwt<any>>(token: string, params?: { /** current ms */ current?: number; /** flag to verify JWT (default true) */ verify?: boolean; }) => Promise<T>; /** * parse of header[HEADER_LEMON_LANGUAGE] to get language-type. */ parseLanguageHeader: (name?: string) => string; /** * parse of header[HEADER_LEMON_LANGUAGE] to get cookie-set. */ parseCookiesHeader: (name?: string) => { [key: string]: string; }; /** * override with AWS request-context */ prepareContext: ($org: NextContext, reqContext: APIGatewayEventRequestContext) => Promise<NextContext>; } export {};