chore: bump version to 0.0.53, update dependencies, and enhance query handling with token support
This commit is contained in:
20
src/query.ts
20
src/query.ts
@@ -1,4 +1,4 @@
|
||||
import { adapter, Method, AdapterOpts } from './adapter.ts';
|
||||
import { adapter, Method, AdapterOpts, methods } from './adapter.ts';
|
||||
import type { QueryWs } from './ws.ts';
|
||||
/**
|
||||
* 请求前处理函数
|
||||
@@ -15,6 +15,7 @@ export type Fn = (opts: {
|
||||
|
||||
export type QueryOpts = {
|
||||
adapter?: typeof adapter;
|
||||
token?: string;
|
||||
[key: string]: any;
|
||||
} & AdapterOpts;
|
||||
|
||||
@@ -155,6 +156,7 @@ export class Query {
|
||||
const _beforeRequest = beforeRequest || this.beforeRequest;
|
||||
const _afterResponse = afterResponse || this.afterResponse;
|
||||
const _timeout = timeout || this.timeout;
|
||||
|
||||
const req = {
|
||||
url: url,
|
||||
headers: _headers,
|
||||
@@ -182,6 +184,10 @@ export class Query {
|
||||
});
|
||||
}
|
||||
}
|
||||
const headers = req.headers || {};
|
||||
if (options?.token && !headers['Authorization']) {
|
||||
headers['Authorization'] = `Bearer ${options.token}`;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('request beforeFn error', e, req);
|
||||
return wrapperError({
|
||||
@@ -254,20 +260,20 @@ export class Query {
|
||||
this.afterResponse = fn;
|
||||
}
|
||||
async fetchText(urlOrOptions?: string | QueryOpts, options?: QueryOpts): Promise<Result<any>> {
|
||||
let _options = { ...options };
|
||||
let _options = { method: 'GET' as const, ...options };
|
||||
if (typeof urlOrOptions === 'string' && !_options.url) {
|
||||
_options.url = urlOrOptions;
|
||||
}
|
||||
if (typeof urlOrOptions === 'object') {
|
||||
_options = { ...urlOrOptions, ..._options };
|
||||
}
|
||||
const headers = { ...this.headers, ..._options.headers };
|
||||
if (options?.token && !headers['Authorization'] && _options.method !== 'GET') {
|
||||
headers['Authorization'] = `Bearer ${options.token}`;
|
||||
}
|
||||
const res = await adapter({
|
||||
method: 'GET',
|
||||
..._options,
|
||||
headers: {
|
||||
...this.headers,
|
||||
...(_options?.headers || {}),
|
||||
},
|
||||
headers,
|
||||
});
|
||||
if (res && !res.code) {
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user