tachybase_todo/packages/client/src/resource.ts

170 lines
4.4 KiB
TypeScript
Raw Normal View History

2021-07-23 18:08:44 +08:00
import { request } from './schemas';
2021-05-23 08:38:08 +08:00
export interface ResourceOptions {
resourceName: string;
associatedKey?: any;
associatedName?: string;
resourceKey?: any;
}
export interface GetOptions {
resourceKey?: any;
defaultAppends?: any[];
2021-05-23 08:38:08 +08:00
appends?: string[];
}
export interface SaveOptions {
resourceKey?: any;
}
export interface ListOptions {
2021-08-04 12:38:08 +08:00
defaultFilter?: any;
filter?: any;
defaultAppends?: any[];
appends?: string[];
2021-05-23 08:38:08 +08:00
}
export class Resource {
2021-07-30 00:03:10 +08:00
public options: ResourceOptions;
2021-05-23 08:38:08 +08:00
constructor(options: string | ResourceOptions) {
if (typeof options === 'string') {
this.options = { resourceName: options }
} else {
this.options = options;
}
}
2021-08-04 12:38:08 +08:00
sort(options) {
const { resourceName } = this.options;
const { resourceKey, target, field = 'sort' } = options;
return request(`${resourceName}:sort/${resourceKey}`, {
method: 'post',
data: {
target,
field,
},
});
}
2021-05-23 08:38:08 +08:00
list(options: ListOptions = {}) {
const { defaultAppends = [], appends = [], defaultFilter, filter, ...others } = options;
2021-08-11 00:08:55 +08:00
const { associatedKey, associatedName, resourceName } = this.options;
let url = `${resourceName}:list`;
if (associatedName && associatedKey) {
url = `${associatedName}/${associatedKey}/${resourceName}:list`;
}
return request(url, {
2021-08-04 12:38:08 +08:00
method: 'get',
params: {
filter: decodeURIComponent(JSON.stringify({ and: [defaultFilter, filter].filter(Boolean) })),
'fields[appends]': defaultAppends.concat(appends).join(','),
2021-08-04 12:38:08 +08:00
...others,
},
});
2021-05-23 08:38:08 +08:00
}
get(options: GetOptions = {}) {
const resourceKey = options.resourceKey || this.options.resourceKey;
const { resourceName } = this.options;
if (!resourceKey) {
return Promise.resolve({ data: {} });
}
const { defaultAppends = [], appends = [], ...others } = options;
2021-08-08 12:06:33 +08:00
return request(`${resourceName}:get/${resourceKey}`, {
params: {
...others,
'fields[appends]': defaultAppends.concat(appends).join(','),
},
2021-08-08 12:06:33 +08:00
});
2021-05-23 08:38:08 +08:00
}
2021-07-30 00:03:10 +08:00
create(values: any) {
const { resourceName } = this.options;
const url = `${resourceName}:create`;
return request(url, {
method: 'post',
data: values,
});
}
2021-05-23 08:38:08 +08:00
save(values: any, options: SaveOptions = {}) {
const resourceKey = options.resourceKey || this.options.resourceKey;
const { resourceName } = this.options;
2021-07-23 18:08:44 +08:00
const url = `${resourceName}:${resourceKey ? `update/${resourceKey}` : 'create'}`;
2021-05-23 08:38:08 +08:00
return request(url, {
method: 'post',
data: values,
});
}
2021-08-13 09:57:21 +08:00
export(options: any) {
const { resourceName } = this.options;
const { columns, ...others } = options;
const url = `${resourceName}:export`;
return request(url, {
method: 'post',
params: {
columns: JSON.stringify(columns),
...others,
},
parseResponse: false,
responseType: 'blob'
}).then(async (response: Response) => {
const filename = decodeURI(response.headers.get('Content-Disposition').replace('attachment; filename=', ''));
// ReadableStream
let res = new Response(response.body);
let blob = await res.blob();
let url = URL.createObjectURL(blob);
let a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
// cleanup
URL.revokeObjectURL(url);
document.body.removeChild(a);
a = null;
blob = null;
url = null;
res = null;
});
}
2021-07-23 18:08:44 +08:00
destroy(filter: any) {
const { resourceName } = this.options;
const url = `${resourceName}:destroy`;
return request(url, {
method: 'get',
params: {
filter
},
});
}
2021-08-11 14:27:45 +08:00
toggle(options?: any) {
const { associatedKey, associatedName, resourceName } = this.options;
const { resourceKey } = options;
let url = `${associatedName}/${associatedKey}/${resourceName}:toggle/${resourceKey}`;
return request(url, {
method: 'post',
});
}
2021-07-30 00:03:10 +08:00
static make(options: null | string | Resource | ResourceOptions): Resource | null {
2021-05-23 08:38:08 +08:00
if (typeof options === 'string') {
return new Resource({ resourceName: options });
}
if (options instanceof Resource) {
return options;
}
if (typeof options === 'object' && options.resourceName) {
return new Resource(options);
}
2021-07-30 00:03:10 +08:00
console.warn('resource 初始化参数错误');
return null;
2021-05-23 08:38:08 +08:00
}
}