pangea-server 1.0.42 → 1.0.43
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.
|
@@ -2,9 +2,9 @@ import { Router } from 'express';
|
|
|
2
2
|
import { BaseAuth } from '../authentication';
|
|
3
3
|
import type { AuthMap, AuthUsers } from '../authentication/authentication.types';
|
|
4
4
|
import type { RouteValidator as Validator, RouteValidate as Validate } from '../validator/validator.types';
|
|
5
|
-
import type { SetRouteArgs,
|
|
5
|
+
import type { SetRouteArgs, CallControllerConfig } from './router.types';
|
|
6
6
|
type ApiMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
7
|
-
export declare function getAppRouter<AM extends AuthMap, AU extends AuthUsers<AM>, BA extends BaseAuth<AM>>(
|
|
7
|
+
export declare function getAppRouter<AM extends AuthMap, AU extends AuthUsers<AM>, BA extends BaseAuth<AM>>(config: CallControllerConfig<AM, AU, BA>): {
|
|
8
8
|
new (app: App, basePath: string): {
|
|
9
9
|
router: Router;
|
|
10
10
|
setRoute<V extends Validator>(method: ApiMethod, path: string, validate: Validate<V>, ...args: SetRouteArgs<V, AM, BA>): void;
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getAppRouter = void 0;
|
|
4
4
|
const express_1 = require("express");
|
|
5
5
|
const call_controller_1 = require("./call-controller");
|
|
6
|
-
function getAppRouter(
|
|
6
|
+
function getAppRouter(config) {
|
|
7
7
|
return class AppRouter {
|
|
8
8
|
constructor(app, basePath) {
|
|
9
9
|
this.router = (0, express_1.Router)();
|
|
@@ -12,7 +12,7 @@ function getAppRouter(authConfig) {
|
|
|
12
12
|
setRoute(method, path, validate, ...args) {
|
|
13
13
|
const middlewares = args.slice(0, -1);
|
|
14
14
|
const controller = args[args.length - 1];
|
|
15
|
-
this.router[method](path, ...middlewares, (0, call_controller_1.callController)(controller, validate,
|
|
15
|
+
this.router[method](path, ...middlewares, (0, call_controller_1.callController)(controller, validate, config));
|
|
16
16
|
}
|
|
17
17
|
get(path, validate, ...args) {
|
|
18
18
|
this.setRoute('get', path, validate, ...args);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseAuth } from '../authentication';
|
|
2
2
|
import type { AuthMap, AuthUsers } from '../authentication/authentication.types';
|
|
3
3
|
import type { RouteValidator, RouteValidate } from '../validator/validator.types';
|
|
4
|
-
import type { Controller,
|
|
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>,
|
|
4
|
+
import type { Controller, CallControllerConfig } 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>, config: CallControllerConfig<AM, AU, BA>): (req: Req, res: Res) => Promise<void>;
|
|
@@ -10,16 +10,17 @@ const database_1 = require("../database");
|
|
|
10
10
|
const authentication_1 = require("../authentication");
|
|
11
11
|
// validator
|
|
12
12
|
const validate_request_1 = require("../validator/validate-request");
|
|
13
|
-
function callController(controller, validate,
|
|
13
|
+
function callController(controller, validate, config) {
|
|
14
|
+
const { authMap, authCtor, accessToken, businessCtor } = config;
|
|
14
15
|
return async function (req, res) {
|
|
16
|
+
const headers = req.headers;
|
|
17
|
+
const xBusiness = headers['x-business'];
|
|
15
18
|
const inputs = (0, validate_request_1.validateRequest)(validate, req);
|
|
16
|
-
const xBusiness = req.headers['x-business'];
|
|
17
|
-
const headers = { ...req.headers, 'x-business': xBusiness && Number(xBusiness) };
|
|
18
|
-
const ctx = { headers, file: req.file, files: req.files, ...inputs };
|
|
19
19
|
const tx = await (0, database_1.getDbClient)().transaction();
|
|
20
20
|
const db = new database_1.Db(tx);
|
|
21
21
|
try {
|
|
22
|
-
const
|
|
22
|
+
const business = xBusiness && businessCtor ? await db.findOne(businessCtor, Number(xBusiness)) : undefined;
|
|
23
|
+
const ctx = { headers, ...inputs, file: req.file, files: req.files, business };
|
|
23
24
|
const authUsers = await (0, authentication_1.getUsersFromToken)(headers.authorization, db, accessToken, authMap);
|
|
24
25
|
const auth = new authCtor(authUsers);
|
|
25
26
|
const result = await controller(ctx, db, auth);
|
|
@@ -7,9 +7,10 @@ export type SetRouteArgs<V extends RouteValidator, AM extends AuthMap, BA extend
|
|
|
7
7
|
...Middleware[],
|
|
8
8
|
Controller<V, AM, BA>
|
|
9
9
|
];
|
|
10
|
-
export type
|
|
10
|
+
export type CallControllerConfig<AM extends AuthMap, AU extends AuthUsers<AM>, BA extends BaseAuth<AM>> = {
|
|
11
11
|
authMap: AM;
|
|
12
12
|
authCtor: AuthCtor<AM, AU, BA>;
|
|
13
13
|
accessToken: AccessToken;
|
|
14
|
+
businessCtor?: BaseModelCtor;
|
|
14
15
|
};
|
|
15
16
|
export type SetRoutes = (app: App) => void;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="multer" />
|
|
2
|
+
import { BaseModel } from '../database';
|
|
2
3
|
import type { Express, Request, Response, NextFunction } from 'express';
|
|
3
4
|
import type { RouteValidator } from '../validator/validator.types';
|
|
4
5
|
declare global {
|
|
@@ -8,7 +9,6 @@ declare global {
|
|
|
8
9
|
type Next = NextFunction;
|
|
9
10
|
type ReqHeaders = Record<string, string | undefined> & {
|
|
10
11
|
'x-language': string;
|
|
11
|
-
'x-business'?: ModelId;
|
|
12
12
|
};
|
|
13
13
|
interface Ctx<V extends RouteValidator> {
|
|
14
14
|
headers: ReqHeaders;
|
|
@@ -17,6 +17,7 @@ declare global {
|
|
|
17
17
|
body: V['body'];
|
|
18
18
|
file?: MulterFile;
|
|
19
19
|
files?: MulterFile[];
|
|
20
|
+
business?: BaseModel;
|
|
20
21
|
}
|
|
21
22
|
type MulterFile = Express.Multer.File;
|
|
22
23
|
type UploadableImage = string | MulterFile;
|