@yopdev/dev-server 1.3.9 → 1.3.15

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,15 +1,20 @@
1
1
  import { HttpSettings } from "./http-server";
2
- import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
2
+ import { APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
3
3
  import { IncomingMessage } from "http";
4
4
  import { Service, Callback } from "./services";
5
+ export declare const UNAUTHORIZED: Error;
5
6
  export declare const newLambdaHttpProxy: (name: string, config: {
6
7
  settings: HttpSettings;
7
8
  routes: Route[];
8
- mapper?: (request: IncomingMessage, body: string) => APIGatewayProxyEvent;
9
+ mapper?: (request: IncomingMessage, body: string) => Promise<APIGatewayProxyEvent>;
10
+ authorizer?: Authorizer;
9
11
  }, callback?: Callback<string>) => Service<string>;
10
12
  export type Route = {
11
13
  method: RegExp;
12
14
  path: RegExp;
13
15
  weight: number;
16
+ authorizer?: Authorizer;
14
17
  handler: (event: APIGatewayProxyEvent) => Promise<APIGatewayProxyResult>;
15
18
  };
19
+ type Authorizer = (authorization: string) => Promise<APIGatewayEventDefaultAuthorizerContext | undefined>;
20
+ export {};
@@ -36,33 +36,51 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.newLambdaHttpProxy = void 0;
39
+ exports.newLambdaHttpProxy = exports.UNAUTHORIZED = void 0;
40
40
  var logging_1 = require("@yopdev/logging");
41
41
  var http_server_1 = require("./http-server");
42
42
  var mappers_1 = require("./mappers");
43
43
  var responses_1 = require("./responses");
44
44
  var services_1 = require("./services");
45
- var newLambdaHttpProxy = function (name, config, callback) { var _a; return new services_1.Service(new LambdaHttpProxy(name, config.settings, config.routes, (_a = config.mapper) !== null && _a !== void 0 ? _a : mappers_1.mapToLambdaEvent), callback); };
45
+ exports.UNAUTHORIZED = new Error('UNAUTHORIZED');
46
+ var newLambdaHttpProxy = function (name, config, callback) {
47
+ var _a, _b;
48
+ return new services_1.Service(new LambdaHttpProxy(name, config.settings, config.routes, (_a = config.mapper) !== null && _a !== void 0 ? _a : mappers_1.mapToLambdaEvent, (_b = config.authorizer) !== null && _b !== void 0 ? _b : (function () { return Promise.resolve(undefined); })), callback);
49
+ };
46
50
  exports.newLambdaHttpProxy = newLambdaHttpProxy;
47
51
  var LambdaHttpProxy = /** @class */ (function () {
48
- function LambdaHttpProxy(name, settings, routes, mapper) {
52
+ function LambdaHttpProxy(name, settings, routes, mapper, defaultAuthorizer) {
49
53
  var _this = this;
50
54
  this.name = name;
51
55
  this.routes = routes;
52
56
  this.mapper = mapper;
57
+ this.defaultAuthorizer = defaultAuthorizer;
53
58
  this.start = function () { return _this.server.start(); };
54
59
  this.stop = function () { return _this.server.stop(); };
55
- this.handler = function (lambdaHandler) {
60
+ this.handler = function (authorizer, lambdaHandler) {
56
61
  return function (request, body, response) {
57
- return lambdaHandler(_this.mapper(request, body))
58
- .then(function (lambda) {
59
- var _a, _b, _c, _d, _e, _f;
60
- return (0, responses_1.writeResponse)(response, lambda.statusCode, lambda.body, (_c = (_b = (_a = lambda.headers) === null || _a === void 0 ? void 0 : _a['Content-Type']) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : undefined, (_f = (_e = (_d = lambda.headers) === null || _d === void 0 ? void 0 : _d['Location']) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : undefined);
61
- })
62
- .catch(function (e) {
63
- _this.LOGGER.error(e, 'request failed to execute');
64
- (0, responses_1.internalServerError)(response, e.body);
65
- });
62
+ return _this.mapper(request, body)
63
+ .then(function (proxyEvent) { return __awaiter(_this, void 0, void 0, function () {
64
+ var _this = this;
65
+ return __generator(this, function (_a) {
66
+ return [2 /*return*/, authorizer(proxyEvent.headers['authorization'])
67
+ .then(function (context) { return __awaiter(_this, void 0, void 0, function () {
68
+ var _this = this;
69
+ return __generator(this, function (_a) {
70
+ proxyEvent.requestContext.authorizer = context;
71
+ return [2 /*return*/, lambdaHandler(proxyEvent)
72
+ .then(function (lambda) {
73
+ var _a, _b, _c, _d, _e, _f;
74
+ return (0, responses_1.writeResponse)(response, lambda.statusCode, lambda.body, (_c = (_b = (_a = lambda.headers) === null || _a === void 0 ? void 0 : _a['Content-Type']) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : undefined, (_f = (_e = (_d = lambda.headers) === null || _d === void 0 ? void 0 : _d['Location']) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : undefined);
75
+ })
76
+ .catch(function (e) {
77
+ _this.LOGGER.error(e, 'request failed to execute');
78
+ (0, responses_1.internalServerError)(response, e.body);
79
+ })];
80
+ });
81
+ }); }, function (e) { return e === exports.UNAUTHORIZED ? (0, responses_1.writeResponse)(response, 401, '') : Promise.reject(e); })];
82
+ });
83
+ }); });
66
84
  };
67
85
  };
68
86
  this.fallback = function (request, _, response) { return __awaiter(_this, void 0, void 0, function () {
@@ -77,7 +95,7 @@ var LambdaHttpProxy = /** @class */ (function () {
77
95
  return ((_a = _this.routes.filter(function (r) { return request.method.match(r.method); })
78
96
  .filter(function (r) { return request.url.match(r.path); })
79
97
  .sort(function (r1, r2) { return r2.weight - r1.weight; })
80
- .map(function (r) { return _this.handler(r.handler); })
98
+ .map(function (r) { var _a; return _this.handler((_a = r.authorizer) !== null && _a !== void 0 ? _a : _this.defaultAuthorizer, r.handler); })
81
99
  .find(function () { return true; })) !== null && _a !== void 0 ? _a : _this.fallback)(request, body, response);
82
100
  };
83
101
  this.server = new http_server_1.HttpServer(name, settings, this.resolveRoute);
@@ -3,4 +3,4 @@ import { Message } from "@aws-sdk/client-sqs";
3
3
  import { APIGatewayProxyEvent, SQSRecord } from "aws-lambda";
4
4
  import { IncomingMessage } from "http";
5
5
  export declare const mapToLambdaSqsRecord: (message: Message) => SQSRecord;
6
- export declare function mapToLambdaEvent(req: IncomingMessage, requestBody: string): APIGatewayProxyEvent;
6
+ export declare function mapToLambdaEvent(req: IncomingMessage, requestBody: string): Promise<APIGatewayProxyEvent>;
@@ -39,7 +39,7 @@ function mapToLambdaEvent(req, requestBody) {
39
39
  headers['accept'] = req.headers['accept'];
40
40
  if (req.headers['authorization'])
41
41
  headers['authorization'] = req.headers['authorization'];
42
- return {
42
+ return Promise.resolve({
43
43
  httpMethod: req.method,
44
44
  body: requestBody,
45
45
  headers: headers,
@@ -87,7 +87,7 @@ function mapToLambdaEvent(req, requestBody) {
87
87
  resourcePath: '',
88
88
  stage: '',
89
89
  },
90
- };
90
+ });
91
91
  }
92
92
  exports.mapToLambdaEvent = mapToLambdaEvent;
93
93
  function extractClaims(req) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yopdev/dev-server",
3
- "version": "1.3.9",
3
+ "version": "1.3.15",
4
4
  "scripts": {
5
5
  "compile": "tsc",
6
6
  "pretest": "npm run compile",