pangea-server 1.0.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.
Files changed (150) hide show
  1. package/README.md +1 -0
  2. package/dist/authentication/access-token.class.d.ts +13 -0
  3. package/dist/authentication/access-token.class.js +28 -0
  4. package/dist/authentication/authentication.helpers.d.ts +17 -0
  5. package/dist/authentication/authentication.helpers.js +77 -0
  6. package/dist/authentication/authentication.types.d.ts +11 -0
  7. package/dist/authentication/authentication.types.js +2 -0
  8. package/dist/authentication/base-auth.class.d.ts +9 -0
  9. package/dist/authentication/base-auth.class.js +29 -0
  10. package/dist/authentication/index.d.ts +4 -0
  11. package/dist/authentication/index.js +20 -0
  12. package/dist/database/base-service.class.d.ts +15 -0
  13. package/dist/database/base-service.class.js +28 -0
  14. package/dist/database/database.helpers.d.ts +4 -0
  15. package/dist/database/database.helpers.js +19 -0
  16. package/dist/database/database.types.d.ts +59 -0
  17. package/dist/database/database.types.js +2 -0
  18. package/dist/database/db-client.d.ts +11 -0
  19. package/dist/database/db-client.js +69 -0
  20. package/dist/database/db.class.d.ts +64 -0
  21. package/dist/database/db.class.js +270 -0
  22. package/dist/database/decorators/columns/column.d.ts +10 -0
  23. package/dist/database/decorators/columns/column.js +47 -0
  24. package/dist/database/decorators/columns/date.columns.d.ts +4 -0
  25. package/dist/database/decorators/columns/date.columns.js +16 -0
  26. package/dist/database/decorators/columns/general.columns.d.ts +4 -0
  27. package/dist/database/decorators/columns/general.columns.js +54 -0
  28. package/dist/database/decorators/columns/json.columns.d.ts +3 -0
  29. package/dist/database/decorators/columns/json.columns.js +22 -0
  30. package/dist/database/decorators/columns/number.columns.d.ts +10 -0
  31. package/dist/database/decorators/columns/number.columns.js +33 -0
  32. package/dist/database/decorators/columns/string.columns.d.ts +10 -0
  33. package/dist/database/decorators/columns/string.columns.js +55 -0
  34. package/dist/database/decorators/index.d.ts +23 -0
  35. package/dist/database/decorators/index.js +50 -0
  36. package/dist/database/decorators/relations.d.ts +12 -0
  37. package/dist/database/decorators/relations.js +42 -0
  38. package/dist/database/decorators/table.d.ts +6 -0
  39. package/dist/database/decorators/table.js +57 -0
  40. package/dist/database/index.d.ts +8 -0
  41. package/dist/database/index.js +26 -0
  42. package/dist/database/init-database.d.ts +11 -0
  43. package/dist/database/init-database.js +69 -0
  44. package/dist/database/model-decorators/column.d.ts +11 -0
  45. package/dist/database/model-decorators/column.js +51 -0
  46. package/dist/database/model-decorators/date-columns.d.ts +6 -0
  47. package/dist/database/model-decorators/date-columns.js +41 -0
  48. package/dist/database/model-decorators/decorators.types.d.ts +20 -0
  49. package/dist/database/model-decorators/decorators.types.js +2 -0
  50. package/dist/database/model-decorators/general-columns.d.ts +4 -0
  51. package/dist/database/model-decorators/general-columns.js +54 -0
  52. package/dist/database/model-decorators/index.d.ts +23 -0
  53. package/dist/database/model-decorators/index.js +50 -0
  54. package/dist/database/model-decorators/json-columns.d.ts +3 -0
  55. package/dist/database/model-decorators/json-columns.js +46 -0
  56. package/dist/database/model-decorators/number-columns.d.ts +10 -0
  57. package/dist/database/model-decorators/number-columns.js +58 -0
  58. package/dist/database/model-decorators/relations.d.ts +12 -0
  59. package/dist/database/model-decorators/relations.js +42 -0
  60. package/dist/database/model-decorators/string-columns.d.ts +9 -0
  61. package/dist/database/model-decorators/string-columns.js +51 -0
  62. package/dist/database/model-decorators/table.d.ts +6 -0
  63. package/dist/database/model-decorators/table.js +57 -0
  64. package/dist/database/models/base-model.class.d.ts +33 -0
  65. package/dist/database/models/base-model.class.js +76 -0
  66. package/dist/database/models/index.d.ts +2 -0
  67. package/dist/database/models/index.js +18 -0
  68. package/dist/database/models/user.model.d.ts +5 -0
  69. package/dist/database/models/user.model.js +25 -0
  70. package/dist/database/seed.helpers.d.ts +4 -0
  71. package/dist/database/seed.helpers.js +44 -0
  72. package/dist/helpers/console.helpers.d.ts +8 -0
  73. package/dist/helpers/console.helpers.js +29 -0
  74. package/dist/helpers/controllers.helpers.d.ts +2 -0
  75. package/dist/helpers/controllers.helpers.js +6 -0
  76. package/dist/helpers/env.helpers.d.ts +4 -0
  77. package/dist/helpers/env.helpers.js +32 -0
  78. package/dist/helpers/error.helpers.d.ts +20 -0
  79. package/dist/helpers/error.helpers.js +29 -0
  80. package/dist/helpers/file.helpers.d.ts +4 -0
  81. package/dist/helpers/file.helpers.js +78 -0
  82. package/dist/helpers/hashing.helpers.d.ts +2 -0
  83. package/dist/helpers/hashing.helpers.js +16 -0
  84. package/dist/helpers/html-sanitize.helpers.d.ts +4 -0
  85. package/dist/helpers/html-sanitize.helpers.js +30 -0
  86. package/dist/helpers/index.d.ts +11 -0
  87. package/dist/helpers/index.js +27 -0
  88. package/dist/helpers/job.helpers.d.ts +8 -0
  89. package/dist/helpers/job.helpers.js +20 -0
  90. package/dist/helpers/mailer.helpers.d.ts +33 -0
  91. package/dist/helpers/mailer.helpers.js +34 -0
  92. package/dist/helpers/multer.helpers.d.ts +2 -0
  93. package/dist/helpers/multer.helpers.js +52 -0
  94. package/dist/helpers/print.helpers.d.ts +8 -0
  95. package/dist/helpers/print.helpers.js +29 -0
  96. package/dist/helpers/random.helpers.d.ts +4 -0
  97. package/dist/helpers/random.helpers.js +27 -0
  98. package/dist/index.d.ts +8 -0
  99. package/dist/index.js +26 -0
  100. package/dist/main.d.ts +13 -0
  101. package/dist/main.js +65 -0
  102. package/dist/middlewares/delay-request.middleware.d.ts +1 -0
  103. package/dist/middlewares/delay-request.middleware.js +16 -0
  104. package/dist/middlewares/handle-error.middleware.d.ts +1 -0
  105. package/dist/middlewares/handle-error.middleware.js +13 -0
  106. package/dist/resources/color.resources.d.ts +18 -0
  107. package/dist/resources/color.resources.js +20 -0
  108. package/dist/resources/index.d.ts +2 -0
  109. package/dist/resources/index.js +18 -0
  110. package/dist/resources/refs.d.ts +32 -0
  111. package/dist/resources/refs.js +35 -0
  112. package/dist/resources/string.resources.d.ts +15 -0
  113. package/dist/resources/string.resources.js +16 -0
  114. package/dist/router/app-router.class.d.ts +18 -0
  115. package/dist/router/app-router.class.js +34 -0
  116. package/dist/router/call-controller.d.ts +5 -0
  117. package/dist/router/call-controller.js +47 -0
  118. package/dist/router/index.d.ts +1 -0
  119. package/dist/router/index.js +17 -0
  120. package/dist/router/router.types.d.ts +15 -0
  121. package/dist/router/router.types.js +2 -0
  122. package/dist/types/error.types.d.ts +4 -0
  123. package/dist/types/error.types.js +2 -0
  124. package/dist/types/global.types.d.ts +22 -0
  125. package/dist/types/global.types.js +2 -0
  126. package/dist/types/index.d.ts +2 -0
  127. package/dist/types/index.js +4 -0
  128. package/dist/validations/general-schemas.d.ts +20 -0
  129. package/dist/validations/general-schemas.js +15 -0
  130. package/dist/validations/index.d.ts +3 -0
  131. package/dist/validations/index.js +19 -0
  132. package/dist/validations/validate-request.middleware.d.ts +2 -0
  133. package/dist/validations/validate-request.middleware.js +33 -0
  134. package/dist/validations/validation-locations.d.ts +1 -0
  135. package/dist/validations/validation-locations.js +4 -0
  136. package/dist/validations/validations.types.d.ts +23 -0
  137. package/dist/validations/validations.types.js +21 -0
  138. package/dist/validator/general-schemas.d.ts +1 -0
  139. package/dist/validator/general-schemas.js +25 -0
  140. package/dist/validator/index.d.ts +1 -0
  141. package/dist/validator/index.js +17 -0
  142. package/dist/validator/tags.d.ts +1 -0
  143. package/dist/validator/tags.js +5 -0
  144. package/dist/validator/validate-request.d.ts +2 -0
  145. package/dist/validator/validate-request.js +71 -0
  146. package/dist/validator/validator.types.d.ts +6 -0
  147. package/dist/validator/validator.types.js +2 -0
  148. package/dist/validator/validators.helpers.d.ts +38 -0
  149. package/dist/validator/validators.helpers.js +8 -0
  150. package/package.json +78 -0
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAppRouter = void 0;
4
+ const express_1 = require("express");
5
+ const call_controller_1 = require("./call-controller");
6
+ function getAppRouter(authConfig) {
7
+ return class AppRouter {
8
+ constructor(app, basePath) {
9
+ this.router = (0, express_1.Router)();
10
+ app.use(basePath, this.router);
11
+ }
12
+ setRoute(method, path, validate, ...args) {
13
+ const middlewares = args.slice(0, -1);
14
+ const controller = args[args.length - 1];
15
+ this.router[method](path, ...middlewares, (0, call_controller_1.callController)(controller, validate, authConfig));
16
+ }
17
+ get(path, validate, ...args) {
18
+ this.setRoute('get', path, validate, ...args);
19
+ }
20
+ post(path, validate, ...args) {
21
+ this.setRoute('post', path, validate, ...args);
22
+ }
23
+ put(path, validate, ...args) {
24
+ this.setRoute('put', path, validate, ...args);
25
+ }
26
+ patch(path, validate, ...args) {
27
+ this.setRoute('patch', path, validate, ...args);
28
+ }
29
+ delete(path, validate, ...args) {
30
+ this.setRoute('delete', path, validate, ...args);
31
+ }
32
+ };
33
+ }
34
+ exports.getAppRouter = getAppRouter;
@@ -0,0 +1,5 @@
1
+ import { BaseAuth } from '../authentication';
2
+ import type { AuthMap, AuthUsers } from '../authentication/authentication.types';
3
+ import type { RouteValidator, RouteValidate } from '../validator/validator.types';
4
+ import type { Controller, AuthConfig } from './router.types';
5
+ export declare function callController<V extends RouteValidator, AM extends AuthMap, AU extends AuthUsers<AM>, BA extends BaseAuth<AM>>(controller: Controller<V, AM, BA>, validate: RouteValidate<V>, authConfig: AuthConfig<AM, AU, BA>): (req: Req, res: Res) => Promise<void>;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.callController = void 0;
4
+ const http_status_codes_1 = require("http-status-codes");
5
+ // helpers
6
+ const pangea_helpers_1 = require("pangea-helpers");
7
+ // database
8
+ const database_1 = require("../database");
9
+ // authentication
10
+ const authentication_1 = require("../authentication");
11
+ // validator
12
+ const validate_request_1 = require("../validator/validate-request");
13
+ function callController(controller, validate, authConfig) {
14
+ return async function (req, res) {
15
+ const inputs = (0, validate_request_1.validateRequest)(validate, req);
16
+ const ctx = { headers: req.headers, file: req.file, files: req.files, ...inputs };
17
+ const tx = await (0, database_1.getDbClient)().transaction();
18
+ try {
19
+ const { authMap, authCtor, accessToken } = authConfig;
20
+ const authUsers = await (0, authentication_1.getUsersFromToken)(req.headers.authorization, tx, accessToken, authMap);
21
+ const auth = new authCtor(authUsers);
22
+ const result = await controller(ctx, tx, auth);
23
+ await tx.commit();
24
+ let data = result;
25
+ let totalCount;
26
+ if (hasInstancesAndTotalCount(result)) {
27
+ data = result.instances;
28
+ totalCount = result.totalCount;
29
+ }
30
+ const jsonBody = {
31
+ data: data || null,
32
+ totalCount: typeof totalCount === 'number' ? totalCount : null,
33
+ successCode: (0, pangea_helpers_1.camelToUpperSnake)(controller.name),
34
+ };
35
+ res.status(http_status_codes_1.StatusCodes.OK).json(jsonBody);
36
+ }
37
+ catch (err) {
38
+ await tx.rollback();
39
+ throw err;
40
+ }
41
+ };
42
+ }
43
+ exports.callController = callController;
44
+ // internal functions
45
+ function hasInstancesAndTotalCount(obj) {
46
+ return typeof obj === 'object' && obj !== null && 'instances' in obj && 'totalCount' in obj;
47
+ }
@@ -0,0 +1 @@
1
+ export * from './app-router.class';
@@ -0,0 +1,17 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./app-router.class"), exports);
@@ -0,0 +1,15 @@
1
+ import { BaseAuth, AccessToken } from '../authentication';
2
+ import type { AuthMap, AuthUsers, AuthCtor } from '../authentication/authentication.types';
3
+ import type { RouteValidator } from '../validator/validator.types';
4
+ export type Middleware = (req: Req, res: Res, next: Next) => void;
5
+ export type Controller<V extends RouteValidator, AM extends AuthMap, BA extends BaseAuth<AM>> = (ctx: Ctx<V>, tx: Tx, auth: BA) => unknown;
6
+ export type SetRouteArgs<V extends RouteValidator, AM extends AuthMap, BA extends BaseAuth<AM>> = [
7
+ ...Middleware[],
8
+ Controller<V, AM, BA>
9
+ ];
10
+ export type AuthConfig<AM extends AuthMap, AU extends AuthUsers<AM>, BA extends BaseAuth<AM>> = {
11
+ authMap: AM;
12
+ authCtor: AuthCtor<AM, AU, BA>;
13
+ accessToken: AccessToken;
14
+ };
15
+ export type SetRoutes = (app: App) => void;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ import { StatusCodes } from 'http-status-codes';
2
+ export type StatusCodeName = keyof typeof StatusCodes;
3
+ export type StatusCode = (typeof StatusCodes)[StatusCodeName];
4
+ export type ErrorCode = string;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,22 @@
1
+ /// <reference types="multer" />
2
+ import type { Express, Request, Response, NextFunction } from 'express';
3
+ import type { RouteValidator } from '../validator/validator.types';
4
+ declare global {
5
+ type App = Express;
6
+ type Req = Request;
7
+ type Res = Response;
8
+ type Next = NextFunction;
9
+ type ReqHeaders = Req['headers'] & {
10
+ language: string;
11
+ };
12
+ interface Ctx<V extends RouteValidator> {
13
+ headers: ReqHeaders;
14
+ params: V['params'];
15
+ query: V['query'];
16
+ body: V['body'];
17
+ file?: MulterFile;
18
+ files?: MulterFile[];
19
+ }
20
+ type MulterFile = Express.Multer.File;
21
+ type UploadableImage = string | MulterFile;
22
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import './error.types';
2
+ import './global.types';
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("./error.types");
4
+ require("./global.types");
@@ -0,0 +1,20 @@
1
+ export type IdSchema = ModelId;
2
+ export declare const idSchema: import("joi").NumberSchema<number>;
3
+ export type IdSchemaOptional = IdSchema | 0;
4
+ export declare const idSchemaOptional: import("joi").NumberSchema<number>;
5
+ export type ArrayOfIdsSchema = IdSchema[];
6
+ export declare const arrayOfIdsSchema: import("joi").ArraySchema<any[]>;
7
+ export interface ParamsIdSchema {
8
+ id: IdSchema;
9
+ }
10
+ export declare const paramsIdSchema: import("joi").ObjectSchema<any>;
11
+ export interface QueryGetManySchema {
12
+ page?: number | null;
13
+ pageSize?: number | null;
14
+ search?: string | null;
15
+ }
16
+ export declare const queryGetManySchemaObject: {
17
+ page: import("joi").NumberSchema<number>;
18
+ pageSize: import("joi").NumberSchema<number>;
19
+ search: import("joi").StringSchema<string>;
20
+ };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.queryGetManySchemaObject = exports.paramsIdSchema = exports.arrayOfIdsSchema = exports.idSchemaOptional = exports.idSchema = void 0;
4
+ const express_validation_1 = require("express-validation");
5
+ exports.idSchema = express_validation_1.Joi.number().integer().min(1);
6
+ exports.idSchemaOptional = exports.idSchema.allow(0);
7
+ exports.arrayOfIdsSchema = express_validation_1.Joi.array().items(exports.idSchema);
8
+ exports.paramsIdSchema = express_validation_1.Joi.object({
9
+ id: exports.idSchema.required(),
10
+ });
11
+ exports.queryGetManySchemaObject = {
12
+ page: express_validation_1.Joi.number().integer().min(0).allow(null),
13
+ pageSize: express_validation_1.Joi.number().integer().min(0).allow(null),
14
+ search: express_validation_1.Joi.string().allow('').allow(null),
15
+ };
@@ -0,0 +1,3 @@
1
+ export * from './general-schemas';
2
+ export * from './validate-request.middleware';
3
+ export * from './validations.types';
@@ -0,0 +1,19 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./general-schemas"), exports);
18
+ __exportStar(require("./validate-request.middleware"), exports);
19
+ __exportStar(require("./validations.types"), exports);
@@ -0,0 +1,2 @@
1
+ import type { ValidationRoute } from './validations.types';
2
+ export declare function validateRequest(validation: ValidationRoute | null): (req: ExpressRequest, res: ExpressResponse, next: ExpressNext) => Promise<void>;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateRequest = void 0;
4
+ const express_validation_1 = require("express-validation");
5
+ const validation_locations_1 = require("./validation-locations");
6
+ // helpers
7
+ const helpers_1 = require("pangea-backend/helpers");
8
+ function validateRequest(validation) {
9
+ return async function (req, res, next) {
10
+ if (!validation) {
11
+ (0, helpers_1.printWarning)('validation', 'no data validation');
12
+ removeUnvalidatedData(req);
13
+ next();
14
+ return;
15
+ }
16
+ const evOptions = { context: true, keyByField: true };
17
+ const validationOptions = { allowUnknown: true, stripUnknown: true, convert: true, dateFormat: 'iso' };
18
+ await (0, express_validation_1.validate)(validation, evOptions, validationOptions)(req, res, (err) => {
19
+ if (err)
20
+ helpers_1.AppError.Throw({ statusCodeName: 'BAD_REQUEST', errorCode: 'INVALID_INPUT_DATA', err });
21
+ removeUnvalidatedData(req, validation);
22
+ next();
23
+ });
24
+ };
25
+ }
26
+ exports.validateRequest = validateRequest;
27
+ // internal functions
28
+ function removeUnvalidatedData(req, validation) {
29
+ for (const location of validation_locations_1.validationLocations) {
30
+ if (!validation || !validation[location])
31
+ req[location] = {};
32
+ }
33
+ }
@@ -0,0 +1 @@
1
+ export declare const validationLocations: readonly ["params", "query", "body"];
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validationLocations = void 0;
4
+ exports.validationLocations = ['params', 'query', 'body'];
@@ -0,0 +1,23 @@
1
+ import Joi from 'joi';
2
+ import { validationLocations } from './validation-locations';
3
+ import type { ParamsIdSchema, QueryGetManySchema } from './general-schemas';
4
+ import type { PlainObject, EmptyObject } from 'pangea-helpers';
5
+ export type ValidationLocation = (typeof validationLocations)[number];
6
+ export type ValidationRoute = {
7
+ [key in ValidationLocation]?: Joi.ObjectSchema;
8
+ };
9
+ export declare class ValidationRouteType<P extends PlainObject = EmptyObject, Q extends PlainObject = EmptyObject, B extends PlainObject = EmptyObject> {
10
+ params: P;
11
+ query: Q;
12
+ body: B;
13
+ }
14
+ export declare class ValidationRouteTypeGetOne extends ValidationRouteType<ParamsIdSchema, EmptyObject, EmptyObject> {
15
+ }
16
+ export declare class ValidationRouteTypeGetMany<Q extends PlainObject = EmptyObject> extends ValidationRouteType<EmptyObject, QueryGetManySchema & Q, EmptyObject> {
17
+ }
18
+ export declare class ValidationRouteTypePostOne<B extends PlainObject = PlainObject> extends ValidationRouteType<EmptyObject, EmptyObject, B> {
19
+ }
20
+ export declare class ValidationRouteTypePutOne<B extends PlainObject = PlainObject> extends ValidationRouteType<ParamsIdSchema, EmptyObject, B> {
21
+ }
22
+ export declare class ValidationRouteTypeDeleteOne extends ValidationRouteType<ParamsIdSchema, EmptyObject, EmptyObject> {
23
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidationRouteTypeDeleteOne = exports.ValidationRouteTypePutOne = exports.ValidationRouteTypePostOne = exports.ValidationRouteTypeGetMany = exports.ValidationRouteTypeGetOne = exports.ValidationRouteType = void 0;
4
+ class ValidationRouteType {
5
+ }
6
+ exports.ValidationRouteType = ValidationRouteType;
7
+ class ValidationRouteTypeGetOne extends ValidationRouteType {
8
+ }
9
+ exports.ValidationRouteTypeGetOne = ValidationRouteTypeGetOne;
10
+ class ValidationRouteTypeGetMany extends ValidationRouteType {
11
+ }
12
+ exports.ValidationRouteTypeGetMany = ValidationRouteTypeGetMany;
13
+ class ValidationRouteTypePostOne extends ValidationRouteType {
14
+ }
15
+ exports.ValidationRouteTypePostOne = ValidationRouteTypePostOne;
16
+ class ValidationRouteTypePutOne extends ValidationRouteType {
17
+ }
18
+ exports.ValidationRouteTypePutOne = ValidationRouteTypePutOne;
19
+ class ValidationRouteTypeDeleteOne extends ValidationRouteType {
20
+ }
21
+ exports.ValidationRouteTypeDeleteOne = ValidationRouteTypeDeleteOne;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ // import { Joi } from 'express-validation';
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ // export type IdSchema = ModelId;
5
+ // export const idSchema = Joi.number().integer().min(1);
6
+ // export type IdSchemaOptional = IdSchema | 0;
7
+ // export const idSchemaOptional = idSchema.allow(0);
8
+ // export type ArrayOfIdsSchema = IdSchema[];
9
+ // export const arrayOfIdsSchema = Joi.array().items(idSchema);
10
+ // export interface ParamsIdSchema {
11
+ // id: IdSchema;
12
+ // }
13
+ // export const paramsIdSchema = Joi.object({
14
+ // id: idSchema.required(),
15
+ // });
16
+ // export interface QueryGetManySchema {
17
+ // page?: number | null;
18
+ // pageSize?: number | null;
19
+ // search?: string | null;
20
+ // }
21
+ // export const queryGetManySchemaObject = {
22
+ // page: Joi.number().integer().min(0).allow(null),
23
+ // pageSize: Joi.number().integer().min(0).allow(null),
24
+ // search: Joi.string().allow('').allow(null),
25
+ // };
@@ -0,0 +1 @@
1
+ export * from './validators.helpers';
@@ -0,0 +1,17 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./validators.helpers"), exports);
@@ -0,0 +1 @@
1
+ export { tags as val } from 'typia';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.val = void 0;
4
+ var typia_1 = require("typia");
5
+ Object.defineProperty(exports, "val", { enumerable: true, get: function () { return typia_1.tags; } });
@@ -0,0 +1,2 @@
1
+ import type { RouteValidator, RouteValidate } from './validator.types';
2
+ export declare function validateRequest<T extends RouteValidator>(validate: RouteValidate<T>, req: Req): T;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateRequest = void 0;
4
+ // helpers
5
+ const helpers_1 = require("../helpers");
6
+ const pangea_helpers_1 = require("pangea-helpers");
7
+ function validateRequest(validate, req) {
8
+ try {
9
+ const input = {
10
+ params: normalizeInput('params', req.params),
11
+ query: normalizeInput('query', req.query),
12
+ body: normalizeInput('body', req.body),
13
+ };
14
+ return validate(input);
15
+ }
16
+ catch (err) {
17
+ helpers_1.AppError.Throw({ statusCodeName: 'BAD_REQUEST', errorCode: 'INVALID_INPUT_DATA', err: err });
18
+ }
19
+ }
20
+ exports.validateRequest = validateRequest;
21
+ // internal functions
22
+ function normalizeInput(type, obj) {
23
+ if (!Object.keys(obj).length)
24
+ return undefined;
25
+ return normalizeObj(type, obj);
26
+ }
27
+ function normalizeObj(type, obj) {
28
+ const result = {};
29
+ for (const [k, v] of Object.entries(obj)) {
30
+ const finalKey = (0, pangea_helpers_1.snakeToCamel)(k);
31
+ if (type === 'query' && finalKey === 'search') {
32
+ result[finalKey] = v;
33
+ continue;
34
+ }
35
+ if (Array.isArray(v)) {
36
+ result[finalKey] = v.map((item) => {
37
+ return item && typeof item === 'object' ? normalizeObj(type, item) : normalizeVal(type, finalKey, item);
38
+ });
39
+ }
40
+ else if (v && typeof v === 'object') {
41
+ result[finalKey] = normalizeObj(type, v);
42
+ }
43
+ else {
44
+ result[finalKey] = normalizeVal(type, finalKey, v);
45
+ }
46
+ }
47
+ return result;
48
+ }
49
+ function normalizeVal(type, key, val) {
50
+ if (typeof val !== 'string')
51
+ return val;
52
+ const finalVal = val.trim();
53
+ if ((0, pangea_helpers_1.isDatetime)(finalVal))
54
+ return new Date(finalVal);
55
+ if (key === 'search' && type === 'query')
56
+ return finalVal;
57
+ if (type === 'body')
58
+ return finalVal;
59
+ if (finalVal !== '' && !isNaN(Number(finalVal)))
60
+ return Number(finalVal);
61
+ const valInLowerCase = finalVal.toLowerCase();
62
+ if (valInLowerCase === 'true')
63
+ return true;
64
+ if (valInLowerCase === 'false')
65
+ return false;
66
+ if (valInLowerCase === 'null')
67
+ return null;
68
+ if (valInLowerCase === 'undefined')
69
+ return undefined;
70
+ return finalVal;
71
+ }
@@ -0,0 +1,6 @@
1
+ export type RouteValidator = {
2
+ params?: unknown;
3
+ query?: unknown;
4
+ body?: unknown;
5
+ };
6
+ export type RouteValidate<T extends RouteValidator> = (input: T) => T;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,38 @@
1
+ import typia, { tags } from 'typia';
2
+ import { STRING_LENGTHS, type StringLength } from '../resources';
3
+ export declare const createValidate: typeof typia.misc.createAssertPrune;
4
+ type StrLenValue<T extends StringLength | number> = T extends StringLength ? (typeof STRING_LENGTHS)[T] : T;
5
+ export declare namespace Val {
6
+ type Str = string & tags.Pattern<'.+'>;
7
+ type MinLen<T extends StringLength | number> = Val.Str & tags.MinLength<StrLenValue<T>>;
8
+ type MaxLen<T extends StringLength | number> = Val.Str & tags.MaxLength<StrLenValue<T>>;
9
+ type Email = Val.Str & tags.Format<'email'>;
10
+ type Regex<T extends string> = string & tags.Pattern<T>;
11
+ type Num = number;
12
+ type Int = Val.Num & tags.Type<'int32'>;
13
+ type Greater<T extends number = 0> = Val.Num & tags.Minimum<T>;
14
+ type GreaterThan<T extends number = 0> = Val.Num & tags.ExclusiveMinimum<T>;
15
+ type Less<T extends number = 0> = Val.Num & tags.Maximum<T>;
16
+ type LessThan<T extends number = 0> = Val.Num & tags.ExclusiveMaximum<T>;
17
+ type Bool = boolean;
18
+ type Datetime = globalThis.Date;
19
+ type Date = string & tags.Format<'date'>;
20
+ type Time = string & tags.Format<'time'>;
21
+ type Id = Val.Int & Val.Greater<1>;
22
+ type IdOptional = Val.Id | 0;
23
+ type Ids = Val.Id[];
24
+ interface IdParams {
25
+ id: Val.Id;
26
+ }
27
+ interface GetManyQueryBase {
28
+ page?: (Val.Int & Val.Greater<0>) | null;
29
+ pageSize?: (Val.Int & Val.Greater<0>) | null;
30
+ search?: Val.Str | '' | null;
31
+ }
32
+ }
33
+ export interface BaseValidator {
34
+ IdParamsRoute: {
35
+ params: Val.IdParams;
36
+ };
37
+ }
38
+ export {};
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createValidate = void 0;
7
+ const typia_1 = __importDefault(require("typia"));
8
+ exports.createValidate = typia_1.default.misc.createAssertPrune;
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "pangea-server",
3
+ "description": "",
4
+ "version": "1.0.1",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "exports": {
9
+ ".": {
10
+ "require": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ },
13
+ "./helpers": {
14
+ "require": "./dist/helpers/index.js",
15
+ "types": "./dist/helpers/index.d.ts"
16
+ }
17
+ },
18
+ "scripts": {
19
+ "build": "tsc && tsc-alias",
20
+ "deploy": "node deploy.js",
21
+ "type-check": "tsc --noEmit"
22
+ },
23
+ "keywords": [],
24
+ "author": "Gianfranco Raselli",
25
+ "license": "ISC",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/GianfrancoRaselli/pangea-server.git"
29
+ },
30
+ "bugs": {
31
+ "url": "https://github.com/GianfrancoRaselli/pangea-server/issues"
32
+ },
33
+ "homepage": "https://github.com/GianfrancoRaselli/pangea-server#readme",
34
+ "dependencies": {
35
+ "aws-sdk": "2.1692.0",
36
+ "bcrypt": "5.1.1",
37
+ "compression": "1.7.5",
38
+ "cors": "2.8.5",
39
+ "crypto": "1.0.1",
40
+ "dompurify": "3.2.3",
41
+ "dotenv": "16.5.0",
42
+ "express": "4.21.2",
43
+ "express-async-errors": "3.1.1",
44
+ "express-rate-limit": "^8.1.0",
45
+ "helmet": "8.1.0",
46
+ "http-status-codes": "2.3.0",
47
+ "jsdom": "26.0.0",
48
+ "jsonwebtoken": "9.0.2",
49
+ "morgan": "1.10.0",
50
+ "multer": "1.4.5-lts.2",
51
+ "mysql2": "3.14.0",
52
+ "nodemailer": "6.10.0",
53
+ "pangea-helpers": "1.3.85",
54
+ "sequelize": "6.37.7",
55
+ "sequelize-typescript": "2.1.6",
56
+ "sharp": "0.34.1",
57
+ "typia": "9.7.2"
58
+ },
59
+ "devDependencies": {
60
+ "@types/basic-auth": "1.1.8",
61
+ "@types/bcrypt": "5.0.2",
62
+ "@types/compression": "1.7.5",
63
+ "@types/cors": "2.8.17",
64
+ "@types/dotenv": "8.2.3",
65
+ "@types/express": "4.17.21",
66
+ "@types/express-rate-limit": "^6.0.2",
67
+ "@types/helmet": "4.0.0",
68
+ "@types/jsdom": "21.1.7",
69
+ "@types/jsonwebtoken": "9.0.6",
70
+ "@types/morgan": "1.9.9",
71
+ "@types/multer": "1.4.12",
72
+ "@types/node": "20.2.5",
73
+ "@types/nodemailer": "6.4.17",
74
+ "tsc-alias": "1.8.15",
75
+ "typescript": "5.2.2",
76
+ "yarn": "1.22.22"
77
+ }
78
+ }