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
|
|
|
import Application, { ApplicationOptions } from '@nocobase/server';
|
|
|
|
import { getConfig } from './mockDatabase';
|
|
|
|
|
2021-09-09 22:38:39 +08:00
|
|
|
interface ActionParams {
|
|
|
|
fields?: string[] | {
|
|
|
|
only?: string[];
|
|
|
|
except?: string[];
|
|
|
|
appends?: string[];
|
|
|
|
};
|
|
|
|
filter?: any;
|
|
|
|
sort?: string[];
|
|
|
|
page?: number;
|
|
|
|
perPage?: number;
|
|
|
|
values?: any;
|
|
|
|
resourceName?: string;
|
|
|
|
resourceKey?: string;
|
|
|
|
associatedName?: string;
|
|
|
|
associatedKey?: string;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SortActionParams {
|
|
|
|
resourceName?: string;
|
|
|
|
resourceKey?: any;
|
|
|
|
associatedName?: string;
|
|
|
|
associatedKey?: 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>;
|
2021-09-09 22:38:39 +08:00
|
|
|
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 {
|
2021-09-16 00:38:48 +08:00
|
|
|
agent(): SuperAgentTest & { resource: (name: string) => Resource } {
|
|
|
|
const agent = supertest.agent(this.callback());
|
2021-09-09 13:09:25 +08:00
|
|
|
const prefix = this.resourcer.options.prefix;
|
2021-12-03 07:31:22 +08:00
|
|
|
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') {
|
|
|
|
return (name: string) => {
|
|
|
|
const keys = name.split('.');
|
|
|
|
const proxy = new Proxy({}, {
|
|
|
|
get(target, method: string, receiver) {
|
|
|
|
return (params: ActionParams = {}) => {
|
|
|
|
const {
|
|
|
|
associatedKey,
|
|
|
|
resourceKey,
|
|
|
|
values = {},
|
|
|
|
file,
|
|
|
|
...restParams
|
|
|
|
} = params;
|
|
|
|
let url = prefix;
|
|
|
|
if (keys.length > 1) {
|
|
|
|
url = `/${keys[0]}/${associatedKey}/${keys[1]}`
|
|
|
|
} else {
|
|
|
|
url = `/${name}`;
|
|
|
|
}
|
|
|
|
url += `:${method as string}`;
|
|
|
|
if (resourceKey) {
|
|
|
|
url += `/${resourceKey}`;
|
|
|
|
}
|
2021-12-03 07:31:22 +08:00
|
|
|
|
2021-09-16 00:38:48 +08:00
|
|
|
switch (method) {
|
|
|
|
case 'upload':
|
|
|
|
return agent
|
|
|
|
.post(`${url}?${qs.stringify(restParams)}`)
|
|
|
|
.attach('file', file)
|
|
|
|
.field(values);
|
|
|
|
case 'list':
|
|
|
|
case 'get':
|
|
|
|
return agent.get(`${url}?${qs.stringify(restParams)}`);
|
|
|
|
default:
|
|
|
|
return agent
|
|
|
|
.post(`${url}?${qs.stringify(restParams)}`)
|
|
|
|
.send(values);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return proxy;
|
2021-09-09 13:09:25 +08:00
|
|
|
}
|
2021-09-16 00:38:48 +08:00
|
|
|
}
|
|
|
|
return (...args: any[]) => {
|
|
|
|
return agent[method](...args);
|
2021-09-09 13:09:25 +08:00
|
|
|
};
|
2021-09-16 00:38:48 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mockServer(options?: ApplicationOptions) {
|
|
|
|
return new MockServer({
|
|
|
|
...options,
|
|
|
|
database: getConfig(options?.database),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default mockServer;
|