tspace-spear 1.0.0-rc

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,202 @@
1
+ /// <reference types="node" />
2
+ import { Server, ServerResponse } from 'http';
3
+ import { Router, TContext, TNextFunction, TApplication } from '../..';
4
+ /**
5
+ *
6
+ * The 'Spear' class is used to create a server and handle HTTP requests.
7
+ *
8
+ * @returns {Spear} application
9
+ * @example
10
+ * new Application()
11
+ * .get('/' , () => 'Hello world!')
12
+ * .get('/json' , () => {
13
+ * return {
14
+ * message : 'Hello world!'
15
+ * }
16
+ * })
17
+ * .listen(3000 , () => console.log('server listening on port : 3000'))
18
+ *
19
+ */
20
+ declare class Spear {
21
+ private readonly _controllers?;
22
+ private readonly _middlewares?;
23
+ private readonly _globalPrefix;
24
+ private readonly _router;
25
+ private _errorHandler;
26
+ private _globalMiddlewares;
27
+ private _formatResponse;
28
+ private _onListeners;
29
+ private _fileUploadOptions;
30
+ constructor({ controllers, middlewares, globalPrefix, logger }?: TApplication);
31
+ /**
32
+ * The 'enableCors' is used to enable the cors origins on the server.
33
+ *
34
+ * @params {Object}
35
+ * @property {(string | RegExp)[]} origins
36
+ * @property {boolean} credentials
37
+ * @returns
38
+ */
39
+ enableCors({ origins, credentials }?: {
40
+ origins?: (string | RegExp)[];
41
+ credentials?: boolean;
42
+ }): this;
43
+ /**
44
+ * The 'use' method is used to add the middleware into the request pipeline.
45
+ *
46
+ * @callback {Function} middleware
47
+ * @property {Object} ctx - context { req , res , query , params , cookies , files , body}
48
+ * @property {Function} next - go to next function
49
+ * @returns {this}
50
+ */
51
+ use(middleware: (ctx: TContext, next: TNextFunction) => void): this;
52
+ /**
53
+ * The 'useRouter' method is used to add the router in the request context.
54
+ *
55
+ * @parms {Function} router
56
+ * @property {Function} router - get() , post() , put() , patch() , delete()
57
+ * @returns {this}
58
+ */
59
+ useRouter(router: Router): this;
60
+ /**
61
+ * The 'useBodyParser' method is a middleware used to parse the request body of incoming HTTP requests.
62
+ *
63
+ * @returns {this}
64
+ */
65
+ useBodyParser(): this;
66
+ /**
67
+ * The 'useCookiesParser' method is a middleware used to parses cookies attached to the client request object.
68
+ *
69
+ * @returns {this}
70
+ */
71
+ useCookiesParser(): this;
72
+ /**
73
+ * The 'useFileUpload' method is a middleware used to handler file uploads. It adds a file upload of incoming HTTP requests.
74
+ *
75
+ * @param {?Object}
76
+ * @property {?number} limits
77
+ * @property {?boolean} useTempFiles
78
+ * @property {?string} tempFileDir
79
+ * @property {?Object} removeTempFile
80
+ * @property {boolean} removeTempFile.remove
81
+ * @property {number} removeTempFile.ms
82
+ * @returns
83
+ */
84
+ useFileUpload({ limits, useTempFiles, tempFileDir, removeTempFile }?: {
85
+ limits?: number;
86
+ useTempFiles?: boolean;
87
+ tempFileDir?: string;
88
+ removeTempFile?: {
89
+ remove: boolean;
90
+ ms: number;
91
+ };
92
+ }): this;
93
+ /**
94
+ * The 'formatResponse' method is used to format the response
95
+ *
96
+ * @param {function} format
97
+ * @returns
98
+ */
99
+ formatResponse(format: (r: unknown, statusCode: number) => any): this;
100
+ /**
101
+ * The 'errorHandler' method is middleware that is specifically designed to handle errors that occur during the processing of requests
102
+ *
103
+ * @param {function} error
104
+ * @returns
105
+ */
106
+ errorHandler(error: (err: Error, ctx: TContext) => any): this;
107
+ /**
108
+ * The 'notFoundHandler' method is middleware that is specifically designed to handle errors notfound that occur during the processing of requests
109
+ *
110
+ * @param {function} notfound
111
+ * @returns
112
+ */
113
+ notFoundHandler(notfound: (ctx: TContext) => any): this;
114
+ /**
115
+ * The 'get' method is used to add the request handler to the router for the 'GET' method.
116
+ *
117
+ * @param {string} path
118
+ * @callback {...Function[]} handlers of the middlewares
119
+ * @property {Object} ctx - context { req , res , query , params , cookies , files , body}
120
+ * @property {Function} next - go to next function
121
+ * @returns {this}
122
+ */
123
+ get(path: string, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
124
+ /**
125
+ * The 'post' method is used to add the request handler to the router for the 'POST' method.
126
+ *
127
+ * @param {string} path
128
+ * @callback {...Function[]} handlers of the middlewares
129
+ * @property {Object} ctx - context { req , res , query , params , cookies , files , body}
130
+ * @property {Function} next - go to next function
131
+ * @returns {this}
132
+ */
133
+ post(path: string, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
134
+ /**
135
+ * The 'put' method is used to add the request handler to the router for the 'PUT' method.
136
+ *
137
+ * @param {string} path
138
+ * @callback {...Function[]} handlers of the middlewares
139
+ * @property {Object} ctx - context { req , res , query , params , cookies , files , body}
140
+ * @property {Function} next - go to next function
141
+ * @returns {this}
142
+ */
143
+ put(path: string, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
144
+ /**
145
+ * The 'patch' method is used to add the request handler to the router for the 'PATCH' method.
146
+ *
147
+ * @param {string} path
148
+ * @callback {...Function[]} handlers of the middlewares
149
+ * @property {Object} ctx - context { req , res , query , params , cookies , files , body}
150
+ * @property {Function} next - go to next function
151
+ * @returns {this}
152
+ */
153
+ patch(path: string, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
154
+ /**
155
+ * The 'delete' method is used to add the request handler to the router for the 'DELETE' method.
156
+ *
157
+ * @param {string} path
158
+ * @callback {...Function[]} handlers of the middlewares
159
+ * @property {Object} ctx - context { req , res , query , params , cookies , files , body}
160
+ * @property {Function} next - go to next function
161
+ * @returns {this}
162
+ */
163
+ delete(path: string, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
164
+ /**
165
+ * The 'all' method is used to add the request handler to the router for the 'all' method.
166
+ *
167
+ * @param {string} path
168
+ * @callback {...Function[]} handlers of the middlewares
169
+ * @property {object} ctx - context { req , res , query , params , cookies , files , body}
170
+ * @property {function} next - go to next function
171
+ * @returns {this}
172
+ */
173
+ all(path: string, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
174
+ /**
175
+ * The 'listen' method is used to bind and start a server to a particular port and optionally a hostname.
176
+ *
177
+ * @param {number} port
178
+ * @param {function} cb
179
+ * @returns
180
+ */
181
+ listen(port: number | (() => ServerResponse) | undefined, cb: (callback: {
182
+ server: Server;
183
+ port: number;
184
+ }) => void): Promise<void>;
185
+ private _logger;
186
+ private _import;
187
+ private _registerControllers;
188
+ private _registerMiddlewares;
189
+ private _filesParser;
190
+ private _bodyParser;
191
+ private _cookiesParser;
192
+ private _customizeResponse;
193
+ private _nextFunction;
194
+ private _wrapHandlers;
195
+ private _wrapResponse;
196
+ private _createServer;
197
+ private _normalizePath;
198
+ }
199
+ export { Spear };
200
+ export declare class Application extends Spear {
201
+ }
202
+ export default Spear;