refactor(core): simplify some code (#895)
This commit is contained in:
parent
dce6558b55
commit
6c929039b1
@ -61,7 +61,7 @@ export class Resource {
|
|||||||
public readonly except: Array<ActionName>;
|
public readonly except: Array<ActionName>;
|
||||||
|
|
||||||
constructor(options: ResourceOptions, resourcer: Resourcer) {
|
constructor(options: ResourceOptions, resourcer: Resourcer) {
|
||||||
const { middleware, middlewares, actions = {} } = options;
|
const { middleware, middlewares, actions = {}, only = [], except = [] } = options;
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.resourcer = resourcer;
|
this.resourcer = resourcer;
|
||||||
this.middlewares = Middleware.toInstanceArray(middleware || middlewares);
|
this.middlewares = Middleware.toInstanceArray(middleware || middlewares);
|
||||||
@ -71,7 +71,6 @@ export class Resource {
|
|||||||
actions[name as string] = handler;
|
actions[name as string] = handler;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const { only = [], except = [] } = options;
|
|
||||||
if (except.length > 0) {
|
if (except.length > 0) {
|
||||||
excludes = except;
|
excludes = except;
|
||||||
} else if (only.length > 0) {
|
} else if (only.length > 0) {
|
||||||
|
@ -267,9 +267,8 @@ export class Resourcer {
|
|||||||
this.middlewares.add(middlewares, options);
|
this.middlewares.add(middlewares, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
restApiMiddleware(options: KoaMiddlewareOptions = {}) {
|
restApiMiddleware({ prefix, accessors }: KoaMiddlewareOptions = {}) {
|
||||||
const { prefix, accessors } = options;
|
return async (ctx: ResourcerContext, next: () => Promise<any>) => {
|
||||||
const restApiMiddleware = async (ctx: ResourcerContext, next: () => Promise<any>) => {
|
|
||||||
ctx.resourcer = this;
|
ctx.resourcer = this;
|
||||||
let params = parseRequest(
|
let params = parseRequest(
|
||||||
{
|
{
|
||||||
@ -332,7 +331,6 @@ export class Resourcer {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return restApiMiddleware;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
middleware(options: KoaMiddlewareOptions = {}) {
|
middleware(options: KoaMiddlewareOptions = {}) {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { promisify } from 'util';
|
||||||
import { ACL } from '@nocobase/acl';
|
import { ACL } from '@nocobase/acl';
|
||||||
import { registerActions } from '@nocobase/actions';
|
import { registerActions } from '@nocobase/actions';
|
||||||
import Database, { Collection, CollectionOptions, IDatabaseOptions } from '@nocobase/database';
|
import Database, { Collection, CollectionOptions, IDatabaseOptions } from '@nocobase/database';
|
||||||
@ -378,18 +379,8 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
|
|||||||
|
|
||||||
// close http server
|
// close http server
|
||||||
if (this.listenServer) {
|
if (this.listenServer) {
|
||||||
const closeServer = () =>
|
await promisify(this.listenServer.close)();
|
||||||
new Promise((resolve, reject) => {
|
|
||||||
this.listenServer.close((err) => {
|
|
||||||
if (err) {
|
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
this.listenServer = null;
|
this.listenServer = null;
|
||||||
resolve(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
await closeServer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.emitAsync('afterStop', this, options);
|
await this.emitAsync('afterStop', this, options);
|
||||||
|
Loading…
Reference in New Issue
Block a user