feat: 支持GET请求 因为GET的请求可以缓存

This commit is contained in:
xion 2025-03-22 13:19:40 +08:00
parent dcab87c77a
commit 8f85ed4d4d
3 changed files with 34 additions and 22 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/query", "name": "@kevisual/query",
"version": "0.0.13", "version": "0.0.14",
"main": "dist/index.js", "main": "dist/index.js",
"module": "dist/index.js", "module": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

View File

@ -1,8 +1,11 @@
type AdapterOpts = { export const methods = ['GET', 'POST'] as const;
export type Method = (typeof methods)[number];
export type AdapterOpts = {
url: string; url: string;
headers?: Record<string, string>; headers?: Record<string, string>;
body?: Record<string, any>; body?: Record<string, any>;
timeout?: number; timeout?: number;
method?: Method;
}; };
/** /**
@ -18,16 +21,21 @@ export const adapter = async (opts: AdapterOpts, overloadOpts?: RequestInit) =>
const timer = setTimeout(() => { const timer = setTimeout(() => {
controller.abort(); controller.abort();
}, timeout); }, timeout);
let method = overloadOpts?.method || opts.method || 'POST';
return fetch(opts.url, { let url = new URL(opts.url, window.location.origin);
method: 'POST', const isGet = method === 'GET';
if (isGet) {
url.search = new URLSearchParams(opts.body).toString();
}
return fetch(url, {
method: method.toUpperCase(),
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...opts.headers, ...opts.headers,
}, },
body: JSON.stringify(opts.body),
signal, signal,
...overloadOpts, ...overloadOpts,
body: isGet ? undefined : JSON.stringify(opts.body),
}) })
.then((response) => { .then((response) => {
// 获取 Content-Type 头部信息 // 获取 Content-Type 头部信息

View File

@ -1,4 +1,4 @@
import { adapter } from './adapter.ts'; import { adapter, Method } from './adapter.ts';
/** /**
* *
* @param opts * @param opts
@ -17,6 +17,8 @@ export type QueryOpts = {
adapter?: typeof adapter; adapter?: typeof adapter;
headers?: Record<string, string>; headers?: Record<string, string>;
timeout?: number; timeout?: number;
method?: Method;
[key: string]: any;
}; };
export type Data = { export type Data = {
path?: string; path?: string;
@ -76,7 +78,7 @@ export const setBaseResponse = (res: Result) => {
export class Query { export class Query {
adapter: typeof adapter; adapter: typeof adapter;
url: string; url: string;
beforeRequest?: Fn; beforeRequest?: DataOpts['beforeRequest'];
afterResponse?: DataOpts['afterResponse']; afterResponse?: DataOpts['afterResponse'];
headers?: Record<string, string>; headers?: Record<string, string>;
timeout?: number; timeout?: number;
@ -120,20 +122,22 @@ export class Query {
*/ */
async post<R = any, P = any>(body: Data & P, options?: DataOpts): Promise<Result<R>> { async post<R = any, P = any>(body: Data & P, options?: DataOpts): Promise<Result<R>> {
const url = options?.url || this.url; const url = options?.url || this.url;
const headers = { ...this.headers, ...options?.headers }; const { headers, adapter, beforeRequest, afterResponse, timeout, ...rest } = options || {};
const adapter = options?.adapter || this.adapter; const _headers = { ...this.headers, ...headers };
const beforeRequest = options?.beforeRequest || this.beforeRequest; const _adapter = adapter || this.adapter;
const afterResponse = options?.afterResponse || this.afterResponse; const _beforeRequest = beforeRequest || this.beforeRequest;
const timeout = options?.timeout || this.timeout; const _afterResponse = afterResponse || this.afterResponse;
const _timeout = timeout || this.timeout;
const req = { const req = {
url: url, url: url,
headers: headers, headers: _headers,
body, body,
timeout, timeout: _timeout,
...rest,
}; };
try { try {
if (beforeRequest) { if (_beforeRequest) {
await beforeRequest(req); await _beforeRequest(req);
} }
} catch (e) { } catch (e) {
console.error('request beforeFn error', e, req); console.error('request beforeFn error', e, req);
@ -160,11 +164,11 @@ export class Query {
}, 1000); }, 1000);
}); });
} }
return adapter(req).then(async (res) => { return _adapter(req).then(async (res) => {
try { try {
setBaseResponse(res); setBaseResponse(res);
if (afterResponse) { if (_afterResponse) {
return await afterResponse(res, { return await _afterResponse(res, {
req, req,
res, res,
fetch: adapter, fetch: adapter,
@ -187,14 +191,14 @@ export class Query {
* *
* @param fn * @param fn
*/ */
before(fn: Fn) { before(fn: DataOpts['beforeRequest']) {
this.beforeRequest = fn; this.beforeRequest = fn;
} }
/** /**
* *
* @param fn * @param fn
*/ */
after(fn: (result: Result, req?: any) => Promise<any>) { after(fn: DataOpts['afterResponse']) {
this.afterResponse = fn; this.afterResponse = fn;
} }
} }