@aesop-fables/triginta 0.2.3 → 0.3.0

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.
@@ -1,17 +1,17 @@
1
1
  import { IServiceContainer, IServiceModule, Newable } from '@aesop-fables/containr';
2
- import { APIGatewayProxyEventV2, Handler } from 'aws-lambda';
2
+ import { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2, Handler } from 'aws-lambda';
3
3
  import { IHttpEndpoint } from './IHttpEndpoint';
4
4
  export declare type NonNoisyEvent = Omit<APIGatewayProxyEventV2, 'requestContext'>;
5
5
  export interface BootstrappedHttpLambdaContext {
6
- createHttpLambda<Input, Output>(newable: Newable<IHttpEndpoint<Input, Output>>): Handler<APIGatewayProxyEventV2, Output>;
6
+ createHttpLambda<Input, Output>(newable: Newable<IHttpEndpoint<Input, Output>>): Handler<APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2>;
7
7
  }
8
8
  export interface IHttpLambdaFactory {
9
- createHandler<Input, Output>(newable: Newable<IHttpEndpoint<Input, Output>>): Handler<APIGatewayProxyEventV2, Output>;
9
+ createHandler<Input, Output>(newable: Newable<IHttpEndpoint<Input, Output>>): Handler<APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2>;
10
10
  }
11
11
  export declare class HttpLambdaFactory implements IHttpLambdaFactory {
12
12
  private readonly container;
13
13
  constructor(container: IServiceContainer);
14
- createHandler<Input, Output>(newable: Newable<IHttpEndpoint<Input, Output>>): Handler<APIGatewayProxyEventV2, Output>;
14
+ createHandler<Input, Output>(newable: Newable<IHttpEndpoint<Input, Output>>): Handler<APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2>;
15
15
  }
16
16
  export declare const useTrigintaHttp: IServiceModule;
