tspace-spear 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +268 -19
- package/dist/lib/core/const/index.d.ts +153 -0
- package/dist/lib/core/const/index.js +105 -0
- package/dist/lib/core/const/index.js.map +1 -0
- package/dist/lib/core/decorators/context.d.ts +16 -9
- package/dist/lib/core/decorators/context.js +85 -59
- package/dist/lib/core/decorators/context.js.map +1 -1
- package/dist/lib/core/decorators/headers.d.ts +2 -2
- package/dist/lib/core/decorators/headers.js +1 -1
- package/dist/lib/core/decorators/headers.js.map +1 -1
- package/dist/lib/core/decorators/methods.d.ts +7 -7
- package/dist/lib/core/decorators/methods.js.map +1 -1
- package/dist/lib/core/decorators/middleware.d.ts +3 -3
- package/dist/lib/core/decorators/middleware.js +2 -2
- package/dist/lib/core/decorators/middleware.js.map +1 -1
- package/dist/lib/core/decorators/statusCode.d.ts +1 -1
- package/dist/lib/core/decorators/statusCode.js.map +1 -1
- package/dist/lib/core/decorators/swagger.d.ts +1 -1
- package/dist/lib/core/decorators/swagger.js.map +1 -1
- package/dist/lib/core/server/express.d.ts +295 -0
- package/dist/lib/core/server/express.js +1356 -0
- package/dist/lib/core/server/express.js.map +1 -0
- package/dist/lib/core/server/fast-router.d.ts +133 -0
- package/dist/lib/core/server/fast-router.js +277 -0
- package/dist/lib/core/server/fast-router.js.map +1 -0
- package/dist/lib/core/server/index.d.ts +34 -37
- package/dist/lib/core/server/index.js +192 -501
- package/dist/lib/core/server/index.js.map +1 -1
- package/dist/lib/core/server/net/index.d.ts +20 -0
- package/dist/lib/core/server/net/index.js +393 -0
- package/dist/lib/core/server/net/index.js.map +1 -0
- package/dist/lib/core/server/parser-factory.d.ts +10 -11
- package/dist/lib/core/server/parser-factory.js +259 -437
- package/dist/lib/core/server/parser-factory.js.map +1 -1
- package/dist/lib/core/server/request.d.ts +2 -0
- package/dist/lib/core/server/request.js +7 -0
- package/dist/lib/core/server/request.js.map +1 -0
- package/dist/lib/core/server/response.d.ts +6 -0
- package/dist/lib/core/server/response.js +168 -0
- package/dist/lib/core/server/response.js.map +1 -0
- package/dist/lib/core/server/router.d.ts +2 -12
- package/dist/lib/core/server/router.js +2 -13
- package/dist/lib/core/server/router.js.map +1 -1
- package/dist/lib/core/server/uWS/index.d.ts +30 -0
- package/dist/lib/core/server/uWS/index.js +357 -0
- package/dist/lib/core/server/uWS/index.js.map +1 -0
- package/dist/lib/core/types/index.d.ts +144 -43
- package/dist/lib/core/utils/index.d.ts +12 -0
- package/dist/lib/core/utils/index.js +137 -0
- package/dist/lib/core/utils/index.js.map +1 -0
- package/dist/lib/index.d.ts +3 -3
- package/dist/lib/index.js +3 -2
- package/dist/lib/index.js.map +1 -1
- package/package.json +19 -14
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { Server } from 'http';
|
|
2
|
+
import WebSocket from 'ws';
|
|
3
|
+
import { FastRouter } from './fast-router';
|
|
4
|
+
import { Router } from './router';
|
|
5
|
+
import type { T } from '../types';
|
|
6
|
+
type ExpressHandler = (req: T.Request, res: T.Response, next: T.NextFunction) => any;
|
|
7
|
+
type Handler<Opt> = Opt extends {
|
|
8
|
+
express: true;
|
|
9
|
+
} ? ExpressHandler : T.ContextHandler;
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* The 'Spear' class is used to create a server and handle HTTP requests.
|
|
13
|
+
*
|
|
14
|
+
* @returns {Spear} application
|
|
15
|
+
* @example
|
|
16
|
+
* new Spear()
|
|
17
|
+
* .get('/' , () => 'Hello world!')
|
|
18
|
+
* .get('/json' , () => {
|
|
19
|
+
* return {
|
|
20
|
+
* message : 'Hello world!'
|
|
21
|
+
* }
|
|
22
|
+
* })
|
|
23
|
+
* .listen(3000 , () => console.log('server listening on port : 3000'))
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
declare class Express<Opt extends T.Application = {}> {
|
|
27
|
+
private readonly _controllers?;
|
|
28
|
+
private readonly _middlewares?;
|
|
29
|
+
private readonly _globalPrefix;
|
|
30
|
+
private readonly _router;
|
|
31
|
+
private readonly _parser;
|
|
32
|
+
private _adapter;
|
|
33
|
+
private _cluster?;
|
|
34
|
+
private _cors?;
|
|
35
|
+
private _swagger;
|
|
36
|
+
private _swaggerSpecs;
|
|
37
|
+
private _ws;
|
|
38
|
+
private _errorHandler;
|
|
39
|
+
private _globalMiddlewares;
|
|
40
|
+
private _formatResponse;
|
|
41
|
+
private _onListeners;
|
|
42
|
+
private _fileUploadOptions;
|
|
43
|
+
private _express;
|
|
44
|
+
constructor({ controllers, middlewares, globalPrefix, logger, cluster, adapter, express }: Opt);
|
|
45
|
+
/**
|
|
46
|
+
* The get 'instance' method is used to get the instance of Spear.
|
|
47
|
+
*
|
|
48
|
+
* @returns {this}
|
|
49
|
+
*/
|
|
50
|
+
get instance(): this;
|
|
51
|
+
/**
|
|
52
|
+
* The get 'routers' method is used get the all routers.
|
|
53
|
+
*
|
|
54
|
+
* @returns {FastRouter}
|
|
55
|
+
*/
|
|
56
|
+
get routers(): FastRouter;
|
|
57
|
+
/**
|
|
58
|
+
* The 'ws' method is used to creates the WebSocket server.
|
|
59
|
+
*
|
|
60
|
+
* @callback {Function} WebSocketServer
|
|
61
|
+
* @param {WebSocketServer} wss - WebSocketServer
|
|
62
|
+
* @returns {this}
|
|
63
|
+
*/
|
|
64
|
+
ws(handlers: () => T.WebSocketHandler, options?: WebSocket.ServerOptions): this;
|
|
65
|
+
/**
|
|
66
|
+
* The 'use' method is used to add the middleware into the request pipeline.
|
|
67
|
+
*
|
|
68
|
+
* @callback {Function} middleware
|
|
69
|
+
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
|
|
70
|
+
* @property {Function} next - go to next function
|
|
71
|
+
* @returns {this}
|
|
72
|
+
*/
|
|
73
|
+
use(middleware: Handler<Opt>): this;
|
|
74
|
+
/**
|
|
75
|
+
* The 'useAdater' method is used to switch between different server implementations,
|
|
76
|
+
* such as the native Node.js HTTP server or uWebSockets.js (uWS).
|
|
77
|
+
*
|
|
78
|
+
* @param {T.Adapter} adapter - The adapter instance (e.g., HTTP or uWS).
|
|
79
|
+
* @returns {this} Returns the current instance for chaining
|
|
80
|
+
*/
|
|
81
|
+
useAdater(adapter: T.Adapter): this;
|
|
82
|
+
/**
|
|
83
|
+
* The 'useCluster' method is used cluster run the server
|
|
84
|
+
*
|
|
85
|
+
* @param {boolean | number} cluster
|
|
86
|
+
* @returns {this}
|
|
87
|
+
*/
|
|
88
|
+
useCluster(cluster?: number | boolean): this;
|
|
89
|
+
/**
|
|
90
|
+
* The 'useLogger' method is used to add the middleware view logger response.
|
|
91
|
+
*
|
|
92
|
+
* @callback {Function} middleware
|
|
93
|
+
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
|
|
94
|
+
* @property {Function} next - go to next function
|
|
95
|
+
* @returns {this}
|
|
96
|
+
*/
|
|
97
|
+
useLogger({ methods, exceptPath }?: {
|
|
98
|
+
methods?: T.MethodInput[];
|
|
99
|
+
exceptPath?: string[] | RegExp;
|
|
100
|
+
}): this;
|
|
101
|
+
/**
|
|
102
|
+
* The 'useBodyParser' method is a middleware used to parse the request body of incoming HTTP requests.
|
|
103
|
+
* @param {object?}
|
|
104
|
+
* @property {array?} except the body parser with some methods
|
|
105
|
+
* @returns {this}
|
|
106
|
+
*/
|
|
107
|
+
useBodyParser({ except }?: {
|
|
108
|
+
except?: T.MethodInput[];
|
|
109
|
+
}): this;
|
|
110
|
+
/**
|
|
111
|
+
* The 'useFileUpload' method is a middleware used to handler file uploads. It adds a file upload of incoming HTTP requests.
|
|
112
|
+
*
|
|
113
|
+
* @param {?Object}
|
|
114
|
+
* @property {?number} limits // bytes. default Infinity
|
|
115
|
+
* @property {?string} tempFileDir
|
|
116
|
+
* @property {?Object} removeTempFile
|
|
117
|
+
* @property {boolean} removeTempFile.remove
|
|
118
|
+
* @property {number} removeTempFile.ms
|
|
119
|
+
* @returns
|
|
120
|
+
*/
|
|
121
|
+
useFileUpload({ limit, tempFileDir, removeTempFile }?: {
|
|
122
|
+
limit?: number;
|
|
123
|
+
tempFileDir?: string;
|
|
124
|
+
removeTempFile?: {
|
|
125
|
+
remove: boolean;
|
|
126
|
+
ms: number;
|
|
127
|
+
};
|
|
128
|
+
}): this;
|
|
129
|
+
/**
|
|
130
|
+
* The 'useCookiesParser' method is a middleware used to parses cookies attached to the client request object.
|
|
131
|
+
*
|
|
132
|
+
* @returns {this}
|
|
133
|
+
*/
|
|
134
|
+
useCookiesParser(): this;
|
|
135
|
+
/**
|
|
136
|
+
* The 'useRouter' method is used to add the router in the request context.
|
|
137
|
+
*
|
|
138
|
+
* @parms {Function} router
|
|
139
|
+
* @property {Function} router - get() , post() , put() , patch() , delete()
|
|
140
|
+
* @returns {this}
|
|
141
|
+
*/
|
|
142
|
+
useRouter(router: Router): this;
|
|
143
|
+
/**
|
|
144
|
+
* The 'useSwagger' method is a middleware used to create swagger api.
|
|
145
|
+
*
|
|
146
|
+
* @param {?Object} doc
|
|
147
|
+
* @returns
|
|
148
|
+
*/
|
|
149
|
+
useSwagger(doc?: T.Swagger.Doc): this;
|
|
150
|
+
/**
|
|
151
|
+
* The 'listen' method is used to bind and start a server to a particular port and optionally a hostname.
|
|
152
|
+
*
|
|
153
|
+
* @param {number} port
|
|
154
|
+
* @param {function} callback
|
|
155
|
+
* @returns
|
|
156
|
+
*/
|
|
157
|
+
listen(port: number, hostname?: string | ((callback: {
|
|
158
|
+
server: Server;
|
|
159
|
+
port: number;
|
|
160
|
+
}) => void), callback?: (callback: {
|
|
161
|
+
server: Server;
|
|
162
|
+
port: number;
|
|
163
|
+
}) => void): Promise<Server>;
|
|
164
|
+
/**
|
|
165
|
+
* The 'cors' is used to enable the cors origins on the server.
|
|
166
|
+
*
|
|
167
|
+
* @params {Object}
|
|
168
|
+
* @property {(string | RegExp)[]} origins
|
|
169
|
+
* @property {boolean} credentials
|
|
170
|
+
* @returns
|
|
171
|
+
*/
|
|
172
|
+
cors({ origins, credentials }?: {
|
|
173
|
+
origins?: (string | RegExp)[];
|
|
174
|
+
credentials?: boolean;
|
|
175
|
+
}): this;
|
|
176
|
+
/**
|
|
177
|
+
* The 'response' method is used to format the response
|
|
178
|
+
*
|
|
179
|
+
* @param {function} format
|
|
180
|
+
* @returns
|
|
181
|
+
*/
|
|
182
|
+
response(format: (r: unknown, statusCode: number) => any): this;
|
|
183
|
+
/**
|
|
184
|
+
* The 'catch' method is middleware that is specifically designed to handle errors.
|
|
185
|
+
*
|
|
186
|
+
* that occur during the processing of requests
|
|
187
|
+
*
|
|
188
|
+
* @param {function} error
|
|
189
|
+
* @returns
|
|
190
|
+
*/
|
|
191
|
+
catch(error: (err: any, ctx: T.Context) => any): this;
|
|
192
|
+
/**
|
|
193
|
+
* The 'notfound' method is middleware that is specifically designed to handle errors notfound that occur during the processing of requests
|
|
194
|
+
*
|
|
195
|
+
* @param {function} fn
|
|
196
|
+
* @returns
|
|
197
|
+
*/
|
|
198
|
+
notfound(fn: (ctx: T.Context) => any): this;
|
|
199
|
+
/**
|
|
200
|
+
* The 'get' method is used to add the request handler to the router for the 'GET' method.
|
|
201
|
+
*
|
|
202
|
+
* @param {string} path
|
|
203
|
+
* @callback {...Function[]} handlers of the middlewares
|
|
204
|
+
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
|
|
205
|
+
* @property {Function} next - go to next function
|
|
206
|
+
* @returns {this}
|
|
207
|
+
*/
|
|
208
|
+
get(path: string, ...handlers: Handler<Opt>[]): this;
|
|
209
|
+
getx(path: string, ...handlers: Handler<Opt>[]): this;
|
|
210
|
+
/**
|
|
211
|
+
* The 'post' method is used to add the request handler to the router for the 'POST' method.
|
|
212
|
+
*
|
|
213
|
+
* @param {string} path
|
|
214
|
+
* @callback {...Function[]} handlers of the middlewares
|
|
215
|
+
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
|
|
216
|
+
* @property {Function} next - go to next function
|
|
217
|
+
* @returns {this}
|
|
218
|
+
*/
|
|
219
|
+
post(path: string, ...handlers: Handler<Opt>[]): this;
|
|
220
|
+
/**
|
|
221
|
+
* The 'put' method is used to add the request handler to the router for the 'PUT' method.
|
|
222
|
+
*
|
|
223
|
+
* @param {string} path
|
|
224
|
+
* @callback {...Function[]} handlers of the middlewares
|
|
225
|
+
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
|
|
226
|
+
* @property {Function} next - go to next function
|
|
227
|
+
* @returns {this}
|
|
228
|
+
*/
|
|
229
|
+
put(path: string, ...handlers: Handler<Opt>[]): this;
|
|
230
|
+
/**
|
|
231
|
+
* The 'patch' method is used to add the request handler to the router for the 'PATCH' method.
|
|
232
|
+
*
|
|
233
|
+
* @param {string} path
|
|
234
|
+
* @callback {...Function[]} handlers of the middlewares
|
|
235
|
+
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
|
|
236
|
+
* @property {Function} next - go to next function
|
|
237
|
+
* @returns {this}
|
|
238
|
+
*/
|
|
239
|
+
patch(path: string, ...handlers: Handler<Opt>[]): this;
|
|
240
|
+
/**
|
|
241
|
+
* The 'delete' method is used to add the request handler to the router for the 'DELETE' method.
|
|
242
|
+
*
|
|
243
|
+
* @param {string} path
|
|
244
|
+
* @callback {...Function[]} handlers of the middlewares
|
|
245
|
+
* @property {Object} ctx - context { req , res , query , params , cookies , files , body}
|
|
246
|
+
* @property {Function} next - go to next function
|
|
247
|
+
* @returns {this}
|
|
248
|
+
*/
|
|
249
|
+
delete(path: string, ...handlers: Handler<Opt>[]): this;
|
|
250
|
+
/**
|
|
251
|
+
* The 'head' method is used to add the request handler to the router for 'HEAD' methods.
|
|
252
|
+
*
|
|
253
|
+
* @param {string} path
|
|
254
|
+
* @callback {...Function[]} handlers of the middlewares
|
|
255
|
+
* @property {object} ctx - context { req , res , query , params , cookies , files , body}
|
|
256
|
+
* @property {function} next - go to next function
|
|
257
|
+
* @returns {this}
|
|
258
|
+
*/
|
|
259
|
+
head(path: string, ...handlers: Handler<Opt>[]): this;
|
|
260
|
+
/**
|
|
261
|
+
* The 'head' method is used to add the request handler to the router for 'HEAD' methods.
|
|
262
|
+
*
|
|
263
|
+
* @param {string} path
|
|
264
|
+
* @callback {...Function[]} handlers of the middlewares
|
|
265
|
+
* @property {object} ctx - context { req , res , query , params , cookies , files , body}
|
|
266
|
+
* @property {function} next - go to next function
|
|
267
|
+
* @returns {this}
|
|
268
|
+
*/
|
|
269
|
+
options(path: string, ...handlers: Handler<Opt>[]): this;
|
|
270
|
+
/**
|
|
271
|
+
* The 'all' method is used to add the request handler to the router for 'GET' 'POST' 'PUT' 'PATCH' 'DELETE' 'HEAD' 'OPTIONS' methods.
|
|
272
|
+
*
|
|
273
|
+
* @param {string} path
|
|
274
|
+
* @callback {...Function[]} handlers of the middlewares
|
|
275
|
+
* @property {object} ctx - context { req , res , query , params , cookies , files , body}
|
|
276
|
+
* @property {function} next - go to next function
|
|
277
|
+
* @returns {this}
|
|
278
|
+
*/
|
|
279
|
+
all(path: string, ...handlers: Handler<Opt>[]): this;
|
|
280
|
+
private _import;
|
|
281
|
+
private _registerControllers;
|
|
282
|
+
private _registerMiddlewares;
|
|
283
|
+
private _customizeResponse;
|
|
284
|
+
private _wrapHandlers;
|
|
285
|
+
private _wrapResponse;
|
|
286
|
+
private _nextFunction;
|
|
287
|
+
private _clusterMode;
|
|
288
|
+
private _createServer;
|
|
289
|
+
private _createContext;
|
|
290
|
+
private _uWSRequestResponse;
|
|
291
|
+
private _normalizePath;
|
|
292
|
+
private _swaggerHandler;
|
|
293
|
+
}
|
|
294
|
+
export { Express };
|
|
295
|
+
export default Express;
|