tspace-spear 1.0.0-rc → 1.0.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.
package/README.md CHANGED
@@ -23,16 +23,20 @@ npm install tspace-spear --save
23
23
 
24
24
  ## StartServer
25
25
  ```js
26
- import Spear from "tspace-spear";
26
+ import { Application } from "tspace-spear";
27
27
 
28
- new Spear()
29
- .get('/' , () => 'Hello world!')
30
- .get('/json' , () => {
31
- return {
32
- message : 'Hello world!'
33
- }
34
- })
35
- .listen(3000 , ({ server, port }) => console.log(`server listening on : http://localhost:${port}`))
28
+ (async () => {
29
+ await new Application()
30
+ .get('/' , () => 'Hello world!')
31
+ .get('/json' , () => {
32
+ return {
33
+ message : 'Hello world!'
34
+ }
35
+ })
36
+ .listen(3000 , () => console.log('server listening on port : 3000'))
37
+ })()
38
+
39
+ // localhost:3000
36
40
 
37
41
  ```
38
42
 
@@ -44,12 +48,12 @@ export default (ctx : TContext, next: TNextFunction) =>{
44
48
  return next();
45
49
  }
46
50
 
47
- import Spear { Router, TContext, TNextFunction } from "tspace-spear";
51
+ import { Application , Router, TContext, TNextFunction } from "tspace-spear";
48
52
  import CatMiddleware from './cat-middleware.ts'
49
53
 
50
54
  (async () => {
51
55
  const port = Number(process.env.PORT ?? 3000)
52
- const app = new Spear({
56
+ const app = new Application({
53
57
  middlewares: [ CatMiddleware ]
54
58
  // if you want to import a middleware with a directory can you follow the example
55
59
  // middlewares : {
@@ -165,12 +169,12 @@ class CatController {
165
169
  }
166
170
  }
167
171
 
168
- import Spear , { Router, TContext, TNextFunction } from "tspace-spear";
172
+ import { Application , Router, TContext, TNextFunction } from "tspace-spear";
169
173
  import CatController from './cat-controller.ts'
170
174
 
171
175
  (async () => {
172
176
 
173
- const app = new Spear({
177
+ const app = new Application({
174
178
  controllers: [ CatController ]
175
179
  // if you want to import a controller with a directory can you follow the example
176
180
  // controllers : {
@@ -198,9 +202,9 @@ import CatController from './cat-controller.ts'
198
202
  ## Router
199
203
 
200
204
  ```js
201
- import Spear , { Router, TContext, TNextFunction } from "tspace-spear";
205
+ import { Application , Router, TContext, TNextFunction } from "tspace-spear";
202
206
 
203
- const app = new Spear()
207
+ const app = new Application()
204
208
 
205
209
  const router = new Router()
206
210
 
@@ -242,32 +246,11 @@ app.listen(port , () => console.log(`Server is now listening port: ${port}`))
242
246
  ## Handlers