17
17
  export declare class HttpLambda {
package/lib/HttpLambda.js CHANGED
@@ -31,7 +31,16 @@ class HttpLambdaFactory {
31
31
  const endpoint = this.container.resolve(newable);
32
32
  const { body: request } = event;
33
33
  const response = (yield endpoint.handle(request, event));
34
- return response;
34
+ // TODO -- Break this out into a response writer
35
+ let proxyResponse = response;
36
+ if (typeof (proxyResponse === null || proxyResponse === void 0 ? void 0 : proxyResponse.statusCode) === 'undefined') {
37
+ // TODO -- Is 200 always correct here?
38
+ proxyResponse = {
39
+ statusCode: 200,
40
+ body: response ? JSON.stringify(response) : undefined,
41
+ };
42
+ }
43
+ return proxyResponse;
35
44
  }
36
45
  finally {
37
46
  if (childContainer) {
@@ -1,10 +1 @@
1
- import middy from '@middy/core';
2
- export declare const errorWrapper: () => {
3
- onError: (handler: middy.HandlerLambda, next: middy.NextFunction) => void;
4
- };
5
- export declare const convertNullTo200: () => {
6
- after: (handler: middy.HandlerLambda, next: middy.NextFunction) => void;
7
- };
8
- export declare const xssFilter: () => {
9
- before: (handler: middy.HandlerLambda) => Promise<void>;
10
- };
1
+ export {};
package/lib/Middleware.js CHANGED
@@ -1,72 +1,55 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
2
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.xssFilter = exports.convertNullTo200 = exports.errorWrapper = void 0;
16
- const xss_1 = __importDefault(require("xss"));
17
- const errorWrapper = () => ({
18
- onError: (handler, next) => {
19
- if (handler.error) {
20
- handler.response = {
21
- statusCode: 400,
22
- headers: {
23
- 'Content-Type': 'application/json',
24
- 'Access-Control-Allow-Origin': '*',
25
- 'Access-Control-Allow-Headers': '*',
26
- 'Access-Control-Allow-Methods': '*',
27
- },
28
- body: JSON.stringify({
29
- error: handler.error,
30
- }),
31
- };
32
- return next();
33
- }
34
- return next(handler.error);
35
- },
36
- });
37
- exports.errorWrapper = errorWrapper;
38
- const convertNullTo200 = () => ({
39
- after: (handler, next) => {
40
- if (handler.response === null) {
41
- handler.response = {
42
- statusCode: 200,
43
- headers: {
44
- 'Content-Type': 'application/json',
45
- 'Access-Control-Allow-Origin': '*',
46
- 'Access-Control-Allow-Headers': '*',
47
- 'Access-Control-Allow-Methods': '*',
48
- },
49
- body: '',
50
- };
51
- }
52
- return next();
53
- },
54
- });
55
- exports.convertNullTo200 = convertNullTo200;
56
- const xssFilter = () => ({
57
- before: (handler) => __awaiter(void 0, void 0, void 0, function* () {
58
- const { event } = handler;
59
- const { body } = event;
60
- if (body) {
61
- const keys = Object.keys(body);
62
- for (let i = 0; i < keys.length; i++) {
63
- const key = keys[i];
64
- const value = body[key];
65
- if (value && typeof value === 'string') {
66
- body[key] = (0, xss_1.default)(value);
67
- }
68
- }
69
- }
70
- }),
71
- });
72
- exports.xssFilter = xssFilter;
3
+ // export const errorWrapper = () => ({
4
+ // onError: (handler: middy.MiddyfiedHandler, next: middy.MiddlewareFn) => {
5
+ // if (handler.error) {
6
+ // handler.response = {
7
+ // statusCode: 400,
8
+ // headers: {
9
+ // 'Content-Type': 'application/json',
10
+ // 'Access-Control-Allow-Origin': '*',
11
+ // 'Access-Control-Allow-Headers': '*',
12
+ // 'Access-Control-Allow-Methods': '*',
13
+ // },
14
+ // body: JSON.stringify({
15
+ // error: handler.error,
16
+ // }),
17
+ // };
18
+ // return next();
19
+ // }
20
+ // return next(handler.error);
21
+ // },
22
+ // });
23
+ // export const convertNullTo200 = () => ({
24
+ // after: (handler: middy.HandlerLambda, next: middy.NextFunction) => {
25
+ // if (handler.response === null) {
26
+ // handler.response = {
27
+ // statusCode: 200,
28
+ // headers: {
29
+ // 'Content-Type': 'application/json',
30
+ // 'Access-Control-Allow-Origin': '*',
31
+ // 'Access-Control-Allow-Headers': '*',
32
+ // 'Access-Control-Allow-Methods': '*',
33
+ // },
34
+ // body: '',
35
+ // };
36
+ // }
37
+ // return next();
38
+ // },
39
+ // });
40
+ // export const xssFilter = () => ({
41
+ // before: async (handler: middy.HandlerLambda) => {
42
+ // const { event } = handler;
43
+ // const { body } = event;
44
+ // if (body) {
45
+ // const keys = Object.keys(body);
46
+ // for (let i = 0; i < keys.length; i++) {
47
+ // const key = keys[i];
48
+ // const value = body[key];
49
+ // if (value && typeof value === 'string') {
50
+ // body[key] = xss(value);
51
+ // }
52
+ // }
53
+ // }
54
+ // },
55
+ // });
package/lib/index.d.ts CHANGED
@@ -6,4 +6,5 @@ export * from './HttpLambda';
6
6
  export * from './HttpLambdaServices';
7
7
  export { default as RouteRegistry } from './RouteRegistry';
8
8
  export * from './invokeHttpHandler';
9
+ export * from './IConfiguredRoute';
9
10
  export { IHandler, IHttpEndpoint };
package/lib/index.js CHANGED
@@ -25,3 +25,4 @@ __exportStar(require("./HttpLambdaServices"), exports);
25
25
  var RouteRegistry_1 = require("./RouteRegistry");
26
26
  Object.defineProperty(exports, "RouteRegistry", { enumerable: true, get: function () { return __importDefault(RouteRegistry_1).default; } });
27
27
  __exportStar(require("./invokeHttpHandler"), exports);
28
+ __exportStar(require("./IConfiguredRoute"), exports);
@@ -1,11 +1,14 @@
1
- import { APIGatewayProxyEventPathParameters, APIGatewayProxyEventV2 } from 'aws-lambda';
1
+ import { APIGatewayProxyEventPathParameters, APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2 } from 'aws-lambda';
2
+ import { IServiceContainer } from '@aesop-fables/containr';
2
3
  import { IConfiguredRoute } from './IConfiguredRoute';
3
4
  export interface InvocationContext {
4
5
  configuredRoute: IConfiguredRoute;
6
+ container: IServiceContainer;
5
7
  body?: any;
6
8
  path: string;
7
9
  }
8
10
  export declare function parseRouteParams(route: string, path: string): APIGatewayProxyEventPathParameters;
9
- export declare function parsePathParameters(context: InvocationContext): Partial<APIGatewayProxyEventV2>;
10
- export declare function createApiGatewayEvent(context: InvocationContext): Partial<APIGatewayProxyEventV2>;
11
- export declare function invokeHttpHandler<Output>(context: InvocationContext): Promise<Output>;
11
+ export declare function parsePathParameters(context: EventGenerationContext): Partial<APIGatewayProxyEventV2>;
12
+ export declare type EventGenerationContext = Omit<InvocationContext, 'container'>;
13
+ export declare function createApiGatewayEvent(context: EventGenerationContext): Partial<APIGatewayProxyEventV2>;
14
+ export declare function invokeHttpHandler<Output>(context: InvocationContext): Promise<APIGatewayProxyStructuredResultV2>;
@@ -13,7 +13,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.invokeHttpHandler = exports.createApiGatewayEvent = exports.parsePathParameters = exports.parseRouteParams = void 0;
16
- const HttpLambda_1 = require("./HttpLambda");
17
16
  const node_querystring_1 = __importDefault(require("node:querystring"));
18
17
  const HttpLambdaServices_1 = require("./HttpLambdaServices");
19
18
  function normalizeAndParse(input) {
@@ -78,7 +77,7 @@ function createApiGatewayEvent(context) {
78
77
  accept: '*/*',
79
78
  'accept-encoding': 'gzip, deflate, br',
80
79
  // 'content-length': '0',
81
- // 'content-type': 'application/json',
80
+ 'content-type': 'application/json',
82
81
  host: '',
83
82
  'user-agent': 'triginta/1.0',
84
83
  'x-amzn-trace-id': 'Root=1-63e26f79-577a8db87d3b31fa4da65566',
@@ -112,10 +111,11 @@ exports.createApiGatewayEvent = createApiGatewayEvent;
112
111
  // TODO -- We need to rename this to make it clear that it's for testing ONLY
113
112
  function invokeHttpHandler(context) {
114
113
  return __awaiter(this, void 0, void 0, function* () {
115
- const container = HttpLambda_1.HttpLambda.getContainer();
114
+ const { container } = context;
116
115
  const factory = container.get(HttpLambdaServices_1.HttpLambdaServices.HttpLambdaFactory);
117
116
  const configuredHandler = factory.createHandler(context.configuredRoute.constructor);
118
- return configuredHandler(createApiGatewayEvent(context), {
117
+ const event = createApiGatewayEvent(context);
118
+ const handlerContext = {
119
119
  callbackWaitsForEmptyEventLoop: false,
120
120
  functionName: 'httpLambda',
121
121
  functionVersion: '0.1',
@@ -125,7 +125,7 @@ function invokeHttpHandler(context) {
125
125
  logGroupName: 'test-group',
126
126
  logStreamName: 'test-stream',
127
127
  getRemainingTimeInMillis: function () {
128
- throw new Error('Function not implemented.');
128
+ return 1000;
129
129
  },
130
130
  done: function (error, result) {
131
131
  throw new Error('Function not implemented.');
@@ -136,10 +136,9 @@ function invokeHttpHandler(context) {
136
136
  succeed: function (messageOrObject) {
137
137
  throw new Error('Function not implemented.');
138
138
  },
139
- }, () => {
140
- // no-op
141
- throw new Error('Not supported');
142
- });
139
+ };
140
+ const response = yield configuredHandler(event, handlerContext);
141
+ return response;
143
142
  });
144
143
  }
145
144
  exports.invokeHttpHandler = invokeHttpHandler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aesop-fables/triginta",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "A lightweight framework that wraps the basic infrastructure usages of AWS Lambda (SQS, Kinesis, etc.).",
5
5
  "type": "commonjs",
6
6
  "exports": {
@@ -45,9 +45,9 @@
45
45
  "lib/**/*"
46
46
  ],
47
47
  "dependencies": {
48
- "@middy/core": "^1.5.2",
49
- "@middy/http-error-handler": "^1.5.2",
50
- "@middy/http-json-body-parser": "^1.5.2",
48
+ "@middy/core": "^4.2.3",
49
+ "@middy/http-error-handler": "^4.2.3",
50
+ "@middy/http-json-body-parser": "^4.2.3",
51
51
  "reflect-metadata": "^0.1.13",
52
52
  "xss": "^1.0.9"
53
53
  },