@yopdev/dev-server 1.5.0 → 1.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,10 +3,11 @@ import { DevServerConfig } from './config';
|
|
|
3
3
|
import { HttpSettings } from './http-server';
|
|
4
4
|
import { Authorizer, Route } from './lambda-http-proxy';
|
|
5
5
|
import { Callback, Service, Startable } from './services';
|
|
6
|
-
export declare const newLambdaProxyFromCloudFormationTemplate: (name: string, settings: HttpSettings, config: CloudFormationLambdaProxyConfig
|
|
7
|
-
type CloudFormationLambdaProxyConfig = {
|
|
6
|
+
export declare const newLambdaProxyFromCloudFormationTemplate: <C>(name: string, settings: HttpSettings, config: CloudFormationLambdaProxyConfig<C>, callback: Callback<string>) => Startable<Service<string>>;
|
|
7
|
+
type CloudFormationLambdaProxyConfig<C> = {
|
|
8
8
|
authorizer?: (config: DevServerConfig) => Authorizer | undefined;
|
|
9
|
-
extraRoutes: Route[];
|
|
9
|
+
extraRoutes: Route<C>[];
|
|
10
10
|
prepare?: (config: DevServerConfig) => Promise<void>;
|
|
11
|
+
context?: () => C;
|
|
11
12
|
} & CloudFormationSetupConfig;
|
|
12
13
|
export {};
|
|
@@ -14,7 +14,7 @@ class CloudFormationLambdaProxy extends cloudformation_1.CloudFormationSetup {
|
|
|
14
14
|
method: new RegExp(event.Properties.Method),
|
|
15
15
|
path: new RegExp(`^${pathWithCapturingGroups}${QUERY_STRING_OR_LOCATION_REG_EXP}?$`),
|
|
16
16
|
weight: this.computeWeight(pathWithCapturingGroups),
|
|
17
|
-
handler: this.pathParameterCapture(new RegExp(pathWithCapturingGroups), handler),
|
|
17
|
+
handler: this.pathParameterCapture(new RegExp(pathWithCapturingGroups), handler, config.context),
|
|
18
18
|
})), config.prepare, (name, cfg, routes) => {
|
|
19
19
|
return (0, lambda_http_proxy_1.newLambdaHttpProxy)(`${name}:apigw`, {
|
|
20
20
|
settings: this.settings,
|
|
@@ -29,11 +29,11 @@ class CloudFormationLambdaProxy extends cloudformation_1.CloudFormationSetup {
|
|
|
29
29
|
this.variables = (path) => path.split('(?<').length;
|
|
30
30
|
this.pathWithCapturingGroups = (source) => [...source.matchAll(PATH_VARIABLE_CAPTURE)].reduce((prev, curr) => prev.replaceAll(curr[0], `(?<${this.normalizeCaptureGroupName(curr[1])}>.*)`), source);
|
|
31
31
|
this.normalizeCaptureGroupName = (original) => (original === 'proxy+' ? PROXY_PATH_PARAM : original);
|
|
32
|
-
this.pathParameterCapture = (pathWithCapturingGroups, handler) => async (event) => {
|
|
32
|
+
this.pathParameterCapture = (pathWithCapturingGroups, handler, context) => async (event) => {
|
|
33
33
|
const pathParameters = pathWithCapturingGroups.exec(event.path)?.groups;
|
|
34
34
|
event.pathParameters = Object.fromEntries((Object.entries(pathParameters ?? {}))
|
|
35
35
|
.map(([k, v]) => [k, decodeURIComponent(v)]));
|
|
36
|
-
return handler(event);
|
|
36
|
+
return handler(event, context?.());
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
logHandler(handler) {
|
|
@@ -3,17 +3,18 @@ import { APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEvent, APIGatew
|
|
|
3
3
|
import { IncomingMessage } from "http";
|
|
4
4
|
import { Service, Callback } from "./services";
|
|
5
5
|
export declare const UNAUTHORIZED: Error;
|
|
6
|
-
export declare const newLambdaHttpProxy: (name: string, config: {
|
|
6
|
+
export declare const newLambdaHttpProxy: <C>(name: string, config: {
|
|
7
7
|
settings: HttpSettings;
|
|
8
|
-
routes: Route[];
|
|
8
|
+
routes: Route<C>[];
|
|
9
9
|
mapper?: (request: IncomingMessage, body: string) => Promise<APIGatewayProxyEvent>;
|
|
10
10
|
authorizer?: Authorizer;
|
|
11
|
+
context?: () => C;
|
|
11
12
|
}, callback?: Callback<string>) => Service<string>;
|
|
12
|
-
export type Route = {
|
|
13
|
+
export type Route<C> = {
|
|
13
14
|
method: RegExp;
|
|
14
15
|
path: RegExp;
|
|
15
16
|
weight: number;
|
|
16
17
|
authorizer?: Authorizer;
|
|
17
|
-
handler: (event: APIGatewayProxyEvent) => Promise<APIGatewayProxyResult>;
|
|
18
|
+
handler: (event: APIGatewayProxyEvent, context: C) => Promise<APIGatewayProxyResult>;
|
|
18
19
|
};
|
|
19
20
|
export type Authorizer = (authorization: string) => Promise<APIGatewayEventDefaultAuthorizerContext | undefined>;
|
|
@@ -7,22 +7,23 @@ const mappers_1 = require("./mappers");
|
|
|
7
7
|
const responses_1 = require("./responses");
|
|
8
8
|
const services_1 = require("./services");
|
|
9
9
|
exports.UNAUTHORIZED = new Error('UNAUTHORIZED');
|
|
10
|
-
const newLambdaHttpProxy = (name, config, callback) => new services_1.Service(new LambdaHttpProxy(name, config.settings, config.routes, config.mapper ?? mappers_1.mapToLambdaEvent, config.authorizer ?? (() => Promise.resolve(undefined))), callback);
|
|
10
|
+
const newLambdaHttpProxy = (name, config, callback) => new services_1.Service(new LambdaHttpProxy(name, config.settings, config.routes, config.mapper ?? mappers_1.mapToLambdaEvent, config.authorizer ?? (() => Promise.resolve(undefined)), config.context), callback);
|
|
11
11
|
exports.newLambdaHttpProxy = newLambdaHttpProxy;
|
|
12
12
|
class LambdaHttpProxy {
|
|
13
|
-
constructor(name, settings, routes, mapper, defaultAuthorizer) {
|
|
13
|
+
constructor(name, settings, routes, mapper, defaultAuthorizer, context = () => undefined) {
|
|
14
14
|
this.name = name;
|
|
15
15
|
this.routes = routes;
|
|
16
16
|
this.mapper = mapper;
|
|
17
17
|
this.defaultAuthorizer = defaultAuthorizer;
|
|
18
|
+
this.context = context;
|
|
18
19
|
this.start = () => this.server.start();
|
|
19
20
|
this.stop = () => this.server.stop();
|
|
20
21
|
this.handler = (authorizer, lambdaHandler) => (request, body, response) => this.mapper(request, body)
|
|
21
22
|
.then(async (proxyEvent) => authorizer(proxyEvent.headers['authorization'])
|
|
22
23
|
.then(async (context) => {
|
|
23
24
|
proxyEvent.requestContext.authorizer = context;
|
|
24
|
-
return lambdaHandler(proxyEvent)
|
|
25
|
-
.then((lambda) => (0, responses_1.writeResponse)(response, lambda.statusCode, lambda.body, lambda.headers?.['content-type']?.toString() ?? undefined, lambda.headers?.['location']?.toString() ?? undefined))
|
|
25
|
+
return lambdaHandler(proxyEvent, this.context())
|
|
26
|
+
.then((lambda) => (0, responses_1.writeResponse)(response, lambda.statusCode, lambda.isBase64Encoded ? Buffer.from(lambda.body, 'base64') : lambda.body, lambda.headers?.['content-type']?.toString() ?? undefined, lambda.headers?.['location']?.toString() ?? undefined))
|
|
26
27
|
.catch((e) => {
|
|
27
28
|
this.LOGGER.error(e, 'request failed to execute');
|
|
28
29
|
(0, responses_1.internalServerError)(response, e.body);
|
package/dist/src/responses.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
import { ServerResponse } from 'http';
|
|
3
4
|
export declare function internalServerError(res: ServerResponse, body: any): void;
|
|
4
|
-
export declare function writeResponse(res: ServerResponse, statusCode: number, body: string, contentType?: string, location?: string): void;
|
|
5
|
+
export declare function writeResponse(res: ServerResponse, statusCode: number, body: string | Buffer, contentType?: string, location?: string): void;
|