UNPKG

lemon-core

Version:
201 lines (200 loc) 7.55 kB
import { NextContext } from 'lemon-model'; import { STAGE, ProtocolService, ProtocolParam, ProtocolTransformer, ProtocolBody, CallbackParam } from './../core-services'; import AWS, { SQS, SNS } from 'aws-sdk'; import { APIGatewayProxyEvent, SNSMessage, SQSRecord } from 'aws-lambda'; import { ConfigService } from './../config/config-service'; /** * header name to exchange `next-context` */ export declare const HEADER_PROTOCOL_CONTEXT: any; /** * type: MyProtocolType * - supportable type of protocol */ export declare type MyProtocolType = 'web' | 'sns' | 'sqs' | 'api'; /** * type: MySNSEventParam */ export declare type MySNSEventParam = AWS.SNS.Types.PublishInput; /** * type of ProtocolParam w/ callback * - only valid from transformer. */ export interface MyProtocolParam extends ProtocolParam { callback?: string; } /** * class: `MyConfigService` * - main implementation for `protocol-service` * - support protocol via `API(WEB)` + `SNS` + `SQS` */ export declare class MyProtocolService implements ProtocolService { static REPORT_ERROR: boolean; readonly web: WEBProtocolTransformer; readonly sns: SNSProtocolTransformer; readonly sqs: SQSProtocolTransformer; config: ConfigService; protected selfService: string; /** * default constructor. * * @param service default current service (for debug) * @param config config-service to use (for debug) */ constructor(service?: string, config?: ConfigService); /** * say hello() of this service */ hello: () => string; static asPath: (type: string, id?: string, cmd?: string) => string; /** * load transformer */ asTransformer(name: 'web' | 'sns' | 'sqs'): ProtocolTransformer; /** * convert param to protocol URI. * - URI: `<protocol>://<accountId?>@<service-name>` * * **NOTE** * MUST USE STANDARD NAME FORMAT OF PACKAGE.NAME (ex: `lemon-hello-api`) * * example: * - web://lemon-hello-api, web://lemon-hello-api-dev * - sns://lemon-hello-sns, sns://lemon-hello-sns-dev * - sqs://lemon-hello-sqs, sqs://lemon-hello-sqs-dev * * @param param protocol param. * @param config config-service to use. */ asProtocolURI(protocol: MyProtocolType, param: ProtocolParam, config?: ConfigService): string; /** * get the current service's protocol uri * * @param context the current context. * @param type (optional) resource type * @param id (optional) resource id * @param cmd (optional) action command */ myProtocolURI(context: NextContext, type?: string, id?: string, cmd?: string): string; /** * helper to build protocol-uri from the current config. */ static buildProtocolURI(config: ConfigService, context: NextContext, protocol: MyProtocolType, service: 'self' | string, stage: '' | STAGE, type: string, id?: string, cmd?: string): string; /** * transform param to EventParam * * @param uri * @param param */ transformEvent(uri: string, param: ProtocolParam): SNS.PublishInput | SQS.SendMessageRequest | APIGatewayProxyEvent; /** * internal safe report-error. */ protected doReportError<T>(e: Error, context?: any, event?: any, data?: any): Promise<T>; /** * from string url, transform to protocol-param. * *mode* is dependent on body condition. * - if body is undefined, then mode will be 'GET' * - if body is not undefined, then mode will be 'POST'. * * @param context the current execute context via controller. * @param url url string must start with 'lemon://' like `lemon://lemon-hello-api/hello/0`, or 'api://' * @param param query parameter (optional) * @param body post body (optional) */ fromURL(context: NextContext, url: string, param?: ProtocolBody, body?: ProtocolBody): ProtocolParam; /** * build callback uri of self's type/id/cmd */ asCallbackURI(context: NextContext, param: CallbackParam): string; /** * synchronized call to target function via `Lambda` * * @param param the calling param * @param config config service (for debug) * @param uri (optional) if useing custom uri. */ execute<T>(param: ProtocolParam, config?: ConfigService, uri?: string): Promise<T>; /** * Asynchronized call to target function via `SNS` * * @param param the calling param * @param callback the return target * @param config config service (for debug) */ notify(param: ProtocolParam, callback?: CallbackParam, config?: ConfigService): Promise<string>; /** * Asynchronized call to target function via `SQS` * * @param param the calling param * @param callback the return target * @param delaySeconds the delayed seconds * @param config config service (for debug) */ enqueue(param: ProtocolParam, callback?: CallbackParam, delaySeconds?: number, config?: ConfigService): Promise<string>; /** * broadcast body message via shared `SNS` Subscritions. (see `NotificationHandler`) * - `.service` will be self url like `api://lemon-hello-api#1.2.3` * * @param context the current execute context. (`.identity` will be relayed). * @param endpoint the SNS endpoint like `lemon-hello-out`, or full ARN. * @param body the message body to broadcast. * @returns the message-id if applicable. */ broadcast(context: NextContext, endpoint: string, body: ProtocolBody): Promise<string>; } /** * class: `WEBProtocolTransformer` * - transformer for `WEB` Handler */ export declare class WEBProtocolTransformer implements ProtocolTransformer<APIGatewayProxyEvent, APIGatewayProxyEvent> { constructor(service?: MyProtocolService); /** * transform param to event * @param param the calling param. */ transformToEvent(uri: string, param: ProtocolParam): APIGatewayProxyEvent; /** * transform event data to param * @param event the lambda compartible event data. */ transformToParam(event: APIGatewayProxyEvent): ProtocolParam; } /** * class: `SNSProtocolTransformer` * - transformer for `SNS` Handler */ export declare class SNSProtocolTransformer implements ProtocolTransformer<MySNSEventParam, SNSMessage> { private service; constructor(service: MyProtocolService); /** * transform param to event * @param param the calling param. */ transformToEvent(uri: string, param: ProtocolParam, callback?: string): MySNSEventParam; /** * transform event data to param * @param event the lambda compartible event data. */ transformToParam(event: SNSMessage): MyProtocolParam; } declare type SQSEventParam = AWS.SQS.Types.SendMessageRequest; /** * class: `SQSProtocolTransformer` * - transformer for `SQS` Handler */ export declare class SQSProtocolTransformer implements ProtocolTransformer<SQSEventParam, SQSRecord> { private service; constructor(service: MyProtocolService); /** * transform param to event * @param param the calling param. */ transformToEvent(uri: string, param: ProtocolParam, callback?: string): SQSEventParam; /** * transform event data to param * @param event the lambda compartible event data. */ transformToParam(event: SQSRecord): MyProtocolParam; } export {};