@devbro/neko-router 0.1.6 → 0.1.7

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 (44) hide show
  1. package/dist/CompiledRoute.mjs +23 -49
  2. package/dist/CompiledRoute.mjs.map +1 -1
  3. package/dist/Controller.mjs +14 -35
  4. package/dist/Controller.mjs.map +1 -1
  5. package/dist/Middleware.mjs.map +1 -1
  6. package/dist/MiddlewareFactory.mjs +1 -1
  7. package/dist/MiddlewareFactory.mjs.map +1 -1
  8. package/dist/Route.mjs +2 -1
  9. package/dist/Route.mjs.map +1 -1
  10. package/dist/Router.mjs +7 -29
  11. package/dist/Router.mjs.map +1 -1
  12. package/dist/helpers.mjs +16 -40
  13. package/dist/helpers.mjs.map +1 -1
  14. package/dist/index.js +460 -17
  15. package/dist/index.js.map +1 -1
  16. package/dist/index.mjs +8 -8
  17. package/dist/index.mjs.map +1 -1
  18. package/package.json +6 -3
  19. package/dist/CompiledRoute.d.ts +0 -22
  20. package/dist/CompiledRoute.js +0 -161
  21. package/dist/CompiledRoute.js.map +0 -1
  22. package/dist/Controller.d.ts +0 -42
  23. package/dist/Controller.js +0 -154
  24. package/dist/Controller.js.map +0 -1
  25. package/dist/Middleware-C8dxjlFD.d.ts +0 -30
  26. package/dist/Middleware.d.ts +0 -2
  27. package/dist/Middleware.js +0 -35
  28. package/dist/Middleware.js.map +0 -1
  29. package/dist/MiddlewareFactory.d.ts +0 -8
  30. package/dist/MiddlewareFactory.js +0 -42
  31. package/dist/MiddlewareFactory.js.map +0 -1
  32. package/dist/Route.d.ts +0 -40
  33. package/dist/Route.js +0 -138
  34. package/dist/Route.js.map +0 -1
  35. package/dist/Router.d.ts +0 -19
  36. package/dist/Router.js +0 -118
  37. package/dist/Router.js.map +0 -1
  38. package/dist/helpers.d.ts +0 -6
  39. package/dist/helpers.js +0 -70
  40. package/dist/helpers.js.map +0 -1
  41. package/dist/index.d.ts +0 -8
  42. package/dist/types.d.ts +0 -2
  43. package/dist/types.js +0 -17
  44. package/dist/types.js.map +0 -1
