refactor(server) (#795)
* refactor(server): refactor code * fix(server): fix satisfy check of semver
This commit is contained in:
parent
21806b3418
commit
c50a1923f4
@ -118,7 +118,7 @@ export class MockServer extends Koa {
|
|||||||
await next();
|
await next();
|
||||||
});
|
});
|
||||||
this.use(bodyParser());
|
this.use(bodyParser());
|
||||||
this.use(table2resource());
|
this.use(table2resource);
|
||||||
this.use(
|
this.use(
|
||||||
this.resourcer.restApiMiddleware({
|
this.resourcer.restApiMiddleware({
|
||||||
prefix: '/api',
|
prefix: '/api',
|
||||||
|
@ -119,7 +119,7 @@ describe('application', () => {
|
|||||||
});
|
});
|
||||||
await next();
|
await next();
|
||||||
});
|
});
|
||||||
console.log(app.middleware);
|
|
||||||
const response = await agent.get('/api/foos/1/bars');
|
const response = await agent.get('/api/foos/1/bars');
|
||||||
expect(response.body).toEqual([1, 2]);
|
expect(response.body).toEqual([1, 2]);
|
||||||
});
|
});
|
||||||
|
@ -121,7 +121,7 @@ export class ApplicationVersion {
|
|||||||
if (!version) {
|
if (!version) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return semver.satisfies(version, range);
|
return semver.satisfies(version, range, { includePrerelease: true });
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -71,6 +71,6 @@ export function registerMiddlewares(app: Application, options: ApplicationOption
|
|||||||
app.use(dataWrapping());
|
app.use(dataWrapping());
|
||||||
}
|
}
|
||||||
|
|
||||||
app.use(table2resource());
|
app.use(table2resource);
|
||||||
app.use(app.resourcer.restApiMiddleware());
|
app.use(app.resourcer.restApiMiddleware());
|
||||||
}
|
}
|
||||||
|
@ -1,45 +1,43 @@
|
|||||||
import { getNameByParams, parseRequest, ResourcerContext, ResourceType } from '@nocobase/resourcer';
|
import { getNameByParams, parseRequest, ResourcerContext, ResourceType } from '@nocobase/resourcer';
|
||||||
import Database from '@nocobase/database';
|
import Database from '@nocobase/database';
|
||||||
|
|
||||||
export function table2resource() {
|
export function table2resource(ctx: ResourcerContext & { db: Database }, next: () => Promise<any>) {
|
||||||
return async function table2resource(ctx: ResourcerContext & { db: Database }, next: () => Promise<any>) {
|
const resourcer = ctx.resourcer;
|
||||||
const resourcer = ctx.resourcer;
|
const database = ctx.db;
|
||||||
const database = ctx.db;
|
let params = parseRequest(
|
||||||
let params = parseRequest(
|
{
|
||||||
{
|
path: ctx.request.path,
|
||||||
path: ctx.request.path,
|
method: ctx.request.method,
|
||||||
method: ctx.request.method,
|
},
|
||||||
},
|
{
|
||||||
{
|
prefix: resourcer.options.prefix,
|
||||||
prefix: resourcer.options.prefix,
|
accessors: resourcer.options.accessors,
|
||||||
accessors: resourcer.options.accessors,
|
},
|
||||||
},
|
);
|
||||||
);
|
if (!params) {
|
||||||
if (!params) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
const resourceName = getNameByParams(params);
|
|
||||||
// 如果资源名称未被定义
|
|
||||||
if (resourcer.isDefined(resourceName)) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
const [collectionName, fieldName] = resourceName.split('.');
|
|
||||||
// 如果经过加载后是已经定义的表
|
|
||||||
if (!database.hasCollection(collectionName)) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
const collection = database.getCollection(collectionName);
|
|
||||||
let resourceType: ResourceType = 'single';
|
|
||||||
if (fieldName && collection.hasField(fieldName)) {
|
|
||||||
const field = collection.getField(fieldName);
|
|
||||||
resourceType = field.type;
|
|
||||||
}
|
|
||||||
resourcer.define({
|
|
||||||
type: resourceType,
|
|
||||||
name: resourceName,
|
|
||||||
});
|
|
||||||
return next();
|
return next();
|
||||||
};
|
}
|
||||||
|
const resourceName = getNameByParams(params);
|
||||||
|
// 如果资源名称未被定义
|
||||||
|
if (resourcer.isDefined(resourceName)) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
const [collectionName, fieldName] = resourceName.split('.');
|
||||||
|
// 如果经过加载后是已经定义的表
|
||||||
|
if (!database.hasCollection(collectionName)) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
const collection = database.getCollection(collectionName);
|
||||||
|
let resourceType: ResourceType = 'single';
|
||||||
|
if (fieldName && collection.hasField(fieldName)) {
|
||||||
|
const field = collection.getField(fieldName);
|
||||||
|
resourceType = field.type;
|
||||||
|
}
|
||||||
|
resourcer.define({
|
||||||
|
type: resourceType,
|
||||||
|
name: resourceName,
|
||||||
|
});
|
||||||
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
export default table2resource;
|
export default table2resource;
|
||||||
|
@ -12,6 +12,8 @@ export interface InstallOptions {
|
|||||||
sync?: SyncOptions;
|
sync?: SyncOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PluginConstructor<P, O = any> = { new(app: Application, options: O): P };
|
||||||
|
|
||||||
export class PluginManager {
|
export class PluginManager {
|
||||||
app: Application;
|
app: Application;
|
||||||
protected plugins = new Map<string, Plugin>();
|
protected plugins = new Map<string, Plugin>();
|
||||||
@ -28,7 +30,7 @@ export class PluginManager {
|
|||||||
return this.plugins.get(name);
|
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 instance = new pluginClass(this.app, options);
|
||||||
|
|
||||||
const name = instance.getName();
|
const name = instance.getName();
|
||||||
|
@ -77,22 +77,18 @@ export class ClientPlugin extends Plugin {
|
|||||||
if (!isAbsolute(root)) {
|
if (!isAbsolute(root)) {
|
||||||
root = resolve(process.cwd(), root);
|
root = resolve(process.cwd(), root);
|
||||||
}
|
}
|
||||||
this.app.middleware.unshift(async (ctx, next) => {
|
if (process.env.APP_ENV !== 'production' && root) {
|
||||||
if (process.env.APP_ENV === 'production') {
|
this.app.middleware.unshift(async (ctx, next) => {
|
||||||
return next();
|
if (ctx.path.startsWith(this.app.resourcer.options.prefix)) {
|
||||||
}
|
return next();
|
||||||
if (!root) {
|
}
|
||||||
return next();
|
await serve(root)(ctx, next);
|
||||||
}
|
// console.log('koa-send', root, ctx.status);
|
||||||
if (ctx.path.startsWith(this.app.resourcer.options.prefix)) {
|
if (ctx.status == 404) {
|
||||||
return next();
|
return send(ctx, 'index.html', { root });
|
||||||
}
|
}
|
||||||
await serve(root)(ctx, next);
|
});
|
||||||
// console.log('koa-send', root, ctx.status);
|
}
|
||||||
if (ctx.status == 404) {
|
|
||||||
return send(ctx, 'index.html', { root });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getName(): string {
|
getName(): string {
|
||||||
|
Loading…
Reference in New Issue
Block a user