@alterior/fastify 3.0.0-rc.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2018 William Lahti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # @/express
2
+
3
+ Access Express state within an Alterior app.
4
+
5
+ # Why?
6
+
7
+ `@alterior/web-server` provides `WebEvent.request` which exposes the HTTP request object of the underlying web server framework being used. Alterior supports more than one framework, including `express` and `fastify`. Technically the minimum shape of `WebEvent.request` is that of the `request` object defined by the Node.js `http` module. As an app developer which uses Express as the server framework in my Alterior app, I want to access `WebEvent.request` with a Typescript type that exposes all that Express provides, not just as a generic Node.js `http` request object. I don't want to manually cast this object whenever I use it.
8
+
9
+ Since `WebEvent.request` is static (using the HTTP request's Zone to determine the appropriate value), we can create new ways to access this value which are appropriately typed.
10
+
11
+ Splitting this functionality into a specific package lets us remove unused code when not using Express.
12
+
13
+ # How
14
+
15
+ ```typescript
16
+ import { ExpressContext } from '@alterior/express';
17
+ // ...
18
+
19
+ @WebService()
20
+ export class MyService {
21
+ @Get()
22
+ info() {
23
+ console.log(`user agent is: ${ExpressContext.request.header('User-Agent')}`);
24
+ }
25
+ }
26
+ ```
@@ -0,0 +1,6 @@
1
+ /// <reference types="node" />
2
+ export declare class FastifyEvent {
3
+ static get current(): import("http").IncomingMessage;
4
+ static get request(): import("http").IncomingMessage;
5
+ }
6
+ //# sourceMappingURL=event.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../src/event.ts"],"names":[],"mappings":";AAEA,qBAAa,YAAY;IACrB,MAAM,KAAK,OAAO,mCAA+B;IACjD,MAAM,KAAK,OAAO,mCAA+B;CACpD"}
package/dist/event.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FastifyEvent = void 0;
4
+ var web_server_1 = require("@alterior/web-server");
5
+ var FastifyEvent = /** @class */ (function () {
6
+ function FastifyEvent() {
7
+ }
8
+ Object.defineProperty(FastifyEvent, "current", {
9
+ get: function () { return web_server_1.WebEvent.request; },
10
+ enumerable: false,
11
+ configurable: true
12
+ });
13
+ Object.defineProperty(FastifyEvent, "request", {
14
+ get: function () { return web_server_1.WebEvent.request; },
15
+ enumerable: false,
16
+ configurable: true
17
+ });
18
+ return FastifyEvent;
19
+ }());
20
+ exports.FastifyEvent = FastifyEvent;
21
+ //# sourceMappingURL=event.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event.js","sourceRoot":"","sources":["../src/event.ts"],"names":[],"mappings":";;;AAAA,mDAAgD;AAEhD;IAAA;IAGA,CAAC;IAFG,sBAAW,uBAAO;aAAlB,cAAuB,OAAO,qBAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;;;OAAA;IACjD,sBAAW,uBAAO;aAAlB,cAAuB,OAAO,qBAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;;;OAAA;IACrD,mBAAC;AAAD,CAAC,AAHD,IAGC;AAHY,oCAAY"}
@@ -0,0 +1,17 @@
1
+ /// <reference types="node" />
2
+ import fastify from "fastify";
3
+ import { WebEvent, WebServerEngine, WebServerOptions, ConnectMiddleware } from '@alterior/web-server';
4
+ import * as http from 'http';
5
+ export declare type FastifyConnectMiddleware = ConnectMiddleware & fastify.FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>;
6
+ export declare class FastifyEngine implements WebServerEngine {
7
+ app: FastifyConnectMiddleware;
8
+ providers: any[];
9
+ sendJsonBody(routeEvent: WebEvent, body: any): void;
10
+ private readonly supportedMethods;
11
+ private getRegistrarName;
12
+ addConnectMiddleware(path: string, middleware: any): void;
13
+ listen(options: WebServerOptions): Promise<http.Server>;
14
+ addRoute(method: string, path: string, handler: (event: WebEvent) => void, middleware?: any): void;
15
+ addAnyRoute(handler: (event: WebEvent) => void): void;
16
+ }
17
+ //# sourceMappingURL=fastify-engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fastify-engine.d.ts","sourceRoot":"","sources":["../src/fastify-engine.ts"],"names":[],"mappings":";AAAA,OAAO,OAA4B,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACtG,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,oBAAY,wBAAwB,GAAG,iBAAiB,GAAG,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;AAE3I,qBAAa,aAAc,YAAW,eAAe;IACpD,GAAG,2BAAuC;IAE1C,SAAS,QAAM;IAEf,YAAY,CAAC,UAAU,EAAG,QAAQ,EAAE,IAAI,EAAG,GAAG;IAK9C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAK/B;IAEF,OAAO,CAAC,gBAAgB;IAQxB,oBAAoB,CAAC,IAAI,EAAG,MAAM,EAAE,UAAU,EAAG,GAAG;IAS9C,MAAM,CAAC,OAAO,EAAG,gBAAgB;IAKvC,QAAQ,CAAC,MAAM,EAAG,MAAM,EAAE,IAAI,EAAG,MAAM,EAAE,OAAO,EAAG,CAAC,KAAK,EAAG,QAAQ,KAAK,IAAI,EAAE,UAAU,CAAC,KAAA;IAY1F,WAAW,CAAC,OAAO,EAAG,CAAC,KAAK,EAAG,QAAQ,KAAK,IAAI;CAGhD"}
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FastifyEngine = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var fastify_1 = (0, tslib_1.__importDefault)(require("fastify"));
6
+ var web_server_1 = require("@alterior/web-server");
7
+ var FastifyEngine = /** @class */ (function () {
8
+ function FastifyEngine() {
9
+ this.app = (0, fastify_1.default)();
10
+ this.providers = [];
11
+ this.supportedMethods = [
12
+ "checkout", "copy", "delete", "get", "head", "lock", "merge",
13
+ "mkactivity", "mkcol", "move", "m-search", "notify", "options",
14
+ "patch", "post", "purge", "put", "report", "search", "subscribe",
15
+ "trace", "unlock", "unsubscribe",
16
+ ];
17
+ }
18
+ FastifyEngine.prototype.sendJsonBody = function (routeEvent, body) {
19
+ routeEvent.response.setHeader('Content-Type', 'application/json');
20
+ routeEvent.response['send'](body);
21
+ };
22
+ FastifyEngine.prototype.getRegistrarName = function (method) {
23
+ var registrar = method.toLowerCase();
24
+ if (!this.supportedMethods.includes(registrar))
25
+ throw new Error("The specified method '" + method + "' is not supported by Express.");
26
+ return registrar;
27
+ };
28
+ FastifyEngine.prototype.addConnectMiddleware = function (path, middleware) {
29
+ // TODO: bodyParser.json() cannot be used with fastify
30
+ if (middleware.name === 'jsonParser')
31
+ return;
32
+ this.app['use'](path, middleware);
33
+ };
34
+ FastifyEngine.prototype.listen = function (options) {
35
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
36
+ return (0, tslib_1.__generator)(this, function (_a) {
37
+ switch (_a.label) {
38
+ case 0: return [4 /*yield*/, this.app.listen(options.port)];
39
+ case 1:
40
+ _a.sent();
41
+ return [2 /*return*/, this.app.server];
42
+ }
43
+ });
44
+ });
45
+ };
46
+ FastifyEngine.prototype.addRoute = function (method, path, handler, middleware) {
47
+ var _a;
48
+ if (!middleware)
49
+ middleware = [];
50
+ (_a = this.app)[this.getRegistrarName(method)].apply(_a, (0, tslib_1.__spreadArray)((0, tslib_1.__spreadArray)([path], (0, tslib_1.__read)(middleware), false), [function (req, res) {
51
+ return handler(new web_server_1.WebEvent(req, res));
52
+ }], false));
53
+ };
54
+ FastifyEngine.prototype.addAnyRoute = function (handler) {
55
+ this.app.use(function (req, res) { return handler(new web_server_1.WebEvent(req, res)); });
56
+ };
57
+ return FastifyEngine;
58
+ }());
59
+ exports.FastifyEngine = FastifyEngine;
60
+ //# sourceMappingURL=fastify-engine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fastify-engine.js","sourceRoot":"","sources":["../src/fastify-engine.ts"],"names":[],"mappings":";;;;AAAA,iEAAmD;AACnD,mDAAsG;AAKtG;IAAA;QACC,QAAG,GAA6B,IAAA,iBAAO,GAAE,CAAC;QAE1C,cAAS,GAAG,EAAE,CAAC;QAOE,qBAAgB,GAAG;YACnC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO;YAC5D,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS;YAC9D,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,WAAW;YAChE,OAAO,EAAE,QAAQ,EAAE,aAAa;SAChC,CAAC;IAuCH,CAAC;IAj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IACH,CAAC;IAED,mCAAW,GAAX,UAAY,OAAoC;QAC/C,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,UAAC,GAAG,EAAE,GAAG,IAAK,OAAA,OAAO,CAAC,IAAI,qBAAQ,CAAM,GAAG,EAAO,GAAG,CAAC,CAAC,EAAzC,CAAyC,CAAC,CAAC;IACvE,CAAC;IACF,oBAAC;AAAD,CAAC,AAtDD,IAsDC;AAtDY,sCAAa"}
@@ -0,0 +1,3 @@
1
+ export * from './event';
2
+ export * from './fastify-engine';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,kBAAkB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var tslib_1 = require("tslib");
4
+ (0, tslib_1.__exportStar)(require("./event"), exports);
5
+ (0, tslib_1.__exportStar)(require("./fastify-engine"), exports);
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uDAAwB;AACxB,gEAAiC"}
package/dist/test.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import "reflect-metadata";
2
+ import "source-map-support/register";
3
+ //# sourceMappingURL=test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,6BAA6B,CAAC"}
package/dist/test.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("reflect-metadata");
4
+ require("source-map-support/register");
5
+ var razmin_1 = require("razmin");
6
+ (0, razmin_1.suite)()
7
+ .include(['**/*.test.js'])
8
+ .run();
9
+ //# sourceMappingURL=test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,4BAA0B;AAC1B,uCAAqC;AAErC,iCAA+B;AAE/B,IAAA,cAAK,GAAE;KACF,OAAO,CAAC,CAAC,cAAc,CAAC,CAAC;KACzB,GAAG,EAAE,CACT"}
@@ -0,0 +1,6 @@
1
+ /// <reference types="node" />
2
+ export declare class FastifyEvent {
3
+ static get current(): import("http").IncomingMessage;
4
+ static get request(): import("http").IncomingMessage;
5
+ }
6
+ //# sourceMappingURL=event.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../src/event.ts"],"names":[],"mappings":";AAEA,qBAAa,YAAY;IACrB,MAAM,KAAK,OAAO,mCAA+B;IACjD,MAAM,KAAK,OAAO,mCAA+B;CACpD"}
@@ -0,0 +1,6 @@
1
+ import { WebEvent } from '@alterior/web-server';
2
+ export class FastifyEvent {
3
+ static get current() { return WebEvent.request; }
4
+ static get request() { return WebEvent.request; }
5
+ }
6
+ //# sourceMappingURL=event.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event.js","sourceRoot":"","sources":["../src/event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,MAAM,OAAO,YAAY;IACrB,MAAM,KAAK,OAAO,KAAK,OAAO,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;IACjD,MAAM,KAAK,OAAO,KAAK,OAAO,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;CACpD"}
@@ -0,0 +1,17 @@
1
+ /// <reference types="node" />
2
+ import fastify from "fastify";
3
+ import { WebEvent, WebServerEngine, WebServerOptions, ConnectMiddleware } from '@alterior/web-server';
4
+ import * as http from 'http';
5
+ export declare type FastifyConnectMiddleware = ConnectMiddleware & fastify.FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>;
6
+ export declare class FastifyEngine implements WebServerEngine {
7
+ app: FastifyConnectMiddleware;
8
+ providers: any[];
9
+ sendJsonBody(routeEvent: WebEvent, body: any): void;
10
+ private readonly supportedMethods;
11
+ private getRegistrarName;
12
+ addConnectMiddleware(path: string, middleware: any): void;
13
+ listen(options: WebServerOptions): Promise<http.Server>;
14
+ addRoute(method: string, path: string, handler: (event: WebEvent) => void, middleware?: any): void;
15
+ addAnyRoute(handler: (event: WebEvent) => void): void;
16
+ }
17
+ //# sourceMappingURL=fastify-engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fastify-engine.d.ts","sourceRoot":"","sources":["../src/fastify-engine.ts"],"names":[],"mappings":";AAAA,OAAO,OAA4B,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACtG,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,oBAAY,wBAAwB,GAAG,iBAAiB,GAAG,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;AAE3I,qBAAa,aAAc,YAAW,eAAe;IACpD,GAAG,2BAAuC;IAE1C,SAAS,QAAM;IAEf,YAAY,CAAC,UAAU,EAAG,QAAQ,EAAE,IAAI,EAAG,GAAG;IAK9C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAK/B;IAEF,OAAO,CAAC,gBAAgB;IAQxB,oBAAoB,CAAC,IAAI,EAAG,MAAM,EAAE,UAAU,EAAG,GAAG;IAS9C,MAAM,CAAC,OAAO,EAAG,gBAAgB;IAKvC,QAAQ,CAAC,MAAM,EAAG,MAAM,EAAE,IAAI,EAAG,MAAM,EAAE,OAAO,EAAG,CAAC,KAAK,EAAG,QAAQ,KAAK,IAAI,EAAE,UAAU,CAAC,KAAA;IAY1F,WAAW,CAAC,OAAO,EAAG,CAAC,KAAK,EAAG,QAAQ,KAAK,IAAI;CAGhD"}
@@ -0,0 +1,48 @@
1
+ import { __awaiter } from "tslib";
2
+ import fastify from "fastify";
3
+ import { WebEvent } from '@alterior/web-server';
4
+ export class FastifyEngine {
5
+ constructor() {
6
+ this.app = fastify();
7
+ this.providers = [];
8
+ this.supportedMethods = [
9
+ "checkout", "copy", "delete", "get", "head", "lock", "merge",
10
+ "mkactivity", "mkcol", "move", "m-search", "notify", "options",
11
+ "patch", "post", "purge", "put", "report", "search", "subscribe",
12
+ "trace", "unlock", "unsubscribe",
13
+ ];
14
+ }
15
+ sendJsonBody(routeEvent, body) {
16
+ routeEvent.response.setHeader('Content-Type', 'application/json');
17
+ routeEvent.response['send'](body);
18
+ }
19
+ getRegistrarName(method) {
20
+ let registrar = method.toLowerCase();
21
+ if (!this.supportedMethods.includes(registrar))
22
+ throw new Error(`The specified method '${method}' is not supported by Express.`);
23
+ return registrar;
24
+ }
25
+ addConnectMiddleware(path, middleware) {
26
+ // TODO: bodyParser.json() cannot be used with fastify
27
+ if (middleware.name === 'jsonParser')
28
+ return;
29
+ this.app['use'](path, middleware);
30
+ }
31
+ listen(options) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ yield this.app.listen(options.port);
34
+ return this.app.server;
35
+ });
36
+ }
37
+ addRoute(method, path, handler, middleware) {
38
+ if (!middleware)
39
+ middleware = [];
40
+ this.app[this.getRegistrarName(method)](path, ...middleware, (req, res) => {
41
+ return handler(new WebEvent(req, res));
42
+ });
43
+ }
44
+ addAnyRoute(handler) {
45
+ this.app.use((req, res) => handler(new WebEvent(req, res)));
46
+ }
47
+ }
48
+ //# sourceMappingURL=fastify-engine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fastify-engine.js","sourceRoot":"","sources":["../src/fastify-engine.ts"],"names":[],"mappings":";AAAA,OAAO,OAA4B,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAwD,MAAM,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AM,CAAC;QACxB,CAAC;KAAA;IAED,QAAQ,CAAC,MAAe,EAAE,IAAa,EAAE,OAAoC,EAAE,UAAW;QACzF,IAAI,CAAC,UAAU;YACd,UAAU,GAAG,EAAE,CAAC;QACjB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,CACtC,IAAI,EACJ,GAAG,UAAU,EACb,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;YACZ,OAAO,OAAO,CAAC,IAAI,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;QACxC,CAAC,CACD,CAAC;IACH,CAAC;IAED,WAAW,CAAC,OAAoC;QAC/C,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,QAAQ,CAAM,GAAG,EAAO,GAAG,CAAC,CAAC,CAAC,CAAC;IACvE,CAAC;CACD"}
@@ -0,0 +1,3 @@
1
+ export * from './event';
2
+ export * from './fastify-engine';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './event';
2
+ export * from './fastify-engine';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import "reflect-metadata";
2
+ import "source-map-support/register";
3
+ //# sourceMappingURL=test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,6BAA6B,CAAC"}
@@ -0,0 +1,7 @@
1
+ import "reflect-metadata";
2
+ import "source-map-support/register";
3
+ import { suite } from 'razmin';
4
+ suite()
5
+ .include(['**/*.test.js'])
6
+ .run();
7
+ //# sourceMappingURL=test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,6BAA6B,CAAC;AAErC,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAE/B,KAAK,EAAE;KACF,OAAO,CAAC,CAAC,cAAc,CAAC,CAAC;KACzB,GAAG,EAAE,CACT"}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@alterior/fastify",
3
+ "version": "3.0.0-rc.5",
4
+ "description": "Access Fastify state within an Alterior app",
5
+ "author": "The Alterior Project (https://github.com/alterior-mvc)",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/alterior-mvc/alterior/blob/main/packages/di/README.md",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/alterior-mvc/alterior.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/alterior-mvc/alterior/issues"
14
+ },
15
+ "keywords": [
16
+ "express",
17
+ "api",
18
+ "rest"
19
+ ],
20
+ "main": "dist/index.js",
21
+ "module": "dist.esm/index.js",
22
+ "types": "dist/index.d.ts",
23
+ "access": "public",
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "engines": {
28
+ "node": ">=10"
29
+ },
30
+ "scripts": {
31
+ "clean": "rimraf dist dist.esm",
32
+ "build": "npm run clean && npm run build:cjs && npm run build:esm",
33
+ "build:cjs": "tsc",
34
+ "build:esm": "tsc -p tsconfig.esm.json",
35
+ "prepublishOnly": "npm test",
36
+ "test": "npm run build && node dist/test.js",
37
+ "docs": "typedoc ."
38
+ },
39
+ "dependencies": {
40
+ "@alterior/web-server": "^3.0.0-rc.5",
41
+ "fastify": "^2.15.3"
42
+ },
43
+ "devDependencies": {
44
+ "@types/chai": "^4.2.11",
45
+ "@types/jasmine": "^2.5.41",
46
+ "@types/node": "^8.10.61",
47
+ "chai": "^4.2.0",
48
+ "prettier": "1.13.7",
49
+ "razmin": "^1.1.6",
50
+ "reflect-metadata": "^0.1.12",
51
+ "rimraf": "^2.7.1",
52
+ "tslib": "^2.3.1",
53
+ "tslint": "5.11.0",
54
+ "tslint-config-prettier": "1.17.0",
55
+ "typescript": "^4.1.5"
56
+ },
57
+ "gitHead": "6422cb629b0e7865b58e81eaecf951682d4641e9"
58
+ }
package/src/event.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { WebEvent } from '@alterior/web-server';
2
+
3
+ export class FastifyEvent {
4
+ static get current() { return WebEvent.request; }
5
+ static get request() { return WebEvent.request; }
6
+ }
@@ -0,0 +1,61 @@
1
+ import fastify, { FastifyInstance } from "fastify";
2
+ import { WebEvent, WebServerEngine, WebServerOptions, ConnectMiddleware } from '@alterior/web-server';
3
+ import * as http from 'http';
4
+
5
+ export type FastifyConnectMiddleware = ConnectMiddleware & fastify.FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>;
6
+
7
+ export class FastifyEngine implements WebServerEngine {
8
+ app = <FastifyConnectMiddleware>fastify();
9
+
10
+ providers = [];
11
+
12
+ sendJsonBody(routeEvent : WebEvent, body : any) {
13
+ routeEvent.response.setHeader('Content-Type', 'application/json');
14
+ routeEvent.response['send'](body);
15
+ }
16
+
17
+ private readonly supportedMethods = [
18
+ "checkout", "copy", "delete", "get", "head", "lock", "merge",
19
+ "mkactivity", "mkcol", "move", "m-search", "notify", "options",
20
+ "patch", "post", "purge", "put", "report", "search", "subscribe",
21
+ "trace", "unlock", "unsubscribe",
22
+ ];
23
+
24
+ private getRegistrarName(method : string) {
25
+ let registrar = method.toLowerCase();
26
+ if (!this.supportedMethods.includes(registrar))
27
+ throw new Error(`The specified method '${method}' is not supported by Express.`);
28
+
29
+ return registrar;
30
+ }
31
+
32
+ addConnectMiddleware(path : string, middleware : any) {
33
+
34
+ // TODO: bodyParser.json() cannot be used with fastify
35
+ if (middleware.name === 'jsonParser')
36
+ return;
37
+
38
+ this.app['use'](path, middleware);
39
+ }
40
+
41
+ async listen(options : WebServerOptions) {
42
+ await this.app.listen(options.port);
43
+ return this.app.server;
44
+ }
45
+
46
+ addRoute(method : string, path : string, handler : (event : WebEvent) => void, middleware?) {
47
+ if (!middleware)
48
+ middleware = [];
49
+ this.app[this.getRegistrarName(method)](
50
+ path,
51
+ ...middleware,
52
+ (req, res) => {
53
+ return handler(new WebEvent(req, res));
54
+ }
55
+ );
56
+ }
57
+
58
+ addAnyRoute(handler : (event : WebEvent) => void) {
59
+ this.app.use((req, res) => handler(new WebEvent(<any>req, <any>res)));
60
+ }
61
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './event';
2
+ export * from './fastify-engine';
package/src/test.ts ADDED
@@ -0,0 +1,9 @@
1
+ import "reflect-metadata";
2
+ import "source-map-support/register";
3
+
4
+ import { suite } from 'razmin';
5
+
6
+ suite()
7
+ .include(['**/*.test.js'])
8
+ .run()
9
+ ;
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.esm.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "baseUrl": "./src",
6
+ "outDir": "./dist.esm"
7
+ },
8
+ "include": [ "./src/**/*.ts" ]
9
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "baseUrl": "./src",
6
+ "outDir": "./dist"
7
+ },
8
+ "include": [ "./src/**/*.ts" ],
9
+ "typedocOptions": {
10
+ "mode": "file",
11
+ "out": "../../docs/di",
12
+ "target": "ES5",
13
+ "excludeExternals": true,
14
+ "excludePrivate": true,
15
+ "externalPattern": "**/node_modules/**",
16
+ "exclude": "**/node_modules/**,**/dist*/**,**/example/**,**/*.test.ts",
17
+ "categoryOrder": [
18
+ "Entrypoint",
19
+ "*"
20
+ ]
21
+ }
22
+ }