@@ -1,161 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var __async = (__this, __arguments, generator) => {
20
- return new Promise((resolve, reject) => {
21
- var fulfilled = (value) => {
22
- try {
23
- step(generator.next(value));
24
- } catch (e) {
25
- reject(e);
26
- }
27
- };
28
- var rejected = (value) => {
29
- try {
30
- step(generator.throw(value));
31
- } catch (e) {
32
- reject(e);
33
- }
34
- };
35
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
36
- step((generator = generator.apply(__this, __arguments)).next());
37
- });
38
- };
39
- var CompiledRoute_exports = {};
40
- __export(CompiledRoute_exports, {
41
- CompiledRoute: () => CompiledRoute
42
- });
43
- module.exports = __toCommonJS(CompiledRoute_exports);
44
- var import_Middleware = require("./Middleware");
45
- var import_MiddlewareFactory = require("./MiddlewareFactory");
46
- class CompiledRoute {
47
- constructor(route, request, response, globalMiddlewares = []) {
48
- this.route = route;
49
- this.request = request;
50
- this.response = response;
51
- this.globalMiddlewares = globalMiddlewares;
52
- this.middlewares = [];
53
- this.prepareMiddlewares();
54
- }
55
- getMiddlewares() {
56
- return this.middlewares;
57
- }
58
- prepareMiddlewares() {
59
- this.middlewares = [];
60
- for (const middleware of [...this.globalMiddlewares, ...this.route.getMiddlewares()]) {
61
- if (middleware instanceof import_Middleware.Middleware) {
62
- this.middlewares.push(middleware);
63
- } else if (this.isClass(middleware)) {
64
- this.middlewares.push(middleware.getInstance({}));
65
- } else if (typeof middleware === "function") {
66
- this.middlewares.push(import_MiddlewareFactory.MiddlewareFactory.create(middleware));
67
- } else {
68
- throw new Error("Invalid middleware type");
69
- }
70
- }
71
- }
72
- isClass(func) {
73
- return typeof func === "function" && /^class\s/.test(Function.prototype.toString.call(func));
74
- }
75
- run() {
76
- return __async(this, null, function* () {
77
- return yield this.runMiddlewares(this.middlewares, this.request, this.response);
78
- });
79
- }
80
- prepareOutputJsonFormat(obj) {
81
- function traverse(value) {
82
- if (!value || typeof value !== "object") {
83
- return value;
84
- }
85
- if (typeof value.toJson === "function") {
86
- return traverse(value.toJson());
87
- }
88
- if (Array.isArray(value)) {
89
- return value.map(traverse);
90
- }
91
- const result = {};
92
- for (const key in value) {
93
- if (Object.prototype.hasOwnProperty.call(value, key)) {
94
- result[key] = traverse(value[key]);
95
- }
96
- }
97
- return result;
98
- }
99
- return traverse(obj);
100
- }
101
- convertToString(obj) {
102
- if (typeof obj === "string") {
103
- return obj;
104
- } else if (obj instanceof Buffer) {
105
- return obj.toString();
106
- } else if (typeof obj === "object") {
107
- return JSON.stringify(this.prepareOutputJsonFormat(obj));
108
- }
109
- return String(obj);
110
- }
111
- processResponseBody(res, controller_rc) {
112
- if (controller_rc && res.writableEnded) {
113
- throw new Error("cannot write to response, response has already ended");
114
- }
115
- if (res.writableEnded) {
116
- return;
117
- }
118
- if (controller_rc) {
119
- const header_content_type = res.getHeader("Content-Type");
120
- if (!header_content_type && typeof controller_rc === "object") {
121
- res.setHeader("Content-Type", "application/json");
122
- } else if (!header_content_type) {
123
- res.setHeader("Content-Type", "text/plain");
124
- }
125
- res.end(this.convertToString(controller_rc));
126
- return;
127
- } else {
128
- res.statusCode = [200].includes(res.statusCode) ? 204 : res.statusCode;
129
- res.end();
130
- }
131
- }
132
- runMiddlewares(middlewares, req, res) {
133
- return __async(this, null, function* () {
134
- let index = 0;
135
- const me = this;
136
- function next() {
137
- return __async(this, null, function* () {
138
- if (index >= middlewares.length) {
139
- const controller_rc = yield me.route.callHanlder(req, res);
140
- yield me.processResponseBody(res, controller_rc);
141
- return;
142
- }
143
- const middleware = middlewares[index++];
144
- if (middleware instanceof import_Middleware.Middleware) {
145
- yield middleware.call(req, res, next);
146
- } else if (typeof middleware === "function") {
147
- yield middleware(req, res, next);
148
- } else {
149
- throw new Error("does not know how to run middleware");
150
- }
151
- });
152
- }
153
- yield next();
154
- });
155
- }
156
- }
157
- // Annotate the CommonJS export names for ESM import in node:
158
- 0 && (module.exports = {
159
- CompiledRoute
160
- });
161
- //# sourceMappingURL=CompiledRoute.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/CompiledRoute.ts"],"sourcesContent":["import { Middleware } from './Middleware';\nimport { MiddlewareFactory } from './MiddlewareFactory';\nimport { Route } from './Route';\nimport { HandlerType, MiddlewareProvider } from './types';\nimport { LexerToken, Request, Response } from './types';\n\nexport class CompiledRoute {\n constructor(\n public route: Route,\n public request: Request,\n public response: Response,\n public globalMiddlewares: MiddlewareProvider[] = []\n ) {\n this.prepareMiddlewares();\n }\n\n private middlewares: Middleware[] = [];\n\n getMiddlewares() {\n return this.middlewares;\n }\n\n private prepareMiddlewares() {\n this.middlewares = [];\n for (const middleware of [...this.globalMiddlewares, ...this.route.getMiddlewares()]) {\n if (middleware instanceof Middleware) {\n this.middlewares.push(middleware);\n } else if (this.isClass(middleware)) {\n this.middlewares.push((middleware as any).getInstance({}));\n } else if (typeof middleware === 'function') {\n this.middlewares.push(MiddlewareFactory.create(middleware as HandlerType));\n } else {\n throw new Error('Invalid middleware type');\n }\n }\n }\n\n isClass(func: any) {\n return typeof func === 'function' && /^class\\s/.test(Function.prototype.toString.call(func));\n }\n\n async run() {\n return await this.runMiddlewares(this.middlewares, this.request, this.response);\n }\n\n prepareOutputJsonFormat<T>(obj: object | Array<any>): T {\n function traverse(value: any): any {\n if (!value || typeof value !== 'object') {\n return value;\n }\n\n if (typeof value.toJson === 'function') {\n return traverse(value.toJson());\n }\n\n if (Array.isArray(value)) {\n return value.map(traverse);\n }\n\n const result: Record<string, any> = {};\n for (const key in value) {\n if (Object.prototype.hasOwnProperty.call(value, key)) {\n result[key] = traverse(value[key]);\n }\n }\n return result;\n }\n\n return traverse(obj);\n }\n\n convertToString(obj: any) {\n if (typeof obj === 'string') {\n return obj;\n } else if (obj instanceof Buffer) {\n return obj.toString();\n } else if (typeof obj === 'object') {\n return JSON.stringify(this.prepareOutputJsonFormat(obj));\n }\n return String(obj);\n }\n\n processResponseBody(res: Response, controller_rc: any) {\n if (controller_rc && res.writableEnded) {\n throw new Error('cannot write to response, response has already ended');\n }\n\n if (res.writableEnded) {\n return;\n }\n\n if (controller_rc) {\n const header_content_type = res.getHeader('Content-Type');\n if (!header_content_type && typeof controller_rc === 'object') {\n res.setHeader('Content-Type', 'application/json');\n } else if (!header_content_type) {\n res.setHeader('Content-Type', 'text/plain');\n }\n\n res.end(this.convertToString(controller_rc));\n return;\n } else {\n res.statusCode = [200].includes(res.statusCode) ? 204 : res.statusCode;\n res.end();\n }\n }\n\n async runMiddlewares(middlewares: Middleware[], req: Request, res: Response) {\n let index = 0;\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const me = this;\n\n async function next() {\n if (index >= middlewares.length) {\n const controller_rc = await me.route.callHanlder(req, res);\n await me.processResponseBody(res, controller_rc);\n return;\n }\n\n const middleware: Middleware | any = middlewares[index++];\n\n if (middleware instanceof Middleware) {\n await middleware.call(req, res, next);\n } else if (typeof middleware === 'function') {\n await middleware(req, res, next);\n } else {\n throw new Error('does not know how to run middleware');\n }\n }\n\n await next();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,w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AAK,aAAa;AAC/C;AAAA,UACF;AAEA,gBAAM,aAA+B,YAAY,OAAO;AAExD,cAAI,sBAAsB,8BAAY;AACpC,kBAAM,WAAW,KAAK,KAAK,KAAK,IAAI;AAAA,UACtC,WAAW,OAAO,eAAe,YAAY;AAC3C,kBAAM,WAAW,KAAK,KAAK,IAAI;AAAA,UACjC,OAAO;AACL,kBAAM,IAAI,MAAM,qCAAqC;AAAA,UACvD;AAAA,QACF;AAAA;AAEA,YAAM,KAAK;AAAA,IACb;AAAA;AACF;","names":[]}
@@ -1,42 +0,0 @@
1
- import { b as HttpMethod, M as MiddlewareProvider, C as ControllerDecoratorOptions } from './Middleware-C8dxjlFD.js';
2
- import 'http';
3
-
4
- declare class BaseController {
5
- static routes: {
6
- methods: HttpMethod[];
7
- path: string;
8
- handler: string;
9
- middlewares: MiddlewareProvider[];
10
- }[];
11
- static basePath: string;
12
- static baseMiddlewares: MiddlewareProvider[];
13
- static getInstance(): BaseController;
14
- }
15
- declare function Controller(path: string, options?: ControllerDecoratorOptions): ClassDecorator;
16
- declare function Get(data?: {
17
- path?: string;
18
- middlewares?: MiddlewareProvider[];
19
- }): MethodDecorator;
20
- declare function Post(data?: {
21
- path?: string;
22
- middlewares?: MiddlewareProvider[];
23
- }): MethodDecorator;
24
- declare function Put(data?: {
25
- path?: string;
26
- middlewares?: MiddlewareProvider[];
27
- }): MethodDecorator;
28
- declare function Patch(data?: {
29
- path?: string;
30
- middlewares?: MiddlewareProvider[];
31
- }): MethodDecorator;
32
- declare function Delete(data?: {
33
- path?: string;
34
- middlewares?: MiddlewareProvider[];
35
- }): MethodDecorator;
36
- declare function Options(data?: {
37
- path?: string;
38
- middlewares?: MiddlewareProvider[];
39
- }): MethodDecorator;
40
- declare function createParamDecorator(func: () => Promise<any> | (() => any)): ParameterDecorator;
41
-
42
- export { BaseController, Controller, Delete, Get, Options, Patch, Post, Put, createParamDecorator };
@@ -1,154 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var __async = (__this, __arguments, generator) => {
20
- return new Promise((resolve, reject) => {
21
- var fulfilled = (value) => {
22
- try {
23
- step(generator.next(value));
24
- } catch (e) {
25
- reject(e);
26
- }
27
- };
28
- var rejected = (value) => {
29
- try {
30
- step(generator.throw(value));
31
- } catch (e) {
32
- reject(e);
33
- }
34
- };
35
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
36
- step((generator = generator.apply(__this, __arguments)).next());
37
- });
38
- };
39
- var Controller_exports = {};
40
- __export(Controller_exports, {
41
- BaseController: () => BaseController,
42
- Controller: () => Controller,
43
- Delete: () => Delete,
44
- Get: () => Get,
45
- Options: () => Options,
46
- Patch: () => Patch,
47
- Post: () => Post,
48
- Put: () => Put,
49
- createParamDecorator: () => createParamDecorator
50
- });
51
- module.exports = __toCommonJS(Controller_exports);
52
- class BaseController {
53
- static getInstance() {
54
- return new this();
55
- }
56
- }
57
- BaseController.basePath = "";
58
- function Controller(path, options = {}) {
59
- return function(target) {
60
- target.routes = target.routes || [];
61
- target.basePath = path;
62
- target.baseMiddlewares = options.middlewares || [];
63
- };
64
- }
65
- function createHttpDecorator(data) {
66
- return function(target, propertyKey, descriptor) {
67
- const ctor = target.constructor;
68
- if (!ctor.routes) ctor.routes = [];
69
- ctor.routes.push({
70
- methods: data.methods,
71
- path: data.path,
72
- handler: propertyKey,
73
- middlewares: data.middlewares || []
74
- });
75
- const originalMethod = descriptor.value;
76
- const paramKeys = Reflect.ownKeys(target.constructor);
77
- const methodName = propertyKey.toString();
78
- descriptor.value = function(...args) {
79
- return __async(this, null, function* () {
80
- const paramCustomKeys = paramKeys.filter(
81
- (key) => typeof key === "string" && key.startsWith(`${methodName}:`) && key.endsWith(":custom")
82
- );
83
- for (const paramKey of paramCustomKeys) {
84
- const paramIndex = parseInt(paramKey.split(":")[1]);
85
- let method = Reflect.get(target.constructor, paramKey.toString());
86
- if (typeof paramIndex === "number" && typeof method === "function") {
87
- args[paramIndex] = yield method();
88
- }
89
- }
90
- return originalMethod.apply(this, args);
91
- });
92
- };
93
- };
94
- }
95
- function Get(data = {}) {
96
- return createHttpDecorator({
97
- methods: ["GET", "HEAD"],
98
- path: data.path || "/",
99
- middlewares: data.middlewares || []
100
- });
101
- }
102
- function Post(data = {}) {
103
- return createHttpDecorator({
104
- methods: ["POST"],
105
- path: data.path || "/",
106
- middlewares: data.middlewares || []
107
- });
108
- }
109
- function Put(data = {}) {
110
- return createHttpDecorator({
111
- methods: ["PUT"],
112
- path: data.path || "/",
113
- middlewares: data.middlewares || []
114
- });
115
- }
116
- function Patch(data = {}) {
117
- return createHttpDecorator({
118
- methods: ["PATCH"],
119
- path: data.path || "/",
120
- middlewares: data.middlewares || []
121
- });
122
- }
123
- function Delete(data = {}) {
124
- return createHttpDecorator({
125
- methods: ["DELETE"],
126
- path: data.path || "/",
127
- middlewares: data.middlewares || []
128
- });
129
- }
130
- function Options(data = {}) {
131
- return createHttpDecorator({
132
- methods: ["OPTIONS"],
133
- path: data.path || "/",
134
- middlewares: data.middlewares || []
135
- });
136
- }
137
- function createParamDecorator(func) {
138
- return function MyParamDecorator(target, propertyKey, parameterIndex) {
139
- Reflect.set(target.constructor, `${propertyKey == null ? void 0 : propertyKey.toString()}:${parameterIndex}:custom`, func);
140
- };
141
- }
142
- // Annotate the CommonJS export names for ESM import in node:
143
- 0 && (module.exports = {
144
- BaseController,
145
- Controller,
146
- Delete,
147
- Get,
148
- Options,
149
- Patch,
150
- Post,
151
- Put,
152
- createParamDecorator
153
- });
154
- //# sourceMappingURL=Controller.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/Controller.ts"],"sourcesContent":["import { ControllerDecoratorOptions, HttpMethod, MiddlewareProvider } from './types';\nimport { Middleware } from './Middleware';\n\nexport class BaseController {\n declare static routes: {\n methods: HttpMethod[];\n path: string;\n handler: string;\n middlewares: MiddlewareProvider[];\n }[];\n static basePath: string = '';\n static baseMiddlewares: MiddlewareProvider[];\n\n static getInstance() {\n return new this();\n }\n}\n\nexport function Controller(path: string, options: ControllerDecoratorOptions = {}): ClassDecorator {\n return function (target: any) {\n (target as any).routes = (target as any).routes || [];\n (target as any).basePath = path;\n (target as any).baseMiddlewares = options.middlewares || [];\n };\n}\n\nfunction createHttpDecorator(data: {\n methods: HttpMethod[];\n path: string;\n middlewares: MiddlewareProvider[];\n}): MethodDecorator {\n return function (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {\n const ctor = target.constructor;\n if (!ctor.routes) ctor.routes = [];\n ctor.routes.push({\n methods: data.methods,\n path: data.path,\n handler: propertyKey,\n middlewares: data.middlewares || [],\n });\n\n const originalMethod = descriptor.value!;\n const paramKeys = Reflect.ownKeys(target.constructor);\n const methodName = propertyKey.toString();\n\n descriptor.value = async function (...args: any[]) {\n const paramCustomKeys = paramKeys.filter(\n (key) =>\n typeof key === 'string' && key.startsWith(`${methodName}:`) && key.endsWith(':custom')\n );\n for (const paramKey of paramCustomKeys) {\n const paramIndex = parseInt((paramKey as string).split(':')[1]);\n let method = Reflect.get(target.constructor, paramKey.toString());\n if (typeof paramIndex === 'number' && typeof method === 'function') {\n args[paramIndex] = await method();\n }\n }\n\n return originalMethod.apply(this, args);\n };\n };\n}\n\nexport function Get(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['GET', 'HEAD'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function Post(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['POST'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function Put(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['PUT'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function Patch(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['PATCH'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function Delete(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['DELETE'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function Options(\n data: { path?: string; middlewares?: MiddlewareProvider[] } = {}\n): MethodDecorator {\n return createHttpDecorator({\n methods: ['OPTIONS'],\n path: data.path || '/',\n middlewares: data.middlewares || [],\n });\n}\n\nexport function createParamDecorator(func: () => Promise<any> | (() => any)): ParameterDecorator {\n return function MyParamDecorator(\n target: Object,\n propertyKey: string | symbol | undefined,\n parameterIndex: number\n ) {\n Reflect.set(target.constructor, `${propertyKey?.toString()}:${parameterIndex}:custom`, func);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,MAAM,eAAe;AAAA,EAU1B,OAAO,cAAc;AACnB,WAAO,IAAI,KAAK;AAAA,EAClB;AACF;AAba,eAOJ,WAAmB;AAQrB,SAAS,WAAW,MAAc,UAAsC,CAAC,GAAmB;AACjG,SA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AA8D,CAAC,GAC9C;AACjB,SAAO,oBAAoB;AAAA,IACzB,SAAS,CAAC,MAAM;AAAA,IAChB,MAAM,KAAK,QAAQ;AAAA,IACnB,aAAa,KAAK,eAAe,CAAC;AAAA,EACpC,CAAC;AACH;AAEO,SAAS,IACd,OAA8D,CAAC,GAC9C;AACjB,SAAO,oBAAoB;AAAA,IACzB,SAAS,CAAC,KAAK;AAAA,IACf,MAAM,KAAK,QAAQ;AAAA,IACnB,aAAa,KAAK,eAAe,CAAC;AAAA,EACpC,CAAC;AACH;AAEO,SAAS,MACd,OAA8D,CAAC,GAC9C;AACjB,SAAO,oBAAoB;AAAA,IACzB,SAAS,CAAC,OAAO;AAAA,IACjB,MAAM,KAAK,QAAQ;AAAA,IACnB,aAAa,KAAK,eAAe,CAAC;AAAA,EACpC,CAAC;AACH;AAEO,SAAS,OACd,OAA8D,CAAC,GAC9C;AACjB,SAAO,oBAAoB;AAAA,IACzB,SAAS,CAAC,QAAQ;AAAA,IAClB,MAAM,KAAK,QAAQ;AAAA,IACnB,aAAa,KAAK,eAAe,CAAC;AAAA,EACpC,CAAC;AACH;AAEO,SAAS,QACd,OAA8D,CAAC,GAC9C;AACjB,SAAO,oBAAoB;AAAA,IACzB,SAAS,CAAC,SAAS;AAAA,IACnB,MAAM,KAAK,QAAQ;AAAA,IACnB,aAAa,KAAK,eAAe,CAAC;AAAA,EACpC,CAAC;AACH;AAEO,SAAS,qBAAqB,MAA4D;AAC/F,SAAO,SAAS,iBACd,QACA,aACA,gBACA;AACA,YAAQ,IAAI,OAAO,aAAa,GAAG,2CAAa,UAAU,IAAI,cAAc,WAAW,IAAI;AAAA,EAC7F;AACF;","names":[]}
@@ -1,30 +0,0 @@
1
- import { IncomingMessage, ServerResponse } from 'http';
2
-
3
- type Request = IncomingMessage & {
4
- params: any;
5
- method: HttpMethod;
6
- headers?: Record<string, string>;
7
- body?: any;
8
- raw_body?: any;
9
- files?: any;
10
- query?: Record<string, string>;
11
- };
12
- type Response = ServerResponse;
13
- type LexerToken = {
14
- type: string;
15
- value: string;
16
- };
17
- type HandlerType = (req: Request, res: Response, next?: (() => any) | undefined) => Promise<any> | any;
18
- type ControllerDecoratorOptions = {
19
- middlewares?: MiddlewareProvider[];
20
- };
21
- type MiddlewareProvider = typeof Middleware | Middleware | ((request: Request, response: Response, next: () => Promise<void>) => Promise<void>);
22
- type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
23
-
24
- declare abstract class Middleware {
25
- protected constructor(params?: any);
26
- static getInstance(params: any): Middleware;
27
- abstract call(req: Request, res: Response, next: () => Promise<void>): Promise<void>;
28
- }
29
-
30
- export { type ControllerDecoratorOptions as C, type HandlerType as H, type LexerToken as L, type MiddlewareProvider as M, type Request as R, type Response as a, type HttpMethod as b, Middleware as c };
@@ -1,2 +0,0 @@
1
- export { c as Middleware } from './Middleware-C8dxjlFD.js';
2
- import 'http';
@@ -1,35 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var Middleware_exports = {};
20
- __export(Middleware_exports, {
21
- Middleware: () => Middleware
22
- });
23
- module.exports = __toCommonJS(Middleware_exports);
24
- class Middleware {
25
- constructor(params = {}) {
26
- }
27
- static getInstance(params) {
28
- throw new Error("Method not implemented. Please implement a static getInstance method.");
29
- }
30
- }
31
- // Annotate the CommonJS export names for ESM import in node:
32
- 0 && (module.exports = {
33
- Middleware
34
- });
35
- //# sourceMappingURL=Middleware.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/Middleware.ts"],"sourcesContent":["import { LexerToken, Request, Response } from './types';\n\nexport abstract class Middleware {\n protected constructor(params: any = {}) {}\n static getInstance(params: any): Middleware {\n throw new Error('Method not implemented. Please implement a static getInstance method.');\n }\n\n abstract call(req: Request, res: Response, next: () => Promise<void>): Promise<void>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,MAAe,WAAW;AAAA,EACrB,YAAY,SAAc,CAAC,GAAG;AAAA,EAAC;AAAA,EACzC,OAAO,YAAY,QAAyB;AAC1C,UAAM,IAAI,MAAM,uEAAuE;AAAA,EACzF;AAGF;","names":[]}
@@ -1,8 +0,0 @@
1
- import { H as HandlerType, c as Middleware } from './Middleware-C8dxjlFD.js';
2
- import 'http';
3
-
4
- declare class MiddlewareFactory {
5
- static create(func: HandlerType): Middleware;
6
- }
7
-
8
- export { MiddlewareFactory };
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var MiddlewareFactory_exports = {};
20
- __export(MiddlewareFactory_exports, {
21
- MiddlewareFactory: () => MiddlewareFactory
22
- });
23
- module.exports = __toCommonJS(MiddlewareFactory_exports);
24
- var import_Middleware = require("./Middleware");
25
- class MiddlewareFactory {
26
- static create(func) {
27
- const cls = class extends import_Middleware.Middleware {
28
- call(req, res, next) {
29
- return func(req, res, next);
30
- }
31
- constructor(params = {}) {
32
- super(params);
33
- }
34
- };
35
- return new cls();
36
- }
37
- }
38
- // Annotate the CommonJS export names for ESM import in node:
39
- 0 && (module.exports = {
40
- MiddlewareFactory
41
- });
42
- //# sourceMappingURL=MiddlewareFactory.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/MiddlewareFactory.ts"],"sourcesContent":["import { Middleware } from './Middleware';\nimport { HandlerType, LexerToken, Request, Response } from './types';\n\nexport class MiddlewareFactory {\n public static create(func: HandlerType): Middleware {\n const cls = class extends Middleware {\n call(req: Request, res: Response, next: () => Promise<void>): Promise<void> {\n return func(req, res, next);\n }\n constructor(params: any = {}) {\n super(params);\n }\n };\n\n return new cls();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAA2B;AAGpB,MAAM,kBAAkB;AAAA,EAC7B,OAAc,OAAO,MAA+B;AAClD,UAAM,MAAM,cAAc,6BAAW;AAAA,MACnC,KAAK,KAAc,KAAe,MAA0C;AAC1E,eAAO,KAAK,KAAK,KAAK,IAAI;AAAA,MAC5B;AAAA,MACA,YAAY,SAAc,CAAC,GAAG;AAC5B,cAAM,MAAM;AAAA,MACd;AAAA,IACF;AAEA,WAAO,IAAI,IAAI;AAAA,EACjB;AACF;","names":[]}
package/dist/Route.d.ts DELETED
@@ -1,40 +0,0 @@
1
- import { b as HttpMethod, H as HandlerType, L as LexerToken, R as Request, M as MiddlewareProvider, a as Response } from './Middleware-C8dxjlFD.js';
2
- import 'http';
3
-
4
- declare class Route {
5
- methods: HttpMethod[];
6
- path: string;
7
- handler: HandlerType;
8
- private middlewares;
9
- private urlRegex;
10
- constructor(methods: HttpMethod[], path: string, handler: HandlerType);
11
- pathToRegex(path: string): RegExp;
12
- lexUrlPath(path: string): {
13
- type: string;
14
- value: string;
15
- }[];
16
- tokensToRegex(tokens: LexerToken[]): RegExp;
17
- /**
18
- * to evaludate if request is a match for this route
19
- * @param request http request
20
- * @returns return true if route is a match for this request
21
- */
22
- test(request: Request): boolean;
23
- testPath(pathname: string): boolean;
24
- /**
25
- * returns details of the match, otherwise it returns false
26
- * @param request the request to match
27
- * @returns object cotaining details of match including matched params
28
- */
29
- match(request: Request): false | {
30
- url: URL;
31
- params: {
32
- [key: string]: string;
33
- };
34
- };
35
- addMiddleware(middlewares: MiddlewareProvider | MiddlewareProvider[]): this;
36
- getMiddlewares(): MiddlewareProvider[];
37
- callHanlder(request: Request, response: Response): any;
38
- }
39
-
40
- export { Route };