@bejibun/core 0.1.29 → 0.1.31

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.
Files changed (98) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/{dist/builders → builders}/RouterBuilder.d.ts +2 -2
  3. package/bun.lock +227 -0
  4. package/{dist/facades → facades}/Router.d.ts +2 -2
  5. package/index.d.ts +1 -0
  6. package/index.js +1 -0
  7. package/package.json +30 -57
  8. package/src/bases/BaseController.ts +139 -0
  9. package/src/bases/BaseModel.ts +107 -0
  10. package/src/bases/BaseValidator.ts +8 -0
  11. package/src/bootstrap.ts +13 -0
  12. package/src/builders/ChalkBuilder.ts +60 -0
  13. package/src/builders/EnumBuilder.ts +32 -0
  14. package/src/builders/ResponseBuilder.ts +54 -0
  15. package/src/builders/RouterBuilder.ts +173 -0
  16. package/src/builders/StrBuilder.ts +25 -0
  17. package/src/config/cors.ts +10 -0
  18. package/src/config/database.ts +27 -0
  19. package/src/enums/CorsHeaderEnum.ts +9 -0
  20. package/src/enums/HttpMethodEnum.ts +13 -0
  21. package/src/exceptions/ModelNotFoundException.ts +15 -0
  22. package/src/exceptions/RouterInvalidException.ts +15 -0
  23. package/src/exceptions/ValidatorException.ts +15 -0
  24. package/src/facades/Chalk.ts +7 -0
  25. package/src/facades/Enum.ts +10 -0
  26. package/src/facades/Response.ts +15 -0
  27. package/src/facades/Router.ts +89 -0
  28. package/src/facades/SoftDeletes.ts +66 -0
  29. package/src/facades/Str.ts +7 -0
  30. package/src/index.ts +1 -0
  31. package/src/types/index.d.ts +2 -0
  32. package/{dist → src}/types/middleware.d.ts +1 -1
  33. package/src/utils/utils.ts +72 -0
  34. package/src/utils/vine.ts +2 -0
  35. package/src/utils/vines/exists.ts +41 -0
  36. package/src/utils/vines/unique.ts +41 -0
  37. package/tsconfig.json +17 -0
  38. package/types/index.d.ts +2 -0
  39. package/types/middleware.d.ts +7 -0
  40. package/types/router.d.ts +5 -0
  41. package/types/validator.d.ts +3 -0
  42. package/types/vine.d.ts +13 -0
  43. package/dist/index.d.ts +0 -9
  44. package/dist/index.js +0 -9
  45. package/dist/types/index.d.ts +0 -4
  46. /package/{dist/bases → bases}/BaseController.d.ts +0 -0
  47. /package/{dist/bases → bases}/BaseController.js +0 -0
  48. /package/{dist/bases → bases}/BaseModel.d.ts +0 -0
  49. /package/{dist/bases → bases}/BaseModel.js +0 -0
  50. /package/{dist/bases → bases}/BaseValidator.d.ts +0 -0
  51. /package/{dist/bases → bases}/BaseValidator.js +0 -0
  52. /package/{dist/bootstrap.d.ts → bootstrap.d.ts} +0 -0
  53. /package/{dist/bootstrap.js → bootstrap.js} +0 -0
  54. /package/{dist/builders → builders}/ChalkBuilder.d.ts +0 -0
  55. /package/{dist/builders → builders}/ChalkBuilder.js +0 -0
  56. /package/{dist/builders → builders}/EnumBuilder.d.ts +0 -0
  57. /package/{dist/builders → builders}/EnumBuilder.js +0 -0
  58. /package/{dist/builders → builders}/ResponseBuilder.d.ts +0 -0
  59. /package/{dist/builders → builders}/ResponseBuilder.js +0 -0
  60. /package/{dist/builders → builders}/RouterBuilder.js +0 -0
  61. /package/{dist/builders → builders}/StrBuilder.d.ts +0 -0
  62. /package/{dist/builders → builders}/StrBuilder.js +0 -0
  63. /package/{dist/config → config}/cors.d.ts +0 -0
  64. /package/{dist/config → config}/cors.js +0 -0
  65. /package/{dist/config → config}/database.d.ts +0 -0
  66. /package/{dist/config → config}/database.js +0 -0
  67. /package/{dist/enums → enums}/CorsHeaderEnum.d.ts +0 -0
  68. /package/{dist/enums → enums}/CorsHeaderEnum.js +0 -0
  69. /package/{dist/enums → enums}/HttpMethodEnum.d.ts +0 -0
  70. /package/{dist/enums → enums}/HttpMethodEnum.js +0 -0
  71. /package/{dist/exceptions → exceptions}/ModelNotFoundException.d.ts +0 -0
  72. /package/{dist/exceptions → exceptions}/ModelNotFoundException.js +0 -0
  73. /package/{dist/exceptions → exceptions}/RouterInvalidException.d.ts +0 -0
  74. /package/{dist/exceptions → exceptions}/RouterInvalidException.js +0 -0
  75. /package/{dist/exceptions → exceptions}/ValidatorException.d.ts +0 -0
  76. /package/{dist/exceptions → exceptions}/ValidatorException.js +0 -0
  77. /package/{dist/facades → facades}/Chalk.d.ts +0 -0
  78. /package/{dist/facades → facades}/Chalk.js +0 -0
  79. /package/{dist/facades → facades}/Enum.d.ts +0 -0
  80. /package/{dist/facades → facades}/Enum.js +0 -0
  81. /package/{dist/facades → facades}/Response.d.ts +0 -0
  82. /package/{dist/facades → facades}/Response.js +0 -0
  83. /package/{dist/facades → facades}/Router.js +0 -0
  84. /package/{dist/facades → facades}/SoftDeletes.d.ts +0 -0
  85. /package/{dist/facades → facades}/SoftDeletes.js +0 -0
  86. /package/{dist/facades → facades}/Str.d.ts +0 -0
  87. /package/{dist/facades → facades}/Str.js +0 -0
  88. /package/{dist → src}/types/router.d.ts +0 -0
  89. /package/{dist → src}/types/validator.d.ts +0 -0
  90. /package/{dist → src}/types/vine.d.ts +0 -0
  91. /package/{dist/utils → utils}/utils.d.ts +0 -0
  92. /package/{dist/utils → utils}/utils.js +0 -0
  93. /package/{dist/utils → utils}/vine.d.ts +0 -0
  94. /package/{dist/utils → utils}/vine.js +0 -0
  95. /package/{dist/utils → utils}/vines/exists.d.ts +0 -0
  96. /package/{dist/utils → utils}/vines/exists.js +0 -0
  97. /package/{dist/utils → utils}/vines/unique.d.ts +0 -0
  98. /package/{dist/utils → utils}/vines/unique.js +0 -0
