tachybase_todo/packages/core/test/src/mockServer.ts

148 lines
4.1 KiB
TypeScript
Raw Normal View History

import { Database, mockDatabase } from '@nocobase/database';
2022-02-15 00:20:25 +08:00
import Application, { ApplicationOptions } from '@nocobase/server';
2021-09-09 13:09:25 +08:00
import qs from 'qs';
2021-09-16 00:38:48 +08:00
import supertest, { SuperAgentTest } from 'supertest';
2021-09-09 13:09:25 +08:00
interface ActionParams {
2022-01-23 15:04:50 +08:00
filterByTk?: any;
fields?: string[];
filter?: any;
sort?: string[];
page?: number;
2022-02-18 09:46:10 +08:00
pageSize?: number;
values?: any;
2022-01-23 15:04:50 +08:00
/**
* @deprecated
*/
resourceName?: string;
2022-01-23 15:04:50 +08:00
/**
* @deprecated
*/
resourceIndex?: string;
2022-01-23 15:04:50 +08:00
/**
* @deprecated
*/
associatedName?: string;
2022-01-23 15:04:50 +08:00
/**
* @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;
}
2021-09-09 13:09:25 +08:00
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>;
2021-09-09 13:09:25 +08:00
[name: string]: (params?: ActionParams) => Promise<supertest.Response>;
}
export class MockServer extends Application {
feat: build, cli, devtools, sdk, docs... * feat: nocobase build * chore: update build scripts * chore: update build scripts * chore(versions): 😊 publish v0.7.0-alpha.33 * chore: independent version * chore: nocobase build * chore(versions): 😊 publish v0.7.0-alpha.34 * feat: nocobase-cli * feat: nocobase-cli * chore: update dependencies * feat: improve code * refactor: create-nocobase-app * chore(versions): 😊 publish v0.7.0-alpha.35 * feat: @nocobase/devtools * chore(versions): 😊 publish v0.7.0-alpha.36 * chore: update dependencies * chore(versions): 😊 publish v0.7.0-alpha.37 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.38 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.39 * feat: update deps * chore(versions): 😊 publish v0.7.0-alpha.40 * chore: update devDependencies * chore(versions): 😊 publish v0.7.0-alpha.41 * fix: postinstall * chore(versions): 😊 publish v0.7.0-alpha.42 * chore: improve code * chore(versions): 😊 publish v0.7.0-alpha.43 * chore: execa * chore(versions): 😊 publish v0.7.0-alpha.44 * chore(cli): allow unknown option * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: default envs * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: package argument for build command * chore(versions): 😊 publish v0.7.0-alpha.46 * fix: improve code * chore(versions): 😊 publish v0.7.0-alpha.48 * feat: clean & doc * chore(versions): 😊 publish v0.7.0-alpha.49 * feat: compilation tips * feat: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.50 * fix: unexpected token ] in JSON * chore(versions): 😊 publish v0.7.0-alpha.51 * fix: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.52 * fix: remove export action from available action * fix: db sync after upgrade * chore(versions): 😊 publish v0.7.0-alpha.53 * feat: upgrade log * chore(versions): 😊 publish v0.7.0-alpha.54 * docs: updates * feat: updates * docs(cli): update usage description * feat: updates * docs: updates * docs: updates * docs: toc * feat: sdk * docs: updates * docs: updates * docs: updates * Update index.md * docs: updates * Update release-notes.md * Update roadmap.md * Update index.md * Update contributing.md * Update contributing.md * Update index.md * Update index.md * Update nocobase-cli.md * Update nocobase-cli.md * fix: user plugin initialization data * Update env.md * Update env.md * Update directory-structure.md * Update index.md * Update action-api.md * Update filter-operators.md * docs: update thanks.md * Update index.md * Update javascript-sdk.md * Update rest-api.md * Update installation.md * Update installation.md * Update upgrading.md * Update upgrading.md * Update upgrading.md * Update installation.md * Update installation.md * Create release-notes.md * Update release-notes.md * feat: updates * feat: update docs * feat: update release-notes.md * feat: switch language * feat: updates * Add files via upload * Add files via upload * Update important-features.md * Update thanks.md * feat: nocobase postinstall * Update index.md * Create why-different.md * Update why-different.md * Create who-is-for.md * Rename who-is-for.md to who.md * feat: update docs * Rename why-different.md to why.md * Update why.md * Update menus.ts * Update why-nocobase.md * Create who.md * Create why.md * feat: updates * chore(versions): 😊 publish v0.7.0-alpha.55 * feat: tips * Update who.md * Update who.md * feat: update docs * feat: update doc menus * fix: plugin client dist * docs: update contributing.md * docs: update readme.md * docs: update readme.md * docs: update readme.md * Update functional-zoning.md * fix: br Co-authored-by: Zhou <zhou.working@gmail.com>
2022-05-19 00:40:55 +08:00
async loadAndInstall(options: any = {}) {
await this.install({
feat: build, cli, devtools, sdk, docs... * feat: nocobase build * chore: update build scripts * chore: update build scripts * chore(versions): 😊 publish v0.7.0-alpha.33 * chore: independent version * chore: nocobase build * chore(versions): 😊 publish v0.7.0-alpha.34 * feat: nocobase-cli * feat: nocobase-cli * chore: update dependencies * feat: improve code * refactor: create-nocobase-app * chore(versions): 😊 publish v0.7.0-alpha.35 * feat: @nocobase/devtools * chore(versions): 😊 publish v0.7.0-alpha.36 * chore: update dependencies * chore(versions): 😊 publish v0.7.0-alpha.37 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.38 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.39 * feat: update deps * chore(versions): 😊 publish v0.7.0-alpha.40 * chore: update devDependencies * chore(versions): 😊 publish v0.7.0-alpha.41 * fix: postinstall * chore(versions): 😊 publish v0.7.0-alpha.42 * chore: improve code * chore(versions): 😊 publish v0.7.0-alpha.43 * chore: execa * chore(versions): 😊 publish v0.7.0-alpha.44 * chore(cli): allow unknown option * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: default envs * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: package argument for build command * chore(versions): 😊 publish v0.7.0-alpha.46 * fix: improve code * chore(versions): 😊 publish v0.7.0-alpha.48 * feat: clean & doc * chore(versions): 😊 publish v0.7.0-alpha.49 * feat: compilation tips * feat: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.50 * fix: unexpected token ] in JSON * chore(versions): 😊 publish v0.7.0-alpha.51 * fix: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.52 * fix: remove export action from available action * fix: db sync after upgrade * chore(versions): 😊 publish v0.7.0-alpha.53 * feat: upgrade log * chore(versions): 😊 publish v0.7.0-alpha.54 * docs: updates * feat: updates * docs(cli): update usage description * feat: updates * docs: updates * docs: updates * docs: toc * feat: sdk * docs: updates * docs: updates * docs: updates * Update index.md * docs: updates * Update release-notes.md * Update roadmap.md * Update index.md * Update contributing.md * Update contributing.md * Update index.md * Update index.md * Update nocobase-cli.md * Update nocobase-cli.md * fix: user plugin initialization data * Update env.md * Update env.md * Update directory-structure.md * Update index.md * Update action-api.md * Update filter-operators.md * docs: update thanks.md * Update index.md * Update javascript-sdk.md * Update rest-api.md * Update installation.md * Update installation.md * Update upgrading.md * Update upgrading.md * Update upgrading.md * Update installation.md * Update installation.md * Create release-notes.md * Update release-notes.md * feat: updates * feat: update docs * feat: update release-notes.md * feat: switch language * feat: updates * Add files via upload * Add files via upload * Update important-features.md * Update thanks.md * feat: nocobase postinstall * Update index.md * Create why-different.md * Update why-different.md * Create who-is-for.md * Rename who-is-for.md to who.md * feat: update docs * Rename why-different.md to why.md * Update why.md * Update menus.ts * Update why-nocobase.md * Create who.md * Create why.md * feat: updates * chore(versions): 😊 publish v0.7.0-alpha.55 * feat: tips * Update who.md * Update who.md * feat: update docs * feat: update doc menus * fix: plugin client dist * docs: update contributing.md * docs: update readme.md * docs: update readme.md * docs: update readme.md * Update functional-zoning.md * fix: br Co-authored-by: Zhou <zhou.working@gmail.com>
2022-05-19 00:40:55 +08:00
...options,
sync: {
force: true,
alter: {
drop: false,
},
},
});
2022-01-23 15:04:50 +08:00
}
async cleanDb() {
await this.db.sequelize.getQueryInterface().dropAllTables();
}
agent(): SuperAgentTest & { resource: (name: string, resourceOf?: any) => Resource } {
const agent = supertest.agent(this.appManager.callback());
2021-09-09 13:09:25 +08:00
const prefix = this.resourcer.options.prefix;
const proxy = new Proxy(agent, {
2021-09-09 13:09:25 +08:00
get(target, method: string, receiver) {
2021-09-16 00:38:48 +08:00
if (method === 'resource') {
2022-01-23 15:04:50 +08:00
return (name: string, resourceOf?: any) => {
2021-09-16 00:38:48 +08:00
const keys = name.split('.');
2021-12-06 21:23:34 +08:00
const proxy = new Proxy(
{},
{
get(target, method: string, receiver) {
return (params: ActionParams = {}) => {
2022-01-23 15:04:50 +08:00
let { filterByTk, values = {}, file, ...restParams } = params;
if (params.associatedIndex) {
resourceOf = params.associatedIndex;
}
if (params.resourceIndex) {
filterByTk = params.resourceIndex;
}
let url = prefix || '';
2021-12-06 21:23:34 +08:00
if (keys.length > 1) {
2022-01-23 15:04:50 +08:00
url += `/${keys[0]}/${resourceOf}/${keys[1]}`;
2021-12-06 21:23:34 +08:00
} else {
2022-01-23 15:04:50 +08:00
url += `/${name}`;
2021-12-06 21:23:34 +08:00
}
url += `:${method as string}`;
2022-01-23 15:04:50 +08:00
if (filterByTk) {
url += `/${filterByTk}`;
2021-12-06 21:23:34 +08:00
}
const queryString = qs.stringify(restParams, { arrayFormat: 'brackets' });
2021-12-06 21:23:34 +08:00
switch (method) {
case 'upload':
return agent.post(`${url}?${queryString}`).attach('file', file).field(values);
2021-12-06 21:23:34 +08:00
case 'list':
case 'get':
return agent.get(`${url}?${queryString}`);
2021-12-06 21:23:34 +08:00
default:
return agent.post(`${url}?${queryString}`).send(values);
2021-12-06 21:23:34 +08:00
}
};
},
2021-09-16 00:38:48 +08:00
},
2021-12-06 21:23:34 +08:00
);
2021-09-16 00:38:48 +08:00
return proxy;
2021-12-06 21:23:34 +08:00
};
2021-09-16 00:38:48 +08:00
}
return agent[method];
2021-12-06 21:23:34 +08:00
},
2021-09-09 13:09:25 +08:00
});
2021-09-16 00:38:48 +08:00
return proxy as any;
2021-09-09 13:09:25 +08:00
}
}
2022-02-15 00:20:25 +08:00
export function mockServer(options: ApplicationOptions = {}) {
let database;
if (options?.database instanceof Database) {
database = options.database;
} else {
database = mockDatabase(<any>options?.database || {});
}
2021-09-09 13:09:25 +08:00
return new MockServer({
acl: false,
2021-09-09 13:09:25 +08:00
...options,
2022-02-15 00:20:25 +08:00
database,
2021-09-09 13:09:25 +08:00
});
}
export function createMockServer() {}
2021-09-09 13:09:25 +08:00
export default mockServer;