tspace-spear 1.2.1 → 1.2.3

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