@forklaunch/hyper-express 0.1.0 → 0.1.2

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.
Files changed (73) hide show
  1. package/LICENSE +21 -0
  2. package/lib/config.d.ts +2 -0
  3. package/lib/config.d.ts.map +1 -0
  4. package/lib/config.js +1 -0
  5. package/lib/eslint.config.d.mts +3 -0
  6. package/lib/eslint.config.d.mts.map +1 -0
  7. package/lib/eslint.config.mjs +10 -0
  8. package/lib/index.d.ts +23 -0
  9. package/lib/index.d.ts.map +1 -0
  10. package/lib/index.js +25 -0
  11. package/lib/jest.config.d.ts +4 -0
  12. package/lib/jest.config.d.ts.map +1 -0
  13. package/lib/jest.config.js +21 -0
  14. package/lib/src/hyperExpressApplication.d.ts +36 -0
  15. package/lib/src/hyperExpressApplication.d.ts.map +1 -0
  16. package/lib/src/hyperExpressApplication.js +69 -0
  17. package/lib/src/hyperExpressRouter.d.ts +10 -0
  18. package/lib/src/hyperExpressRouter.d.ts.map +1 -0
  19. package/lib/src/hyperExpressRouter.js +22 -0
  20. package/lib/src/middleware/contentParse.middleware.d.ts +8 -0
  21. package/lib/src/middleware/contentParse.middleware.d.ts.map +1 -0
  22. package/lib/src/middleware/contentParse.middleware.js +26 -0
  23. package/lib/src/middleware/enrichResponseTransmission.middleware.d.ts +14 -0
  24. package/lib/src/middleware/enrichResponseTransmission.middleware.d.ts.map +1 -0
  25. package/{dist/middleware/response.middleware.js → lib/src/middleware/enrichResponseTransmission.middleware.js} +19 -17
  26. package/lib/src/middleware/polyfillGetHeaders.middleware.d.ts +3 -0
  27. package/lib/src/middleware/polyfillGetHeaders.middleware.d.ts.map +1 -0
  28. package/lib/src/middleware/polyfillGetHeaders.middleware.js +8 -0
  29. package/{dist → lib/src}/middleware/swagger.middleware.d.ts +2 -1
  30. package/lib/src/middleware/swagger.middleware.d.ts.map +1 -0
  31. package/{dist → lib/src}/middleware/swagger.middleware.js +9 -19
  32. package/lib/src/types/hyperExpress.types.d.ts +42 -0
  33. package/lib/src/types/hyperExpress.types.d.ts.map +1 -0
  34. package/lib/tests/typebox.forklaunch.hyperExpress.test.d.ts +2 -0
  35. package/lib/tests/typebox.forklaunch.hyperExpress.test.d.ts.map +1 -0
  36. package/{dist/tests/zod.forklaunch.hyperExpress.test.js → lib/tests/typebox.forklaunch.hyperExpress.test.js} +23 -51
  37. package/lib/tests/zod.forklaunch.hyperExpress.test.d.ts +2 -0
  38. package/lib/tests/zod.forklaunch.hyperExpress.test.d.ts.map +1 -0
  39. package/{dist/tests/typebox.forklaunch.hyperExpress.test.js → lib/tests/zod.forklaunch.hyperExpress.test.js} +22 -52
  40. package/lib/tsconfig.tsbuildinfo +1 -0
  41. package/lib/vitest.config.d.ts +3 -0
  42. package/lib/vitest.config.d.ts.map +1 -0
  43. package/lib/vitest.config.js +7 -0
  44. package/package.json +32 -19
  45. package/.prettierignore +0 -2
  46. package/.prettierrc +0 -7
  47. package/dist/forklaunch.hyperExpress.d.ts +0 -192
  48. package/dist/forklaunch.hyperExpress.js +0 -358
  49. package/dist/forklaunch.hyperExpress.js.map +0 -1
  50. package/dist/jest.config.d.ts +0 -3
  51. package/dist/jest.config.js.map +0 -1
  52. package/dist/main.js +0 -90
  53. package/dist/main.js.map +0 -1
  54. package/dist/middleware/contentParse.middleware.d.ts +0 -7
  55. package/dist/middleware/contentParse.middleware.js +0 -30
  56. package/dist/middleware/contentParse.middleware.js.map +0 -1
  57. package/dist/middleware/response.middleware.d.ts +0 -12
  58. package/dist/middleware/response.middleware.js.map +0 -1
  59. package/dist/middleware/swagger.middleware.js.map +0 -1
  60. package/dist/tests/typebox.forklaunch.hyperExpress.test.d.ts +0 -1
  61. package/dist/tests/typebox.forklaunch.hyperExpress.test.js.map +0 -1
  62. package/dist/tests/zod.forklaunch.hyperExpress.test.d.ts +0 -1
  63. package/dist/tests/zod.forklaunch.hyperExpress.test.js.map +0 -1
  64. package/dist/types/forklaunch.hyperExpress.types.d.ts +0 -67
  65. package/dist/types/forklaunch.hyperExpress.types.js +0 -3
  66. package/dist/types/forklaunch.hyperExpress.types.js.map +0 -1
  67. package/eslint.config.mjs +0 -12
  68. package/forklaunch.hyperExpress.ts +0 -649
  69. package/jest.config.ts +0 -10
  70. package/main.ts +0 -97
  71. package/tests/typebox.forklaunch.hyperExpress.test.ts +0 -164
  72. package/tests/zod.forklaunch.hyperExpress.test.ts +0 -164
  73. /package/{dist/main.d.ts → lib/src/types/hyperExpress.types.js} +0 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 forklaunch
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.
@@ -0,0 +1,2 @@
1
+ export declare const basePath = "/testpath";
2
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../config.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,cAAc,CAAC"}
package/lib/config.js ADDED
@@ -0,0 +1 @@
1
+ export const basePath = '/testpath';
@@ -0,0 +1,3 @@
1
+ declare const _default: any[];
2
+ export default _default;
3
+ //# sourceMappingURL=eslint.config.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eslint.config.d.mts","sourceRoot":"","sources":["../eslint.config.mjs"],"names":[],"mappings":""}
@@ -0,0 +1,10 @@
1
+ import pluginJs from '@eslint/js';
2
+ import globals from 'globals';
3
+ import tseslint from 'typescript-eslint';
4
+ export default [
5
+ { files: ['**/*.{ts}'] },
6
+ { ignores: ['tests/**/*', 'dist/**/*', 'node_modules/**/*'] },
7
+ { languageOptions: { globals: globals.browser } },
8
+ pluginJs.configs.recommended,
9
+ ...tseslint.configs.recommended
10
+ ];
package/lib/index.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import { AnySchemaValidator } from '@forklaunch/validator';
2
+ import { Application } from './src/hyperExpressApplication';
3
+ import { Router } from './src/hyperExpressRouter';
4
+ export type App<SV extends AnySchemaValidator> = Application<SV>;
5
+ /**
6
+ * Creates a new instance of Application with the given schema validator.
7
+ *
8
+ * @template SV - A type that extends AnySchemaValidator.
9
+ * @param {SV} schemaValidator - The schema validator.
10
+ * @returns {Application<SV>} - The new application instance.
11
+ */
12
+ export declare function forklaunchExpress<SV extends AnySchemaValidator>(schemaValidator: SV): Application<SV>;
13
+ /**
14
+ * Creates a new instance of Router with the given base path and schema validator.
15
+ *
16
+ * @template SV - A type that extends AnySchemaValidator.
17
+ * @param {string} basePath - The base path for the router.
18
+ * @param {SV} schemaValidator - The schema validator.
19
+ * @returns {Router<SV>} - The new router instance.
20
+ */
21
+ export declare function forklaunchRouter<SV extends AnySchemaValidator, BasePath extends `/${string}`>(basePath: BasePath, schemaValidator: SV): Router<SV, BasePath>;
22
+ export * from './src/types/hyperExpress.types';
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAElD,MAAM,MAAM,GAAG,CAAC,EAAE,SAAS,kBAAkB,IAAI,WAAW,CAAC,EAAE,CAAC,CAAC;AAEjE;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,SAAS,kBAAkB,EAC7D,eAAe,EAAE,EAAE,mBAGpB;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,EAAE,SAAS,kBAAkB,EAC7B,QAAQ,SAAS,IAAI,MAAM,EAAE,EAC7B,QAAQ,EAAE,QAAQ,EAAE,eAAe,EAAE,EAAE,GAAG,MAAM,CAAC,EAAE,EAAE,QAAQ,CAAC,CAG/D;AAED,cAAc,gCAAgC,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,25 @@
1
+ import { Application } from './src/hyperExpressApplication';
2
+ import { Router } from './src/hyperExpressRouter';
3
+ /**
4
+ * Creates a new instance of Application with the given schema validator.
5
+ *
6
+ * @template SV - A type that extends AnySchemaValidator.
7
+ * @param {SV} schemaValidator - The schema validator.
8
+ * @returns {Application<SV>} - The new application instance.
9
+ */
10
+ export function forklaunchExpress(schemaValidator) {
11
+ return new Application(schemaValidator);
12
+ }
13
+ /**
14
+ * Creates a new instance of Router with the given base path and schema validator.
15
+ *
16
+ * @template SV - A type that extends AnySchemaValidator.
17
+ * @param {string} basePath - The base path for the router.
18
+ * @param {SV} schemaValidator - The schema validator.
19
+ * @returns {Router<SV>} - The new router instance.
20
+ */
21
+ export function forklaunchRouter(basePath, schemaValidator) {
22
+ const router = new Router(basePath, schemaValidator);
23
+ return router;
24
+ }
25
+ export * from './src/types/hyperExpress.types';
@@ -0,0 +1,4 @@
1
+ import type { JestConfigWithTsJest } from 'ts-jest';
2
+ declare const jestConfig: JestConfigWithTsJest;
3
+ export default jestConfig;
4
+ //# sourceMappingURL=jest.config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jest.config.d.ts","sourceRoot":"","sources":["../jest.config.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAEpD,QAAA,MAAM,UAAU,EAAE,oBAkBjB,CAAC;AAEF,eAAe,UAAU,CAAC"}
@@ -0,0 +1,21 @@
1
+ // import type { Config } from 'jest';
2
+ const jestConfig = {
3
+ // [...]
4
+ preset: 'ts-jest/presets/default-esm', // or other ESM presets
5
+ moduleNameMapper: {
6
+ '^(\\.{1,2}/.*)\\.js$': '$1'
7
+ },
8
+ transform: {
9
+ // '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
10
+ // '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
11
+ '^.+\\.[tj]sx?$': [
12
+ 'ts-jest',
13
+ {
14
+ useESM: true
15
+ }
16
+ ],
17
+ '^.+\\.js$': 'babel-jest'
18
+ },
19
+ testPathIgnorePatterns: ['.*dist/', '.*node_modules/']
20
+ };
21
+ export default jestConfig;
@@ -0,0 +1,36 @@
1
+ import { ForklaunchExpressLikeApplication, ForklaunchRouter } from '@forklaunch/core/http';
2
+ import { MiddlewareHandler, Server } from '@forklaunch/hyper-express-fork';
3
+ import { AnySchemaValidator } from '@forklaunch/validator';
4
+ import * as uWebsockets from 'uWebSockets.js';
5
+ /**
6
+ * Represents an application built on top of Hyper-Express and Forklaunch.
7
+ *
8
+ * @template SV - A type that extends AnySchemaValidator.
9
+ */
10
+ export declare class Application<SV extends AnySchemaValidator> extends ForklaunchExpressLikeApplication<SV, Server> {
11
+ /**
12
+ * Creates an instance of the Application class.
13
+ *
14
+ * @param {SV} schemaValidator - The schema validator.
15
+ */
16
+ constructor(schemaValidator: SV);
17
+ /**
18
+ * Registers middleware or routers to the application.
19
+ *
20
+ * @param {ForklaunchRouter<SV> | MiddlewareHandler<SV> | MiddlewareHandler<SV>[]} router - The router or middleware to register.
21
+ * @param {...(ForklaunchRouter<SV> | MiddlewareHandler<SV> | MiddlewareHandler<SV>[])} args - Additional arguments.
22
+ * @returns {this} - The application instance.
23
+ */
24
+ use(router: ForklaunchRouter<SV> | MiddlewareHandler | MiddlewareHandler[], ...args: (ForklaunchRouter<SV> | MiddlewareHandler | MiddlewareHandler[])[]): this;
25
+ /**
26
+ * Starts the server and sets up Swagger documentation.
27
+ *
28
+ * @param {string | number} arg0 - The port number or UNIX path to listen on.
29
+ * @param {...unknown[]} args - Additional arguments.
30
+ * @returns {Promise<uWebsockets.us_listen_socket>} - A promise that resolves with the listening socket.
31
+ */
32
+ listen(port: number, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
33
+ listen(port: number, host?: string, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
34
+ listen(unix_path: string, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
35
+ }
36
+ //# sourceMappingURL=hyperExpressApplication.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hyperExpressApplication.d.ts","sourceRoot":"","sources":["../../src/hyperExpressApplication.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gCAAgC,EAChC,gBAAgB,EAEjB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,WAAW,MAAM,gBAAgB,CAAC;AAI9C;;;;GAIG;AACH,qBAAa,WAAW,CACtB,EAAE,SAAS,kBAAkB,CAC7B,SAAQ,gCAAgC,CAAC,EAAE,EAAE,MAAM,CAAC;IACpD;;;;OAIG;gBACS,eAAe,EAAE,EAAE;IAI/B;;;;;;OAMG;IACH,GAAG,CACD,MAAM,EAAE,gBAAgB,CAAC,EAAE,CAAC,GAAG,iBAAiB,GAAG,iBAAiB,EAAE,EACtE,GAAG,IAAI,EAAE,CAAC,gBAAgB,CAAC,EAAE,CAAC,GAAG,iBAAiB,GAAG,iBAAiB,EAAE,CAAC,EAAE,GAC1E,IAAI;IA0BP;;;;;;OAMG;IACH,MAAM,CACJ,IAAI,EAAE,MAAM,EACZ,QAAQ,CAAC,EAAE,CAAC,aAAa,EAAE,WAAW,CAAC,gBAAgB,KAAK,IAAI,GAC/D,OAAO,CAAC,WAAW,CAAC,gBAAgB,CAAC;IACxC,MAAM,CACJ,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,MAAM,EACb,QAAQ,CAAC,EAAE,CAAC,aAAa,EAAE,WAAW,CAAC,gBAAgB,KAAK,IAAI,GAC/D,OAAO,CAAC,WAAW,CAAC,gBAAgB,CAAC;IACxC,MAAM,CACJ,SAAS,EAAE,MAAM,EACjB,QAAQ,CAAC,EAAE,CAAC,aAAa,EAAE,WAAW,CAAC,gBAAgB,KAAK,IAAI,GAC/D,OAAO,CAAC,WAAW,CAAC,gBAAgB,CAAC;CA0CzC"}
@@ -0,0 +1,69 @@
1
+ import { ForklaunchExpressLikeApplication, generateSwaggerDocument } from '@forklaunch/core/http';
2
+ import { Server } from '@forklaunch/hyper-express-fork';
3
+ import { Router } from './hyperExpressRouter';
4
+ import { swagger, swaggerRedirect } from './middleware/swagger.middleware';
5
+ /**
6
+ * Represents an application built on top of Hyper-Express and Forklaunch.
7
+ *
8
+ * @template SV - A type that extends AnySchemaValidator.
9
+ */
10
+ export class Application extends ForklaunchExpressLikeApplication {
11
+ /**
12
+ * Creates an instance of the Application class.
13
+ *
14
+ * @param {SV} schemaValidator - The schema validator.
15
+ */
16
+ constructor(schemaValidator) {
17
+ super(schemaValidator, new Server());
18
+ }
19
+ /**
20
+ * Registers middleware or routers to the application.
21
+ *
22
+ * @param {ForklaunchRouter<SV> | MiddlewareHandler<SV> | MiddlewareHandler<SV>[]} router - The router or middleware to register.
23
+ * @param {...(ForklaunchRouter<SV> | MiddlewareHandler<SV> | MiddlewareHandler<SV>[])} args - Additional arguments.
24
+ * @returns {this} - The application instance.
25
+ */
26
+ use(router, ...args) {
27
+ if (router instanceof Router) {
28
+ this.routers.push(router);
29
+ this.internal.use(router.basePath, router.internal);
30
+ return this;
31
+ }
32
+ else {
33
+ const router = args.pop();
34
+ if (!(router instanceof Router)) {
35
+ throw new Error('Last argument must be a router');
36
+ }
37
+ args.forEach((arg) => {
38
+ if (arg instanceof Router) {
39
+ throw new Error('Only one router is allowed');
40
+ }
41
+ });
42
+ this.internal.use(router.basePath, ...args, router.internal);
43
+ return this;
44
+ }
45
+ }
46
+ listen(arg0, arg1, arg2) {
47
+ if (typeof arg0 === 'number') {
48
+ const port = arg0 || Number(process.env.PORT);
49
+ this.internal.set_error_handler((_req, res, err) => {
50
+ res.locals.errorMessage = err.message;
51
+ // TODO: replace with logger
52
+ console.error(err);
53
+ res
54
+ .status(res.statusCode && res.statusCode >= 400 ? res.statusCode : 500)
55
+ .send(`Internal server error:\n\n${err.message}`);
56
+ });
57
+ const swaggerPath = `/api${process.env.VERSION ?? '/v1'}${process.env.SWAGGER_PATH ?? '/swagger'}`;
58
+ this.internal.use(swaggerPath, swaggerRedirect(swaggerPath));
59
+ this.internal.get(`${swaggerPath}/*`, swagger(swaggerPath, generateSwaggerDocument(this.schemaValidator, port, this.routers)));
60
+ if (arg1 && typeof arg1 === 'string') {
61
+ return this.internal.listen(port, arg1, arg2);
62
+ }
63
+ else if (arg1 && typeof arg1 === 'function') {
64
+ return this.internal.listen(port, arg1);
65
+ }
66
+ }
67
+ return this.internal.listen(arg0, arg1);
68
+ }
69
+ }
@@ -0,0 +1,10 @@
1
+ import { ForklaunchExpressLikeRouter, ForklaunchRouter, TypedMiddlewareDefinition } from '@forklaunch/core/http';
2
+ import { Router as ExpressRouter, MiddlewareHandler } from '@forklaunch/hyper-express-fork';
3
+ import { AnySchemaValidator } from '@forklaunch/validator';
4
+ export declare class Router<SV extends AnySchemaValidator, BasePath extends `/${string}`> extends ForklaunchExpressLikeRouter<SV, BasePath, MiddlewareHandler, ExpressRouter> implements ForklaunchRouter<SV> {
5
+ basePath: BasePath;
6
+ constructor(basePath: BasePath, schemaValidator: SV);
7
+ route(path: string): this;
8
+ any: TypedMiddlewareDefinition<this, SV>;
9
+ }
10
+ //# sourceMappingURL=hyperExpressRouter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hyperExpressRouter.d.ts","sourceRoot":"","sources":["../../src/hyperExpressRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,2BAA2B,EAC3B,gBAAgB,EAMhB,yBAAyB,EAC1B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,MAAM,IAAI,aAAa,EACvB,iBAAiB,EAClB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAM3D,qBAAa,MAAM,CACf,EAAE,SAAS,kBAAkB,EAC7B,QAAQ,SAAS,IAAI,MAAM,EAAE,CAE/B,SAAQ,2BAA2B,CACjC,EAAE,EACF,QAAQ,EACR,iBAAiB,EACjB,aAAa,CAEf,YAAW,gBAAgB,CAAC,EAAE,CAAC;IAGtB,QAAQ,EAAE,QAAQ;gBAAlB,QAAQ,EAAE,QAAQ,EACzB,eAAe,EAAE,EAAE;IAWrB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAKzB,GAAG,EAAE,yBAAyB,CAAC,IAAI,EAAE,EAAE,CAAC,CAgEtC;CAKH"}
@@ -0,0 +1,22 @@
1
+ import { ForklaunchExpressLikeRouter } from '@forklaunch/core/http';
2
+ import { Router as ExpressRouter } from '@forklaunch/hyper-express-fork';
3
+ import { contentParse } from './middleware/contentParse.middleware';
4
+ import { enrichResponseTransmission } from './middleware/enrichResponseTransmission.middleware';
5
+ import { polyfillGetHeaders } from './middleware/polyfillGetHeaders.middleware';
6
+ export class Router extends ForklaunchExpressLikeRouter {
7
+ basePath;
8
+ constructor(basePath, schemaValidator) {
9
+ super(basePath, schemaValidator, new ExpressRouter());
10
+ this.basePath = basePath;
11
+ this.internal.use(polyfillGetHeaders);
12
+ this.internal.use(contentParse);
13
+ this.internal.use(enrichResponseTransmission);
14
+ }
15
+ route(path) {
16
+ this.internal.route(path);
17
+ return this;
18
+ }
19
+ any = (pathOrContractDetailsOrMiddlewareOrTypedHandler, contractDetailsOrMiddlewareOrTypedHandler, ...middlewareOrMiddlewareWithTypedHandler) => {
20
+ return this.registerMiddlewareHandler(this.internal.any, pathOrContractDetailsOrMiddlewareOrTypedHandler, contractDetailsOrMiddlewareOrTypedHandler, ...middlewareOrMiddlewareWithTypedHandler);
21
+ };
22
+ }
@@ -0,0 +1,8 @@
1
+ import { Request } from '@forklaunch/hyper-express-fork';
2
+ /**
3
+ * Middleware function to parse the request body based on the content type.
4
+ *
5
+ * @returns {Function} - The middleware function.
6
+ */
7
+ export declare function contentParse(req: Request): Promise<void>;
8
+ //# sourceMappingURL=contentParse.middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contentParse.middleware.d.ts","sourceRoot":"","sources":["../../../src/middleware/contentParse.middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAEzD;;;;GAIG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,OAAO,iBAsB9C"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Middleware function to parse the request body based on the content type.
3
+ *
4
+ * @returns {Function} - The middleware function.
5
+ */
6
+ export async function contentParse(req) {
7
+ console.debug('[MIDDLEWARE] contentParse started');
8
+ switch (req.headers['content-type'] &&
9
+ req.headers['content-type'].split(';')[0]) {
10
+ case 'application/json':
11
+ req.body = await req.json();
12
+ break;
13
+ case 'application/x-www-form-urlencoded':
14
+ req.body = await req.urlencoded();
15
+ break;
16
+ case 'text/plain':
17
+ req.body = await req.text();
18
+ break;
19
+ case 'application/octet-stream':
20
+ req.body = await req.buffer();
21
+ break;
22
+ default:
23
+ req.body = await req.json();
24
+ break;
25
+ }
26
+ }
@@ -0,0 +1,14 @@
1
+ import { ForklaunchNextFunction, ParamsDictionary } from '@forklaunch/core/http';
2
+ import { AnySchemaValidator } from '@forklaunch/validator';
3
+ import { ParsedQs } from 'qs';
4
+ import { Request, Response } from '../types/hyperExpress.types';
5
+ /**
6
+ * Middleware to enrich the response transmission by intercepting and parsing responses before they are sent.
7
+ *
8
+ * @template SV - A type that extends AnySchemaValidator.
9
+ * @param {Request<SV>} req - The request object.
10
+ * @param {Response} res - The response object.
11
+ * @param {MiddlewareNext} next - The next middleware function.
12
+ */
13
+ export declare function enrichResponseTransmission<SV extends AnySchemaValidator>(req: Request<SV, ParamsDictionary, Record<string, unknown>, ParsedQs, Record<string, string>, Record<string, unknown>>, res: Response<Record<number, unknown>, Record<string, string>, Record<string, unknown>>, next: ForklaunchNextFunction): void;
14
+ //# sourceMappingURL=enrichResponseTransmission.middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enrichResponseTransmission.middleware.d.ts","sourceRoot":"","sources":["../../../src/middleware/enrichResponseTransmission.middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,sBAAsB,EACtB,gBAAgB,EACjB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AAEhE;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CAAC,EAAE,SAAS,kBAAkB,EACtE,GAAG,EAAE,OAAO,CACV,EAAE,EACF,gBAAgB,EAChB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACvB,QAAQ,EACR,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACtB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACxB,EACD,GAAG,EAAE,QAAQ,CACX,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACvB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACtB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACxB,EACD,IAAI,EAAE,sBAAsB,QA6D7B"}
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.enrichResponseTransmission = enrichResponseTransmission;
4
- const core_1 = require("@forklaunch/core");
1
+ import { enrichExpressLikeSend } from '@forklaunch/core/http';
5
2
  /**
6
3
  * Middleware to enrich the response transmission by intercepting and parsing responses before they are sent.
7
4
  *
@@ -10,9 +7,11 @@ const core_1 = require("@forklaunch/core");
10
7
  * @param {Response} res - The response object.
11
8
  * @param {MiddlewareNext} next - The next middleware function.
12
9
  */
13
- function enrichResponseTransmission(req, res, next) {
10
+ export function enrichResponseTransmission(req, res, next) {
11
+ console.debug('[MIDDLEWARE] enrichResponseTransmission');
14
12
  const originalSend = res.send;
15
13
  const originalJson = res.json;
14
+ const originalSetHeader = res.setHeader;
16
15
  /**
17
16
  * Intercepts the JSON response to include additional processing.
18
17
  *
@@ -34,21 +33,24 @@ function enrichResponseTransmission(req, res, next) {
34
33
  if (!res.bodyData) {
35
34
  res.bodyData = data;
36
35
  res.statusCode = res._status_code;
37
- res.corked = res._corked;
38
36
  }
39
- try {
40
- if ((res._cork && !res.corked) || !res._cork) {
41
- (0, core_1.parseResponse)(req, res);
42
- }
43
- return originalSend.call(this, data);
37
+ return enrichExpressLikeSend(this, req, res, originalSend, data, !res.cors && ((res._cork && !res._corked) || !res._cork));
38
+ };
39
+ /**
40
+ * Intercepts the setHeader method to stringify the value before setting the header.
41
+ *
42
+ * @param {string}
43
+ */
44
+ res.setHeader = function (name, value) {
45
+ let stringifiedValue;
46
+ if (Array.isArray(value)) {
47
+ stringifiedValue = value.map((v) => typeof v !== 'string' ? JSON.stringify(v) : v);
44
48
  }
45
- catch (error) {
46
- console.error(error);
47
- res.status(500);
48
- originalSend.call(this, 'Internal Server Error: ' + error.message);
49
- throw error;
49
+ else {
50
+ stringifiedValue =
51
+ typeof value !== 'string' ? JSON.stringify(value) : value;
50
52
  }
53
+ return originalSetHeader.call(this, name, stringifiedValue);
51
54
  };
52
55
  next();
53
56
  }
54
- //# sourceMappingURL=response.middleware.js.map
@@ -0,0 +1,3 @@
1
+ import { MiddlewareNext, Request, Response } from '@forklaunch/hyper-express-fork';
2
+ export declare function polyfillGetHeaders(_req: Request, res: Response, next?: MiddlewareNext): void;
3
+ //# sourceMappingURL=polyfillGetHeaders.middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"polyfillGetHeaders.middleware.d.ts","sourceRoot":"","sources":["../../../src/middleware/polyfillGetHeaders.middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,OAAO,EACP,QAAQ,EACT,MAAM,gCAAgC,CAAC;AAGxC,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,OAAO,EACb,GAAG,EAAE,QAAQ,EACb,IAAI,CAAC,EAAE,cAAc,QAOtB"}
@@ -0,0 +1,8 @@
1
+ // TODO: Move into fork, and create gh issue
2
+ export function polyfillGetHeaders(_req, res, next) {
3
+ console.debug('[MIDDLEWARE] polyfillGetHeaders started');
4
+ res.getHeaders = () => {
5
+ return res._headers;
6
+ };
7
+ next?.();
8
+ }
@@ -1,4 +1,4 @@
1
- import { MiddlewareHandler } from 'hyper-express';
1
+ import { MiddlewareHandler } from '@forklaunch/hyper-express-fork';
2
2
  import { OpenAPIObject } from 'openapi3-ts/oas31';
3
3
  import swaggerUi from 'swagger-ui-express';
4
4
  /**
@@ -22,3 +22,4 @@ export declare function swaggerRedirect(path: string): MiddlewareHandler;
22
22
  * @returns {MiddlewareHandler[]} - An array of middleware handlers for serving the Swagger UI.
23
23
  */
24
24
  export declare function swagger(path: string, document: OpenAPIObject, opts?: swaggerUi.SwaggerUiOptions, options?: swaggerUi.SwaggerOptions, customCss?: string, customfavIcon?: string, swaggerUrl?: string, customSiteTitle?: string): MiddlewareHandler[];
25
+ //# sourceMappingURL=swagger.middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swagger.middleware.d.ts","sourceRoot":"","sources":["../../../src/middleware/swagger.middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAIlB,MAAM,gCAAgC,CAAC;AAExC,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,OAAO,SAAS,MAAM,oBAAoB,CAAC;AAE3C;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,iBAAiB,CAO/D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,aAAa,EACvB,IAAI,CAAC,EAAE,SAAS,CAAC,gBAAgB,EACjC,OAAO,CAAC,EAAE,SAAS,CAAC,cAAc,EAClC,SAAS,CAAC,EAAE,MAAM,EAClB,aAAa,CAAC,EAAE,MAAM,EACtB,UAAU,CAAC,EAAE,MAAM,EACnB,eAAe,CAAC,EAAE,MAAM,GACvB,iBAAiB,EAAE,CA+DrB"}
@@ -1,27 +1,18 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.swaggerRedirect = swaggerRedirect;
7
- exports.swagger = swagger;
8
- const live_directory_1 = __importDefault(require("live-directory"));
9
- const absolute_path_1 = __importDefault(require("swagger-ui-dist/absolute-path"));
10
- const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
1
+ import LiveDirectory from 'live-directory';
2
+ import getAbsoluteSwaggerFsPath from 'swagger-ui-dist/absolute-path';
3
+ import swaggerUi from 'swagger-ui-express';
11
4
  /**
12
5
  * Middleware to redirect requests to the Swagger UI base path.
13
6
  *
14
7
  * @param {string} path - The base path for the Swagger UI.
15
8
  * @returns {MiddlewareHandler} - The middleware handler for redirecting requests.
16
9
  */
17
- function swaggerRedirect(path) {
10
+ export function swaggerRedirect(path) {
18
11
  return (req, res, next) => {
19
12
  if (req.path === path) {
20
13
  res.redirect(`${path}/`);
21
14
  }
22
- if (next) {
23
- return next();
24
- }
15
+ return next?.();
25
16
  };
26
17
  }
27
18
  /**
@@ -37,8 +28,8 @@ function swaggerRedirect(path) {
37
28
  * @param {string} [customSiteTitle] - Custom site title for the Swagger UI.
38
29
  * @returns {MiddlewareHandler[]} - An array of middleware handlers for serving the Swagger UI.
39
30
  */
40
- function swagger(path, document, opts, options, customCss, customfavIcon, swaggerUrl, customSiteTitle) {
41
- const LiveAssets = new live_directory_1.default((0, absolute_path_1.default)(), {
31
+ export function swagger(path, document, opts, options, customCss, customfavIcon, swaggerUrl, customSiteTitle) {
32
+ const LiveAssets = new LiveDirectory(getAbsoluteSwaggerFsPath(), {
42
33
  filter: {
43
34
  keep: {
44
35
  names: [
@@ -56,7 +47,7 @@ function swagger(path, document, opts, options, customCss, customfavIcon, swagge
56
47
  max_file_size: 1024 * 1024 * 1.5
57
48
  }
58
49
  });
59
- const serve = swagger_ui_express_1.default.serve[0];
50
+ const serve = swaggerUi.serve[0];
60
51
  /**
61
52
  * Middleware to serve static assets for the Swagger UI.
62
53
  *
@@ -84,7 +75,6 @@ function swagger(path, document, opts, options, customCss, customfavIcon, swagge
84
75
  return res.type(extension).stream(content);
85
76
  }
86
77
  };
87
- const ui = swagger_ui_express_1.default.setup(document, opts, options, customCss, customfavIcon, swaggerUrl, customSiteTitle);
78
+ const ui = swaggerUi.setup(document, opts, options, customCss, customfavIcon, swaggerUrl, customSiteTitle);
88
79
  return [serve, staticAssets, ui];
89
80
  }
90
- //# sourceMappingURL=swagger.middleware.js.map
@@ -0,0 +1,42 @@
1
+ import { ForklaunchRequest, ForklaunchResponse, ForklaunchSendableData, ForklaunchStatusResponse, ParamsDictionary } from '@forklaunch/core/http';
2
+ import { Request as ExpressRequest, Response as ExpressResponse } from '@forklaunch/hyper-express-fork';
3
+ import { AnySchemaValidator } from '@forklaunch/validator';
4
+ import { ParsedQs } from 'qs';
5
+ /**
6
+ * Extends the Forklaunch request interface with properties from Hyper-Express's request interface.
7
+ *
8
+ * @template SV - A type that extends AnySchemaValidator.
9
+ * @template P - A type for request parameters, defaulting to ParamsDictionary.
10
+ * @template _ResBody - A type for the response body, defaulting to unknown.
11
+ * @template ReqBody - A type for the request body, defaulting to unknown.
12
+ * @template ReqQuery - A type for the request query, defaulting to ParsedQs.
13
+ * @template LocalsObj - A type for local variables, defaulting to an empty object.
14
+ */
15
+ export interface Request<SV extends AnySchemaValidator, P extends ParamsDictionary, ReqBody extends Record<string, unknown>, ReqQuery extends ParsedQs, ReqHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>> extends ForklaunchRequest<SV, P, ReqBody, ReqQuery, ReqHeaders>, Omit<ExpressRequest<LocalsObj>, 'method' | 'params' | 'query' | 'headers'> {
16
+ /** The request body */
17
+ body: ReqBody;
18
+ /** The request query parameters */
19
+ query: ReqQuery;
20
+ /** The request parameters */
21
+ params: P;
22
+ }
23
+ /**
24
+ * Extends the Forklaunch response interface with properties from Hyper-Express's response interface.
25
+ *
26
+ * @template ResBody - A type for the response body, defaulting to unknown.
27
+ * @template LocalsObj - A type for local variables, defaulting to an empty object.
28
+ * @template StatusCode - A type for the status code, defaulting to number.
29
+ */
30
+ export interface Response<ResBodyMap extends Record<number, unknown>, ResHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>> extends ForklaunchResponse<ResBodyMap, ResHeaders, LocalsObj>, Omit<ExpressResponse<LocalsObj>, 'getHeaders' | 'setHeader' | 'headersSent' | 'send' | 'status' | 'statusCode' | 'json' | 'jsonp' | 'end'>, ForklaunchStatusResponse<ForklaunchSendableData> {
31
+ /** The body data of the response */
32
+ bodyData: unknown;
33
+ /** If cors are applied to the response */
34
+ cors: boolean;
35
+ /** The status code of the response */
36
+ _status_code: number;
37
+ /** Whether the response is corked */
38
+ _cork: boolean;
39
+ /** Whether the response is currently corked */
40
+ _corked: boolean;
41
+ }
42
+ //# sourceMappingURL=hyperExpress.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hyperExpress.types.d.ts","sourceRoot":"","sources":["../../../src/types/hyperExpress.types.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,wBAAwB,EACxB,gBAAgB,EACjB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,OAAO,IAAI,cAAc,EACzB,QAAQ,IAAI,eAAe,EAC5B,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAE9B;;;;;;;;;GASG;AACH,MAAM,WAAW,OAAO,CACtB,EAAE,SAAS,kBAAkB,EAC7B,CAAC,SAAS,gBAAgB,EAC1B,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACvC,QAAQ,SAAS,QAAQ,EACzB,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACzC,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACzC,SAAQ,iBAAiB,CAAC,EAAE,EAAE,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,CAAC,EAC7D,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,EAAE,QAAQ,GAAG,QAAQ,GAAG,OAAO,GAAG,SAAS,CAAC;IAC5E,uBAAuB;IACvB,IAAI,EAAE,OAAO,CAAC;IACd,mCAAmC;IACnC,KAAK,EAAE,QAAQ,CAAC;IAChB,6BAA6B;IAC7B,MAAM,EAAE,CAAC,CAAC;CACX;AAED;;;;;;GAMG;AACH,MAAM,WAAW,QAAQ,CACvB,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC1C,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACzC,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACzC,SAAQ,kBAAkB,CAAC,UAAU,EAAE,UAAU,EAAE,SAAS,CAAC,EAC3D,IAAI,CACF,eAAe,CAAC,SAAS,CAAC,EACxB,YAAY,GACZ,WAAW,GACX,aAAa,GACb,MAAM,GACN,QAAQ,GACR,YAAY,GACZ,MAAM,GACN,OAAO,GACP,KAAK,CACR,EACD,wBAAwB,CAAC,sBAAsB,CAAC;IAClD,oCAAoC;IACpC,QAAQ,EAAE,OAAO,CAAC;IAClB,0CAA0C;IAC1C,IAAI,EAAE,OAAO,CAAC;IACd,sCAAsC;IACtC,YAAY,EAAE,MAAM,CAAC;IACrB,qCAAqC;IACrC,KAAK,EAAE,OAAO,CAAC;IACf,+CAA+C;IAC/C,OAAO,EAAE,OAAO,CAAC;CAClB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=typebox.forklaunch.hyperExpress.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox.forklaunch.hyperExpress.test.d.ts","sourceRoot":"","sources":["../../tests/typebox.forklaunch.hyperExpress.test.ts"],"names":[],"mappings":""}