@yopdev/dev-server 3.0.3-alpha.1 → 3.0.3-beta.1
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.
- package/dist/src/cloudformation-lambda-http-proxy.d.ts +4 -5
- package/dist/src/cloudformation-lambda-http-proxy.js +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/lambda-http-proxy.d.ts +7 -7
- package/dist/src/lambda-http-proxy.js +1 -1
- package/dist/src/mappers.d.ts +9 -11
- package/dist/src/mappers.js +6 -2
- package/package.json +1 -1
- package/src/cloudformation-lambda-http-proxy.ts +17 -18
- package/src/index.ts +2 -2
- package/src/lambda-http-proxy.ts +14 -14
- package/src/mappers.ts +23 -19
|
@@ -4,11 +4,10 @@ import { HttpSettings } from './http-server';
|
|
|
4
4
|
import { Authorizer, Route } from './lambda-http-proxy';
|
|
5
5
|
import { Callback, Service, Startable } from './services';
|
|
6
6
|
import { LambdaPayloadVersion } from './mappers';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
extraRoutes: Route<Context, AuthorizerContext, Event, HandlerResponse>[];
|
|
7
|
+
export declare const newLambdaProxyFromCloudFormationTemplate: <Context, Event_1, HandlerResponse>(name: string, settings: HttpSettings, payloadVersion: LambdaPayloadVersion<Event_1, HandlerResponse>, config: CloudFormationLambdaProxyConfig<Context, Event_1, HandlerResponse>, callback: Callback<string>) => Startable<Service<string>>;
|
|
8
|
+
type CloudFormationLambdaProxyConfig<Context, Event, HandlerResponse> = {
|
|
9
|
+
authorizer?: (config: DevServerConfig) => Authorizer | undefined;
|
|
10
|
+
extraRoutes: Route<Context, Event, HandlerResponse>[];
|
|
12
11
|
prepare?: (config: DevServerConfig) => Promise<void>;
|
|
13
12
|
context?: () => Context;
|
|
14
13
|
} & CloudFormationSetupConfig;
|
|
@@ -11,7 +11,7 @@ const newLambdaProxyFromCloudFormationTemplate = (name, settings, payloadVersion
|
|
|
11
11
|
exports.newLambdaProxyFromCloudFormationTemplate = newLambdaProxyFromCloudFormationTemplate;
|
|
12
12
|
class CloudFormationLambdaProxy extends cloudformation_1.CloudFormationSetup {
|
|
13
13
|
constructor(name, settings, extraRoutes, payloadVersion, config, callback) {
|
|
14
|
-
super(name, (event) => event.Type === 'HttpApi', async (event, handler) => Promise.resolve(this.pathWithCapturingGroups(event.Properties.Path)).then((pathWithCapturingGroups) => ({
|
|
14
|
+
super(name, (event) => event.Type === 'HttpApi' || event.Type === 'Api', async (event, handler) => Promise.resolve(this.pathWithCapturingGroups(event.Properties.Path)).then((pathWithCapturingGroups) => ({
|
|
15
15
|
method: new RegExp(this.method(event.Properties.Method)),
|
|
16
16
|
path: new RegExp(`^${pathWithCapturingGroups}${QUERY_STRING_OR_LOCATION_REG_EXP}?$`),
|
|
17
17
|
weight: this.computeWeight(pathWithCapturingGroups),
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { DevServer } from './dev-server';
|
|
2
2
|
export { newHttpServer as httpServer, HttpSettings } from './http-server';
|
|
3
|
-
export { newLambdaHttpProxy as lambdaHttpProxy } from './lambda-http-proxy';
|
|
4
|
-
export { v1 as v1LambdaProxyPayload, v2 as v2LambdaProxyPayload } from './mappers';
|
|
3
|
+
export { newLambdaHttpProxy as lambdaHttpProxy, Route } from './lambda-http-proxy';
|
|
4
|
+
export { v1 as v1LambdaProxyPayload, v2 as v2LambdaProxyPayload, LambdaPayloadVersion } from './mappers';
|
|
5
5
|
export { newLambdaProxyFromCloudFormationTemplate as cloudFormationLambdaProxy } from './cloudformation-lambda-http-proxy';
|
|
6
6
|
export { newDynamoDbTableFromCloudFormationTemplate as cloudFormationDynamoDbTable } from './cloudformation-dynamodb-table';
|
|
7
7
|
export { newSnsHttpProxy as snsHttpProxy } from './sns-http-proxy';
|
|
@@ -3,11 +3,11 @@ import { HttpSettings } from "./http-server";
|
|
|
3
3
|
import { LambdaMapperFactory } from "./mappers";
|
|
4
4
|
import { Service, Callback } from "./services";
|
|
5
5
|
import { APIGatewayAuthorizerResult } from "aws-lambda";
|
|
6
|
-
export declare const newLambdaHttpProxy: <Context,
|
|
6
|
+
export declare const newLambdaHttpProxy: <Context, Event_1, HandlerResponse>(name: string, config: {
|
|
7
7
|
settings: HttpSettings;
|
|
8
|
-
routes: Route<Context,
|
|
9
|
-
mapper: LambdaMapperFactory<
|
|
10
|
-
authorizer?: Authorizer
|
|
8
|
+
routes: Route<Context, Event_1, HandlerResponse>[];
|
|
9
|
+
mapper: LambdaMapperFactory<Event_1, HandlerResponse>;
|
|
10
|
+
authorizer?: Authorizer;
|
|
11
11
|
context?: () => Context;
|
|
12
12
|
}, callback?: Callback<string>) => Service<string>;
|
|
13
13
|
export declare abstract class Response {
|
|
@@ -18,11 +18,11 @@ export declare abstract class Response {
|
|
|
18
18
|
constructor(statusCode: number, contentType: string | undefined, location: string | undefined, cookies: string[] | undefined);
|
|
19
19
|
body: () => string | Buffer | undefined;
|
|
20
20
|
}
|
|
21
|
-
export type Route<Context,
|
|
21
|
+
export type Route<Context, Event, HandlerResponse> = {
|
|
22
22
|
method: RegExp;
|
|
23
23
|
path: RegExp;
|
|
24
24
|
weight: number;
|
|
25
|
-
authorizer?: Authorizer
|
|
25
|
+
authorizer?: Authorizer;
|
|
26
26
|
handler: (event: Event, context: Context) => Promise<HandlerResponse>;
|
|
27
27
|
};
|
|
28
|
-
export type Authorizer
|
|
28
|
+
export type Authorizer = (authorization: string) => Promise<APIGatewayAuthorizerResult | undefined>;
|
|
@@ -20,7 +20,7 @@ class LambdaHttpProxy {
|
|
|
20
20
|
this.handler = (authorizer, lambdaHandler) => (request, body, response) => this.mapper.newInstance(request, body)
|
|
21
21
|
.then(async (mapper) => authorizer(mapper.authorization())
|
|
22
22
|
.then(async (context) => context?.policyDocument?.Statement?.find((s) => s.Effect === 'Deny') === undefined
|
|
23
|
-
?
|
|
23
|
+
? context
|
|
24
24
|
: Promise.reject(UNAUTHORIZED))
|
|
25
25
|
.then((context) => lambdaHandler(mapper.event(context), this.context())
|
|
26
26
|
.then((lambda) => {
|
package/dist/src/mappers.d.ts
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Message } from "@aws-sdk/client-sqs";
|
|
3
|
-
import { APIGatewayProxyEvent, APIGatewayProxyEventPathParameters, APIGatewayProxyEventV2, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2, SQSRecord } from "aws-lambda";
|
|
3
|
+
import { APIGatewayAuthorizerResult, APIGatewayProxyEvent, APIGatewayProxyEventPathParameters, APIGatewayProxyEventV2, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2, SQSRecord } from "aws-lambda";
|
|
4
4
|
import { IncomingMessage } from "http";
|
|
5
5
|
import { Response } from "./lambda-http-proxy";
|
|
6
6
|
export declare const mapToLambdaSqsRecord: (message: Message) => SQSRecord;
|
|
7
|
-
export type LambdaPayloadVersion<
|
|
8
|
-
mapper: LambdaMapperFactory<
|
|
7
|
+
export type LambdaPayloadVersion<Event, HandlerResponse> = {
|
|
8
|
+
mapper: LambdaMapperFactory<Event, HandlerResponse>;
|
|
9
9
|
resolver: PathParameterResolver<Event>;
|
|
10
10
|
};
|
|
11
|
-
export declare const v1:
|
|
12
|
-
export declare const v2:
|
|
13
|
-
interface LambdaMapper<
|
|
14
|
-
event(context:
|
|
15
|
-
[name: string]: any;
|
|
16
|
-
} & AuthorizerContext): Event;
|
|
11
|
+
export declare const v1: () => LambdaPayloadVersion<APIGatewayProxyEvent, APIGatewayProxyResult>;
|
|
12
|
+
export declare const v2: () => LambdaPayloadVersion<APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2>;
|
|
13
|
+
interface LambdaMapper<Event, HandlerResponse> {
|
|
14
|
+
event(context: APIGatewayAuthorizerResult): Event;
|
|
17
15
|
authorization(): string;
|
|
18
16
|
toResponse: (response: HandlerResponse) => Response;
|
|
19
17
|
}
|
|
20
|
-
export interface LambdaMapperFactory<
|
|
21
|
-
newInstance(request: IncomingMessage, body: string): Promise<LambdaMapper<
|
|
18
|
+
export interface LambdaMapperFactory<Event, LambdaResponse> {
|
|
19
|
+
newInstance(request: IncomingMessage, body: string): Promise<LambdaMapper<Event, LambdaResponse>>;
|
|
22
20
|
}
|
|
23
21
|
export type PathParameterResolver<Event> = {
|
|
24
22
|
locate: (event: Event) => string;
|
package/dist/src/mappers.js
CHANGED
|
@@ -70,7 +70,7 @@ class DefaultLambdaMapper {
|
|
|
70
70
|
}
|
|
71
71
|
return [];
|
|
72
72
|
};
|
|
73
|
-
this.event = (
|
|
73
|
+
this.event = (authorizerResult) => ({
|
|
74
74
|
version: '2.0',
|
|
75
75
|
rawPath: this.url.pathname,
|
|
76
76
|
rawQueryString: this.url.search,
|
|
@@ -102,7 +102,11 @@ class DefaultLambdaMapper {
|
|
|
102
102
|
timeEpoch: this.time,
|
|
103
103
|
accountId: '',
|
|
104
104
|
apiId: '',
|
|
105
|
-
authorizer:
|
|
105
|
+
authorizer: {
|
|
106
|
+
...authorizerResult,
|
|
107
|
+
...authorizerResult?.context,
|
|
108
|
+
lambda: authorizerResult?.context,
|
|
109
|
+
},
|
|
106
110
|
httpMethod: '',
|
|
107
111
|
identity: {
|
|
108
112
|
accessKey: '',
|
package/package.json
CHANGED
|
@@ -4,21 +4,20 @@ import { HttpSettings } from './http-server';
|
|
|
4
4
|
import { Authorizer, Route, newLambdaHttpProxy } from './lambda-http-proxy';
|
|
5
5
|
import { Callback, Service, Startable } from './services';
|
|
6
6
|
import { LambdaMapperFactory, LambdaPayloadVersion, PathParameterResolver } from './mappers';
|
|
7
|
-
import { APIGatewayAuthorizerResult } from 'aws-lambda';
|
|
8
7
|
|
|
9
8
|
const PATH_VARIABLE_CAPTURE = /{(.*?)}/g;
|
|
10
9
|
const QUERY_STRING_OR_LOCATION_REG_EXP = '(?:([?#].*))?';
|
|
11
10
|
const PROXY_PATH_PARAM = 'proxy';
|
|
12
11
|
const NOOP_AUTHORIZER = async () => undefined
|
|
13
|
-
export const newLambdaProxyFromCloudFormationTemplate = <Context,
|
|
12
|
+
export const newLambdaProxyFromCloudFormationTemplate = <Context, Event, HandlerResponse>(
|
|
14
13
|
name: string,
|
|
15
14
|
settings: HttpSettings,
|
|
16
|
-
payloadVersion: LambdaPayloadVersion<
|
|
17
|
-
config: CloudFormationLambdaProxyConfig<Context,
|
|
15
|
+
payloadVersion: LambdaPayloadVersion<Event, HandlerResponse>,
|
|
16
|
+
config: CloudFormationLambdaProxyConfig<Context, Event, HandlerResponse>,
|
|
18
17
|
callback: Callback<string>,
|
|
19
18
|
): Startable<Service<string>> => new CloudFormationLambdaProxy(name, settings, config.extraRoutes, payloadVersion, config, callback);
|
|
20
19
|
|
|
21
|
-
class CloudFormationLambdaProxy<Context,
|
|
20
|
+
class CloudFormationLambdaProxy<Context, Event, HandlerResponse> extends CloudFormationSetup<
|
|
22
21
|
{
|
|
23
22
|
Path: string;
|
|
24
23
|
Method: string;
|
|
@@ -27,24 +26,24 @@ class CloudFormationLambdaProxy<Context, AuthorizerContext extends APIGatewayAut
|
|
|
27
26
|
}
|
|
28
27
|
},
|
|
29
28
|
(event: Event) => Promise<HandlerResponse>,
|
|
30
|
-
Route<Context,
|
|
29
|
+
Route<Context, Event, HandlerResponse>,
|
|
31
30
|
Service<string>
|
|
32
31
|
> {
|
|
33
|
-
private mapper: LambdaMapperFactory<
|
|
34
|
-
private authorizer: (cfg: DevServerConfig) => Authorizer
|
|
32
|
+
private mapper: LambdaMapperFactory<Event, HandlerResponse>;
|
|
33
|
+
private authorizer: (cfg: DevServerConfig) => Authorizer;
|
|
35
34
|
private pathParameterResolver: PathParameterResolver<Event>
|
|
36
35
|
|
|
37
36
|
constructor(
|
|
38
37
|
name: string,
|
|
39
38
|
private readonly settings: HttpSettings,
|
|
40
|
-
private readonly extraRoutes: Route<Context,
|
|
41
|
-
payloadVersion: LambdaPayloadVersion<
|
|
42
|
-
config: CloudFormationLambdaProxyConfig<Context,
|
|
39
|
+
private readonly extraRoutes: Route<Context, Event, HandlerResponse>[],
|
|
40
|
+
payloadVersion: LambdaPayloadVersion<Event, HandlerResponse>,
|
|
41
|
+
config: CloudFormationLambdaProxyConfig<Context, Event, HandlerResponse>,
|
|
43
42
|
private readonly callback?: Callback<string>,
|
|
44
43
|
) {
|
|
45
44
|
super(
|
|
46
45
|
name,
|
|
47
|
-
(event) => event.Type === 'HttpApi',
|
|
46
|
+
(event) => event.Type === 'HttpApi' || event.Type === 'Api',
|
|
48
47
|
async (event, handler) =>
|
|
49
48
|
Promise.resolve(this.pathWithCapturingGroups(event.Properties.Path)).then(
|
|
50
49
|
(pathWithCapturingGroups) => ({
|
|
@@ -53,7 +52,7 @@ class CloudFormationLambdaProxy<Context, AuthorizerContext extends APIGatewayAut
|
|
|
53
52
|
weight: this.computeWeight(pathWithCapturingGroups),
|
|
54
53
|
handler: this.pathParameterCapture(new RegExp(pathWithCapturingGroups), handler, config.context),
|
|
55
54
|
authorizer: event.Properties?.Auth?.Authorizer === 'NONE' ? NOOP_AUTHORIZER : undefined,
|
|
56
|
-
} satisfies Route<Context,
|
|
55
|
+
} satisfies Route<Context, Event, HandlerResponse>),
|
|
57
56
|
),
|
|
58
57
|
config.prepare,
|
|
59
58
|
config,
|
|
@@ -63,7 +62,7 @@ class CloudFormationLambdaProxy<Context, AuthorizerContext extends APIGatewayAut
|
|
|
63
62
|
this.pathParameterResolver = payloadVersion.resolver;
|
|
64
63
|
}
|
|
65
64
|
|
|
66
|
-
protected afterStart = (name: string, cfg: DevServerConfig, routes: Route<Context,
|
|
65
|
+
protected afterStart = (name: string, cfg: DevServerConfig, routes: Route<Context, Event, HandlerResponse>[]) =>
|
|
67
66
|
newLambdaHttpProxy(
|
|
68
67
|
`${name}:apigw`,
|
|
69
68
|
{
|
|
@@ -80,7 +79,7 @@ class CloudFormationLambdaProxy<Context, AuthorizerContext extends APIGatewayAut
|
|
|
80
79
|
private segments = (path: string) => path.split('/').length;
|
|
81
80
|
private variables = (path: string) => path.split('(?<').length;
|
|
82
81
|
|
|
83
|
-
logHandler(handler: Route<Context,
|
|
82
|
+
logHandler(handler: Route<Context, Event, HandlerResponse>): string {
|
|
84
83
|
return `detected handler for ${handler.method} on ${handler.path} with weight ${handler.weight}`;
|
|
85
84
|
}
|
|
86
85
|
|
|
@@ -123,9 +122,9 @@ class CloudFormationLambdaProxy<Context, AuthorizerContext extends APIGatewayAut
|
|
|
123
122
|
}
|
|
124
123
|
}
|
|
125
124
|
|
|
126
|
-
type CloudFormationLambdaProxyConfig<Context,
|
|
127
|
-
authorizer?: (config: DevServerConfig) => Authorizer
|
|
128
|
-
extraRoutes: Route<Context,
|
|
125
|
+
type CloudFormationLambdaProxyConfig<Context, Event, HandlerResponse> = {
|
|
126
|
+
authorizer?: (config: DevServerConfig) => Authorizer | undefined,
|
|
127
|
+
extraRoutes: Route<Context, Event, HandlerResponse>[],
|
|
129
128
|
prepare?: (config: DevServerConfig) => Promise<void>,
|
|
130
129
|
context?: () => Context,
|
|
131
130
|
} & CloudFormationSetupConfig;
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { DevServer } from './dev-server'
|
|
2
2
|
export { newHttpServer as httpServer, HttpSettings } from './http-server'
|
|
3
|
-
export { newLambdaHttpProxy as lambdaHttpProxy } from './lambda-http-proxy'
|
|
4
|
-
export { v1 as v1LambdaProxyPayload, v2 as v2LambdaProxyPayload } from './mappers'
|
|
3
|
+
export { newLambdaHttpProxy as lambdaHttpProxy, Route } from './lambda-http-proxy'
|
|
4
|
+
export { v1 as v1LambdaProxyPayload, v2 as v2LambdaProxyPayload, LambdaPayloadVersion } from './mappers'
|
|
5
5
|
export { newLambdaProxyFromCloudFormationTemplate as cloudFormationLambdaProxy } from './cloudformation-lambda-http-proxy'
|
|
6
6
|
export { newDynamoDbTableFromCloudFormationTemplate as cloudFormationDynamoDbTable } from './cloudformation-dynamodb-table'
|
|
7
7
|
export { newSnsHttpProxy as snsHttpProxy } from './sns-http-proxy'
|
package/src/lambda-http-proxy.ts
CHANGED
|
@@ -7,17 +7,17 @@ import { Lifecycle, Service, Callback } from "./services";
|
|
|
7
7
|
import { APIGatewayAuthorizerResult } from "aws-lambda";
|
|
8
8
|
|
|
9
9
|
const UNAUTHORIZED = new Error('UNAUTHORIZED');
|
|
10
|
-
export const newLambdaHttpProxy = <Context,
|
|
10
|
+
export const newLambdaHttpProxy = <Context, Event, HandlerResponse>(
|
|
11
11
|
name: string,
|
|
12
12
|
config: {
|
|
13
13
|
settings: HttpSettings,
|
|
14
|
-
routes: Route<Context,
|
|
15
|
-
mapper: LambdaMapperFactory<
|
|
16
|
-
authorizer?: Authorizer
|
|
14
|
+
routes: Route<Context, Event, HandlerResponse>[],
|
|
15
|
+
mapper: LambdaMapperFactory<Event, HandlerResponse>,
|
|
16
|
+
authorizer?: Authorizer
|
|
17
17
|
context?: () => Context
|
|
18
18
|
},
|
|
19
19
|
callback?: Callback<string>,
|
|
20
|
-
) => new Service(new LambdaHttpProxy<Context,
|
|
20
|
+
) => new Service(new LambdaHttpProxy<Context, Event, HandlerResponse>(
|
|
21
21
|
name,
|
|
22
22
|
config.settings,
|
|
23
23
|
config.routes,
|
|
@@ -26,16 +26,16 @@ export const newLambdaHttpProxy = <Context, AuthorizerContext extends APIGateway
|
|
|
26
26
|
config.context,
|
|
27
27
|
), callback)
|
|
28
28
|
|
|
29
|
-
class LambdaHttpProxy<Context,
|
|
29
|
+
class LambdaHttpProxy<Context, Event, HandlerResponse> implements Lifecycle<string> {
|
|
30
30
|
private LOGGER: Logger
|
|
31
31
|
private readonly server: HttpServer
|
|
32
32
|
|
|
33
33
|
constructor(
|
|
34
34
|
readonly name: string,
|
|
35
35
|
settings: HttpSettings,
|
|
36
|
-
private readonly routes: Route<Context,
|
|
37
|
-
private readonly mapper: LambdaMapperFactory<
|
|
38
|
-
private readonly defaultAuthorizer: Authorizer
|
|
36
|
+
private readonly routes: Route<Context, Event, HandlerResponse>[],
|
|
37
|
+
private readonly mapper: LambdaMapperFactory<Event, HandlerResponse>,
|
|
38
|
+
private readonly defaultAuthorizer: Authorizer,
|
|
39
39
|
private readonly context: () => Context = () => undefined,
|
|
40
40
|
) {
|
|
41
41
|
this.server = new HttpServer(name, settings, this.resolveRoute)
|
|
@@ -48,7 +48,7 @@ class LambdaHttpProxy<Context, AuthorizerContext extends APIGatewayAuthorizerRes
|
|
|
48
48
|
stop = () => this.server.stop()
|
|
49
49
|
|
|
50
50
|
private handler = (
|
|
51
|
-
authorizer: Authorizer
|
|
51
|
+
authorizer: Authorizer,
|
|
52
52
|
lambdaHandler: (event: Event, context: Context) => Promise<HandlerResponse>
|
|
53
53
|
) =>
|
|
54
54
|
(request: IncomingMessage, body: string, response: ServerResponse) =>
|
|
@@ -57,7 +57,7 @@ class LambdaHttpProxy<Context, AuthorizerContext extends APIGatewayAuthorizerRes
|
|
|
57
57
|
authorizer(mapper.authorization())
|
|
58
58
|
.then(async (context) =>
|
|
59
59
|
context?.policyDocument?.Statement?.find((s) => s.Effect === 'Deny') === undefined
|
|
60
|
-
?
|
|
60
|
+
? context
|
|
61
61
|
: Promise.reject(UNAUTHORIZED),
|
|
62
62
|
)
|
|
63
63
|
.then((context) =>
|
|
@@ -106,12 +106,12 @@ export abstract class Response {
|
|
|
106
106
|
body: () => string | Buffer | undefined
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
export type Route<Context,
|
|
109
|
+
export type Route<Context, Event, HandlerResponse> = {
|
|
110
110
|
method: RegExp;
|
|
111
111
|
path: RegExp;
|
|
112
112
|
weight: number;
|
|
113
|
-
authorizer?: Authorizer
|
|
113
|
+
authorizer?: Authorizer;
|
|
114
114
|
handler: (event: Event, context: Context) => Promise<HandlerResponse>;
|
|
115
115
|
};
|
|
116
116
|
|
|
117
|
-
export type Authorizer
|
|
117
|
+
export type Authorizer = (authorization: string) => Promise<APIGatewayAuthorizerResult | undefined>;
|
package/src/mappers.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Message } from "@aws-sdk/client-sqs";
|
|
2
|
-
import { APIGatewayEventRequestContextV2WithAuthorizer, APIGatewayProxyEvent, APIGatewayProxyEventHeaders, APIGatewayProxyEventPathParameters, APIGatewayProxyEventQueryStringParameters, APIGatewayProxyEventV2, APIGatewayProxyEventV2WithRequestContext, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2, SQSRecord } from "aws-lambda";
|
|
2
|
+
import { APIGatewayAuthorizerResult, APIGatewayEventRequestContextV2WithAuthorizer, APIGatewayProxyEvent, APIGatewayProxyEventHeaders, APIGatewayProxyEventPathParameters, APIGatewayProxyEventQueryStringParameters, APIGatewayProxyEventV2, APIGatewayProxyEventV2WithRequestContext, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2, SQSRecord } from "aws-lambda";
|
|
3
3
|
import { IncomingMessage } from "http";
|
|
4
4
|
import { URL } from "url";
|
|
5
5
|
import { Response } from "./lambda-http-proxy";
|
|
@@ -25,12 +25,12 @@ export const mapToLambdaSqsRecord = (message: Message): SQSRecord => {
|
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
const payloadV1JwtAuthorizerLambdaMapper =
|
|
29
|
-
newInstance: (request, body) => Promise.resolve(new DefaultLambdaMapper
|
|
28
|
+
const payloadV1JwtAuthorizerLambdaMapper = (): LambdaMapperFactory<APIGatewayProxyEvent, APIGatewayProxyResult> => ({
|
|
29
|
+
newInstance: (request, body) => Promise.resolve(new DefaultLambdaMapper(request, body))
|
|
30
30
|
})
|
|
31
31
|
|
|
32
|
-
const payloadV2LambdaAuthorizerLambdaMapper =
|
|
33
|
-
newInstance: (request, body) => Promise.resolve(new DefaultLambdaMapper
|
|
32
|
+
const payloadV2LambdaAuthorizerLambdaMapper = (): LambdaMapperFactory<APIGatewayEventRequestContextV2WithGenericAuthorizer, APIGatewayProxyStructuredResultV2> => ({
|
|
33
|
+
newInstance: (request, body) => Promise.resolve(new DefaultLambdaMapper(request, body))
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
const payloadV1PathParameterResolver: PathParameterResolver<APIGatewayProxyEvent> = {
|
|
@@ -43,22 +43,22 @@ const payloadV2PathParameterResolver: PathParameterResolver<APIGatewayProxyEvent
|
|
|
43
43
|
store: (event: APIGatewayProxyEventV2, params: APIGatewayProxyEventPathParameters) => event.pathParameters = params
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
export type LambdaPayloadVersion<
|
|
47
|
-
mapper: LambdaMapperFactory<
|
|
46
|
+
export type LambdaPayloadVersion<Event, HandlerResponse> = {
|
|
47
|
+
mapper: LambdaMapperFactory<Event, HandlerResponse>,
|
|
48
48
|
resolver: PathParameterResolver<Event>,
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
export const v1 =
|
|
52
|
-
mapper: payloadV1JwtAuthorizerLambdaMapper
|
|
51
|
+
export const v1 = (): LambdaPayloadVersion<APIGatewayProxyEvent, APIGatewayProxyResult> => ({
|
|
52
|
+
mapper: payloadV1JwtAuthorizerLambdaMapper(),
|
|
53
53
|
resolver: payloadV1PathParameterResolver,
|
|
54
54
|
})
|
|
55
55
|
|
|
56
|
-
export const v2 =
|
|
57
|
-
mapper: payloadV2LambdaAuthorizerLambdaMapper
|
|
56
|
+
export const v2 = (): LambdaPayloadVersion<APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2> => ({
|
|
57
|
+
mapper: payloadV2LambdaAuthorizerLambdaMapper(),
|
|
58
58
|
resolver: payloadV2PathParameterResolver,
|
|
59
59
|
})
|
|
60
60
|
|
|
61
|
-
class DefaultLambdaMapper
|
|
61
|
+
class DefaultLambdaMapper implements LambdaMapper<APIGatewayProxyEvent & APIGatewayEventRequestContextV2WithGenericAuthorizer, APIGatewayProxyResult & APIGatewayProxyStructuredResultV2> {
|
|
62
62
|
private readonly url: URL
|
|
63
63
|
private readonly authorizationHeaderValue: string
|
|
64
64
|
private readonly time: number
|
|
@@ -120,7 +120,7 @@ class DefaultLambdaMapper<AuthorizerContext> implements LambdaMapper<AuthorizerC
|
|
|
120
120
|
return [];
|
|
121
121
|
};
|
|
122
122
|
|
|
123
|
-
event = (
|
|
123
|
+
event = (authorizerResult: APIGatewayAuthorizerResult): APIGatewayProxyEvent & APIGatewayEventRequestContextV2WithGenericAuthorizer => ({
|
|
124
124
|
version: '2.0',
|
|
125
125
|
rawPath: this.url.pathname,
|
|
126
126
|
rawQueryString: this.url.search,
|
|
@@ -152,7 +152,11 @@ class DefaultLambdaMapper<AuthorizerContext> implements LambdaMapper<AuthorizerC
|
|
|
152
152
|
timeEpoch: this.time,
|
|
153
153
|
accountId: '',
|
|
154
154
|
apiId: '',
|
|
155
|
-
authorizer:
|
|
155
|
+
authorizer: {
|
|
156
|
+
...authorizerResult,
|
|
157
|
+
...authorizerResult?.context,
|
|
158
|
+
lambda: authorizerResult?.context,
|
|
159
|
+
},
|
|
156
160
|
httpMethod: '',
|
|
157
161
|
identity: {
|
|
158
162
|
accessKey: '',
|
|
@@ -189,14 +193,14 @@ class DefaultLambdaMapper<AuthorizerContext> implements LambdaMapper<AuthorizerC
|
|
|
189
193
|
|
|
190
194
|
authorization = () => this.authorizationHeaderValue
|
|
191
195
|
}
|
|
192
|
-
interface LambdaMapper<
|
|
193
|
-
event(context:
|
|
196
|
+
interface LambdaMapper<Event, HandlerResponse> {
|
|
197
|
+
event(context: APIGatewayAuthorizerResult): Event
|
|
194
198
|
authorization(): string
|
|
195
199
|
toResponse: (response: HandlerResponse) => Response
|
|
196
200
|
}
|
|
197
201
|
|
|
198
|
-
export interface LambdaMapperFactory<
|
|
199
|
-
newInstance(request: IncomingMessage, body: string): Promise<LambdaMapper<
|
|
202
|
+
export interface LambdaMapperFactory<Event, LambdaResponse> {
|
|
203
|
+
newInstance(request: IncomingMessage, body: string): Promise<LambdaMapper<Event, LambdaResponse>>;
|
|
200
204
|
}
|
|
201
205
|
|
|
202
206
|
class StringResponse extends Response {
|
|
@@ -229,7 +233,7 @@ class Base64EncodedResponse extends Response {
|
|
|
229
233
|
}
|
|
230
234
|
}
|
|
231
235
|
|
|
232
|
-
type APIGatewayEventRequestContextV2WithGenericAuthorizer
|
|
236
|
+
type APIGatewayEventRequestContextV2WithGenericAuthorizer = APIGatewayProxyEventV2WithRequestContext<APIGatewayEventRequestContextV2WithAuthorizer<APIGatewayAuthorizerResult>>;
|
|
233
237
|
|
|
234
238
|
export type PathParameterResolver<Event> = {
|
|
235
239
|
locate: (event: Event) => string
|