refactor(server) (#795)

* refactor(server): refactor code

* fix(server): fix satisfy check of semver
This commit is contained in:
Junyi 2022-09-02 11:44:22 +08:00 committed by GitHub
parent 21806b3418
commit c50a1923f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 59 deletions

View File

@ -118,7 +118,7 @@ export class MockServer extends Koa {
await next();
});
this.use(bodyParser());
this.use(table2resource());
this.use(table2resource);
this.use(
this.resourcer.restApiMiddleware({
prefix: '/api',

View File

@ -119,7 +119,7 @@ describe('application', () => {
});
await next();
});
console.log(app.middleware);
const response = await agent.get('/api/foos/1/bars');
expect(response.body).toEqual([1, 2]);
});

View File

@ -121,7 +121,7 @@ export class ApplicationVersion {
if (!version) {
return true;
}
return semver.satisfies(version, range);
return semver.satisfies(version, range, { includePrerelease: true });
}
return true;
}

View File

@ -71,6 +71,6 @@ export function registerMiddlewares(app: Application, options: ApplicationOption
app.use(dataWrapping());
}
app.use(table2resource());
app.use(table2resource);
app.use(app.resourcer.restApiMiddleware());
}

View File

@ -1,8 +1,7 @@
import { getNameByParams, parseRequest, ResourcerContext, ResourceType } from '@nocobase/resourcer';
import Database from '@nocobase/database';
export function table2resource() {
return async function table2resource(ctx: ResourcerContext & { db: Database }, next: () => Promise<any>) {
export function table2resource(ctx: ResourcerContext & { db: Database }, next: () => Promise<any>) {
const resourcer = ctx.resourcer;
const database = ctx.db;
let params = parseRequest(
@ -39,7 +38,6 @@ export function table2resource() {
name: resourceName,
});
return next();
};
}
export default table2resource;

View File

@ -12,6 +12,8 @@ export interface InstallOptions {
sync?: SyncOptions;
}
type PluginConstructor<P, O = any> = { new(app: Application, options: O): P };
export class PluginManager {
app: Application;
protected plugins = new Map<string, Plugin>();
@ -28,7 +30,7 @@ export class PluginManager {
return this.plugins.get(name);
}
add<P = Plugin, O = any>(pluginClass: any, options?: O): P {
add<P extends Plugin = Plugin, O = any>(pluginClass: PluginConstructor<P, O>, options?: O): P {
const instance = new pluginClass(this.app, options);
const name = instance.getName();

View File

@ -77,13 +77,8 @@ export class ClientPlugin extends Plugin {
if (!isAbsolute(root)) {
root = resolve(process.cwd(), root);
}
if (process.env.APP_ENV !== 'production' && root) {
this.app.middleware.unshift(async (ctx, next) => {
if (process.env.APP_ENV === 'production') {
return next();
}
if (!root) {
return next();
}
if (ctx.path.startsWith(this.app.resourcer.options.prefix)) {
return next();
}
@ -94,6 +89,7 @@ export class ClientPlugin extends Plugin {
}
});
}
}
getName(): string {
return this.getPackageName(__dirname);