Skip to content

Commit

Permalink
descriptors are defined once for all the services, and not anymore on…
Browse files Browse the repository at this point in the history
… each operation.
  • Loading branch information
pomgui committed Sep 8, 2020
1 parent 88d08e8 commit a4bdd80
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
19 changes: 12 additions & 7 deletions lib/service/PiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import {
PiServiceOptions, PiExceptionHandlerParams,
PiSecurityDef, PiSecurityDefItem, PiSecurity
} from "./types";
import { PiTypeDescriptor, PiRestError } from '@pomgui/rest-lib';
import { PiTypeDescriptor, PiRestError, PiDescriptor } from '@pomgui/rest-lib';
import { PiDatabasePool } from '@pomgui/database';
import { assert } from 'console';

var
_router: Router = Router(),
_dbPool: PiDatabasePool | undefined,
_security: PiSecurityDef;
_security: PiSecurityDef,
_descriptors = new Map<string, PiTypeDescriptor>();

export function PiGET(path: string, options?: PiServiceOptions) { return decorator(path, _router.get, options) }
export function PiPOST(path: string, options?: PiServiceOptions) { return decorator(path, _router.post, options) }
Expand All @@ -20,20 +21,20 @@ export function PiDELETE(path: string, options?: PiServiceOptions) { return deco

function decorator(path: string, defineRoute: IRouterMatcher<void>, options?: PiServiceOptions) {
const opts = Object.assign({ database: true, errorHandler: defaultErrorHandler }, options);
return function (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
const orig = descriptor.value;
return function (target: any, propertyKey: string | symbol, propDescriptor: PropertyDescriptor) {
const orig = propDescriptor.value;
const descriptor = _descriptors.get(orig.name);

/** Real operation */
const operation = async (req: Request, res: Response): Promise<any> => {
const db = (_dbPool && !opts.noDb) ? await _dbPool.get() : null;
const desc = opts.descriptor && (opts.descriptor.o || (opts.descriptor.o = new PiTypeDescriptor(opts.descriptor)));
const security = opts.security ? checkSecurity(opts.security, req) : Promise.resolve(true);

if (db) {
let result: any;
return security
.then(() => db.beginTransaction())
.then(() => orig.call(target, normalizeQueryParams(req, desc), db, req, res))
.then(() => orig.call(target, normalizeQueryParams(req, descriptor), db, req, res))
.then(r => result = r)
.then(() => db.commit())
.then(() => opts.customSend || res.send(result))
Expand All @@ -42,7 +43,7 @@ function decorator(path: string, defineRoute: IRouterMatcher<void>, options?: Pi
.catch(error => opts.errorHandler!({ db: null, req, res, error }));
} else
return security
.then(() => orig.call(target, normalizeQueryParams(req, desc), req, res))
.then(() => orig.call(target, normalizeQueryParams(req, descriptor), req, res))
.then(result => opts.customSend || res.send(result))
.catch(error => opts.errorHandler!({ db: null, req, res, error }));
};
Expand Down Expand Up @@ -126,11 +127,15 @@ function decorator(path: string, defineRoute: IRouterMatcher<void>, options?: Pi

export function PiService(config: {
services: { new(): any }[],
descriptors?: { [name: string]: PiDescriptor },
dbPool?: PiDatabasePool,
security?: PiSecurityDef
}): Router {
config.services.forEach(s => new s()); // Create an instance, just to access to the decorators
if (config.security) _security = config.security;
_dbPool = config.dbPool;
if (config.descriptors)
Object.entries(config.descriptors)
.forEach(([name, value]) => _descriptors.set(name, new PiTypeDescriptor(name, value)));
return _router;
}
1 change: 0 additions & 1 deletion lib/service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export type PiSecurity = {
export type PiServiceOptions = {
customSend?: boolean;
noDb?: boolean; // Don't use database for this operation
descriptor?: PiDescriptor,
errorHandler?: PiExceptionHandler;

/** see https://swagger.io/docs/specification/2-0/authentication/ */
Expand Down

0 comments on commit a4bdd80

Please sign in to comment.