@inversifyjs/http-fastify 2.0.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @inversifyjs/http-fastify
2
+
3
+ ## 2.0.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Added `FastifyMiddleware`
8
+ - Added `InversifyFastifyHttpAdapter`
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies
13
+ - @inversifyjs/http-core@2.0.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 inversify
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,6 @@
1
+ [![Test coverage](https://codecov.io/gh/inversify/monorepo/branch/main/graph/badge.svg?flag=%40inversifyjs%2Fhttp-fastify)](https://codecov.io/gh/inversify/monorepo/branch/main/graph/badge.svg?flag=%40inversifyjs%2Fhttp-fastify)
2
+ [![npm version](https://img.shields.io/github/package-json/v/inversify/monorepo?filename=packages%2Fframework%2Flibraries%2Ffastify%2Fpackage.json&style=plastic)](https://www.npmjs.com/package/@inversifyjs/http-fastify)
3
+
4
+ # @inversifyjs/http-fastify
5
+
6
+ Inversify monorepo fastify package.
@@ -0,0 +1,22 @@
1
+ import { Readable } from 'node:stream';
2
+ import { HttpStatusCode, InversifyHttpAdapter, RouterParams } from '@inversifyjs/http-core';
3
+ import { FastifyInstance, FastifyReply, FastifyRequest, HookHandlerDoneFunction } from 'fastify';
4
+ import { Container } from 'inversify';
5
+ import { FastifyHttpAdapterOptions } from '../models/FastifyHttpAdapterOptions';
6
+ export declare class InversifyFastifyHttpAdapter extends InversifyHttpAdapter<FastifyRequest, FastifyReply, HookHandlerDoneFunction, void, FastifyHttpAdapterOptions> {
7
+ #private;
8
+ constructor(container: Container, httpAdapterOptions?: FastifyHttpAdapterOptions, customApp?: FastifyInstance);
9
+ build(): Promise<FastifyInstance>;
10
+ protected _getBody(request: FastifyRequest, parameterName?: string): unknown;
11
+ protected _getParams(request: FastifyRequest, parameterName?: string): unknown;
12
+ protected _getQuery(request: FastifyRequest, parameterName?: string): unknown;
13
+ protected _getHeaders(request: FastifyRequest, parameterName?: string): unknown;
14
+ protected _getCookies(request: FastifyRequest, _response: FastifyReply, parameterName?: string): unknown;
15
+ protected _replyText(_request: FastifyRequest, response: FastifyReply, value: string): void;
16
+ protected _replyJson(_request: FastifyRequest, response: FastifyReply, value?: object): void;
17
+ protected _replyStream(_request: FastifyRequest, response: FastifyReply, value: Readable): void;
18
+ protected _setStatus(_request: FastifyRequest, response: FastifyReply, statusCode: HttpStatusCode): void;
19
+ protected _setHeader(_request: FastifyRequest, response: FastifyReply, key: string, value: string): void;
20
+ protected _buildRouter(routerParams: RouterParams<FastifyRequest, FastifyReply, (err?: Error) => void, void>): void;
21
+ }
22
+ //# sourceMappingURL=InversifyFastifyHttpAdapter.d.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E,YAAY,EACtB,KAAK,EAAE,MAAM,GACZ,IAAI;IAIP,SAAS,CAAC,UAAU,CAClB,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,YAAY,EACtB,KAAK,CAAC,EAAE,MAAM,GACb,IAAI;IAIP,SAAS,CAAC,YAAY,CACpB,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,YAAY,EACtB,KAAK,EAAE,QAAQ,GACd,IAAI;IAIP,SAAS,CAAC,UAAU,CAClB,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,YAAY,EACtB,UAAU,EAAE,cAAc,GACzB,IAAI;IAIP,SAAS,CAAC,UAAU,CAClB,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,YAAY,EACtB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,GACZ,IAAI;IAIP,SAAS,CAAC,YAAY,CACpB,YAAY,EAAE,YAAY,CACxB,cAAc,EACd,YAAY,EACZ,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,EACrB,IAAI,CACL,GACA,IAAI;CAyLR"}
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InversifyFastifyHttpAdapter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const cookie_1 = tslib_1.__importDefault(require("@fastify/cookie"));
6
+ const formbody_1 = tslib_1.__importDefault(require("@fastify/formbody"));
7
+ const http_core_1 = require("@inversifyjs/http-core");
8
+ const fastify_1 = require("fastify");
9
+ class InversifyFastifyHttpAdapter extends http_core_1.InversifyHttpAdapter {
10
+ #app;
11
+ constructor(container, httpAdapterOptions, customApp) {
12
+ super(container, { logger: true, useCookies: false, useFormUrlEncoded: false }, httpAdapterOptions);
13
+ this.#app = this.#buildDefaultFastifyApp(customApp);
14
+ }
15
+ async build() {
16
+ await this._buildServer();
17
+ return this.#app;
18
+ }
19
+ _getBody(request, parameterName) {
20
+ return parameterName !== undefined
21
+ ? request.body[parameterName]
22
+ : request.body;
23
+ }
24
+ _getParams(request, parameterName) {
25
+ return parameterName !== undefined
26
+ ? request.params[parameterName]
27
+ : request.params;
28
+ }
29
+ _getQuery(request, parameterName) {
30
+ return parameterName !== undefined
31
+ ? request.query[parameterName]
32
+ : request.query;
33
+ }
34
+ _getHeaders(request, parameterName) {
35
+ return parameterName !== undefined
36
+ ? request.headers[parameterName]
37
+ : request.headers;
38
+ }
39
+ _getCookies(request, _response, parameterName) {
40
+ return parameterName !== undefined
41
+ ? request.cookies[parameterName]
42
+ : request.cookies;
43
+ }
44
+ _replyText(_request, response, value) {
45
+ response.send(value);
46
+ }
47
+ _replyJson(_request, response, value) {
48
+ response.send(value);
49
+ }
50
+ _replyStream(_request, response, value) {
51
+ response.send(value);
52
+ }
53
+ _setStatus(_request, response, statusCode) {
54
+ response.status(statusCode);
55
+ }
56
+ _setHeader(_request, response, key, value) {
57
+ response.header(key, value);
58
+ }
59
+ _buildRouter(routerParams) {
60
+ const router = (fastifyInstance, _opts, done) => {
61
+ for (const routeParams of routerParams.routeParamsList) {
62
+ const orderedMiddlewareList = [
63
+ ...routeParams.preHandlerMiddlewareList,
64
+ ...routeParams.guardList,
65
+ ];
66
+ const normalizedUrl = routeParams.path === '/' ? '' : routeParams.path;
67
+ fastifyInstance.route({
68
+ handler: this.#buildFastifyHandler(routeParams.handler),
69
+ method: this.#convertRequestMethodType(routeParams.requestMethodType),
70
+ onResponse: this.#buildFastifyOnResponseAsyncMiddlewareList(routeParams.postHandlerMiddlewareList),
71
+ preHandler: this.#buildFastifyPreHandlerAsyncMiddlewareList(orderedMiddlewareList),
72
+ url: normalizedUrl,
73
+ });
74
+ }
75
+ done();
76
+ };
77
+ this.#app.register(router, { prefix: routerParams.path });
78
+ }
79
+ #buildDefaultFastifyApp(customApp) {
80
+ const app = customApp ?? (0, fastify_1.fastify)();
81
+ if (this.httpAdapterOptions.useCookies) {
82
+ app.register(cookie_1.default);
83
+ }
84
+ if (this.httpAdapterOptions.useFormUrlEncoded) {
85
+ app.register(formbody_1.default);
86
+ }
87
+ return app;
88
+ }
89
+ #buildFastifyHandler(handler) {
90
+ return async (request, reply) => {
91
+ await new Promise((resolve, reject) => {
92
+ const done = (err) => {
93
+ if (err !== undefined) {
94
+ reject(err);
95
+ }
96
+ else {
97
+ resolve();
98
+ }
99
+ };
100
+ resolve(handler(request, reply, done));
101
+ });
102
+ };
103
+ }
104
+ #buildFastifyPreHandlerAsyncMiddlewareList(middlewareList) {
105
+ return middlewareList.map((middleware) => this.#buildFastifyPreHandlerAsyncMiddleware(middleware));
106
+ }
107
+ #buildFastifyOnResponseAsyncMiddlewareList(middlewareList) {
108
+ return middlewareList.map((middleware) => this.#buildFastifyOnResponseAsyncMiddleware(middleware));
109
+ }
110
+ #buildFastifyPreHandlerAsyncMiddleware(middleware) {
111
+ return async (request, reply) => {
112
+ await new Promise((resolve, reject) => {
113
+ const done = (err) => {
114
+ if (err !== undefined) {
115
+ reject(err);
116
+ }
117
+ else {
118
+ resolve();
119
+ }
120
+ };
121
+ resolve(middleware(request, reply, done));
122
+ });
123
+ };
124
+ }
125
+ #buildFastifyOnResponseAsyncMiddleware(middleware) {
126
+ return async (request, reply) => {
127
+ await new Promise((resolve, reject) => {
128
+ const done = (err) => {
129
+ if (err !== undefined) {
130
+ reject(err);
131
+ }
132
+ else {
133
+ resolve();
134
+ }
135
+ };
136
+ resolve(middleware(request, reply, done));
137
+ });
138
+ };
139
+ }
140
+ #convertRequestMethodType(requestMethodType) {
141
+ return requestMethodType === http_core_1.RequestMethodType.All
142
+ ? Object.values(http_core_1.RequestMethodType).filter((method) => method !== http_core_1.RequestMethodType.All)
143
+ : requestMethodType;
144
+ }
145
+ }
146
+ exports.InversifyFastifyHttpAdapter = InversifyFastifyHttpAdapter;
147
+ //# sourceMappingURL=InversifyFastifyHttpAdapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InversifyFastifyHttpAdapter.js","sourceRoot":"","sources":["../../../src/adapter/InversifyFastifyHttp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C;gBAEF,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;YAC5C,CAAC,CACF,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;IAED,yBAAyB,CACvB,iBAAoC;QAEpC,OAAO,iBAAiB,KAAK,6BAAiB,CAAC,GAAG;YAChD,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,6BAAiB,CAAC,CAAC,MAAM,CACrC,CAAC,MAAyB,EAAE,EAAE,CAAC,MAAM,KAAK,6BAAiB,CAAC,GAAG,CAChE;YACH,CAAC,CAAC,iBAAiB,CAAC;IACxB,CAAC;CACF;AA/SD,kEA+SC"}
@@ -0,0 +1,5 @@
1
+ import { InversifyFastifyHttpAdapter } from './adapter/InversifyFastifyHttpAdapter';
2
+ import { FastifyMiddleware } from './models/FastifyMiddleware';
3
+ export type { FastifyMiddleware };
4
+ export { InversifyFastifyHttpAdapter };
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,uCAAuC,CAAC;AACpF,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAE/D,YAAY,EAAE,iBAAiB,EAAE,CAAC;AAElC,OAAO,EAAE,2BAA2B,EAAE,CAAC"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InversifyFastifyHttpAdapter = void 0;
4
+ const InversifyFastifyHttpAdapter_1 = require("./adapter/InversifyFastifyHttpAdapter");
5
+ Object.defineProperty(exports, "InversifyFastifyHttpAdapter", { enumerable: true, get: function () { return InversifyFastifyHttpAdapter_1.InversifyFastifyHttpAdapter; } });
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,uFAAoF;AAK3E,4GALA,yDAA2B,OAKA"}
@@ -0,0 +1,6 @@
1
+ import { HttpAdapterOptions } from '@inversifyjs/http-core';
2
+ export interface FastifyHttpAdapterOptions extends HttpAdapterOptions {
3
+ useCookies?: boolean;
4
+ useFormUrlEncoded?: boolean;
5
+ }
6
+ //# sourceMappingURL=FastifyHttpAdapterOptions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FastifyHttpAdapterOptions.d.ts","sourceRoot":"","sources":["../../../src/models/FastifyHttpAdapterOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAE5D,MAAM,WAAW,yBAA0B,SAAQ,kBAAkB;IACnE,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=FastifyHttpAdapterOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FastifyHttpAdapterOptions.js","sourceRoot":"","sources":["../../../src/models/FastifyHttpAdapterOptions.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import { Middleware } from '@inversifyjs/http-core';
2
+ import { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from 'fastify';
3
+ export type FastifyMiddleware = Middleware<FastifyRequest, FastifyReply, HookHandlerDoneFunction, void>;
4
+ //# sourceMappingURL=FastifyMiddleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FastifyMiddleware.d.ts","sourceRoot":"","sources":["../../../src/models/FastifyMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAEhF,MAAM,MAAM,iBAAiB,GAAG,UAAU,CACxC,cAAc,EACd,YAAY,EACZ,uBAAuB,EACvB,IAAI,CACL,CAAC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=FastifyMiddleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FastifyMiddleware.js","sourceRoot":"","sources":["../../../src/models/FastifyMiddleware.ts"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,31 @@
1
+ import { Readable } from 'node:stream';
2
+ import { HttpAdapterOptions, InversifyHttpAdapter, HttpStatusCode, RouterParams, Middleware } from '@inversifyjs/http-core';
3
+ import { FastifyRequest, FastifyReply, HookHandlerDoneFunction, FastifyInstance } from 'fastify';
4
+ import { Container } from 'inversify';
5
+
6
+ interface FastifyHttpAdapterOptions extends HttpAdapterOptions {
7
+ useCookies?: boolean;
8
+ useFormUrlEncoded?: boolean;
9
+ }
10
+
11
+ declare class InversifyFastifyHttpAdapter extends InversifyHttpAdapter<FastifyRequest, FastifyReply, HookHandlerDoneFunction, void, FastifyHttpAdapterOptions> {
12
+ #private;
13
+ constructor(container: Container, httpAdapterOptions?: FastifyHttpAdapterOptions, customApp?: FastifyInstance);
14
+ build(): Promise<FastifyInstance>;
15
+ protected _getBody(request: FastifyRequest, parameterName?: string): unknown;
16
+ protected _getParams(request: FastifyRequest, parameterName?: string): unknown;
17
+ protected _getQuery(request: FastifyRequest, parameterName?: string): unknown;
18
+ protected _getHeaders(request: FastifyRequest, parameterName?: string): unknown;
19
+ protected _getCookies(request: FastifyRequest, _response: FastifyReply, parameterName?: string): unknown;
20
+ protected _replyText(_request: FastifyRequest, response: FastifyReply, value: string): void;
21
+ protected _replyJson(_request: FastifyRequest, response: FastifyReply, value?: object): void;
22
+ protected _replyStream(_request: FastifyRequest, response: FastifyReply, value: Readable): void;
23
+ protected _setStatus(_request: FastifyRequest, response: FastifyReply, statusCode: HttpStatusCode): void;
24
+ protected _setHeader(_request: FastifyRequest, response: FastifyReply, key: string, value: string): void;
25
+ protected _buildRouter(routerParams: RouterParams<FastifyRequest, FastifyReply, (err?: Error) => void, void>): void;
26
+ }
27
+
28
+ type FastifyMiddleware = Middleware<FastifyRequest, FastifyReply, HookHandlerDoneFunction, void>;
29
+
30
+ export { InversifyFastifyHttpAdapter };
31
+ export type { FastifyMiddleware };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,uCAAuC,CAAC;AACpF,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAE/D,YAAY,EAAE,iBAAiB,EAAE,CAAC;AAElC,OAAO,EAAE,2BAA2B,EAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ import e from"@fastify/cookie";import r from"@fastify/formbody";import{InversifyHttpAdapter as t,RequestMethodType as s}from"@inversifyjs/http-core";import{fastify as i}from"fastify";class a extends t{#e;constructor(e,r,t){super(e,{logger:!0,useCookies:!1,useFormUrlEncoded:!1},r),this.#e=this.#r(t)}async build(){return await this._buildServer(),this.#e}_getBody(e,r){return void 0!==r?e.body[r]:e.body}_getParams(e,r){return void 0!==r?e.params[r]:e.params}_getQuery(e,r){return void 0!==r?e.query[r]:e.query}_getHeaders(e,r){return void 0!==r?e.headers[r]:e.headers}_getCookies(e,r,t){return void 0!==t?e.cookies[t]:e.cookies}_replyText(e,r,t){r.send(t)}_replyJson(e,r,t){r.send(t)}_replyStream(e,r,t){r.send(t)}_setStatus(e,r,t){r.status(t)}_setHeader(e,r,t,s){r.header(t,s)}_buildRouter(e){this.#e.register((r,t,s)=>{for(const t of e.routeParamsList){const e=[...t.preHandlerMiddlewareList,...t.guardList],s="/"===t.path?"":t.path;r.route({handler:this.#t(t.handler),method:this.#s(t.requestMethodType),onResponse:this.#i(t.postHandlerMiddlewareList),preHandler:this.#a(e),url:s})}s()},{prefix:e.path})}#r(t){const s=t??i();return this.httpAdapterOptions.useCookies&&s.register(e),this.httpAdapterOptions.useFormUrlEncoded&&s.register(r),s}#t(e){return async(r,t)=>{await new Promise((s,i)=>{s(e(r,t,e=>{void 0!==e?i(e):s()}))})}}#a(e){return e.map(e=>this.#d(e))}#i(e){return e.map(e=>this.#o(e))}#d(e){return async(r,t)=>{await new Promise((s,i)=>{s(e(r,t,e=>{void 0!==e?i(e):s()}))})}}#o(e){return async(r,t)=>{await new Promise((s,i)=>{s(e(r,t,e=>{void 0!==e?i(e):s()}))})}}#s(e){return e===s.All?Object.values(s).filter(e=>e!==s.All):e}}export{a as InversifyFastifyHttpAdapter};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/adapter/InversifyFastifyHttpAdapter.ts"],"sourcesContent":[null],"names":["InversifyFastifyHttpAdapter","InversifyHttpAdapter","app","constructor","container","httpAdapterOptions","customApp","super","logger","useCookies","useFormUrlEncoded","this","buildDefaultFastifyApp","build","_buildServer","_getBody","request","parameterName","undefined","body","_getParams","params","_getQuery","query","_getHeaders","headers","_getCookies","_response","cookies","_replyText","_request","response","value","send","_replyJson","_replyStream","_setStatus","statusCode","status","_setHeader","key","header","_buildRouter","routerParams","register","fastifyInstance","_opts","done","routeParams","routeParamsList","orderedMiddlewareList","preHandlerMiddlewareList","guardList","normalizedUrl","path","route","handler","buildFastifyHandler","method","convertRequestMethodType","requestMethodType","onResponse","buildFastifyOnResponseAsyncMiddlewareList","postHandlerMiddlewareList","preHandler","buildFastifyPreHandlerAsyncMiddlewareList","url","prefix","fastify","cookie","fastifyFormbody","async","reply","Promise","resolve","reject","err","middlewareList","map","middleware","buildFastifyPreHandlerAsyncMiddleware","buildFastifyOnResponseAsyncMiddleware","RequestMethodType","All","Object","values","filter"],"mappings":"uLA4BM,MAAOA,UAAoCC,EAOtCC,GAET,WAAAC,CACEC,EACAC,EACAC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BX,EAAYY,mBACnDC,WAAYlD,MAAKmD,EACfd,EAAYe,2BAEdC,WAAYrD,MAAKsD,EACff,GAEFgB,IAAKb,GAET,CAEAN,KAGyB,CAAEoB,OAAQxB,EAAaW,MACpD,CAEA,EAAA1C,CAAwBN,GACtB,MAAMJ,EAAuBI,GAAa8D,IAc1C,OAZIzD,KAAKN,mBAAmBI,YAC1BP,EAAI0C,SACFyB,GAMA1D,KAAKN,mBAAmBK,mBAC1BR,EAAI0C,SAAS0B,GAGRpE,CACT,CAEA,EAAAuD,CACED,GAMA,OAAOe,MACLvD,EACAwD,WAEM,IAAIC,QACR,CACEC,EACAC,KAUAD,EAAQlB,EAAQxC,EAASwD,EARYI,SACvB1D,IAAR0D,EACFD,EAAOC,GAEPF,SAQZ,CAEA,EAAAT,CACEY,GAMA,OAAOA,EAAeC,IAElBC,GAKGpE,MAAKqE,EAAuCD,GAErD,CAEA,EAAAjB,CACEe,GAMA,OAAOA,EAAeC,IAElBC,GAKGpE,MAAKsE,EAAuCF,GAErD,CAEA,EAAAC,CACED,GAMA,OAAOR,MAAOvD,EAAyBwD,WAC/B,IAAIC,QACR,CACEC,EACAC,KAUAD,EAAQK,EAAW/D,EAASwD,EARSI,SACvB1D,IAAR0D,EACFD,EAAOC,GAEPF,SAQZ,CAEA,EAAAO,CACEF,GAMA,OAAOR,MAAOvD,EAAyBwD,WAC/B,IAAIC,QACR,CACEC,EACAC,KAUAD,EAAQK,EAAW/D,EAASwD,EARSI,SACvB1D,IAAR0D,EACFD,EAAOC,GAEPF,SAQZ,CAEA,EAAAf,CACEC,GAEA,OAAOA,IAAsBsB,EAAkBC,IAC3CC,OAAOC,OAAOH,GAAmBI,OAC9B5B,GAA8BA,IAAWwB,EAAkBC,KAE9DvB,CACN"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "author": "Adrián Martínez Jiménez",
3
+ "bugs": {
4
+ "url": "https://github.com/inversify/monorepo/issues"
5
+ },
6
+ "description": "InversifyJs http fastify package",
7
+ "dependencies": {
8
+ "@fastify/cookie": "11.0.2",
9
+ "@fastify/formbody": "8.0.2",
10
+ "@inversifyjs/http-core": "2.0.0"
11
+ },
12
+ "devDependencies": {
13
+ "@stryker-mutator/core": "9.1.1",
14
+ "@stryker-mutator/typescript-checker": "9.1.1",
15
+ "@stryker-mutator/vitest-runner": "9.1.1",
16
+ "@types/node": "22.18.6",
17
+ "@vitest/coverage-v8": "3.2.4",
18
+ "eslint": "9.36.0",
19
+ "prettier": "3.6.2",
20
+ "rimraf": "6.0.1",
21
+ "rollup": "4.52.0",
22
+ "ts-loader": "9.5.4",
23
+ "tslib": "2.8.1",
24
+ "typescript": "5.9.2",
25
+ "vitest": "3.2.4"
26
+ },
27
+ "devEngines": {
28
+ "node": "^22.10.2",
29
+ "pnpm": "^10.13.1"
30
+ },
31
+ "homepage": "https://inversify.io",
32
+ "keywords": [
33
+ "dependency injection",
34
+ "dependency inversion",
35
+ "di",
36
+ "inversion of control container",
37
+ "ioc",
38
+ "javascript",
39
+ "node",
40
+ "typescript"
41
+ ],
42
+ "license": "MIT",
43
+ "main": "lib/cjs/index.js",
44
+ "module": "lib/esm/index.js",
45
+ "exports": {
46
+ ".": {
47
+ "import": "./lib/esm/index.js",
48
+ "require": "./lib/cjs/index.js"
49
+ }
50
+ },
51
+ "name": "@inversifyjs/http-fastify",
52
+ "peerDependencies": {
53
+ "fastify": "^5.6.1",
54
+ "inversify": "^7.10.0"
55
+ },
56
+ "publishConfig": {
57
+ "access": "public"
58
+ },
59
+ "repository": {
60
+ "type": "git",
61
+ "url": "git+https://github.com/inversify/monorepo.git"
62
+ },
63
+ "version": "2.0.0",
64
+ "scripts": {
65
+ "build": "pnpm run build:cjs && pnpm run build:esm",
66
+ "build:cjs": "tsc --build tsconfig.cjs.json && pnpm exec foundation-ts-package-cjs ./lib/cjs",
67
+ "build:esm": "rollup -c ./rollup.config.mjs && pnpm exec foundation-ts-package-esm ./lib/esm",
68
+ "build:clean": "rimraf lib",
69
+ "format": "prettier --write ./src",
70
+ "lint": "eslint ./src",
71
+ "prebuild": "pnpm run build:clean",
72
+ "test": "vitest run",
73
+ "test:integration": "pnpm run test --project Integration",
74
+ "test:coverage": "pnpm run test:unit --coverage",
75
+ "test:uncommitted": "pnpm run test --changed=HEAD",
76
+ "test:unit": "pnpm run test --project Unit"
77
+ }
78
+ }