@@ -0,0 +1,8 @@
1
+ import vine from "@vinejs/vine";
2
+ import "@/utils/vine";
3
+
4
+ export default class BaseValidator {
5
+ public static get validator(): typeof vine {
6
+ return vine;
7
+ }
8
+ }
@@ -0,0 +1,13 @@
1
+ import BaseModel from "@/bases/BaseModel";
2
+ import fs from "fs";
3
+ import knex from "knex";
4
+ import path from "path";
5
+
6
+ const configPath = path.resolve(process.cwd(), "config/database.ts");
7
+
8
+ let config: any;
9
+
10
+ if (fs.existsSync(configPath)) config = require(configPath).default;
11
+ else config = require("@/config/database").default;
12
+
13
+ BaseModel.knex(knex(config));
@@ -0,0 +1,60 @@
1
+ import chalk from "chalk";
2
+ import os from "os";
3
+
4
+ export default class ChalkBuilder {
5
+ protected value: string;
6
+ protected isNewLine: boolean;
7
+ protected clk: any;
8
+
9
+ public constructor() {
10
+ this.value = "";
11
+ this.isNewLine = true;
12
+ }
13
+
14
+ public setValue(value: string): ChalkBuilder {
15
+ this.value = value;
16
+
17
+ return this;
18
+ }
19
+
20
+ public inline(): ChalkBuilder {
21
+ this.isNewLine = false;
22
+
23
+ return this;
24
+ }
25
+
26
+ public bold(): ChalkBuilder {
27
+ if (this.clk) this.clk = this.clk.bold;
28
+ else this.clk = chalk.bold;
29
+
30
+ return this;
31
+ }
32
+
33
+ public danger(): ChalkBuilder {
34
+ if (this.clk) this.clk = this.clk.red;
35
+ else this.clk = chalk.red;
36
+
37
+ return this;
38
+ }
39
+
40
+ public info(): ChalkBuilder {
41
+ if (this.clk) this.clk = this.clk.hex("#0CCAF0");
42
+ else this.clk = chalk.hex("#0CCAF0");
43
+
44
+ return this;
45
+ }
46
+
47
+ public line(): void {
48
+ console.log("-".repeat(process.stdout.columns));
49
+ }
50
+
51
+ public show(): string {
52
+ return this.clk(this._value);
53
+ }
54
+
55
+ private get _value(): string {
56
+ if (this.isNewLine) return `${this.value}${os.EOL}`;
57
+
58
+ return this.value;
59
+ }
60
+ }
@@ -0,0 +1,32 @@
1
+ import {isEmpty} from "@/utils/utils";
2
+
3
+ export type EnumItem = {
4
+ name: string;
5
+ value: number;
6
+ }
7
+
8
+ export default class EnumBuilder {
9
+ protected enums: any;
10
+
11
+ public constructor(enums: any) {
12
+ if (isEmpty(enums)) throw new Error("The enum parameter is required.");
13
+ this.enums = enums;
14
+ }
15
+
16
+ public getName(value: number): string | undefined {
17
+ return Object.keys(this.enums).find(item => this.enums[item] === value);
18
+ }
19
+
20
+ public getValue(key: string): number {
21
+ return this.enums[key];
22
+ }
23
+
24
+ public toArray(): Array<any> {
25
+ return Object.keys(this.enums)
26
+ .filter(key => isNaN(Number(key)))
27
+ .map(key => ({
28
+ name: key,
29
+ value: this.getValue(key)
30
+ }));
31
+ }
32
+ }
@@ -0,0 +1,54 @@
1
+ import {cors} from "@/utils/utils";
2
+
3
+ export default class ResponseBuilder {
4
+ protected data?: any;
5
+ protected message: string;
6
+ protected status: number;
7
+
8
+ public constructor() {
9
+ this.data = null;
10
+ this.message = "Success";
11
+ this.status = 200;
12
+ }
13
+
14
+ public setData(data?: any): ResponseBuilder {
15
+ this.data = data;
16
+
17
+ return this;
18
+ }
19
+
20
+ public setMessage(message: string): ResponseBuilder {
21
+ this.message = message;
22
+
23
+ return this;
24
+ }
25
+
26
+ public setStatus(status: number): ResponseBuilder {
27
+ this.status = status;
28
+
29
+ return this;
30
+ }
31
+
32
+ public send(): globalThis.Response {
33
+ return globalThis.Response.json({
34
+ data: this.data,
35
+ message: this.message,
36
+ status: this.status
37
+ }, {
38
+ headers: {
39
+ ...cors()
40
+ },
41
+ status: this.status
42
+ });
43
+ }
44
+
45
+ public stream(options: ResponseInit = {}): globalThis.Response {
46
+ return new globalThis.Response(Bun.file(this.data), {
47
+ ...options,
48
+ headers: {
49
+ ...cors()
50
+ },
51
+ status: this.status
52
+ });
53
+ }
54
+ }
@@ -0,0 +1,173 @@
1
+ import type {IMiddleware} from "@/types/middleware";
2
+ import type {HandlerType, ResourceAction, RouterGroup} from "@/types/router";
3
+ import HttpMethodEnum from "@/enums/HttpMethodEnum";
4
+ import RouterInvalidException from "@/exceptions/RouterInvalidException";
5
+ import {isEmpty} from "@/utils/utils";
6
+ import path from "path";
7
+
8
+ export interface ResourceOptions {
9
+ only?: Array<ResourceAction>;
10
+ except?: Array<ResourceAction>;
11
+ }
12
+
13
+ export default class RouterBuilder {
14
+ private basePath: string = "";
15
+ private middlewares: Array<IMiddleware> = [];
16
+
17
+ public prefix(basePath: string): RouterBuilder {
18
+ this.basePath = basePath;
19
+
20
+ return this;
21
+ }
22
+
23
+ public middleware(...middlewares: Array<IMiddleware>): RouterBuilder {
24
+ this.middlewares.push(...middlewares);
25
+
26
+ return this;
27
+ }
28
+
29
+ public group(routes: RouterGroup | Array<RouterGroup>): RouterGroup {
30
+ const routeList = Array.isArray(routes) ? routes : [routes];
31
+ const newRoutes: RouterGroup = {};
32
+
33
+ for (const route of routeList) {
34
+ for (const path in route) {
35
+ const fullPath = this.joinPaths(this.basePath, path);
36
+ const routeHandlers = route[path];
37
+ const wrappedHandlers: Record<string, HandlerType> = {};
38
+
39
+ for (const method in routeHandlers) {
40
+ let handler = routeHandlers[method];
41
+
42
+ for (const middleware of this.middlewares) {
43
+ handler = middleware.handle(handler);
44
+ }
45
+
46
+ wrappedHandlers[method] = handler;
47
+ }
48
+
49
+ if (isEmpty(newRoutes[fullPath])) newRoutes[fullPath] = {};
50
+
51
+ Object.assign(newRoutes[fullPath], wrappedHandlers);
52
+ }
53
+ }
54
+
55
+ return newRoutes;
56
+ }
57
+
58
+ public resources(controller: Record<string, HandlerType>, options?: ResourceOptions): RouterGroup {
59
+ const allRoutes: Record<string, Record<string, ResourceAction>> = {
60
+ "": {
61
+ GET: "index",
62
+ POST: "store"
63
+ },
64
+ ":id": {
65
+ GET: "show",
66
+ PUT: "update",
67
+ DELETE: "destroy"
68
+ }
69
+ };
70
+
71
+ const includedActions = this.resolveIncludedActions(options);
72
+
73
+ const filteredRoutes: RouterGroup = {};
74
+
75
+ for (const path in allRoutes) {
76
+ const methods = allRoutes[path];
77
+ const methodHandlers: Record<string, HandlerType> = {};
78
+
79
+ for (const method in methods) {
80
+ const action = methods[method];
81
+ if (includedActions.has(action) && controller[action]) {
82
+ methodHandlers[method] = controller[action];
83
+ }
84
+ }
85
+
86
+ if (Object.keys(methodHandlers).length > 0) {
87
+ filteredRoutes[path] = methodHandlers;
88
+ }
89
+ }
90
+
91
+ return this.group(filteredRoutes);
92
+ }
93
+
94
+ public buildSingle(method: HttpMethodEnum, path: string, handler: string | HandlerType): RouterGroup {
95
+ const cleanPath = this.joinPaths(this.basePath, path);
96
+
97
+ let resolvedHandler: HandlerType = typeof handler === "string" ?
98
+ this.resolveControllerString(handler) :
99
+ handler;
100
+
101
+ for (const middleware of this.middlewares) {
102
+ resolvedHandler = middleware.handle(resolvedHandler);
103
+ }
104
+
105
+ return {
106
+ [cleanPath]: {
107
+ [method]: resolvedHandler
108
+ }
109
+ };
110
+ }
111
+
112
+ private joinPaths(base: string, path: string): string {
113
+ base = base.replace(/\/+$/, "");
114
+ path = path.replace(/^\/+/, "");
115
+
116
+ return "/" + [base, path].filter(Boolean).join("/");
117
+ }
118
+
119
+ private resolveControllerString(definition: string): HandlerType {
120
+ const [controllerName, methodName] = definition.split("@");
121
+
122
+ if (isEmpty(controllerName) || isEmpty(methodName)) {
123
+ throw new RouterInvalidException(`[RouterInvalidException]: Invalid router controller definition: ${definition}.`);
124
+ }
125
+
126
+ const controllerPath = path.resolve(process.cwd(), "app/controllers");
127
+ const location = `${controllerPath}/${controllerName}`;
128
+
129
+ let ControllerClass: any;
130
+
131
+ try {
132
+ ControllerClass = require(location).default;
133
+ } catch {
134
+ return async (...args: any[]) => {
135
+ const module = await import(location);
136
+ const ESMController = module.default;
137
+ const instance = new ESMController();
138
+
139
+ if (typeof instance[methodName] !== "function") {
140
+ throw new RouterInvalidException(`[RouterInvalidException]: Method "${methodName}" not found in ${controllerName}.`);
141
+ }
142
+
143
+ return instance[methodName](...args);
144
+ };
145
+ }
146
+
147
+ if (isEmpty(ControllerClass)) {
148
+ throw new RouterInvalidException(`[RouterInvalidException]: Controller not found: ${controllerName}.`);
149
+ }
150
+
151
+ const instance = new ControllerClass();
152
+
153
+ if (typeof instance[methodName] !== "function") {
154
+ throw new RouterInvalidException(`[RouterInvalidException]: Method "${methodName}" not found in ${controllerName}.`);
155
+ }
156
+
157
+ return instance[methodName].bind(instance);
158
+ }
159
+
160
+ private resolveIncludedActions(options?: ResourceOptions): Set<ResourceAction> {
161
+ const all: Array<ResourceAction> = ["index", "store", "show", "update", "destroy"];
162
+
163
+ if (options?.only) {
164
+ return new Set(options.only);
165
+ }
166
+
167
+ if (options?.except) {
168
+ return new Set(all.filter(action => !options.except!.includes(action)));
169
+ }
170
+
171
+ return new Set(all);
172
+ }
173
+ }
@@ -0,0 +1,25 @@
1
+ import {defineValue, isNotEmpty} from "@/utils/utils";
2
+
3
+ export default class StrBuilder {
4
+ protected value: string;
5
+
6
+ public constructor() {
7
+ this.value = "";
8
+ }
9
+
10
+ public setValue(value: string): StrBuilder {
11
+ this.value = value;
12
+
13
+ return this;
14
+ }
15
+
16
+ public toPascalCase(combine?: boolean): StrBuilder | string {
17
+ this.value = defineValue(this.value.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g), [])
18
+ .map((x: string) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
19
+ .join("");
20
+
21
+ if (isNotEmpty(combine)) return this;
22
+
23
+ return this.value;
24
+ }
25
+ }
@@ -0,0 +1,10 @@
1
+ const config: Record<string, any> = {
2
+ allowedHeaders: "*",
3
+ credentials: false,
4
+ exposedHeaders: [],
5
+ maxAge: 86400,
6
+ methods: "*",
7
+ origin: "*"
8
+ };
9
+
10
+ export default config;
@@ -0,0 +1,27 @@
1
+ import type {Knex} from "knex";
2
+
3
+ const config: Knex.Config = {
4
+ client: "pg",
5
+ connection: {
6
+ host: "127.0.0.1",
7
+ port: 5432,
8
+ user: "postgres",
9
+ password: "",
10
+ database: "bejibun"
11
+ },
12
+ migrations: {
13
+ extension: "ts",
14
+ directory: "./database/migrations",
15
+ tableName: "migrations"
16
+ },
17
+ pool: {
18
+ min: 0,
19
+ max: 1
20
+ },
21
+ seeds: {
22
+ extension: "ts",
23
+ directory: "./database/seeders"
24
+ }
25
+ };
26
+
27
+ export default config;
@@ -0,0 +1,9 @@
1
+ enum CorsHeaderEnum {
2
+ Accept = "Accept",
3
+ Authorization = "Authorization",
4
+ ContentType = "Content-Type",
5
+ Origin = "Origin",
6
+ XRequestedWith = "X-Requested-With"
7
+ }
8
+
9
+ export default CorsHeaderEnum;
@@ -0,0 +1,13 @@
1
+ enum HttpMethodEnum {
2
+ Connect = "CONNECT",
3
+ Delete = "DELETE",
4
+ Get = "GET",
5
+ Head = "HEAD",
6
+ Options = "OPTIONS",
7
+ Patch = "PATCH",
8
+ Post = "POST",
9
+ Put = "PUT",
10
+ Trace = "TRACE"
11
+ }
12
+
13
+ export default HttpMethodEnum;
@@ -0,0 +1,15 @@
1
+ import {defineValue} from "@/utils/utils";
2
+
3
+ export default class ModelNotFoundException extends Error {
4
+ public code: number;
5
+
6
+ public constructor(message?: string, code?: number) {
7
+ super(message);
8
+ this.name = "ModelNotFoundException";
9
+ this.code = defineValue(code, 404);
10
+
11
+ if (Error.captureStackTrace) {
12
+ Error.captureStackTrace(this, ModelNotFoundException);
13
+ }
14
+ }
15
+ }
@@ -0,0 +1,15 @@
1
+ import {defineValue} from "@/utils/utils";
2
+
3
+ export default class RouterInvalidException extends Error {
4
+ public code: number;
5
+
6
+ public constructor(message?: string, code?: number) {
7
+ super(message);
8
+ this.name = "RouterInvalidException";
9
+ this.code = defineValue(code, 500);
10
+
11
+ if (Error.captureStackTrace) {
12
+ Error.captureStackTrace(this, RouterInvalidException);
13
+ }
14
+ }
15
+ }
@@ -0,0 +1,15 @@
1
+ import {defineValue} from "@/utils/utils";
2
+
3
+ export default class ValidatorException extends Error {
4
+ public code: number;
5
+
6
+ public constructor(message?: string, code?: number) {
7
+ super(message);
8
+ this.name = "ValidatorException";
9
+ this.code = defineValue(code, 422);
10
+
11
+ if (Error.captureStackTrace) {
12
+ Error.captureStackTrace(this, ValidatorException);
13
+ }
14
+ }
15
+ }
@@ -0,0 +1,7 @@
1
+ import ChalkBuilder from "@/builders/ChalkBuilder";
2
+
3
+ export default class Chalk {
4
+ public static setValue(value: string): ChalkBuilder {
5
+ return new ChalkBuilder().setValue(value);
6
+ }
7
+ }
@@ -0,0 +1,10 @@
1
+ import EnumBuilder from "@/builders/EnumBuilder";
2
+ import type {EnumItem} from "@/builders/EnumBuilder";
3
+
4
+ export type {EnumItem};
5
+
6
+ export default class Enum {
7
+ public static setEnums(enums: any): EnumBuilder {
8
+ return new EnumBuilder(enums);
9
+ }
10
+ }
@@ -0,0 +1,15 @@
1
+ import ResponseBuilder from "@/builders/ResponseBuilder";
2
+
3
+ export default class Response {
4
+ public static setData(data?: any): ResponseBuilder {
5
+ return new ResponseBuilder().setData(data);
6
+ }
7
+
8
+ public static setMessage(message: string): ResponseBuilder {
9
+ return new ResponseBuilder().setMessage(message);
10
+ }
11
+
12
+ public static setStatus(status: number): ResponseBuilder {
13
+ return new ResponseBuilder().setStatus(status);
14
+ }
15
+ }
@@ -0,0 +1,89 @@
1
+ import type {IMiddleware} from "@/types/middleware";
2
+ import type {HandlerType, RouterGroup} from "@/types/router";
3
+ import RouterBuilder, {ResourceOptions} from "@/builders/RouterBuilder";
4
+ import HttpMethodEnum from "@/enums/HttpMethodEnum";
5
+ import Enum from "@/facades/Enum";
6
+ import {isEmpty} from "@/utils/utils";
7
+
8
+ export default class Router {
9
+ public static prefix(basePath: string): RouterBuilder {
10
+ return new RouterBuilder().prefix(basePath);
11
+ }
12
+
13
+ public static middleware(...middlewares: Array<IMiddleware>): RouterBuilder {
14
+ return new RouterBuilder().middleware(...middlewares);
15
+ }
16
+
17
+ public static resources(
18
+ controller: Record<string, HandlerType>,
19
+ options?: ResourceOptions
20
+ ): RouterGroup {
21
+ return new RouterBuilder().resources(controller, options);
22
+ }
23
+
24
+ public static group(routes: RouterGroup, prefix?: string, middlewares?: Array<IMiddleware>) {
25
+ const builder = new RouterBuilder();
26
+
27
+ if (prefix) builder.prefix(prefix);
28
+ if (middlewares?.length) builder.middleware(...middlewares);
29
+
30
+ return builder.group(routes);
31
+ }
32
+
33
+ public static connect(path: string, handler: string | HandlerType): RouterGroup {
34
+ return new RouterBuilder().buildSingle(HttpMethodEnum.Connect, path, handler);
35
+ }
36
+
37
+ public static delete(path: string, handler: string | HandlerType): RouterGroup {
38
+ return new RouterBuilder().buildSingle(HttpMethodEnum.Delete, path, handler);
39
+ }
40
+
41
+ public static get(path: string, handler: string | HandlerType): RouterGroup {
42
+ return new RouterBuilder().buildSingle(HttpMethodEnum.Get, path, handler);
43
+ }
44
+
45
+ public static head(path: string, handler: string | HandlerType): RouterGroup {
46
+ return new RouterBuilder().buildSingle(HttpMethodEnum.Head, path, handler);
47
+ }
48
+
49
+ public static options(path: string, handler: string | HandlerType): RouterGroup {
50
+ return new RouterBuilder().buildSingle(HttpMethodEnum.Options, path, handler);
51
+ }
52
+
53
+ public static patch(path: string, handler: string | HandlerType): RouterGroup {
54
+ return new RouterBuilder().buildSingle(HttpMethodEnum.Patch, path, handler);
55
+ }
56
+
57
+ public static post(path: string, handler: string | HandlerType): RouterGroup {
58
+ return new RouterBuilder().buildSingle(HttpMethodEnum.Post, path, handler);
59
+ }
60
+
61
+ public static put(path: string, handler: string | HandlerType): RouterGroup {
62
+ return new RouterBuilder().buildSingle(HttpMethodEnum.Put, path, handler);
63
+ }
64
+
65
+ public static trace(path: string, handler: string | HandlerType): RouterGroup {
66
+ return new RouterBuilder().buildSingle(HttpMethodEnum.Trace, path, handler);
67
+ }
68
+
69
+ public static match(methods: Array<HttpMethodEnum>, path: string, handler: string | HandlerType): RouterGroup {
70
+ const builder = new RouterBuilder();
71
+ const routeMap: RouterGroup = {};
72
+
73
+ for (const method of methods) {
74
+ const single = builder.buildSingle(method, path, handler);
75
+ const fullPath = Object.keys(single)[0];
76
+ const handlers = single[fullPath];
77
+
78
+ if (isEmpty(routeMap[fullPath])) routeMap[fullPath] = {};
79
+
80
+ Object.assign(routeMap[fullPath], handlers);
81
+ }
82
+
83
+ return routeMap;
84
+ }
85
+
86
+ public static any(path: string, handler: string | HandlerType): RouterGroup {
87
+ return this.match(Enum.setEnums(HttpMethodEnum).toArray(), path, handler);
88
+ }
89
+ }
@@ -0,0 +1,66 @@
1
+ import {DateTime} from "luxon";
2
+ import {QueryBuilder, ModelClass, QueryContext, Model} from "objection";
3
+
4
+ interface SoftDeleteQueryContext extends QueryContext {
5
+ withTrashed?: boolean;
6
+ onlyTrashed?: boolean;
7
+ }
8
+
9
+ export default class SoftDeletes<M extends Model, R = M[]> extends QueryBuilder<M, R> {
10
+ private hasFilterApplied = false;
11
+
12
+ constructor(modelClass: ModelClass<M>) {
13
+ // @ts-ignore
14
+ super(modelClass);
15
+
16
+ this.onBuild((builder: QueryBuilder<M, R>) => {
17
+ const context = this.context() as SoftDeleteQueryContext;
18
+
19
+ if (!this.hasFilterApplied) {
20
+ const tableName = this.modelClass().tableName;
21
+
22
+ if (context.onlyTrashed) {
23
+ builder.whereNotNull(`${tableName}.${(this.modelClass() as any).deletedColumn}`);
24
+ } else if (!context.withTrashed) {
25
+ builder.whereNull(`${tableName}.${(this.modelClass() as any).deletedColumn}`);
26
+ }
27
+
28
+ this.hasFilterApplied = true;
29
+ }
30
+ });
31
+ }
32
+
33
+ withTrashed(): this {
34
+ return (this as any).context({
35
+ ...(this as any).context(),
36
+ withTrashed: true
37
+ });
38
+ }
39
+
40
+ onlyTrashed(): this {
41
+ return (this as any).context({
42
+ ...(this as any).context(),
43
+ onlyTrashed: true
44
+ });
45
+ }
46
+
47
+ delete(): QueryBuilder<M, number> {
48
+ return this.update({
49
+ [(this.modelClass() as any).deletedColumn]: DateTime.now()
50
+ } as any);
51
+ }
52
+
53
+ del(): QueryBuilder<M, number> {
54
+ return this.delete();
55
+ }
56
+
57
+ forceDelete(): QueryBuilder<M, number> {
58
+ return super.delete();
59
+ }
60
+
61
+ restore(): QueryBuilder<M, number> {
62
+ return this.onlyTrashed().update({
63
+ [(this.modelClass() as any).deletedColumn]: null
64
+ } as any);
65
+ }
66
+ }
@@ -0,0 +1,7 @@
1
+ import StrBuilder from "@/builders/StrBuilder";
2
+
3
+ export default class Str {
4
+ public static toPascalCase(value: string, combine?: boolean): StrBuilder | string {
5
+ return new StrBuilder().setValue(value).toPascalCase(combine);
6
+ }
7
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "@/utils/utils";