@webiny/handler-aws 0.0.0-unstable.1e66d121db → 0.0.0-unstable.40876133bb

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/gateway/index.js CHANGED
@@ -29,6 +29,48 @@ var _plugins = require("../plugins");
29
29
 
30
30
  var _types = require("../types");
31
31
 
32
+ const getHeader = (headers, header) => {
33
+ for (const key in headers) {
34
+ if (key.toLowerCase() !== header) {
35
+ continue;
36
+ }
37
+
38
+ return headers[key];
39
+ }
40
+
41
+ return undefined;
42
+ };
43
+
44
+ const defaultContentType = "application/json";
45
+ const defaultCharset = "utf-8";
46
+ /**
47
+ * We need to attach default headers to the request, so it does not break if there is none sent.
48
+ */
49
+
50
+ const attachRequiredProperties = event => {
51
+ /**
52
+ * A possibility that headers are not defined?
53
+ * Maybe during testing?
54
+ */
55
+ if (!event.headers) {
56
+ event.headers = {};
57
+ }
58
+
59
+ const contentType = getHeader(event.headers, "content-type");
60
+ /**
61
+ * We check the existing content type and add the default one if it does not exist.
62
+ *
63
+ * Also, if the content-type is the application/json, and the body is not sent, we add it.
64
+ */
65
+
66
+ if (!contentType) {
67
+ event.headers["content-type"] = [defaultContentType, `charset=${defaultCharset}`].join(";");
68
+ event.body = "{}";
69
+ } else if (!event.body && contentType.startsWith(defaultContentType)) {
70
+ event.body = "{}";
71
+ }
72
+ };
73
+
32
74
  const createHandler = params => {
33
75
  return (event, context) => {
34
76
  var _params$http;
@@ -49,6 +91,7 @@ const createHandler = params => {
49
91
  throw new Error(`To run @webiny/handler-aws/gateway, you must have at least one RoutePlugin set.`);
50
92
  }
51
93
 
94
+ attachRequiredProperties(event);
52
95
  const appLambda = (0, _awsLambda.default)(app, (0, _objectSpread2.default)({
53
96
  decorateRequest: true,
54
97
  serializeLambdaArguments: true,
@@ -1 +1 @@
1
- {"version":3,"names":["createHandler","params","event","context","app","createBaseHandler","plugins","options","logger","http","debug","registerDefaultPlugins","webiny","byType","RoutePlugin","type","length","Error","appLambda","awsLambdaFastify","decorateRequest","serializeLambdaArguments","decorationPropertyName","enforceBase64","response","headers","Base64EncodeHeader","encoded","binary","lambdaOptions"],"sources":["index.ts"],"sourcesContent":["import awsLambdaFastify, {\n LambdaFastifyOptions as LambdaOptions,\n LambdaResponse\n} from \"@fastify/aws-lambda\";\nimport { APIGatewayEvent, Context as LambdaContext } from \"aws-lambda\";\nimport {\n createHandler as createBaseHandler,\n CreateHandlerParams as BaseCreateHandlerParams,\n RoutePlugin,\n createRoute\n} from \"@webiny/handler\";\nimport { registerDefaultPlugins } from \"~/plugins\";\nimport { Base64EncodeHeader } from \"~/types\";\n\nexport interface HandlerCallable {\n (event: APIGatewayEvent, ctx: LambdaContext): Promise<LambdaResponse>;\n}\n\nexport interface CreateHandlerParams extends BaseCreateHandlerParams {\n http?: {\n debug?: boolean;\n };\n lambdaOptions?: LambdaOptions;\n}\n\nexport const createHandler = (params: CreateHandlerParams): HandlerCallable => {\n return (event, context) => {\n const app = createBaseHandler({\n plugins: params.plugins,\n options: {\n logger: params.http?.debug === true,\n ...(params.options || {})\n }\n });\n /**\n * We always must add our default plugins to the app.\n */\n registerDefaultPlugins(app.webiny);\n\n if (app.webiny.plugins.byType<RoutePlugin>(RoutePlugin.type).length === 0) {\n throw new Error(\n `To run @webiny/handler-aws/gateway, you must have at least one RoutePlugin set.`\n );\n }\n const appLambda = awsLambdaFastify(app, {\n decorateRequest: true,\n serializeLambdaArguments: true,\n decorationPropertyName: \"awsLambda\",\n enforceBase64: response => {\n return (\n !!response.headers[Base64EncodeHeader.encoded] ||\n !!response.headers[Base64EncodeHeader.binary]\n );\n },\n ...(params.lambdaOptions || {})\n });\n return appLambda(event, context);\n };\n};\n\nexport { RoutePlugin, createRoute };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;;AAKA;;AAMA;;AACA;;AAaO,MAAMA,aAAa,GAAIC,MAAD,IAAkD;EAC3E,OAAO,CAACC,KAAD,EAAQC,OAAR,KAAoB;IAAA;;IACvB,MAAMC,GAAG,GAAG,IAAAC,sBAAA,EAAkB;MAC1BC,OAAO,EAAEL,MAAM,CAACK,OADU;MAE1BC,OAAO;QACHC,MAAM,EAAE,iBAAAP,MAAM,CAACQ,IAAP,8DAAaC,KAAb,MAAuB;MAD5B,GAECT,MAAM,CAACM,OAAP,IAAkB,EAFnB;IAFmB,CAAlB,CAAZ;IAOA;AACR;AACA;;IACQ,IAAAI,+BAAA,EAAuBP,GAAG,CAACQ,MAA3B;;IAEA,IAAIR,GAAG,CAACQ,MAAJ,CAAWN,OAAX,CAAmBO,MAAnB,CAAuCC,oBAAA,CAAYC,IAAnD,EAAyDC,MAAzD,KAAoE,CAAxE,EAA2E;MACvE,MAAM,IAAIC,KAAJ,CACD,iFADC,CAAN;IAGH;;IACD,MAAMC,SAAS,GAAG,IAAAC,kBAAA,EAAiBf,GAAjB;MACdgB,eAAe,EAAE,IADH;MAEdC,wBAAwB,EAAE,IAFZ;MAGdC,sBAAsB,EAAE,WAHV;MAIdC,aAAa,EAAEC,QAAQ,IAAI;QACvB,OACI,CAAC,CAACA,QAAQ,CAACC,OAAT,CAAiBC,yBAAA,CAAmBC,OAApC,CAAF,IACA,CAAC,CAACH,QAAQ,CAACC,OAAT,CAAiBC,yBAAA,CAAmBE,MAApC,CAFN;MAIH;IATa,GAUV3B,MAAM,CAAC4B,aAAP,IAAwB,EAVd,EAAlB;IAYA,OAAOX,SAAS,CAAChB,KAAD,EAAQC,OAAR,CAAhB;EACH,CA/BD;AAgCH,CAjCM"}
1
+ {"version":3,"names":["getHeader","headers","header","key","toLowerCase","undefined","defaultContentType","defaultCharset","attachRequiredProperties","event","contentType","join","body","startsWith","createHandler","params","context","app","createBaseHandler","plugins","options","logger","http","debug","registerDefaultPlugins","webiny","byType","RoutePlugin","type","length","Error","appLambda","awsLambdaFastify","decorateRequest","serializeLambdaArguments","decorationPropertyName","enforceBase64","response","Base64EncodeHeader","encoded","binary","lambdaOptions"],"sources":["index.ts"],"sourcesContent":["import awsLambdaFastify, {\n LambdaFastifyOptions as LambdaOptions,\n LambdaResponse\n} from \"@fastify/aws-lambda\";\nimport { APIGatewayEvent, Context as LambdaContext } from \"aws-lambda\";\nimport {\n createHandler as createBaseHandler,\n CreateHandlerParams as BaseCreateHandlerParams,\n RoutePlugin,\n createRoute\n} from \"@webiny/handler\";\nimport { registerDefaultPlugins } from \"~/plugins\";\nimport { Base64EncodeHeader } from \"~/types\";\nimport { APIGatewayProxyEventHeaders } from \"aws-lambda/trigger/api-gateway-proxy\";\n\nexport interface HandlerCallable {\n (event: APIGatewayEvent, ctx: LambdaContext): Promise<LambdaResponse>;\n}\n\nexport interface CreateHandlerParams extends BaseCreateHandlerParams {\n http?: {\n debug?: boolean;\n };\n lambdaOptions?: LambdaOptions;\n}\n\nconst getHeader = (headers: APIGatewayProxyEventHeaders, header: string): string | undefined => {\n for (const key in headers) {\n if (key.toLowerCase() !== header) {\n continue;\n }\n return headers[key];\n }\n return undefined;\n};\n\nconst defaultContentType = \"application/json\";\nconst defaultCharset = \"utf-8\";\n/**\n * We need to attach default headers to the request, so it does not break if there is none sent.\n */\nconst attachRequiredProperties = (event: APIGatewayEvent): void => {\n /**\n * A possibility that headers are not defined?\n * Maybe during testing?\n */\n if (!event.headers) {\n event.headers = {};\n }\n const contentType = getHeader(event.headers, \"content-type\");\n /**\n * We check the existing content type and add the default one if it does not exist.\n *\n * Also, if the content-type is the application/json, and the body is not sent, we add it.\n */\n if (!contentType) {\n event.headers[\"content-type\"] = [defaultContentType, `charset=${defaultCharset}`].join(\";\");\n event.body = \"{}\";\n } else if (!event.body && contentType.startsWith(defaultContentType)) {\n event.body = \"{}\";\n }\n};\n\nexport const createHandler = (params: CreateHandlerParams): HandlerCallable => {\n return (event, context) => {\n const app = createBaseHandler({\n plugins: params.plugins,\n options: {\n logger: params.http?.debug === true,\n ...(params.options || {})\n }\n });\n /**\n * We always must add our default plugins to the app.\n */\n registerDefaultPlugins(app.webiny);\n\n if (app.webiny.plugins.byType<RoutePlugin>(RoutePlugin.type).length === 0) {\n throw new Error(\n `To run @webiny/handler-aws/gateway, you must have at least one RoutePlugin set.`\n );\n }\n attachRequiredProperties(event);\n\n const appLambda = awsLambdaFastify(app, {\n decorateRequest: true,\n serializeLambdaArguments: true,\n decorationPropertyName: \"awsLambda\",\n enforceBase64: response => {\n return (\n !!response.headers[Base64EncodeHeader.encoded] ||\n !!response.headers[Base64EncodeHeader.binary]\n );\n },\n ...(params.lambdaOptions || {})\n });\n return appLambda(event, context);\n };\n};\n\nexport { RoutePlugin, createRoute };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;;AAKA;;AAMA;;AACA;;AAcA,MAAMA,SAAS,GAAG,CAACC,OAAD,EAAuCC,MAAvC,KAA8E;EAC5F,KAAK,MAAMC,GAAX,IAAkBF,OAAlB,EAA2B;IACvB,IAAIE,GAAG,CAACC,WAAJ,OAAsBF,MAA1B,EAAkC;MAC9B;IACH;;IACD,OAAOD,OAAO,CAACE,GAAD,CAAd;EACH;;EACD,OAAOE,SAAP;AACH,CARD;;AAUA,MAAMC,kBAAkB,GAAG,kBAA3B;AACA,MAAMC,cAAc,GAAG,OAAvB;AACA;AACA;AACA;;AACA,MAAMC,wBAAwB,GAAIC,KAAD,IAAkC;EAC/D;AACJ;AACA;AACA;EACI,IAAI,CAACA,KAAK,CAACR,OAAX,EAAoB;IAChBQ,KAAK,CAACR,OAAN,GAAgB,EAAhB;EACH;;EACD,MAAMS,WAAW,GAAGV,SAAS,CAACS,KAAK,CAACR,OAAP,EAAgB,cAAhB,CAA7B;EACA;AACJ;AACA;AACA;AACA;;EACI,IAAI,CAACS,WAAL,EAAkB;IACdD,KAAK,CAACR,OAAN,CAAc,cAAd,IAAgC,CAACK,kBAAD,EAAsB,WAAUC,cAAe,EAA/C,EAAkDI,IAAlD,CAAuD,GAAvD,CAAhC;IACAF,KAAK,CAACG,IAAN,GAAa,IAAb;EACH,CAHD,MAGO,IAAI,CAACH,KAAK,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@@
1
1
  {
2
2
  "name": "@webiny/handler-aws",
3
- "version": "0.0.0-unstable.1e66d121db",
3
+ "version": "0.0.0-unstable.40876133bb",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,19 +14,19 @@
14
14
  "dependencies": {
15
15
  "@babel/runtime": "7.19.0",
16
16
  "@fastify/aws-lambda": "3.1.3",
17
- "@webiny/handler": "0.0.0-unstable.1e66d121db",
18
- "@webiny/handler-client": "0.0.0-unstable.1e66d121db",
19
- "@webiny/plugins": "0.0.0-unstable.1e66d121db",
17
+ "@webiny/handler": "0.0.0-unstable.40876133bb",
18
+ "@webiny/handler-client": "0.0.0-unstable.40876133bb",
19
+ "@webiny/plugins": "0.0.0-unstable.40876133bb",
20
20
  "aws-lambda": "1.0.7",
21
- "fastify": "4.7.0"
21
+ "fastify": "4.9.2"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@babel/cli": "^7.19.3",
25
25
  "@babel/core": "^7.19.3",
26
26
  "@babel/preset-env": "^7.19.4",
27
27
  "@babel/preset-typescript": "^7.18.6",
28
- "@webiny/cli": "^0.0.0-unstable.1e66d121db",
29
- "@webiny/project-utils": "^0.0.0-unstable.1e66d121db",
28
+ "@webiny/cli": "^0.0.0-unstable.40876133bb",
29
+ "@webiny/project-utils": "^0.0.0-unstable.40876133bb",
30
30
  "rimraf": "^3.0.2",
31
31
  "ttypescript": "^1.5.13",
32
32
  "typescript": "4.7.4"
@@ -39,5 +39,5 @@
39
39
  "build": "yarn webiny run build",
40
40
  "watch": "yarn webiny run watch"
41
41
  },
42
- "gitHead": "b670bf27c5039de1a2b0be764a09ba4cb94ad5e2"
42
+ "gitHead": "f33811072795d25c5787ae39808e75e3312fb247"
43
43
  }