@webiny/handler 0.0.0-mt-2 β†’ 0.0.0-unstable.0156a4c312

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/README.md CHANGED
@@ -1,9 +1,11 @@
1
1
  # @webiny/handler
2
- [![](https://img.shields.io/npm/dw/@webiny/handler.svg)](https://www.npmjs.com/package/@webiny/handler)
3
- [![](https://img.shields.io/npm/v/@webiny/handler.svg)](https://www.npmjs.com/package/@webiny/handler)
4
- [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
- [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
2
 
7
- A tiny, universal, and plugin-based framework for creating (serverless) handlers, used within services like AWS Lambda, Azure Serverless Functions, and others.
3
+ > [!NOTE]
4
+ > This package is part of the [Webiny](https://www.webiny.com) monorepo.
5
+ > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
8
6
 
9
- A handler can be anything from a simple event-handler, to a fully-fledged GraphQL server. It all depends on the additional plugins that are passed upon the creating the handler.
7
+ πŸ“˜ **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
8
+
9
+ ---
10
+
11
+ _This README file is automatically generated during the publish process._
@@ -0,0 +1,24 @@
1
+ import type * as http from "http";
2
+ type ExtraHeaders = {
3
+ "content-type"?: string | undefined;
4
+ "x-webiny-version"?: http.OutgoingHttpHeader | undefined;
5
+ };
6
+ type AllHeaders = http.OutgoingHttpHeaders & ExtraHeaders;
7
+ export type StandardHeaderValue = http.OutgoingHttpHeader | undefined;
8
+ export type StandardHeaders = {
9
+ [K in keyof AllHeaders as string extends K ? never : number extends K ? never : K]: http.OutgoingHttpHeaders[K];
10
+ } & {
11
+ [name: string]: StandardHeaderValue;
12
+ };
13
+ type Setter<T> = ((value: T) => T) | T;
14
+ export declare class ResponseHeaders {
15
+ private readonly headers;
16
+ private constructor();
17
+ set<T extends keyof StandardHeaders>(header: T, setter: Setter<StandardHeaders[T]>): this;
18
+ merge(headers: ResponseHeaders): ResponseHeaders;
19
+ getHeaders(): {
20
+ [k: string]: StandardHeaderValue;
21
+ };
22
+ static create(initialHeaders?: StandardHeaders): ResponseHeaders;
23
+ }
24
+ export {};
@@ -0,0 +1,36 @@
1
+ function isFunction(setter) {
2
+ return "function" == typeof setter;
3
+ }
4
+ class ResponseHeaders {
5
+ constructor(initialHeaders){
6
+ this.headers = new Map();
7
+ if (initialHeaders) Object.keys(initialHeaders).forEach((key)=>{
8
+ this.headers.set(key, initialHeaders[key]);
9
+ });
10
+ }
11
+ set(header, setter) {
12
+ if (isFunction(setter)) {
13
+ const previousValue = this.headers.get(header);
14
+ const newValue = setter(previousValue);
15
+ this.headers.set(header, newValue);
16
+ return this;
17
+ }
18
+ this.headers.set(header, setter);
19
+ return this;
20
+ }
21
+ merge(headers) {
22
+ return ResponseHeaders.create({
23
+ ...this.getHeaders(),
24
+ ...headers.getHeaders()
25
+ });
26
+ }
27
+ getHeaders() {
28
+ return Object.fromEntries(this.headers);
29
+ }
30
+ static create(initialHeaders) {
31
+ return new ResponseHeaders(initialHeaders);
32
+ }
33
+ }
34
+ export { ResponseHeaders };
35
+
36
+ //# sourceMappingURL=ResponseHeaders.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ResponseHeaders.js","sources":["../src/ResponseHeaders.ts"],"sourcesContent":["import type * as http from \"http\";\n\ntype ExtraHeaders = {\n \"content-type\"?: string | undefined;\n \"x-webiny-version\"?: http.OutgoingHttpHeader | undefined;\n};\n\ntype AllHeaders = http.OutgoingHttpHeaders & ExtraHeaders;\n\nexport type StandardHeaderValue = http.OutgoingHttpHeader | undefined;\n\n// Extract known standard headers, and remove all non-string keys.\nexport type StandardHeaders = {\n [\n K in keyof AllHeaders as string extends K ? never : number extends K ? never : K\n ]: http.OutgoingHttpHeaders[K];\n} & {\n [name: string]: StandardHeaderValue;\n};\n\nfunction isFunction<T>(setter: unknown): setter is (value: T) => T {\n return typeof setter === \"function\";\n}\n\ntype Setter<T> = ((value: T) => T) | T;\n\nexport class ResponseHeaders {\n private readonly headers = new Map<keyof StandardHeaders, StandardHeaderValue>();\n\n private constructor(initialHeaders?: StandardHeaders) {\n if (initialHeaders) {\n (Object.keys(initialHeaders) as Array<keyof StandardHeaders>).forEach(key => {\n this.headers.set(key, initialHeaders[key]);\n });\n }\n }\n\n set<T extends keyof StandardHeaders>(header: T, setter: Setter<StandardHeaders[T]>) {\n if (isFunction<StandardHeaders[T]>(setter)) {\n const previousValue = this.headers.get(header) as StandardHeaders[T];\n const newValue = setter(previousValue);\n this.headers.set(header, newValue);\n return this;\n }\n\n this.headers.set(header, setter);\n\n return this;\n }\n\n merge(headers: ResponseHeaders) {\n return ResponseHeaders.create({ ...this.getHeaders(), ...headers.getHeaders() });\n }\n\n getHeaders() {\n return Object.fromEntries(this.headers);\n }\n\n static create(initialHeaders?: StandardHeaders) {\n return new ResponseHeaders(initialHeaders);\n }\n}\n"],"names":["isFunction","setter","ResponseHeaders","initialHeaders","Map","Object","key","header","previousValue","newValue","headers"],"mappings":"AAoBA,SAASA,WAAcC,MAAe;IAClC,OAAO,AAAkB,cAAlB,OAAOA;AAClB;AAIO,MAAMC;IAGT,YAAoBC,cAAgC,CAAE;aAFrC,OAAO,GAAG,IAAIC;QAG3B,IAAID,gBACCE,OAAO,IAAI,CAACF,gBAAiD,OAAO,CAACG,CAAAA;YAClE,IAAI,CAAC,OAAO,CAAC,GAAG,CAACA,KAAKH,cAAc,CAACG,IAAI;QAC7C;IAER;IAEA,IAAqCC,MAAS,EAAEN,MAAkC,EAAE;QAChF,IAAID,WAA+BC,SAAS;YACxC,MAAMO,gBAAgB,IAAI,CAAC,OAAO,CAAC,GAAG,CAACD;YACvC,MAAME,WAAWR,OAAOO;YACxB,IAAI,CAAC,OAAO,CAAC,GAAG,CAACD,QAAQE;YACzB,OAAO,IAAI;QACf;QAEA,IAAI,CAAC,OAAO,CAAC,GAAG,CAACF,QAAQN;QAEzB,OAAO,IAAI;IACf;IAEA,MAAMS,OAAwB,EAAE;QAC5B,OAAOR,gBAAgB,MAAM,CAAC;YAAE,GAAG,IAAI,CAAC,UAAU,EAAE;YAAE,GAAGQ,QAAQ,UAAU,EAAE;QAAC;IAClF;IAEA,aAAa;QACT,OAAOL,OAAO,WAAW,CAAC,IAAI,CAAC,OAAO;IAC1C;IAEA,OAAO,OAAOF,cAAgC,EAAE;QAC5C,OAAO,IAAID,gBAAgBC;IAC/B;AACJ"}
@@ -0,0 +1,5 @@
1
+ import type { Reply as IReply } from "../types.js";
2
+ export declare const Reply: import("@webiny/di").Abstraction<IReply>;
3
+ export declare namespace Reply {
4
+ type Interface = IReply;
5
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const Reply = createAbstraction("Reply");
3
+ export { Reply };
4
+
5
+ //# sourceMappingURL=Reply.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abstractions/Reply.js","sources":["../../src/abstractions/Reply.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Reply as IReply } from \"~/types.js\";\n\nexport const Reply = createAbstraction<IReply>(\"Reply\");\n\nexport namespace Reply {\n export type Interface = IReply;\n}\n"],"names":["Reply","createAbstraction"],"mappings":";AAGO,MAAMA,QAAQC,kBAA0B"}
@@ -0,0 +1,5 @@
1
+ import type { Request as IRequest } from "../types.js";
2
+ export declare const Request: import("@webiny/di").Abstraction<IRequest>;
3
+ export declare namespace Request {
4
+ type Interface = IRequest;
5
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const Request = createAbstraction("Request");
3
+ export { Request };
4
+
5
+ //# sourceMappingURL=Request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abstractions/Request.js","sources":["../../src/abstractions/Request.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Request as IRequest } from \"~/types.js\";\n\nexport const Request = createAbstraction<IRequest>(\"Request\");\n\nexport namespace Request {\n export type Interface = IRequest;\n}\n"],"names":["Request","createAbstraction"],"mappings":";AAGO,MAAMA,UAAUC,kBAA4B"}
File without changes
package/exports/api.js ADDED
File without changes
package/index.d.ts CHANGED
@@ -1 +1,6 @@
1
- export { default as createHandler } from "./createHandler";
1
+ export { RegisterExtensionPlugin, createRegisterExtensionPlugin, registerExtensions, RegisterBuildParamPlugin, createRegisterBuildParamPlugin, registerBuildParams } from "./plugins/RegisterExtensionPlugin.js";
2
+ export { stringifyError } from "./stringifyError.js";
3
+ export { Request } from "./abstractions/Request.js";
4
+ export { Reply } from "./abstractions/Reply.js";
5
+ export { ResponseHeaders } from "./ResponseHeaders.js";
6
+ export type { StandardHeaders, StandardHeaderValue } from "./ResponseHeaders.js";
package/index.js CHANGED
@@ -1,15 +1,5 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- Object.defineProperty(exports, "createHandler", {
9
- enumerable: true,
10
- get: function () {
11
- return _createHandler.default;
12
- }
13
- });
14
-
15
- var _createHandler = _interopRequireDefault(require("./createHandler"));
1
+ export { RegisterBuildParamPlugin, RegisterExtensionPlugin, createRegisterBuildParamPlugin, createRegisterExtensionPlugin, registerBuildParams, registerExtensions } from "./plugins/RegisterExtensionPlugin.js";
2
+ export { stringifyError } from "./stringifyError.js";
3
+ export { Request } from "./abstractions/Request.js";
4
+ export { Reply } from "./abstractions/Reply.js";
5
+ export { ResponseHeaders } from "./ResponseHeaders.js";
package/package.json CHANGED
@@ -1,38 +1,36 @@
1
1
  {
2
2
  "name": "@webiny/handler",
3
- "version": "0.0.0-mt-2",
4
- "main": "index.js",
5
- "license": "MIT",
3
+ "version": "0.0.0-unstable.0156a4c312",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./index.js",
7
+ "./*": "./*"
8
+ },
6
9
  "repository": {
7
10
  "type": "git",
8
11
  "url": "https://github.com/webiny/webiny-js.git"
9
12
  },
10
- "description": "A pluginable function for the cloud.",
13
+ "description": "Our small wrapper around fastify to ease handling of http requests for the system.",
11
14
  "contributors": [
12
- "Pavel Denisjuk <pavel@webiny.com>",
13
- "Sven Al Hamad <sven@webiny.com>",
14
- "Adrian Smijulj <adrian@webiny.com>"
15
+ "Bruno Zorić <bruno@webiny.com>"
15
16
  ],
17
+ "license": "MIT",
16
18
  "dependencies": {
17
- "@babel/runtime": "7.15.4",
18
- "@webiny/plugins": "0.0.0-mt-2"
19
+ "@webiny/di": "2.0.0",
20
+ "@webiny/feature": "0.0.0-unstable.0156a4c312",
21
+ "@webiny/plugins": "0.0.0-unstable.0156a4c312"
19
22
  },
20
23
  "devDependencies": {
21
- "@babel/cli": "^7.5.5",
22
- "@babel/core": "^7.5.5",
23
- "@babel/preset-env": "^7.5.5",
24
- "@webiny/cli": "^0.0.0-mt-2",
25
- "@webiny/project-utils": "^0.0.0-mt-2",
26
- "rimraf": "^3.0.2",
27
- "typescript": "^4.1.3"
24
+ "@webiny/build-tools": "0.0.0-unstable.0156a4c312",
25
+ "rimraf": "6.1.3",
26
+ "typescript": "7.0.2",
27
+ "vitest": "5.0.5"
28
28
  },
29
29
  "publishConfig": {
30
- "access": "public",
31
- "directory": "dist"
32
- },
33
- "scripts": {
34
- "build": "yarn webiny run build",
35
- "watch": "yarn webiny run watch"
30
+ "access": "public"
36
31
  },
37
- "gitHead": "3c0dcfb3c22c9f83107fdb97b25014cd03d9db7d"
32
+ "gitHead": "8476da73b653c89cc1474d968baf55c1b0ae0e5f",
33
+ "webiny": {
34
+ "publishFrom": "dist"
35
+ }
38
36
  }
@@ -0,0 +1,36 @@
1
+ import { Plugin } from "@webiny/plugins";
2
+ import type { Container } from "@webiny/di";
3
+ import type { Context } from "../types.js";
4
+ export interface IRegisterExtensionPluginCb<C extends Context = Context> {
5
+ (context: C): Promise<void> | void;
6
+ }
7
+ export declare class RegisterExtensionPlugin<C extends Context = Context> extends Plugin {
8
+ private readonly cb;
9
+ static readonly type: string;
10
+ constructor(cb: IRegisterExtensionPluginCb<C>);
11
+ apply(context: C): Promise<void> | void;
12
+ }
13
+ export declare const createRegisterExtensionPlugin: <C extends Context = Context>(cb: IRegisterExtensionPluginCb<C>) => RegisterExtensionPlugin<C>;
14
+ /**
15
+ * Apply RegisterExtensionPlugins at register() time β€” synchronously with the request container,
16
+ * BEFORE anything lists the model set. Each plugin's callback only does DI registration via
17
+ * registerExtension(ctx.container, ...) (feature/decorator/registration), which is register-time
18
+ * safe. Doing this early is important for code-defined CMS models (ModelFactory): the first
19
+ * request-time listing (e.g. the ACO folder schema) caches the model set for the rest of the
20
+ * request, so any model registered after that point is silently missing.
21
+ */
22
+ export declare function registerExtensions(container: Container, plugins: unknown | unknown[]): Promise<void>;
23
+ /**
24
+ * Registers a BuildParam. A separate type from RegisterExtensionPlugin so the request stack can apply
25
+ * build params BEFORE any feature registers: features gate themselves on FeatureFlags (a BuildParam)
26
+ * at register() time, and would otherwise read an empty flag set. `registerExtensions` skips these.
27
+ */
28
+ export declare class RegisterBuildParamPlugin<C extends Context = Context> extends RegisterExtensionPlugin<C> {
29
+ static readonly type: string;
30
+ }
31
+ export declare const createRegisterBuildParamPlugin: <C extends Context = Context>(cb: IRegisterExtensionPluginCb<C>) => RegisterBuildParamPlugin<C>;
32
+ /**
33
+ * Apply RegisterBuildParamPlugins. Call this before registering any feature that reads BuildParams
34
+ * (e.g. FeatureFlags) at register() time.
35
+ */
36
+ export declare function registerBuildParams(container: Container, plugins: unknown | unknown[]): Promise<void>;
@@ -0,0 +1,38 @@
1
+ import { Plugin } from "@webiny/plugins";
2
+ class RegisterExtensionPlugin extends Plugin {
3
+ static{
4
+ this.type = "handler.register.extension";
5
+ }
6
+ constructor(cb){
7
+ super(), this.cb = cb;
8
+ }
9
+ apply(context) {
10
+ return this.cb(context);
11
+ }
12
+ }
13
+ const createRegisterExtensionPlugin = (cb)=>new RegisterExtensionPlugin(cb);
14
+ async function registerExtensions(container, plugins) {
15
+ const flat = [
16
+ plugins
17
+ ].flat(1 / 0).filter(Boolean);
18
+ for (const plugin of flat)if (plugin?.type === RegisterExtensionPlugin.type && "function" == typeof plugin.apply) await plugin.apply({
19
+ container
20
+ });
21
+ }
22
+ class RegisterBuildParamPlugin extends RegisterExtensionPlugin {
23
+ static{
24
+ this.type = "handler.register.buildParam";
25
+ }
26
+ }
27
+ const createRegisterBuildParamPlugin = (cb)=>new RegisterBuildParamPlugin(cb);
28
+ async function registerBuildParams(container, plugins) {
29
+ const flat = [
30
+ plugins
31
+ ].flat(1 / 0).filter(Boolean);
32
+ for (const plugin of flat)if (plugin?.type === RegisterBuildParamPlugin.type && "function" == typeof plugin.apply) await plugin.apply({
33
+ container
34
+ });
35
+ }
36
+ export { RegisterBuildParamPlugin, RegisterExtensionPlugin, createRegisterBuildParamPlugin, createRegisterExtensionPlugin, registerBuildParams, registerExtensions };
37
+
38
+ //# sourceMappingURL=RegisterExtensionPlugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugins/RegisterExtensionPlugin.js","sources":["../../src/plugins/RegisterExtensionPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport type { Container } from \"@webiny/di\";\nimport type { Context } from \"~/types.js\";\n\nexport interface IRegisterExtensionPluginCb<C extends Context = Context> {\n (context: C): Promise<void> | void;\n}\n\nexport class RegisterExtensionPlugin<C extends Context = Context> extends Plugin {\n public static override readonly type: string = \"handler.register.extension\";\n\n public constructor(private readonly cb: IRegisterExtensionPluginCb<C>) {\n super();\n }\n\n public apply(context: C): Promise<void> | void {\n return this.cb(context);\n }\n}\n\nexport const createRegisterExtensionPlugin = <C extends Context = Context>(\n cb: IRegisterExtensionPluginCb<C>\n) => {\n return new RegisterExtensionPlugin<C>(cb);\n};\n\n/**\n * Apply RegisterExtensionPlugins at register() time β€” synchronously with the request container,\n * BEFORE anything lists the model set. Each plugin's callback only does DI registration via\n * registerExtension(ctx.container, ...) (feature/decorator/registration), which is register-time\n * safe. Doing this early is important for code-defined CMS models (ModelFactory): the first\n * request-time listing (e.g. the ACO folder schema) caches the model set for the rest of the\n * request, so any model registered after that point is silently missing.\n */\nexport async function registerExtensions(\n container: Container,\n plugins: unknown | unknown[]\n): Promise<void> {\n const flat = [plugins].flat(Infinity as 1).filter(Boolean) as RegisterExtensionPlugin[];\n for (const plugin of flat) {\n if (plugin?.type === RegisterExtensionPlugin.type && typeof plugin.apply === \"function\") {\n await plugin.apply({ container } as unknown as Context);\n }\n }\n}\n\n/**\n * Registers a BuildParam. A separate type from RegisterExtensionPlugin so the request stack can apply\n * build params BEFORE any feature registers: features gate themselves on FeatureFlags (a BuildParam)\n * at register() time, and would otherwise read an empty flag set. `registerExtensions` skips these.\n */\nexport class RegisterBuildParamPlugin<\n C extends Context = Context\n> extends RegisterExtensionPlugin<C> {\n public static override readonly type: string = \"handler.register.buildParam\";\n}\n\nexport const createRegisterBuildParamPlugin = <C extends Context = Context>(\n cb: IRegisterExtensionPluginCb<C>\n) => {\n return new RegisterBuildParamPlugin<C>(cb);\n};\n\n/**\n * Apply RegisterBuildParamPlugins. Call this before registering any feature that reads BuildParams\n * (e.g. FeatureFlags) at register() time.\n */\nexport async function registerBuildParams(\n container: Container,\n plugins: unknown | unknown[]\n): Promise<void> {\n const flat = [plugins].flat(Infinity as 1).filter(Boolean) as RegisterBuildParamPlugin[];\n for (const plugin of flat) {\n if (plugin?.type === RegisterBuildParamPlugin.type && typeof plugin.apply === \"function\") {\n await plugin.apply({ container } as unknown as Context);\n }\n }\n}\n"],"names":["RegisterExtensionPlugin","Plugin","cb","context","createRegisterExtensionPlugin","registerExtensions","container","plugins","flat","Infinity","Boolean","plugin","RegisterBuildParamPlugin","createRegisterBuildParamPlugin","registerBuildParams"],"mappings":";AAQO,MAAMA,gCAA6DC;;aACtC,IAAI,GAAW;;IAE/C,YAAoCC,EAAiC,CAAE;QACnE,KAAK,SAD2BA,EAAE,GAAFA;IAEpC;IAEO,MAAMC,OAAU,EAAwB;QAC3C,OAAO,IAAI,CAAC,EAAE,CAACA;IACnB;AACJ;AAEO,MAAMC,gCAAgC,CACzCF,KAEO,IAAIF,wBAA2BE;AAWnC,eAAeG,mBAClBC,SAAoB,EACpBC,OAA4B;IAE5B,MAAMC,OAAO;QAACD;KAAQ,CAAC,IAAI,CAACE,KAAQ,EAAO,MAAM,CAACC;IAClD,KAAK,MAAMC,UAAUH,KACjB,IAAIG,QAAQ,SAASX,wBAAwB,IAAI,IAAI,AAAwB,cAAxB,OAAOW,OAAO,KAAK,EACpE,MAAMA,OAAO,KAAK,CAAC;QAAEL;IAAU;AAG3C;AAOO,MAAMM,iCAEHZ;;aAC0B,IAAI,GAAW;;AACnD;AAEO,MAAMa,iCAAiC,CAC1CX,KAEO,IAAIU,yBAA4BV;AAOpC,eAAeY,oBAClBR,SAAoB,EACpBC,OAA4B;IAE5B,MAAMC,OAAO;QAACD;KAAQ,CAAC,IAAI,CAACE,KAAQ,EAAO,MAAM,CAACC;IAClD,KAAK,MAAMC,UAAUH,KACjB,IAAIG,QAAQ,SAASC,yBAAyB,IAAI,IAAI,AAAwB,cAAxB,OAAOD,OAAO,KAAK,EACrE,MAAMA,OAAO,KAAK,CAAC;QAAEL;IAAU;AAG3C"}
@@ -0,0 +1,5 @@
1
+ export interface CustomError extends Error {
2
+ code?: string;
3
+ data?: Record<string, any>;
4
+ }
5
+ export declare const stringifyError: (error: CustomError) => string;
@@ -0,0 +1,15 @@
1
+ const stringifyError = (error)=>{
2
+ const { name, message, code, stack, data } = error;
3
+ return JSON.stringify({
4
+ ...error,
5
+ constructorName: error.constructor?.name || "UnknownError",
6
+ name: name || "No error name",
7
+ message: message || "No error message",
8
+ code: code || "NO_CODE",
9
+ data,
10
+ stack: "true" === process.env.DEBUG ? stack : "Turn on the debug flag to see the stack."
11
+ });
12
+ };
13
+ export { stringifyError };
14
+
15
+ //# sourceMappingURL=stringifyError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stringifyError.js","sources":["../src/stringifyError.ts"],"sourcesContent":["export interface CustomError extends Error {\n code?: string;\n data?: Record<string, any>;\n}\n\nexport const stringifyError = (error: CustomError) => {\n const { name, message, code, stack, data } = error;\n return JSON.stringify({\n ...error,\n constructorName: error.constructor?.name || \"UnknownError\",\n name: name || \"No error name\",\n message: message || \"No error message\",\n code: code || \"NO_CODE\",\n data,\n stack: process.env.DEBUG === \"true\" ? stack : \"Turn on the debug flag to see the stack.\"\n });\n};\n"],"names":["stringifyError","error","name","message","code","stack","data","JSON","process"],"mappings":"AAKO,MAAMA,iBAAiB,CAACC;IAC3B,MAAM,EAAEC,IAAI,EAAEC,OAAO,EAAEC,IAAI,EAAEC,KAAK,EAAEC,IAAI,EAAE,GAAGL;IAC7C,OAAOM,KAAK,SAAS,CAAC;QAClB,GAAGN,KAAK;QACR,iBAAiBA,MAAM,WAAW,EAAE,QAAQ;QAC5C,MAAMC,QAAQ;QACd,SAASC,WAAW;QACpB,MAAMC,QAAQ;QACdE;QACA,OAAOE,AAAsB,WAAtBA,QAAQ,GAAG,CAAC,KAAK,GAAcH,QAAQ;IAClD;AACJ"}
package/types.d.ts CHANGED
@@ -1,37 +1,24 @@
1
- import { Plugin, PluginsContainer } from "@webiny/plugins/types";
2
- export declare type HandlerArgs = any[];
3
- export interface HandlerContext {
4
- plugins: PluginsContainer;
5
- args: HandlerArgs;
6
- readonly WEBINY_VERSION: string;
1
+ import type { Container } from "@webiny/feature/api";
2
+ import type { PluginsContainer } from "@webiny/plugins";
3
+ export interface Request {
4
+ url?: string;
5
+ method?: string;
6
+ headers: Record<string, string | undefined>;
7
+ body?: unknown;
8
+ params?: Record<string, string>;
9
+ query?: Record<string, string | string[]>;
10
+ path?: string;
11
+ [key: string]: unknown;
7
12
  }
8
- export interface ContextInterface {
9
- plugins: PluginsContainer;
10
- args: HandlerArgs;
11
- readonly WEBINY_VERSION: string;
13
+ export interface Reply {
14
+ send(data?: unknown): void | Promise<void>;
15
+ code(statusCode: number): Reply;
16
+ headers(headers: Record<string, string>): Reply;
17
+ header(name: string, value: string): Reply;
18
+ setCookie(name: string, value: string, options?: Record<string, unknown>): Reply;
19
+ [key: string]: unknown;
12
20
  }
13
- export declare type Context<C0 = Record<string, any>, C1 = Record<string, any>, C2 = Record<string, any>, C3 = Record<string, any>, C4 = Record<string, any>, C5 = Record<string, any>, C6 = Record<string, any>, C7 = Record<string, any>, C8 = Record<string, any>, C9 = Record<string, any>> = {
21
+ export interface Context {
14
22
  plugins: PluginsContainer;
15
- args: HandlerArgs;
16
- readonly WEBINY_VERSION: string;
17
- } & C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9;
18
- export interface BeforeHandlerPlugin<T extends ContextInterface = ContextInterface> extends Plugin {
19
- type: "before-handler";
20
- apply: (context: T) => Promise<void>;
23
+ container: Container;
21
24
  }
22
- export declare type ContextPlugin<C0 = Context, C1 = Context, C2 = Context, C3 = Context, C4 = Context, C5 = Context, C6 = Context, C7 = Context, C8 = Context, C9 = Context> = Plugin & {
23
- type: "context";
24
- apply(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9): Promise<void>;
25
- };
26
- export declare type HandlerPlugin<C0 = Context, C1 = Context, C2 = Context, C3 = Context, C4 = Context, C5 = Context, C6 = Context, C7 = Context, C8 = Context, C9 = Context> = Plugin & {
27
- type: "handler";
28
- handle(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9, next: Function): any;
29
- };
30
- export declare type HandlerResultPlugin<C0 = Context, C1 = Context, C2 = Context, C3 = Context, C4 = Context, C5 = Context, C6 = Context, C7 = Context, C8 = Context, C9 = Context> = Plugin & {
31
- type: "handler-result";
32
- handle(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9, result: any): any;
33
- };
34
- export declare type HandlerErrorPlugin<C0 = Context, C1 = Context, C2 = Context, C3 = Context, C4 = Context, C5 = Context, C6 = Context, C7 = Context, C8 = Context, C9 = Context> = Plugin & {
35
- type: "handler-error";
36
- handle(context: C0 & C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9, error: any, next: Function): Promise<any>;
37
- };
package/types.js CHANGED
@@ -1,5 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
@@ -1,2 +0,0 @@
1
- declare const _default: (...plugins: any[]) => (...args: any[]) => Promise<any>;
2
- export default _default;
package/createHandler.js DELETED
@@ -1,70 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.default = void 0;
9
-
10
- var _plugins = require("@webiny/plugins");
11
-
12
- var _middleware = _interopRequireDefault(require("./middleware"));
13
-
14
- var _default = (...plugins) => async (...args) => {
15
- const context = {
16
- plugins: new _plugins.PluginsContainer(plugins),
17
- args,
18
- // @ts-ignore
19
- // this is injected using webpack.DefinePlugin at build time
20
- WEBINY_VERSION: process.env.WEBINY_VERSION
21
- };
22
- const result = await handle(args, context);
23
- const handlerPlugins = context.plugins.byType("handler-result");
24
-
25
- for (let i = 0; i < handlerPlugins.length; i++) {
26
- if (handlerPlugins[i].apply) {
27
- await handlerPlugins[i].apply(result, context);
28
- }
29
- }
30
-
31
- return result;
32
- };
33
-
34
- exports.default = _default;
35
-
36
- async function handle(_, context) {
37
- try {
38
- const contextPlugins = context.plugins.byType("context");
39
-
40
- for (let i = 0; i < contextPlugins.length; i++) {
41
- if (contextPlugins[i].apply) {
42
- await contextPlugins[i].apply(context);
43
- }
44
- }
45
-
46
- const beforeHandlerPlugins = context.plugins.byType("before-handler");
47
-
48
- for (let i = 0; i < beforeHandlerPlugins.length; i++) {
49
- if (beforeHandlerPlugins[i].apply) {
50
- await beforeHandlerPlugins[i].apply(context);
51
- }
52
- }
53
-
54
- const handlers = context.plugins.byType("handler");
55
- const handler = (0, _middleware.default)(handlers.map(pl => pl.handle));
56
- const result = await handler(context);
57
-
58
- if (!result) {
59
- throw Error(`No result was returned from registered handlers.`);
60
- }
61
-
62
- return result;
63
- } catch (error) {
64
- // Log error to cloud, as these can be extremely annoying to debug!
65
- console.log(error);
66
- const handlers = context.plugins.byType("handler-error");
67
- const handler = (0, _middleware.default)(handlers.map(pl => pl.handle));
68
- return handler(context, error);
69
- }
70
- }
package/middleware.d.ts DELETED
@@ -1,5 +0,0 @@
1
- declare const _default: (functions?: Array<Function>) => Function;
2
- /**
3
- * Compose a single middleware from the array of middleware functions
4
- */
5
- export default _default;
package/middleware.js DELETED
@@ -1,51 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- /**
9
- * Compose a single middleware from the array of middleware functions
10
- */
11
- var _default = (functions = []) => {
12
- return (...args) => {
13
- if (!functions.length) {
14
- return Promise.resolve();
15
- } // Create a clone of function chain to prevent modifying the original array with `shift()`
16
-
17
-
18
- const chain = [...functions];
19
- return new Promise((parentResolve, parentReject) => {
20
- const next = async () => {
21
- const fn = chain.shift();
22
-
23
- if (!fn) {
24
- return Promise.resolve();
25
- }
26
-
27
- return new Promise(async (resolve, reject) => {
28
- try {
29
- const result = await fn(...args, resolve);
30
-
31
- if (typeof result !== "undefined") {
32
- return parentResolve(result);
33
- }
34
- } catch (e) {
35
- reject(e);
36
- }
37
- }).then(() => {
38
- return next();
39
- }).then(() => {
40
- parentResolve(...args);
41
- }).catch(e => {
42
- parentReject(e);
43
- });
44
- };
45
-
46
- return next();
47
- });
48
- };
49
- };
50
-
51
- exports.default = _default;
@@ -1,11 +0,0 @@
1
- import { Plugin } from "@webiny/plugins";
2
- interface Callable<TContext> {
3
- (context: TContext): void | Promise<void>;
4
- }
5
- export declare class BeforeHandlerPlugin<TContext> extends Plugin {
6
- static readonly type = "before-handler";
7
- private readonly _callable;
8
- constructor(callable?: Callable<TContext>);
9
- apply(context: TContext): void | Promise<void>;
10
- }
11
- export {};
@@ -1,32 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.BeforeHandlerPlugin = void 0;
9
-
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
- var _plugins = require("@webiny/plugins");
13
-
14
- class BeforeHandlerPlugin extends _plugins.Plugin {
15
- constructor(callable) {
16
- super();
17
- (0, _defineProperty2.default)(this, "_callable", void 0);
18
- this._callable = callable;
19
- }
20
-
21
- apply(context) {
22
- if (typeof this._callable !== "function") {
23
- throw Error(`Missing callable in BeforeHandlerPlugin! Either pass a callable to plugin constructor or extend the plugin and override the "apply" method.`);
24
- }
25
-
26
- return this._callable(context);
27
- }
28
-
29
- }
30
-
31
- exports.BeforeHandlerPlugin = BeforeHandlerPlugin;
32
- (0, _defineProperty2.default)(BeforeHandlerPlugin, "type", "before-handler");
@@ -1,11 +0,0 @@
1
- import { Plugin } from "@webiny/plugins";
2
- interface Callable<TContext> {
3
- (context: TContext): void | Promise<void>;
4
- }
5
- export declare class ContextPlugin<TContext> extends Plugin {
6
- static readonly type = "context";
7
- private readonly _callable;
8
- constructor(callable?: Callable<TContext>);
9
- apply(context: TContext): void | Promise<void>;
10
- }
11
- export {};
@@ -1,32 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.ContextPlugin = void 0;
9
-
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
- var _plugins = require("@webiny/plugins");
13
-
14
- class ContextPlugin extends _plugins.Plugin {
15
- constructor(callable) {
16
- super();
17
- (0, _defineProperty2.default)(this, "_callable", void 0);
18
- this._callable = callable;
19
- }
20
-
21
- apply(context) {
22
- if (typeof this._callable !== "function") {
23
- throw Error(`Missing callable in ContextPlugin! Either pass a callable to plugin constructor or extend the plugin and override the "apply" method.`);
24
- }
25
-
26
- return this._callable(context);
27
- }
28
-
29
- }
30
-
31
- exports.ContextPlugin = ContextPlugin;
32
- (0, _defineProperty2.default)(ContextPlugin, "type", "context");