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,18 @@
1
+ import { TContext, TNextFunction } from "../..";
2
+ declare class Router {
3
+ private _routes;
4
+ get routes(): {
5
+ path: string;
6
+ method: "get" | "post" | "patch" | "put" | "delete" | "all";
7
+ handlers: ((ctx: TContext, next: TNextFunction) => any)[];
8
+ }[];
9
+ groups(prefix: `/${string}`, router: (router: Router) => Router): void;
10
+ get(path: `/${string}`, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
11
+ post(path: `/${string}`, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
12
+ put(path: `/${string}`, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
13
+ patch(path: `/${string}`, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
14
+ delete(path: `/${string}`, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
15
+ all(path: `/${string}`, ...handlers: ((ctx: TContext, next: TNextFunction) => any)[]): this;
16
+ }
17
+ export { Router };
18
+ export default Router;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Router = void 0;
4
+ class Router {
5
+ constructor() {
6
+ this._routes = [];
7
+ }
8
+ get routes() {
9
+ return this._routes;
10
+ }
11
+ groups(prefix, router) {
12
+ const routes = router(new Router);
13
+ for (const route of routes._routes) {
14
+ route.path = `${prefix}${route.path}`.replace(/^\/+/, '/');
15
+ this._routes.push(route);
16
+ }
17
+ }
18
+ get(path, ...handlers) {
19
+ this._routes.push({
20
+ path,
21
+ method: 'get',
22
+ handlers
23
+ });
24
+ return this;
25
+ }
26
+ post(path, ...handlers) {
27
+ this._routes.push({
28
+ path,
29
+ method: 'post',
30
+ handlers
31
+ });
32
+ return this;
33
+ }
34
+ put(path, ...handlers) {
35
+ this._routes.push({
36
+ path,
37
+ method: 'put',
38
+ handlers
39
+ });
40
+ return this;
41
+ }
42
+ patch(path, ...handlers) {
43
+ this._routes.push({
44
+ path,
45
+ method: 'patch',
46
+ handlers
47
+ });
48
+ return this;
49
+ }
50
+ delete(path, ...handlers) {
51
+ this._routes.push({
52
+ path,
53
+ method: 'delete',
54
+ handlers
55
+ });
56
+ return this;
57
+ }
58
+ all(path, ...handlers) {
59
+ this._routes.push({
60
+ path,
61
+ method: 'all',
62
+ handlers
63
+ });
64
+ return this;
65
+ }
66
+ }
67
+ exports.Router = Router;
68
+ exports.default = Router;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The entry point.
3
+ *
4
+ * @module tspace-ez-node
5
+ */
6
+ export * from './core/decorators';
7
+ export * from './types';
8
+ export * from './core/server/router';
9
+ export * from './core/server';
10
+ import Spear from './core/server';
11
+ export default Spear;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ /**
21
+ * The entry point.
22
+ *
23
+ * @module tspace-ez-node
24
+ */
25
+ __exportStar(require("./core/decorators"), exports);
26
+ __exportStar(require("./types"), exports);
27
+ __exportStar(require("./core/server/router"), exports);
28
+ __exportStar(require("./core/server"), exports);
29
+ const server_1 = __importDefault(require("./core/server"));
30
+ exports.default = server_1.default;
@@ -0,0 +1,79 @@
1
+ /// <reference types="node" />
2
+ import { IncomingMessage, ServerResponse } from "http";
3
+ export type TContext = {
4
+ req: TRequest;
5
+ res: TResponse;
6
+ query: TQuery;
7
+ params: TParams;
8
+ body: TBody;
9
+ files: TFiles;
10
+ cookies: TCookies;
11
+ };
12
+ export type TQuery<T = Record<string, string> | null> = T;
13
+ export type TParams<T = Record<string, string> | null> = T;
14
+ export type TBody<T = Record<string, string | number | boolean | undefined | null | any[] | Record<string, string | number | boolean | undefined | null | any[]>> | null> = T;
15
+ export type TCookies<T = Record<string, any> | null> = T;
16
+ export type TFiles<T = Record<string, {
17
+ size: number;
18
+ tempFilePath: string;
19
+ tempName: string;
20
+ mimetype: string;
21
+ name: string;
22
+ }> | null> = T;
23
+ export type TNextFunction<T = any> = (err?: Error) => T | Promise<T>;
24
+ export type TRequest<T = any> = IncomingMessage & Partial<T>;
25
+ type HttpStatus = {
26
+ json: (data?: Record<string, any>) => void;
27
+ error: (err: any) => void;
28
+ ok: (data?: Record<string, any>) => void;
29
+ created: (data?: Record<string, any>) => void;
30
+ accepted: (data?: Record<string, any>) => void;
31
+ noContent: (message?: string) => void;
32
+ badRequest: (message?: string) => void;
33
+ unauthorized: (message?: string) => void;
34
+ paymentRequired: (message?: string) => void;
35
+ forbidden: (message?: string) => void;
36
+ notFound: (message?: string) => void;
37
+ serverError: (message: string) => void;
38
+ forceStatus: (code: number) => void;
39
+ setCookies: (cookies: Record<string, string | {
40
+ value: string;
41
+ sameSite?: 'Strict' | 'Lax' | 'None';
42
+ domain?: string;
43
+ secure?: boolean;
44
+ httpOnly?: boolean;
45
+ expires?: Date;
46
+ }>) => void;
47
+ };
48
+ export type TResponse<T = any> = ServerResponse & {
49
+ status: (code: 200 | 201 | 202 | 203 | 204 | 300 | 301 | 302 | 303 | 304 | 400 | 401 | 402 | 403 | 404 | 500 | 501 | 502 | 503 | 504) => {
50
+ json: (data?: Record<string, any>) => void;
51
+ send: (message: string) => void;
52
+ };
53
+ } & HttpStatus & Partial<T>;
54
+ export type TRouter = {
55
+ method: TMethods;
56
+ path: string;
57
+ handler: string | symbol;
58
+ };
59
+ export type IRoute = {
60
+ method: TMethods;
61
+ path: string;
62
+ handlers: TContext[];
63
+ };
64
+ export type TMethods = 'get' | 'post' | 'patch' | 'put' | 'delete';
65
+ export type TApplication = {
66
+ controllers?: (new () => any)[] | {
67
+ folder: string;
68
+ name?: RegExp;
69
+ };
70
+ middlewares?: TRequestFunction[] | {
71
+ folder: string;
72
+ name?: RegExp;
73
+ };
74
+ globalPrefix?: string;
75
+ logger?: boolean;
76
+ };
77
+ export type TRequestFunction = (ctx: TContext, next: TNextFunction) => any;
78
+ export type TErrorFunction = (err: Error, ctx: TContext) => any;
79
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "tspace-spear",
3
+ "version": "1.0.0-rc",
4
+ "description": "Build a REST API with high speed using a native HTTP server for Node.js.",
5
+ "main": "./build/lib/index.js",
6
+ "types": "./build/lib/index.d.ts",
7
+ "files": [
8
+ "build"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/thanathip41/tspace-spear.git"
13
+ },
14
+ "keywords": [
15
+ "tspace",
16
+ "spear",
17
+ "fast",
18
+ "http",
19
+ "node",
20
+ "rest api",
21
+ "decorators",
22
+ "middlewares",
23
+ "controllers",
24
+ "routers"
25
+ ],
26
+ "author": "Thanathip (https://github.com/thanathip41)",
27
+ "license": "MIT",
28
+ "bugs": {
29
+ "url": "https://github.com/thanathip41/tspace-spear"
30
+ },
31
+ "homepage": "https://github.com/thanathip41",
32
+ "scripts": {
33
+ "build": "tsc",
34
+ "prepare": "npm run build"
35
+ },
36
+ "devDependencies": {
37
+ "@types/formidable": "^3.4.5",
38
+ "@types/node": "^16.4.0",
39
+ "@types/on-finished": "^2.3.4"
40
+ },
41
+ "dependencies": {
42
+ "find-my-way": "^8.1.0",
43
+ "formidable": "^3.5.1",
44
+ "on-finished": "^2.4.1",
45
+ "reflect-metadata": "^0.1.14"
46
+ }
47
+ }