This commit is contained in:
2024-09-07 03:43:42 +08:00
commit 443a41110a
6 changed files with 132 additions and 0 deletions

37
src/index.ts Normal file
View File

@@ -0,0 +1,37 @@
import { adapter } from './adapter';
type Fn = (opts: { url?: string; headers?: Record<string, string>; body?: Record<string, any>; [key: string]: any }) => Promise<Record<string, any>>;
type QueryOpts = {
url?: string;
adapter?: typeof adapter;
};
export class Query {
adapter: typeof adapter;
url: string;
beforeRequest?: Fn;
headers = {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + localStorage.getItem('token'),
};
constructor(opts: QueryOpts) {
this.adapter = opts.adapter || adapter;
this.url = opts.url || '/api/router';
}
async get(params: Record<string, any>) {
return this.post(params);
}
async post(body: Record<string, any>) {
const req = {
url: this.url,
headers: this.headers,
body,
};
if (this.beforeRequest) {
await this.beforeRequest(req);
}
return this.adapter(req);
}
before(fn: Fn) {
this.beforeRequest = fn;
}
}