243
247
  ```js
244
248
 
245
- const app = new Spear({
249
+ const app = new Application({
246
250
  logger : true, // logging
247
- globalPrefix : '/api' // prefix all routes
251
+ globalPrefix : '/api' // prefix all routes in Router and Controller only!
248
252
  })
249
253
 
250
- app.enableCors({
251
- origins: [
252
- /^http:\/\/localhost:\d+$/
253
- ],
254
- credentials: true
255
- })
256
-
257
- app.useFileUpload({
258
- limits : 1000 * 1000, // limits for file upload 1_000_000 bytes
259
- useTempFiles : true, // whether to use temporary files
260
- tempFileDir : 'tmp', // temporary directory
261
- removeTempFile : {
262
- remove : true, // remove temporary files
263
- ms : 1000 * 60 // remove temporary after 60 seconds
264
- }
265
- })
266
-
267
- app.useBodyParser()
268
-
269
- app.useCookiesParser()
270
-
271
254
  app.get('/' , ({ res } : TContext) => {
272
255
  return res.json({
273
256
  message : 'hello world!'
@@ -1,11 +1,11 @@
1
- /// <reference types="node" />
2
- import { Server, ServerResponse } from 'http';
3
- import { Router, TContext, TNextFunction, TApplication } from '../..';
1
+ import { ServerResponse } from 'http';
2
+ import { Router } from '../..';
3
+ import type { TContext, TNextFunction, TApplication } from "../..";
4
4
  /**
5
5
  *
6
- * The 'Spear' class is used to create a server and handle HTTP requests.
6
+ * The 'Application' class is used to create a server and handle HTTP requests.
7
7
  *
8
- * @returns {Spear} application
8
+ * @returns {Application} application
9
9
  * @example
10
10
  * new Application()
11
11
  * .get('/' , () => 'Hello world!')
@@ -17,7 +17,7 @@ import { Router, TContext, TNextFunction, TApplication } from '../..';
17
17
  * .listen(3000 , () => console.log('server listening on port : 3000'))
18
18
  *
19
19
  */
20
- declare class Spear {
20
+ declare class Application {
21
21
  private readonly _controllers?;
22
22
  private readonly _middlewares?;
23
23
  private readonly _globalPrefix;
@@ -178,10 +178,7 @@ declare class Spear {
178
178
  * @param {function} cb
179
179
  * @returns
180
180
  */
181
- listen(port: number | (() => ServerResponse) | undefined, cb: (callback: {
182
- server: Server;
183
- port: number;
184
- }) => void): Promise<void>;
181
+ listen(port: number | (() => ServerResponse) | undefined, cb: () => void): Promise<void>;
185
182
  private _logger;
186
183
  private _import;
187
184
  private _registerControllers;
@@ -194,9 +191,6 @@ declare class Spear {
194
191
  private _wrapHandlers;
195
192
  private _wrapResponse;
196
193
  private _createServer;
197
- private _normalizePath;
198
194
  }
199
- export { Spear };
200
- export declare class Application extends Spear {
201
- }
202
- export default Spear;
195
+ export { Application };
196
+ export default Application;
@@ -35,7 +35,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.Application = exports.Spear = void 0;
38
+ exports.Application = void 0;
39
39
  const fs_1 = __importDefault(require("fs"));
40
40
  const path_1 = __importDefault(require("path"));
41
41
  const find_my_way_1 = __importDefault(require("find-my-way"));
@@ -46,9 +46,9 @@ const string_decoder_1 = require("string_decoder");
46
46
  const http_1 = __importStar(require("http"));
47
47
  /**
48
48
  *
49
- * The 'Spear' class is used to create a server and handle HTTP requests.
49
+ * The 'Application' class is used to create a server and handle HTTP requests.
50
50
  *
51
- * @returns {Spear} application
51
+ * @returns {Application} application
52
52
  * @example
53
53
  * new Application()
54
54
  * .get('/' , () => 'Hello world!')
@@ -60,7 +60,7 @@ const http_1 = __importStar(require("http"));
60
60
  * .listen(3000 , () => console.log('server listening on port : 3000'))
61
61
  *
62
62
  */
63
- class Spear {
63
+ class Application {
64
64
  constructor({ controllers, middlewares, globalPrefix, logger } = {}) {
65
65
  this._router = (0, find_my_way_1.default)();
66
66
  this._errorHandler = null;
@@ -264,7 +264,14 @@ class Spear {
264
264
  useRouter(router) {
265
265
  const routes = router.routes;
266
266
  for (const { path, method, handlers } of routes) {
267
- this[method](this._normalizePath(this._globalPrefix, path), ...handlers);
267
+ let normalizePath = [
268
+ this._globalPrefix,
269
+ path
270
+ ].join('/')
271
+ .replace(/\/+/g, '/')
272
+ .replace(/\/+$/, '');
273
+ normalizePath = normalizePath.startsWith('/') ? normalizePath : `/${normalizePath}`;
274
+ this[method](normalizePath, ...handlers);
268
275
  }
269
276
  return this;
270
277
  }
@@ -399,7 +406,7 @@ class Spear {
399
406
  */
400
407
  get(path, ...handlers) {
401
408
  this._onListeners.push(() => {
402
- return this._router.get(this._normalizePath(this._globalPrefix, path), this._wrapHandlers(...this._globalMiddlewares, ...handlers));
409
+ return this._router.get(path, this._wrapHandlers(...this._globalMiddlewares, ...handlers));
403
410
  });
404
411
  return this;
405
412
  }
@@ -414,7 +421,7 @@ class Spear {
414
421
  */
415
422
  post(path, ...handlers) {
416
423
  this._onListeners.push(() => {
417
- return this._router.post(this._normalizePath(this._globalPrefix, path), this._wrapHandlers(...this._globalMiddlewares, ...handlers));
424
+ return this._router.post(path, this._wrapHandlers(...this._globalMiddlewares, ...handlers));
418
425
  });
419
426
  return this;
420
427
  }
@@ -429,7 +436,7 @@ class Spear {
429
436
  */
430
437
  put(path, ...handlers) {
431
438
  this._onListeners.push(() => {
432
- return this._router.put(this._normalizePath(this._globalPrefix, path), this._wrapHandlers(...this._globalMiddlewares, ...handlers));
439
+ return this._router.put(path, this._wrapHandlers(...this._globalMiddlewares, ...handlers));
433
440
  });
434
441
  return this;
435
442
  }
@@ -444,7 +451,7 @@ class Spear {
444
451
  */
445
452
  patch(path, ...handlers) {
446
453
  this._onListeners.push(() => {
447
- return this._router.patch(this._normalizePath(this._globalPrefix, path), this._wrapHandlers(...this._globalMiddlewares, ...handlers));
454
+ return this._router.patch(path, this._wrapHandlers(...this._globalMiddlewares, ...handlers));
448
455
  });
449
456
  return this;
450
457
  }
@@ -459,7 +466,7 @@ class Spear {
459
466
  */
460
467
  delete(path, ...handlers) {
461
468
  this._onListeners.push(() => {
462
- return this._router.delete(this._normalizePath(this._globalPrefix, path), this._wrapHandlers(...this._globalMiddlewares, ...handlers));
469
+ return this._router.delete(path, this._wrapHandlers(...this._globalMiddlewares, ...handlers));
463
470
  });
464
471
  return this;
465
472
  }
@@ -474,7 +481,7 @@ class Spear {
474
481
  */
475
482
  all(path, ...handlers) {
476
483
  this._onListeners.push(() => {
477
- return this._router.all(this._normalizePath(this._globalPrefix, path), this._wrapHandlers(...this._globalMiddlewares, ...handlers));
484
+ return this._router.all(path, this._wrapHandlers(...this._globalMiddlewares, ...handlers));
478
485
  });
479
486
  return this;
480
487
  }
@@ -492,10 +499,6 @@ class Spear {
492
499
  port = 3000;
493
500
  }
494
501
  const server = yield this._createServer();
495
- server.listen(port == null ? 3000 : port, () => {
496
- if (cb)
497
- cb({ server, port });
498
- });
499
502
  server.on('listening', () => {
500
503
  this._onListeners.forEach(listener => listener());
501
504
  });
@@ -503,6 +506,10 @@ class Spear {
503
506
  port = Math.floor(Math.random() * 8999) + 1000;
504
507
  server.listen(port);
505
508
  });
509
+ server.listen(port == null ? 3000 : port, () => {
510
+ if (cb)
511
+ cb();
512
+ });
506
513
  return;
507
514
  });
508
515
  }
@@ -566,7 +573,15 @@ class Spear {
566
573
  if (prefixPath == null)
567
574
  continue;
568
575
  for (const { method, path, handler } of routers) {
569
- this[method](this._normalizePath(this._globalPrefix, prefixPath, path), this._wrapResponse(controllerInstance[String(handler)].bind(controllerInstance)));
576
+ let normalizePath = [
577
+ this._globalPrefix,
578
+ prefixPath,
579
+ path
580
+ ].join('/')
581
+ .replace(/\/+/g, '/')
582
+ .replace(/\/+$/, '');
583
+ normalizePath = normalizePath.startsWith('/') ? normalizePath : `/${normalizePath}`;
584
+ this[method](normalizePath, this._wrapResponse(controllerInstance[String(handler)].bind(controllerInstance)));
570
585
  }
571
586
  }
572
587
  return;
@@ -578,7 +593,15 @@ class Spear {
578
593
  if (prefixPath == null)
579
594
  continue;
580
595
  for (const { method, path, handler } of routers) {
581
- this[method](this._normalizePath(this._globalPrefix, prefixPath, path), this._wrapResponse(controllerInstance[String(handler)].bind(controllerInstance)));
596
+ let normalizePath = [
597
+ this._globalPrefix,
598
+ prefixPath,
599
+ path
600
+ ].join('/')
601
+ .replace(/\/+/g, '/')
602
+ .replace(/\/+$/, '');
603
+ normalizePath = normalizePath.startsWith('/') ? normalizePath : `/${normalizePath}`;
604
+ this[method](normalizePath, this._wrapResponse(controllerInstance[String(handler)].bind(controllerInstance)));
582
605
  }
583
606
  }
584
607
  });
@@ -853,16 +876,6 @@ class Spear {
853
876
  return server;
854
877
  });
855
878
  }
856
- _normalizePath(...paths) {
857
- let path = paths
858
- .join('/')
859
- .replace(/\/+/g, '/')
860
- .replace(/\/+$/, '');
861
- return path.startsWith('/') ? path : `/${path}`;
862
- }
863
- }
864
- exports.Spear = Spear;
865
- class Application extends Spear {
866
879
  }
867
880
  exports.Application = Application;
868
- exports.default = Spear;
881
+ exports.default = Application;
@@ -7,5 +7,14 @@ export * from './core/decorators';
7
7
  export * from './types';
8
8
  export * from './core/server/router';
9
9
  export * from './core/server';
10
- import Spear from './core/server';
11
- export default Spear;
10
+ import * as decorators from './core/decorators';
11
+ import * as types from './types';
12
+ import * as router from './core/server/router';
13
+ import * as server from './core/server';
14
+ declare const _default: {
15
+ decorators: typeof decorators;
16
+ types: typeof types;
17
+ router: typeof router;
18
+ server: typeof server;
19
+ };
20
+ export default _default;
@@ -10,11 +10,20 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
13
18
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
19
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
20
  };
16
- var __importDefault = (this && this.__importDefault) || function (mod) {
17
- return (mod && mod.__esModule) ? mod : { "default": mod };
21
+ var __importStar = (this && this.__importStar) || function (mod) {
22
+ if (mod && mod.__esModule) return mod;
23
+ var result = {};
24
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
+ __setModuleDefault(result, mod);
26
+ return result;
18
27
  };
19
28
  Object.defineProperty(exports, "__esModule", { value: true });
20
29
  /**
@@ -26,5 +35,13 @@ __exportStar(require("./core/decorators"), exports);
26
35
  __exportStar(require("./types"), exports);
27
36
  __exportStar(require("./core/server/router"), exports);
28
37
  __exportStar(require("./core/server"), exports);
29
- const server_1 = __importDefault(require("./core/server"));
30
- exports.default = server_1.default;
38
+ const decorators = __importStar(require("./core/decorators"));
39
+ const types = __importStar(require("./types"));
40
+ const router = __importStar(require("./core/server/router"));
41
+ const server = __importStar(require("./core/server"));
42
+ exports.default = {
43
+ decorators,
44
+ types,
45
+ router,
46
+ server
47
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-spear",
3
- "version": "1.0.0-rc",
3
+ "version": "1.0.0",
4
4
  "description": "Build a REST API with high speed using a native HTTP server for Node.js.",
5
5
  "main": "./build/lib/index.js",
6
6
  "types": "./build/lib/index.d.ts",