tspace-spear 1.1.9 → 1.2.1-beta.1
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 +747 -705
- package/{build → dist}/lib/core/decorators/context.js.map +1 -1
- package/{build → dist}/lib/core/decorators/controller.js +1 -1
- package/{build → dist}/lib/core/decorators/controller.js.map +1 -1
- package/{build → dist}/lib/core/decorators/headers.js.map +1 -1
- package/dist/lib/core/decorators/methods.js.map +1 -0
- package/dist/lib/core/decorators/middleware.js.map +1 -0
- package/{build → dist}/lib/core/decorators/statusCode.js +2 -2
- package/dist/lib/core/decorators/statusCode.js.map +1 -0
- package/dist/lib/core/decorators/swagger.js.map +1 -0
- package/{build → dist}/lib/core/server/index.js +298 -212
- package/dist/lib/core/server/index.js.map +1 -0
- package/{build → dist}/lib/core/server/parser-factory.js +29 -7
- package/dist/lib/core/server/parser-factory.js.map +1 -0
- package/dist/lib/core/server/radix-router.js +65 -0
- package/dist/lib/core/server/radix-router.js.map +1 -0
- package/dist/lib/core/server/router.js.map +1 -0
- package/{build → dist}/lib/index.js +0 -1
- package/{build → dist}/lib/index.js.map +1 -1
- package/{build → dist}/tests/benchmark.test.js +48 -9
- package/dist/tests/benchmark.test.js.map +1 -0
- package/package.json +17 -11
- package/build/lib/core/decorators/context.d.ts +0 -5
- package/build/lib/core/decorators/controller.d.ts +0 -1
- package/build/lib/core/decorators/headers.d.ts +0 -3
- package/build/lib/core/decorators/index.d.ts +0 -9
- package/build/lib/core/decorators/methods.d.ts +0 -5
- package/build/lib/core/decorators/methods.js.map +0 -1
- package/build/lib/core/decorators/middleware.d.ts +0 -2
- package/build/lib/core/decorators/middleware.js.map +0 -1
- package/build/lib/core/decorators/statusCode.d.ts +0 -1
- package/build/lib/core/decorators/statusCode.js.map +0 -1
- package/build/lib/core/decorators/swagger.d.ts +0 -2
- package/build/lib/core/decorators/swagger.js.map +0 -1
- package/build/lib/core/server/index.d.ts +0 -309
- package/build/lib/core/server/index.js.map +0 -1
- package/build/lib/core/server/parser-factory.d.ts +0 -24
- package/build/lib/core/server/parser-factory.js.map +0 -1
- package/build/lib/core/server/router.d.ts +0 -79
- package/build/lib/core/server/router.js.map +0 -1
- package/build/lib/core/types/index.d.ts +0 -167
- package/build/lib/index.d.ts +0 -11
- package/build/tests/benchmark.test.d.ts +0 -1
- package/build/tests/benchmark.test.js.map +0 -1
- /package/{build → dist}/lib/core/decorators/context.js +0 -0
- /package/{build → dist}/lib/core/decorators/headers.js +0 -0
- /package/{build → dist}/lib/core/decorators/index.js +0 -0
- /package/{build → dist}/lib/core/decorators/index.js.map +0 -0
- /package/{build → dist}/lib/core/decorators/methods.js +0 -0
- /package/{build → dist}/lib/core/decorators/middleware.js +0 -0
- /package/{build → dist}/lib/core/decorators/swagger.js +0 -0
- /package/{build → dist}/lib/core/server/router.js +0 -0
- /package/{build → dist}/lib/core/types/index.js +0 -0
- /package/{build → dist}/lib/core/types/index.js.map +0 -0
package/README.md
CHANGED
|
@@ -1,706 +1,748 @@
|
|
|
1
|
-
# tspace-spear
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com)
|
|
4
|
-
[](https://www.npmjs.com)
|
|
5
|
-
|
|
6
|
-
tspace-spear is a lightweight API framework for Node.js that is fast and highly focused on providing the best developer experience.
|
|
7
|
-
It utilizes the native HTTP server.
|
|
8
|
-
|
|
9
|
-
## Install
|
|
10
|
-
|
|
11
|
-
Install with [npm](https://www.npmjs.com/):
|
|
12
|
-
|
|
13
|
-
```sh
|
|
14
|
-
npm install tspace-spear --save
|
|
15
|
-
|
|
16
|
-
```
|
|
17
|
-
## Basic Usage
|
|
18
|
-
- [Start Server](#start-server)
|
|
19
|
-
- [Cluster](#cluster)
|
|
20
|
-
- [Global Prefix](#global-prefix)
|
|
21
|
-
- [Logger](#logger)
|
|
22
|
-
- [Format Response](#format-response)
|
|
23
|
-
- [Notfound](#notfound)
|
|
24
|
-
- [Response](#response)
|
|
25
|
-
- [Catch](#catch)
|
|
26
|
-
- [Cors](#cors)
|
|
27
|
-
- [Body](#body)
|
|
28
|
-
- [File Upload](#file-upload)
|
|
29
|
-
- [Cookie](#cookie)
|
|
30
|
-
- [Middleware](#middleware)
|
|
31
|
-
- [Controller](#controller)
|
|
32
|
-
- [Router](#router)
|
|
33
|
-
- [Swagger](#swagger)
|
|
34
|
-
- [
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
.get('/
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
.get('/
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
.
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
import
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
//
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
.
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
//
|
|
254
|
-
//
|
|
255
|
-
// }
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
return
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
@
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
})
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
id :
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
(
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
})
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
{
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
.
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
}
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
1
|
+
# tspace-spear
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com)
|
|
4
|
+
[](https://www.npmjs.com)
|
|
5
|
+
|
|
6
|
+
tspace-spear is a lightweight API framework for Node.js that is fast and highly focused on providing the best developer experience.
|
|
7
|
+
It utilizes the native HTTP server.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
Install with [npm](https://www.npmjs.com/):
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install tspace-spear --save
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
## Basic Usage
|
|
18
|
+
- [Start Server](#start-server)
|
|
19
|
+
- [Cluster](#cluster)
|
|
20
|
+
- [Global Prefix](#global-prefix)
|
|
21
|
+
- [Logger](#logger)
|
|
22
|
+
- [Format Response](#format-response)
|
|
23
|
+
- [Notfound](#notfound)
|
|
24
|
+
- [Response](#response)
|
|
25
|
+
- [Catch](#catch)
|
|
26
|
+
- [Cors](#cors)
|
|
27
|
+
- [Body](#body)
|
|
28
|
+
- [File Upload](#file-upload)
|
|
29
|
+
- [Cookie](#cookie)
|
|
30
|
+
- [Middleware](#middleware)
|
|
31
|
+
- [Controller](#controller)
|
|
32
|
+
- [Router](#router)
|
|
33
|
+
- [Swagger](#swagger)
|
|
34
|
+
- [WebSocket](#web-socket)
|
|
35
|
+
- [Example CRUD](#example-crud)
|
|
36
|
+
|
|
37
|
+
## Start Server
|
|
38
|
+
```js
|
|
39
|
+
import { Spear } from "tspace-spear";
|
|
40
|
+
|
|
41
|
+
new Spear()
|
|
42
|
+
.get('/' , () => 'Hello world!')
|
|
43
|
+
.get('/json' , () => {
|
|
44
|
+
return {
|
|
45
|
+
message : 'Hello world!'
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Cluster
|
|
53
|
+
```js
|
|
54
|
+
import { Spear } from "tspace-spear";
|
|
55
|
+
new Spear({
|
|
56
|
+
cluster : 3
|
|
57
|
+
})
|
|
58
|
+
.get('/' , () => 'Hello world!')
|
|
59
|
+
.get('/json' , () => {
|
|
60
|
+
return {
|
|
61
|
+
message : 'Hello world!'
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Global Prefix
|
|
69
|
+
```js
|
|
70
|
+
const app = new Spear({
|
|
71
|
+
globalPrefix : '/api' // prefix all routes
|
|
72
|
+
})
|
|
73
|
+
.get('/' , () => 'Hello world!')
|
|
74
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
75
|
+
|
|
76
|
+
// http://localhost:3000/api => 'Hello world!'
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Logger
|
|
80
|
+
```js
|
|
81
|
+
const app = new Spear({
|
|
82
|
+
logger : true
|
|
83
|
+
})
|
|
84
|
+
// or use this for logging
|
|
85
|
+
.useLogger({
|
|
86
|
+
methods : ['GET','POST'],
|
|
87
|
+
exceptPath : /\/benchmark(\/|$)|\/favicon\.ico(\/|$)/ // or use Array ['/']
|
|
88
|
+
})
|
|
89
|
+
.get('/' , () => 'Hello world!')
|
|
90
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Format Response
|
|
95
|
+
|
|
96
|
+
### Notfound
|
|
97
|
+
```js
|
|
98
|
+
const app = new Spear()
|
|
99
|
+
.get('/' , () => {
|
|
100
|
+
return {
|
|
101
|
+
message: 'Hello world'
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
.notfound(({ res } : T.Context) => {
|
|
105
|
+
return res.notFound('Not found!')
|
|
106
|
+
})
|
|
107
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
108
|
+
// http://localhost:3000/notfound => { success: false , message : 'Not found!' , statusCode: 404 }
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Response
|
|
113
|
+
```js
|
|
114
|
+
const app = new Spear()
|
|
115
|
+
.get('/' , () => {
|
|
116
|
+
return {
|
|
117
|
+
message: 'Hello world'
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
.response((results, statusCode) => {
|
|
121
|
+
|
|
122
|
+
if(typeof results === 'string') return results
|
|
123
|
+
|
|
124
|
+
/// ...
|
|
125
|
+
return {
|
|
126
|
+
success : statusCode < 400,
|
|
127
|
+
...results,
|
|
128
|
+
statusCode
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
132
|
+
// http://localhost:3000 => { success: true , message : 'Hello World' , statusCode: 200 }
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Catch
|
|
137
|
+
```js
|
|
138
|
+
const app = new Spear()
|
|
139
|
+
.get('/' , () => {
|
|
140
|
+
throw new Error('Catching failed')
|
|
141
|
+
})
|
|
142
|
+
.catch((err : Error , { res } : T.Context) => {
|
|
143
|
+
|
|
144
|
+
return res
|
|
145
|
+
.status(500)
|
|
146
|
+
.json({
|
|
147
|
+
success : false,
|
|
148
|
+
message : err?.message,
|
|
149
|
+
statusCode : 500
|
|
150
|
+
});
|
|
151
|
+
})
|
|
152
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
153
|
+
// http://localhost:3000 => { success: false , message : 'Catching failed' , statusCode: 500 }
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Cors
|
|
158
|
+
|
|
159
|
+
```js
|
|
160
|
+
const app = new Spear()
|
|
161
|
+
.cors({
|
|
162
|
+
origins: [
|
|
163
|
+
/^http:\/\/localhost:\d+$/
|
|
164
|
+
],
|
|
165
|
+
credentials: true
|
|
166
|
+
})
|
|
167
|
+
//.cors() allow *
|
|
168
|
+
.listen(port , () => console.log(`Server is now allow cors localhost:* `))
|
|
169
|
+
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Body
|
|
173
|
+
```js
|
|
174
|
+
|
|
175
|
+
new Spear()
|
|
176
|
+
// enable body payload
|
|
177
|
+
.useBodyParser()
|
|
178
|
+
.post('/' , ({ body }) => {
|
|
179
|
+
return {
|
|
180
|
+
yourBody : body
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
184
|
+
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## File Upload
|
|
188
|
+
|
|
189
|
+
```js
|
|
190
|
+
|
|
191
|
+
import { Spear, type T } from 'tspace-spear';
|
|
192
|
+
import path from 'path'
|
|
193
|
+
|
|
194
|
+
new Spear()
|
|
195
|
+
// use this for enable file upload
|
|
196
|
+
.useFileUpload({
|
|
197
|
+
limit : 1000 * 1000, // limit for file upload 1_000_000 bytes by default Infinity
|
|
198
|
+
tempFileDir : 'temp', // folder temporary directory by default tmp
|
|
199
|
+
removeTempFile : {
|
|
200
|
+
remove : true, // remove temporary files by default false
|
|
201
|
+
ms : 1000 * 60 // remove file temporary after 60 seconds
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
.post('/' , ({ files } : T.Context) => {
|
|
205
|
+
|
|
206
|
+
// you can move the file from temporary to other folder
|
|
207
|
+
// for example please validate the your input file
|
|
208
|
+
const file = files.file[0]
|
|
209
|
+
const folder = 'uploads'
|
|
210
|
+
|
|
211
|
+
await file.write(path.join(path.resolve(),`${folder}/${+new Date()}.${file.extension}`))
|
|
212
|
+
|
|
213
|
+
// after writed the file you should remove the temporary file
|
|
214
|
+
await file.remove()
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
files
|
|
218
|
+
}
|
|
219
|
+
})
|
|
220
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
221
|
+
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Cookie
|
|
225
|
+
```js
|
|
226
|
+
|
|
227
|
+
new Spear()
|
|
228
|
+
.useCookiesParser()
|
|
229
|
+
.post('/' , ({ cookies }) => {
|
|
230
|
+
return {
|
|
231
|
+
yourCookies : cookies
|
|
232
|
+
}
|
|
233
|
+
})
|
|
234
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Middleware
|
|
238
|
+
```js
|
|
239
|
+
import { type T } from "tspace-spear"
|
|
240
|
+
// file cat-middleware.ts
|
|
241
|
+
export default (ctx : T.Context, next: T.NextFunction) =>{
|
|
242
|
+
console.log('cat middleware globals');
|
|
243
|
+
return next();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
import Spear { Router, type T } from "tspace-spear";
|
|
247
|
+
import CatMiddleware from './cat-middleware.ts'
|
|
248
|
+
|
|
249
|
+
(async () => {
|
|
250
|
+
const port = Number(process.env.PORT ?? 3000)
|
|
251
|
+
const app = new Spear({
|
|
252
|
+
middlewares: [ CatMiddleware ]
|
|
253
|
+
// if you want to import middlewares with a directory can you follow the example
|
|
254
|
+
// middlewares : {
|
|
255
|
+
// folder : `${__dirname}/middlewares`,
|
|
256
|
+
// name : /middleware\.(ts|js)$/i
|
|
257
|
+
// }
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
// or add a middleware
|
|
261
|
+
app.use((ctx : T.Context , next : T.NextFunction) => {
|
|
262
|
+
console.log('global middlewares')
|
|
263
|
+
return next()
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
app.get('/' ((ctx,next) => {
|
|
267
|
+
console.log('middleware on the crrent route')
|
|
268
|
+
return next()
|
|
269
|
+
}), ({ res } : T.Context) => {
|
|
270
|
+
return res.json({
|
|
271
|
+
message : 'hello world!'
|
|
272
|
+
});
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
app.get('/' , ({ res } : T.Context) => {
|
|
276
|
+
return res.json({
|
|
277
|
+
message : 'hello world!'
|
|
278
|
+
});
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
app.listen(port , () => console.log(`Server is now listening http://localhost:3000`))
|
|
282
|
+
|
|
283
|
+
// localhost:3000
|
|
284
|
+
|
|
285
|
+
})()
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## Controller
|
|
289
|
+
```js
|
|
290
|
+
import {
|
|
291
|
+
Controller ,
|
|
292
|
+
Middleware ,
|
|
293
|
+
Get ,
|
|
294
|
+
Post,
|
|
295
|
+
Patch,
|
|
296
|
+
Put,
|
|
297
|
+
Delete,
|
|
298
|
+
WriteHeader,
|
|
299
|
+
Query,
|
|
300
|
+
Body,
|
|
301
|
+
Params,
|
|
302
|
+
Cookies,
|
|
303
|
+
Files,
|
|
304
|
+
StatusCode,
|
|
305
|
+
type T
|
|
306
|
+
} from 'tspace-spear';
|
|
307
|
+
|
|
308
|
+
import CatMiddleware from './cat-middleware.ts'
|
|
309
|
+
|
|
310
|
+
// file cat-controller.ts
|
|
311
|
+
@Controller('/cats')
|
|
312
|
+
class CatController {
|
|
313
|
+
@Get('/')
|
|
314
|
+
@Middleware(CatMiddleware)
|
|
315
|
+
@Query('test','id')
|
|
316
|
+
@Cookies('name')
|
|
317
|
+
public async index({ query , cookies } : {
|
|
318
|
+
query : T.Query<{ id : string }>
|
|
319
|
+
cookies : T.Cookies<{ name : string}>
|
|
320
|
+
}) {
|
|
321
|
+
|
|
322
|
+
return {
|
|
323
|
+
query,
|
|
324
|
+
cookies
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
@Get('/:id')
|
|
329
|
+
@Middleware(CatMiddleware)
|
|
330
|
+
@Params('id')
|
|
331
|
+
public async show({ params } : T.Context) {
|
|
332
|
+
return {
|
|
333
|
+
params
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
@Post('/')
|
|
338
|
+
@Middleware(CatMiddleware)
|
|
339
|
+
public async store({ body } : T.Context) {
|
|
340
|
+
return {
|
|
341
|
+
body
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
@Put('/:id')
|
|
346
|
+
@Middleware(CatMiddleware)
|
|
347
|
+
public async update({ files } : T.Context) {
|
|
348
|
+
return {
|
|
349
|
+
files
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
@Post('/upload')
|
|
354
|
+
@Middleware(CatMiddleware)
|
|
355
|
+
public async upload({ files } : T.Context) {
|
|
356
|
+
return {
|
|
357
|
+
files
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
@Delete('/:id')
|
|
362
|
+
@Middleware(CatMiddleware)
|
|
363
|
+
public async destroy({ params } : T.Context) {
|
|
364
|
+
return {
|
|
365
|
+
params
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
import { Spear } , { Router, type T } from "tspace-spear";
|
|
371
|
+
|
|
372
|
+
import CatController from './cat-controller.ts'
|
|
373
|
+
|
|
374
|
+
(async () => {
|
|
375
|
+
|
|
376
|
+
const app = new Spear({
|
|
377
|
+
controllers: [ CatController ]
|
|
378
|
+
// if you want to import controllers with a directory can you follow the example
|
|
379
|
+
// controllers : {
|
|
380
|
+
// folder : `${__dirname}/controllers`,
|
|
381
|
+
// name : /controller\.(ts|js)$/i
|
|
382
|
+
// }
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
app.useBodyParser()
|
|
386
|
+
app.useCookiesParser()
|
|
387
|
+
app.useFileUpload()
|
|
388
|
+
|
|
389
|
+
app.get('/' , ( { res } : T.Context) => {
|
|
390
|
+
return res.json({
|
|
391
|
+
message : 'hello world!'
|
|
392
|
+
});
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
app.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
396
|
+
|
|
397
|
+
// localhost:3000/cats
|
|
398
|
+
// localhost:3000/cats/41
|
|
399
|
+
|
|
400
|
+
})()
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
## Router
|
|
404
|
+
|
|
405
|
+
```js
|
|
406
|
+
import { Spear, Router, type T } from "tspace-spear";
|
|
407
|
+
|
|
408
|
+
const app = new Spear()
|
|
409
|
+
|
|
410
|
+
const router = new Router()
|
|
411
|
+
|
|
412
|
+
router.groups('/my',(r) => {
|
|
413
|
+
|
|
414
|
+
r.get('/cats' , ({ req , res }) => {
|
|
415
|
+
|
|
416
|
+
return res.json({
|
|
417
|
+
message : 'Hello, World!'
|
|
418
|
+
})
|
|
419
|
+
})
|
|
420
|
+
|
|
421
|
+
return r
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
router.get('/cats' , ({ req , res }) => {
|
|
425
|
+
return res.json({
|
|
426
|
+
message : 'Hello, World!'
|
|
427
|
+
})
|
|
428
|
+
})
|
|
429
|
+
|
|
430
|
+
app.useRouter(router)
|
|
431
|
+
|
|
432
|
+
app.get('/' , ({ res } : T.Context) => {
|
|
433
|
+
return res.json({
|
|
434
|
+
message : 'hello world!'
|
|
435
|
+
});
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
let port = 3000
|
|
439
|
+
|
|
440
|
+
app.listen(port , () => console.log(`Server is now listening http://localhost:3000`))
|
|
441
|
+
|
|
442
|
+
// localhost:3000/my/cats
|
|
443
|
+
// localhost:3000/cats
|
|
444
|
+
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
## Swagger
|
|
448
|
+
```js
|
|
449
|
+
|
|
450
|
+
// file cat-controller.ts
|
|
451
|
+
import {
|
|
452
|
+
type T,
|
|
453
|
+
Controller,
|
|
454
|
+
Get ,
|
|
455
|
+
Post,
|
|
456
|
+
Put,
|
|
457
|
+
Patch,
|
|
458
|
+
Delete,
|
|
459
|
+
Swagger
|
|
460
|
+
} from 'tspace-spear';
|
|
461
|
+
|
|
462
|
+
@Controller('/cats')
|
|
463
|
+
class CatController {
|
|
464
|
+
@Get('/')
|
|
465
|
+
@Swagger({
|
|
466
|
+
query : {
|
|
467
|
+
id : {
|
|
468
|
+
type : 'integer'
|
|
469
|
+
},
|
|
470
|
+
name : {
|
|
471
|
+
type : 'string'
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
})
|
|
475
|
+
public async index({ query } : T.Context) {
|
|
476
|
+
|
|
477
|
+
return {
|
|
478
|
+
query
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
@Get('/:id')
|
|
483
|
+
@Swagger({
|
|
484
|
+
description : '- message',
|
|
485
|
+
query : {
|
|
486
|
+
id : {
|
|
487
|
+
type : 'integer'
|
|
488
|
+
}
|
|
489
|
+
},
|
|
490
|
+
responses : [
|
|
491
|
+
{ status : 200 , description : "OK" , example : { id : 'catz' }},
|
|
492
|
+
{ status : 400 , description : "Bad request" , example : { id : 'catz' }}
|
|
493
|
+
]
|
|
494
|
+
})
|
|
495
|
+
public async show({ params } : T.Context) {
|
|
496
|
+
return {
|
|
497
|
+
params
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
@Post('/')
|
|
502
|
+
@Swagger({
|
|
503
|
+
bearerToken : true,
|
|
504
|
+
body : {
|
|
505
|
+
description : 'The description !',
|
|
506
|
+
required : true,
|
|
507
|
+
properties : {
|
|
508
|
+
id : {
|
|
509
|
+
type : 'integer',
|
|
510
|
+
example : 1
|
|
511
|
+
},
|
|
512
|
+
name : {
|
|
513
|
+
type : 'string',
|
|
514
|
+
example : "xxxxx"
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
})
|
|
519
|
+
public async store({ body } : T.Context) {
|
|
520
|
+
return {
|
|
521
|
+
body
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
@Put('/:uuid')
|
|
526
|
+
@Swagger({
|
|
527
|
+
bearerToken : true,
|
|
528
|
+
body : {
|
|
529
|
+
description : 'The description !',
|
|
530
|
+
required : true,
|
|
531
|
+
properties : {
|
|
532
|
+
id : {
|
|
533
|
+
type : 'integer',
|
|
534
|
+
example : 1
|
|
535
|
+
},
|
|
536
|
+
name : {
|
|
537
|
+
type : 'string',
|
|
538
|
+
example : "xxxxx"
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
})
|
|
543
|
+
public async updated({ body } : T.Context) {
|
|
544
|
+
return {
|
|
545
|
+
body
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
@Patch('/:uuid')
|
|
550
|
+
@Swagger({
|
|
551
|
+
bearerToken : true,
|
|
552
|
+
body : {
|
|
553
|
+
description : 'The description !',
|
|
554
|
+
required : true,
|
|
555
|
+
properties : {
|
|
556
|
+
id : {
|
|
557
|
+
type : 'integer',
|
|
558
|
+
example : 1
|
|
559
|
+
},
|
|
560
|
+
name : {
|
|
561
|
+
type : 'string',
|
|
562
|
+
example : "xxxxx"
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
})
|
|
567
|
+
public async update({ body } : T.Context) {
|
|
568
|
+
return {
|
|
569
|
+
body
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
@Delete('/:uuid')
|
|
574
|
+
@Swagger({
|
|
575
|
+
bearerToken : true
|
|
576
|
+
})
|
|
577
|
+
public async delete({ params } : T.Context) {
|
|
578
|
+
return {
|
|
579
|
+
params
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
@Post('/upload')
|
|
584
|
+
@Swagger({
|
|
585
|
+
bearerToken : true,
|
|
586
|
+
files : {
|
|
587
|
+
required : true,
|
|
588
|
+
properties : {
|
|
589
|
+
file : {
|
|
590
|
+
type : 'array',
|
|
591
|
+
items: {
|
|
592
|
+
type:"file",
|
|
593
|
+
format:"binary"
|
|
594
|
+
}
|
|
595
|
+
},
|
|
596
|
+
name : {
|
|
597
|
+
type : 'string'
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
})
|
|
602
|
+
public async upload({ body , files } : T.Context) {
|
|
603
|
+
return {
|
|
604
|
+
body,
|
|
605
|
+
files
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
(async () => {
|
|
611
|
+
|
|
612
|
+
await new Spear({
|
|
613
|
+
controllers: [ CatController ]
|
|
614
|
+
})
|
|
615
|
+
.get('/' , ({ res } : T.Context) => {
|
|
616
|
+
return res.json({
|
|
617
|
+
message : 'hello world!'
|
|
618
|
+
});
|
|
619
|
+
})
|
|
620
|
+
// .useSwagger() // by default path is "/api/docs"
|
|
621
|
+
.useSwagger({
|
|
622
|
+
path : "/docs",
|
|
623
|
+
servers : [
|
|
624
|
+
{ url : "http://localhost:3000" , description : "development"},
|
|
625
|
+
{ url : "http://localhost:8000" , description : "production"}
|
|
626
|
+
],
|
|
627
|
+
info : {
|
|
628
|
+
"title" : "Welcome to the the documentation",
|
|
629
|
+
"description" : "This is the documentation"
|
|
630
|
+
}
|
|
631
|
+
})
|
|
632
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
633
|
+
|
|
634
|
+
// localhost:3000/docs
|
|
635
|
+
})()
|
|
636
|
+
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
## WebSocket
|
|
640
|
+
```js
|
|
641
|
+
import { Spear } from "tspace-spear";
|
|
642
|
+
|
|
643
|
+
new Spear()
|
|
644
|
+
.get('/', () => 'Hello wor1ld!')
|
|
645
|
+
.post('/', ({ req , res , headers , query })=> {
|
|
646
|
+
return res.tooManyRequests()
|
|
647
|
+
})
|
|
648
|
+
.ws(() => {
|
|
649
|
+
return {
|
|
650
|
+
connection: (ws) => {
|
|
651
|
+
ws.send(JSON.stringify({ type: 'welcome', message: 'Welcome to the server!' }));
|
|
652
|
+
},
|
|
653
|
+
message: (ws, msg) => {
|
|
654
|
+
const data = JSON.parse(msg.toString());
|
|
655
|
+
|
|
656
|
+
switch (data.type) {
|
|
657
|
+
case 'chat':
|
|
658
|
+
console.log('Chat message:', data.text);
|
|
659
|
+
ws.send(JSON.stringify({ type: 'chat_ack', message: 'Chat received!' }));
|
|
660
|
+
break;
|
|
661
|
+
|
|
662
|
+
case 'ping':
|
|
663
|
+
console.log('Ping received');
|
|
664
|
+
ws.send(JSON.stringify({ type: 'pong' }));
|
|
665
|
+
break;
|
|
666
|
+
|
|
667
|
+
default:
|
|
668
|
+
ws.send(JSON.stringify({ type: 'error', message: 'Unknown message type' }));
|
|
669
|
+
}
|
|
670
|
+
},
|
|
671
|
+
close: (ws, code, reason) => {
|
|
672
|
+
console.log('Client disconnected');
|
|
673
|
+
},
|
|
674
|
+
error: (ws, error) => {
|
|
675
|
+
console.error('WebSocket error:', error);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
})
|
|
679
|
+
.listen(3000 , ({ port , server }) => {
|
|
680
|
+
console.log(`server listening on : http://localhost:${port}`)
|
|
681
|
+
})
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
```
|
|
685
|
+
|
|
686
|
+
## Example CRUD
|
|
687
|
+
```js
|
|
688
|
+
import { Spear } from "tspace-spear";
|
|
689
|
+
|
|
690
|
+
const spears = [
|
|
691
|
+
{
|
|
692
|
+
id : 1,
|
|
693
|
+
damage : 100
|
|
694
|
+
},
|
|
695
|
+
{
|
|
696
|
+
id : 2,
|
|
697
|
+
damage : 75
|
|
698
|
+
},
|
|
699
|
+
{
|
|
700
|
+
id : 3,
|
|
701
|
+
damage : 50
|
|
702
|
+
}
|
|
703
|
+
]
|
|
704
|
+
|
|
705
|
+
new Spear()
|
|
706
|
+
// enable body payload
|
|
707
|
+
.useBodyParser()
|
|
708
|
+
.get('/' , () => spears)
|
|
709
|
+
.get('/:id' , ({ params }) => spears.find(spear => spear.id === Number(params.id ?? 0)))
|
|
710
|
+
.post('/' , ({ body }) => {
|
|
711
|
+
// please validation the your body
|
|
712
|
+
const damage = Number(body.damage ?? (Math.random() * 100).toFixed(0))
|
|
713
|
+
|
|
714
|
+
const id = spears.reduce((max, spear) => spear.id > max ? spear.id : max, 0) + 1
|
|
715
|
+
|
|
716
|
+
spears.push({ id , damage })
|
|
717
|
+
|
|
718
|
+
return spears.find(spear => spear.id === id)
|
|
719
|
+
})
|
|
720
|
+
.patch('/:id' , ({ params , body , res }) => {
|
|
721
|
+
|
|
722
|
+
const damage = Number(body.damage ?? (Math.random() * 100).toFixed(0))
|
|
723
|
+
|
|
724
|
+
const id = Number(params.id)
|
|
725
|
+
|
|
726
|
+
const spear = spears.find(spear => spear.id === id)
|
|
727
|
+
|
|
728
|
+
if (spear == null) return res.status(404).json({ message : 'Spear not found'})
|
|
729
|
+
|
|
730
|
+
spear.damage = damage;
|
|
731
|
+
|
|
732
|
+
return spears.find(spear => spear.id === id)
|
|
733
|
+
})
|
|
734
|
+
.delete('/:id', ({ params , res }) => {
|
|
735
|
+
|
|
736
|
+
const id = Number(params.id)
|
|
737
|
+
|
|
738
|
+
const spear = spears.find(spear => spear.id === id)
|
|
739
|
+
|
|
740
|
+
if (spear == null) return res.status(404).json({ message : 'Spear not found'})
|
|
741
|
+
|
|
742
|
+
spears.splice(spears.findIndex(spear => spear.id === Number(params.id ?? 0)), 1)
|
|
743
|
+
|
|
744
|
+
return res.status(204).json()
|
|
745
|
+
})
|
|
746
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
747
|
+
|
|
706
748
|
```
|