@athenna/http 4.24.0 → 4.25.0

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.
@@ -6,11 +6,474 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
- import { Request } from '#src/context/Request';
10
- import { Response } from '#src/context/Response';
9
+ import type { SendOptions } from '@fastify/static';
10
+ import type { FastifyRequest, FastifyReply } from 'fastify';
11
+ import type { FastifyHelmetOptions } from '@fastify/helmet';
12
+ export type Request = {
13
+ /**
14
+ * The fastify request object.
15
+ */
16
+ request: FastifyRequest;
17
+ /**
18
+ * Get the request id.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * console.log(request.id) // '12345'
23
+ * ```
24
+ */
25
+ get id(): string;
26
+ /**
27
+ * Get the request ip.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * console.log(request.ip) // '192.168.0.1'
32
+ * ```
33
+ */
34
+ get ip(): string;
35
+ /**
36
+ * Get the request hostname.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * console.log(request.hostname) // 'localhost'
41
+ * ```
42
+ */
43
+ get hostname(): string;
44
+ /**
45
+ * Get the server port.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * console.log(request.port) // 3000
50
+ * ```
51
+ */
52
+ get port(): number;
53
+ /**
54
+ * Get the http version.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * console.log(request.version) // 1
59
+ * ```
60
+ */
61
+ get version(): string;
62
+ /**
63
+ * Get the request protocol.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * console.log(request.protocol) // 'http'
68
+ * ```
69
+ */
70
+ get protocol(): 'http' | 'https';
71
+ /**
72
+ * Get the request method.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * console.log(request.method) // 'GET'
77
+ * ```
78
+ */
79
+ get method(): string;
80
+ /**
81
+ * Get the base url from request.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * console.log(request.baseUrl) // '/users/1'
86
+ * ```
87
+ */
88
+ get baseUrl(): string;
89
+ /**
90
+ * Get the base url with host and port info from request.
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * console.log(request.baseHostUrl) // 'http://localhost:3030/users/1'
95
+ * ```
96
+ */
97
+ get baseHostUrl(): string;
98
+ /**
99
+ * Get the route url from request.
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * console.log(request.routeUrl) // '/users/:id'
104
+ * ```
105
+ */
106
+ get routeUrl(): string;
107
+ /**
108
+ * Get the route url with host and port info from request.
109
+ *
110
+ * @example
111
+ * ```ts
112
+ * console.log(request.routeHostUrl) // 'http://localhost:3030/users/:id'
113
+ * ```
114
+ */
115
+ get routeHostUrl(): string;
116
+ /**
117
+ * Get the original url from request.
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * console.log(request.originalUrl) // '/users/1?query=true'
122
+ * ```
123
+ */
124
+ get originalUrl(): string;
125
+ /**
126
+ * Get the original url with host and port info from request.
127
+ *
128
+ * @example
129
+ * ```ts
130
+ * console.log(request.originalHostUrl) // 'http://localhost:3000/users/1?query=true'
131
+ * ```
132
+ */
133
+ get originalHostUrl(): string;
134
+ /**
135
+ * Get all body from request.
136
+ *
137
+ * @example
138
+ * ```ts
139
+ * const { name, email } = request.body
140
+ * ```
141
+ */
142
+ get body(): any | any[];
143
+ /**
144
+ * Get all params from request.
145
+ *
146
+ * @example
147
+ * ```ts
148
+ * const { id } = request.params
149
+ * ```
150
+ */
151
+ get params(): any;
152
+ /**
153
+ * Get all queries from request.
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * const { page, limit } = request.queries
158
+ * ```
159
+ */
160
+ get queries(): any;
161
+ /**
162
+ * Get all headers from request.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * const { accept } = request.headers
167
+ * ```
168
+ */
169
+ get headers(): any;
170
+ /**
171
+ * Get a value from the request params or return
172
+ * the default value.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * const id = request.param('id', '1')
177
+ * ```
178
+ */
179
+ param(param: string, defaultValue?: any): any;
180
+ /**
181
+ * Get a value from the request query param or return
182
+ * the default value.
183
+ *
184
+ * @example
185
+ * ```ts
186
+ * const page = request.query('page', '1')
187
+ * ```
188
+ */
189
+ query(query: string, defaultValue?: any): any;
190
+ /**
191
+ * Get a value from the request header or return
192
+ * the default value.
193
+ *
194
+ * @example
195
+ * ```ts
196
+ * const accept = request.header('accept', 'application/json')
197
+ * ```
198
+ */
199
+ header(header: string, defaultValue?: any): any;
200
+ /**
201
+ * Get a value from the request body or return
202
+ * the default value.
203
+ *
204
+ * @example
205
+ * ```ts
206
+ * const name = request.input('name', 'lenon')
207
+ * ```
208
+ */
209
+ input(key: string, defaultValue?: any): any;
210
+ /**
211
+ * Get a value from the request body or return
212
+ * the default value.
213
+ *
214
+ * @example
215
+ * ```ts
216
+ * const name = request.payload('name', 'lenon')
217
+ * ```
218
+ */
219
+ payload(key: string, defaultValue?: any): any;
220
+ /**
221
+ * Get only the selected values from the request body.
222
+ *
223
+ * @example
224
+ * ```ts
225
+ * const body = request.only(['name', 'email'])
226
+ * ```
227
+ */
228
+ only(keys: string[]): any;
229
+ /**
230
+ * Get all the values from the request body except the
231
+ * selected ones.
232
+ *
233
+ * @example
234
+ * ```ts
235
+ * const body = request.except(['name'])
236
+ * ```
237
+ */
238
+ except(keys: string[]): any;
239
+ };
240
+ export type Response = {
241
+ /**
242
+ * The fastify response object.
243
+ */
244
+ response: FastifyReply;
245
+ /**
246
+ * The request object from request context.
247
+ */
248
+ request: Request;
249
+ /**
250
+ * Verify if the response has been already sent. Keep
251
+ * in mind that this method will only return `true`
252
+ * after `response.send()`, `response.view()`,
253
+ * `response.sendFile()` or `response.download()` methods
254
+ * call.
255
+ *
256
+ * @example
257
+ * ```ts
258
+ * if (response.sent) {
259
+ * // do something
260
+ * }
261
+ * ```
262
+ */
263
+ get sent(): boolean;
264
+ /**
265
+ * Get the response body sent in response. Keep
266
+ * in mind that this method will only return `true`
267
+ * after `response.send()`, `response.view()`,
268
+ * `response.sendFile()` or `response.download()` methods
269
+ * call.
270
+ *
271
+ * @example
272
+ * ```ts
273
+ * const { createdAt, updatedAt } = response.body
274
+ * ```
275
+ */
276
+ get body(): any | any[];
277
+ /**
278
+ * Get the status code sent in response. Keep
279
+ * in mind that this method will only return `true`
280
+ * after `response.send()`, `response.view()`,
281
+ * `response.sendFile()` or `response.download()` methods
282
+ * call.
283
+ *
284
+ * @example
285
+ * ```ts
286
+ * if (response.statusCode !== 200) {
287
+ * // do something
288
+ * }
289
+ * ```
290
+ */
291
+ get statusCode(): number;
292
+ /**
293
+ * Get the headers set in the response.
294
+ *
295
+ * @example
296
+ * ```ts
297
+ * const headers = response.headers
298
+ * ```
299
+ */
300
+ get headers(): any;
301
+ /**
302
+ * Get the time in MS of how much the request has
303
+ * taken to response. Keep in mind that this method
304
+ * will only return `true` after `response.send()`,
305
+ * `response.view()`, `response.sendFile()` or
306
+ * `response.download()` methods call.
307
+ *
308
+ * @example
309
+ * ```ts
310
+ * console.log(response.responseTime) // 1000
311
+ * ```
312
+ */
313
+ get responseTime(): number;
314
+ /**
315
+ * Terminate the request sending a view to be rendered.
316
+ *
317
+ * @example
318
+ * ```ts
319
+ * return response.view('welcome', { name: 'lenon' })
320
+ * ```
321
+ */
322
+ view(view: string, data?: any): Promise<Response>;
323
+ /**
324
+ * Terminate the request sending the response body or not.
325
+ *
326
+ * @example
327
+ * ```ts
328
+ * return response.send({ name: 'lenon' })
329
+ * ```
330
+ */
331
+ send(data?: any): Promise<Response>;
332
+ /**
333
+ * @example
334
+ * ```ts
335
+ * return response.sendFile('img.png')
336
+ * ```
337
+ */
338
+ sendFile(filename: string, filepath?: string): Promise<Response>;
339
+ /**
340
+ * @example
341
+ * ```ts
342
+ * return response.sendFile('img.png', { cacheControl: false })
343
+ * ```
344
+ */
345
+ sendFile(filename: string, options?: string | SendOptions): Promise<Response>;
346
+ /**
347
+ * @example
348
+ * ```ts
349
+ * return response.sendFile('img.png', Path.tmp(), {
350
+ * cacheControl: false
351
+ * })
352
+ * ```
353
+ */
354
+ sendFile(filename: string, filepath?: string, options?: SendOptions): Promise<Response>;
355
+ /**
356
+ * Terminate the request sending a file.
357
+ *
358
+ * @example
359
+ * ```ts
360
+ * return response.sendFile('img.png')
361
+ * ```
362
+ */
363
+ sendFile(filename: string, filepath?: string, options?: SendOptions): Promise<Response>;
364
+ /**
365
+ * @example
366
+ * ```ts
367
+ * return response.download('img.png', 'custom-img.png')
368
+ * ```
369
+ */
370
+ download(filepath: string, filename: string): Promise<Response>;
371
+ /**
372
+ * @example
373
+ * ```ts
374
+ * return response.download('img.png', 'custom-img.png', {
375
+ * cacheControl: false
376
+ * })
377
+ * ```
378
+ */
379
+ download(filepath: string, filename: string, options?: SendOptions): Promise<Response>;
380
+ /**
381
+ * Terminate the request sending a file with custom name.
382
+ *
383
+ * @example
384
+ * ```ts
385
+ * return response.download('img.png', 'custom-img.png')
386
+ * ```
387
+ */
388
+ download(filepath: string, filename: string, options?: SendOptions): Promise<Response>;
389
+ /**
390
+ * Set the response status code.
391
+ *
392
+ * @example
393
+ * ```ts
394
+ * return response.status(200).send()
395
+ * ```
396
+ */
397
+ status(code: number): Response;
398
+ /**
399
+ * Add some header to the response.
400
+ *
401
+ * @example
402
+ * ```ts
403
+ * response.header('content-type', 'application/json')
404
+ *
405
+ * return response.header('accept-encoding', 'gzip').send(user)
406
+ * ```
407
+ */
408
+ header(header: string, value: any): Response;
409
+ /**
410
+ * Verify if response has some header.
411
+ *
412
+ * @example
413
+ * ```ts
414
+ * if (response.hasHeader('content-type')) {
415
+ * // do something
416
+ * }
417
+ * ```
418
+ */
419
+ hasHeader(header: string): boolean;
420
+ /**
421
+ * Add some header safely to the response. This means that
422
+ * the header is not going to be added if is already set.
423
+ *
424
+ * @example
425
+ * ```ts
426
+ * response.safeHeader('content-type', 'application/json')
427
+ * ```
428
+ */
429
+ safeHeader(header: string, value: any): Response;
430
+ /**
431
+ * Remove some header from the response.
432
+ *
433
+ * @example
434
+ * ```ts
435
+ * response.removeHeader('content-type')
436
+ * ```
437
+ */
438
+ removeHeader(header: string): Response;
439
+ /**
440
+ * Redirect the response to other url. You can also set a
441
+ * different status code for the redirect.
442
+ *
443
+ * @example
444
+ * ```ts
445
+ * return response.redirect('users', 304)
446
+ * ```
447
+ */
448
+ redirectTo(url: string, status?: number): Promise<Response>;
449
+ /**
450
+ * Apply helmet in response.
451
+ *
452
+ * @example
453
+ * ```ts
454
+ * return response
455
+ * .helmet({ enableCSPNonces: false })
456
+ * .view('profile', user)
457
+ * ```
458
+ */
459
+ helmet(options: FastifyHelmetOptions): Response;
460
+ };
11
461
  export type Context = {
462
+ /**
463
+ * Retrieve any kind of data from your request by using the
464
+ * request object.
465
+ */
12
466
  request: Request;
467
+ /**
468
+ * Return a response from the request using the response
469
+ * object.
470
+ */
13
471
  response: Response;
472
+ /**
473
+ * Save any kind of data that will be shared in all of your
474
+ * request flow. The data defined here will be available in
475
+ * middlewares, route handlers, interceptors and terminators.
476
+ */
14
477
  data: any;
15
478
  };
16
479
  export type RequestHandler = (ctx: Context) => any | Promise<any>;
@@ -6,5 +6,4 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
- import { Request } from '#src/context/Request';
10
- import { Response } from '#src/context/Response';
9
+ export {};