mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-09 15:39:24 +08:00
* chore: skip yarn install in pm command * feat: dump sub app by sub app name * feat: dump & restore by sub app * chore: enable application name to edit * chore: field belongsTo uiSchema * test: drop schema * feat: uiSchema migrator * fix: test * fix: remove uiSchema * fix: rerun migration * chore: migrate fieldsHistory uiSchema * fix: set uiSchema options * chore: transaction params * fix: sql error in mysql * fix: sql compatibility * feat: collection group api * chore: restore & dump action template * chore: tmp commit * chore: collectionGroupAction * feat: dumpableCollection api * refactor: dump command * fix: remove uiSchemaUid * chore: get uiSchemaUid from tmp field * feat: return dumped file url in dumper.dump * feat: dump api * refactor: collection groyoup * chore: comment * feat: restore command force option * feat: dump with collection groups * refactor: restore command * feat: restore http api * fix: test * fix: test * fix: restore test * chore: volta pin * fix: sub app load collection options * fix: stop sub app * feat: add stopped status to application to prevent duplicate application stop * chore: tmp commit * test: upgrade * feat: pass upgrade event to sub app * fix: app manager client * fix: remove stopped status * fix: emit beforeStop event * feat: support dump & restore subApp through api * chore: dumpable collections api * refactor: getTableNameWithSchema * fix: schema name * feat: cname * refactor: collection 同步实现方式 * refactor: move collection group manager to database * fix: test * fix: remove uiSchema * fix: uiSchema * fix: remove settings * chore: plugin enable & disable event * feat: modal warning * fix: users_jobs namespace * fix: rolesUischemas namespace * fix: am snippet * feat: beforeSubAppInstall event * fix: improve NOCOBASE_LOCALE_KEY & NOCOBASE_ROLE_KEY --------- Co-authored-by: chenos <chenlinxh@gmail.com>
181 lines
4.7 KiB
TypeScript
181 lines
4.7 KiB
TypeScript
import { Database, mockDatabase } from '@nocobase/database';
|
|
import Application, { ApplicationOptions, PluginManager } from '@nocobase/server';
|
|
import qs from 'qs';
|
|
import supertest, { SuperAgentTest } from 'supertest';
|
|
|
|
interface ActionParams {
|
|
filterByTk?: any;
|
|
fields?: string[];
|
|
filter?: any;
|
|
sort?: string[];
|
|
page?: number;
|
|
pageSize?: number;
|
|
values?: any;
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
resourceName?: string;
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
resourceIndex?: string;
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
associatedName?: string;
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
associatedIndex?: string;
|
|
|
|
[key: string]: any;
|
|
}
|
|
|
|
interface SortActionParams {
|
|
resourceName?: string;
|
|
resourceIndex?: any;
|
|
associatedName?: string;
|
|
associatedIndex?: any;
|
|
sourceId?: any;
|
|
targetId?: any;
|
|
sortField?: string;
|
|
method?: string;
|
|
target?: any;
|
|
sticky?: boolean;
|
|
|
|
[key: string]: any;
|
|
}
|
|
|
|
interface Resource {
|
|
get: (params?: ActionParams) => Promise<supertest.Response>;
|
|
list: (params?: ActionParams) => Promise<supertest.Response>;
|
|
create: (params?: ActionParams) => Promise<supertest.Response>;
|
|
update: (params?: ActionParams) => Promise<supertest.Response>;
|
|
destroy: (params?: ActionParams) => Promise<supertest.Response>;
|
|
sort: (params?: SortActionParams) => Promise<supertest.Response>;
|
|
|
|
[name: string]: (params?: ActionParams) => Promise<supertest.Response>;
|
|
}
|
|
|
|
export class MockServer extends Application {
|
|
async loadAndInstall(options: any = {}) {
|
|
await this.load({ method: 'install' });
|
|
|
|
if (options.afterLoad) {
|
|
await options.afterLoad(this);
|
|
}
|
|
|
|
await this.install({
|
|
...options,
|
|
sync: {
|
|
force: false,
|
|
alter: {
|
|
drop: false,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async cleanDb() {
|
|
await this.db.clean({ drop: true });
|
|
}
|
|
|
|
agent(): SuperAgentTest & { resource: (name: string, resourceOf?: any) => Resource } {
|
|
const agent = supertest.agent(this.appManager.callback());
|
|
const prefix = this.resourcer.options.prefix;
|
|
const proxy = new Proxy(agent, {
|
|
get(target, method: string, receiver) {
|
|
if (method === 'resource') {
|
|
return (name: string, resourceOf?: any) => {
|
|
const keys = name.split('.');
|
|
const proxy = new Proxy(
|
|
{},
|
|
{
|
|
get(target, method: string, receiver) {
|
|
return (params: ActionParams = {}) => {
|
|
let { filterByTk, values = {}, file, ...restParams } = params;
|
|
if (params.associatedIndex) {
|
|
resourceOf = params.associatedIndex;
|
|
}
|
|
if (params.resourceIndex) {
|
|
filterByTk = params.resourceIndex;
|
|
}
|
|
let url = prefix || '';
|
|
if (keys.length > 1) {
|
|
url += `/${keys[0]}/${resourceOf}/${keys[1]}`;
|
|
} else {
|
|
url += `/${name}`;
|
|
}
|
|
url += `:${method as string}`;
|
|
if (filterByTk) {
|
|
url += `/${filterByTk}`;
|
|
}
|
|
|
|
const queryString = qs.stringify(restParams, { arrayFormat: 'brackets' });
|
|
|
|
switch (method) {
|
|
case 'upload':
|
|
return agent.post(`${url}?${queryString}`).attach('file', file).field(values);
|
|
case 'list':
|
|
case 'get':
|
|
return agent.get(`${url}?${queryString}`);
|
|
default:
|
|
return agent.post(`${url}?${queryString}`).send(values);
|
|
}
|
|
};
|
|
},
|
|
},
|
|
);
|
|
return proxy;
|
|
};
|
|
}
|
|
return agent[method];
|
|
},
|
|
});
|
|
return proxy as any;
|
|
}
|
|
}
|
|
|
|
export function mockServer(options: ApplicationOptions = {}) {
|
|
if (typeof TextEncoder === 'undefined') {
|
|
global.TextEncoder = require('util').TextEncoder;
|
|
}
|
|
|
|
if (typeof TextDecoder === 'undefined') {
|
|
global.TextDecoder = require('util').TextDecoder;
|
|
}
|
|
|
|
// @ts-ignore
|
|
if (!PluginManager.findPackagePatched) {
|
|
PluginManager.getPackageJson = () => {
|
|
return {
|
|
version: '0.0.0',
|
|
};
|
|
};
|
|
|
|
// @ts-ignore
|
|
PluginManager.findPackagePatched = true;
|
|
}
|
|
|
|
let database;
|
|
if (options?.database instanceof Database) {
|
|
database = options.database;
|
|
} else {
|
|
database = mockDatabase(<any>options?.database || {});
|
|
}
|
|
|
|
const app = new MockServer({
|
|
acl: false,
|
|
...options,
|
|
database,
|
|
});
|
|
|
|
app.pm.generateClientFile = async () => {};
|
|
|
|
return app;
|
|
}
|
|
|
|
export function createMockServer() {}
|
|
|
|
export default mockServer;
|