feat: 更新打包方式,dts
This commit is contained in:
135
src/index.ts
135
src/index.ts
@@ -1,134 +1 @@
|
||||
import { adapter } from './adapter.ts';
|
||||
import { QueryWs, QueryWsOpts } from './ws.ts';
|
||||
export { QueryOpts, QueryWs };
|
||||
type Fn = (opts: {
|
||||
url?: string;
|
||||
headers?: Record<string, string>;
|
||||
body?: Record<string, any>;
|
||||
[key: string]: any;
|
||||
timeout?: number;
|
||||
}) => Promise<Record<string, any>>;
|
||||
|
||||
type QueryOpts = {
|
||||
url?: string;
|
||||
adapter?: typeof adapter;
|
||||
headers?: Record<string, string>;
|
||||
timeout?: number;
|
||||
};
|
||||
type Data = {
|
||||
path?: string;
|
||||
key?: string;
|
||||
payload?: Record<string, any>;
|
||||
[key: string]: any;
|
||||
};
|
||||
type Result<S = any> = {
|
||||
code: number;
|
||||
data?: S;
|
||||
message?: string;
|
||||
success: boolean;
|
||||
};
|
||||
// 额外功能
|
||||
type DataOpts = Partial<QueryOpts> & {
|
||||
beforeRequest?: Fn;
|
||||
afterResponse?: (result: Result) => Promise<any>;
|
||||
};
|
||||
/**
|
||||
* const query = new Query();
|
||||
* const res = await query.post({
|
||||
* path: 'demo',
|
||||
* key: '1',
|
||||
* });
|
||||
*
|
||||
* U是参数 V是返回值
|
||||
*/
|
||||
export class Query<U = any, V = any> {
|
||||
adapter: typeof adapter;
|
||||
url: string;
|
||||
beforeRequest?: Fn;
|
||||
afterResponse?: (result: Result) => Promise<any>;
|
||||
headers?: Record<string, string>;
|
||||
timeout?: number;
|
||||
constructor(opts?: QueryOpts) {
|
||||
this.adapter = opts?.adapter || adapter;
|
||||
this.url = opts?.url || '/api/router';
|
||||
this.headers = opts?.headers || {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
||||
}
|
||||
async get<T = any, S = any>(params: Record<string, any> & Data & U & T, options?: DataOpts): Promise<Result<V & S>> {
|
||||
return this.post(params, options);
|
||||
}
|
||||
async post<T = any, S = any>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>> {
|
||||
const url = options?.url || this.url;
|
||||
const headers = { ...this.headers, ...options?.headers };
|
||||
const adapter = options?.adapter || this.adapter;
|
||||
const beforeRequest = options?.beforeRequest || this.beforeRequest;
|
||||
const afterResponse = options?.afterResponse || this.afterResponse;
|
||||
const timeout = options?.timeout || this.timeout;
|
||||
const req = {
|
||||
url: url,
|
||||
headers: headers,
|
||||
body,
|
||||
timeout,
|
||||
};
|
||||
if (beforeRequest) {
|
||||
await beforeRequest(req);
|
||||
}
|
||||
return adapter(req).then(async (res) => {
|
||||
res.success = res.code === 200;
|
||||
if (afterResponse) {
|
||||
return await afterResponse(res);
|
||||
}
|
||||
return res;
|
||||
});
|
||||
}
|
||||
before(fn: Fn) {
|
||||
this.beforeRequest = fn;
|
||||
}
|
||||
after(fn: (result: Result) => Promise<any>) {
|
||||
this.afterResponse = fn;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 前端调用后端QueryRouter
|
||||
*/
|
||||
export class QueryClient<U = any, V = any> extends Query<U, V> {
|
||||
tokenName: string;
|
||||
storage: Storage;
|
||||
token: string;
|
||||
qws: QueryWs;
|
||||
constructor(opts?: QueryOpts & { tokenName?: string; storage?: Storage; io?: boolean }) {
|
||||
super(opts);
|
||||
this.tokenName = opts?.tokenName || 'token';
|
||||
this.storage = opts?.storage || localStorage;
|
||||
this.beforeRequest = async (opts) => {
|
||||
const token = this.token || this.getToken();
|
||||
if (token) {
|
||||
opts.headers = {
|
||||
...opts.headers,
|
||||
Authorization: `Bearer ${token}`,
|
||||
};
|
||||
}
|
||||
return opts;
|
||||
};
|
||||
if (opts?.io) {
|
||||
this.createWs();
|
||||
}
|
||||
}
|
||||
createWs(opts?: QueryWsOpts) {
|
||||
this.qws = new QueryWs({ url: this.url, ...opts });
|
||||
}
|
||||
getToken() {
|
||||
return this.storage.getItem(this.tokenName);
|
||||
}
|
||||
saveToken(token: string) {
|
||||
this.storage.setItem(this.tokenName, token);
|
||||
}
|
||||
removeToken() {
|
||||
this.storage.removeItem(this.tokenName);
|
||||
}
|
||||
}
|
||||
export const client = new QueryClient();
|
||||
|
||||
export { adapter };
|
||||
export * from './query-browser.ts'
|
||||
@@ -1,57 +0,0 @@
|
||||
import http from 'http';
|
||||
|
||||
type AdapterOpts = {
|
||||
url: string;
|
||||
headers?: Record<string, string>;
|
||||
body?: Record<string, any>;
|
||||
};
|
||||
export const nodeAdapter = async (opts: AdapterOpts): Promise<any> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const postData = JSON.stringify(opts.body || '');
|
||||
const _url = new URL(opts.url);
|
||||
const { hostname, port, pathname } = _url;
|
||||
const options = {
|
||||
hostname: hostname,
|
||||
port: port,
|
||||
path: pathname || '/api/router',
|
||||
method: 'POST', // Assuming it's a POST request
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(postData),
|
||||
...opts.headers,
|
||||
},
|
||||
};
|
||||
|
||||
const req = http.request(options, (res) => {
|
||||
let data = '';
|
||||
|
||||
// Collect data chunks
|
||||
res.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
// Resolve when the response is complete
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const parsedData = JSON.parse(data);
|
||||
resolve(parsedData);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Handle request errors
|
||||
req.on('error', (error) => {
|
||||
reject(error);
|
||||
});
|
||||
|
||||
// Write the request body and end the request
|
||||
if (opts.body) {
|
||||
req.write(postData);
|
||||
}
|
||||
req.end();
|
||||
});
|
||||
};
|
||||
|
||||
export const adapter = nodeAdapter;
|
||||
54
src/query-browser.ts
Normal file
54
src/query-browser.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { adapter } from './adapter.ts';
|
||||
import { QueryWs, QueryWsOpts } from './ws.ts';
|
||||
export { QueryOpts, QueryWs };
|
||||
import { Query } from './query.ts';
|
||||
|
||||
type QueryOpts = {
|
||||
url?: string;
|
||||
adapter?: typeof adapter;
|
||||
headers?: Record<string, string>;
|
||||
timeout?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* 前端调用后端QueryRouter
|
||||
*/
|
||||
export class QueryClient<U = any, V = any> extends Query<U, V> {
|
||||
tokenName: string;
|
||||
storage: Storage;
|
||||
token: string;
|
||||
qws: QueryWs;
|
||||
constructor(opts?: QueryOpts & { tokenName?: string; storage?: Storage; io?: boolean }) {
|
||||
super(opts);
|
||||
this.tokenName = opts?.tokenName || 'token';
|
||||
this.storage = opts?.storage || localStorage;
|
||||
this.beforeRequest = async (opts) => {
|
||||
const token = this.token || this.getToken();
|
||||
if (token) {
|
||||
opts.headers = {
|
||||
...opts.headers,
|
||||
Authorization: `Bearer ${token}`,
|
||||
};
|
||||
}
|
||||
return opts;
|
||||
};
|
||||
if (opts?.io) {
|
||||
this.createWs();
|
||||
}
|
||||
}
|
||||
createWs(opts?: QueryWsOpts) {
|
||||
this.qws = new QueryWs({ url: this.url, ...opts });
|
||||
}
|
||||
getToken() {
|
||||
return this.storage.getItem(this.tokenName);
|
||||
}
|
||||
saveToken(token: string) {
|
||||
this.storage.setItem(this.tokenName, token);
|
||||
}
|
||||
removeToken() {
|
||||
this.storage.removeItem(this.tokenName);
|
||||
}
|
||||
}
|
||||
export const client = new QueryClient();
|
||||
|
||||
export { adapter };
|
||||
92
src/query.ts
Normal file
92
src/query.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { adapter } from './adapter.ts';
|
||||
type Fn = (opts: {
|
||||
url?: string;
|
||||
headers?: Record<string, string>;
|
||||
body?: Record<string, any>;
|
||||
[key: string]: any;
|
||||
timeout?: number;
|
||||
}) => Promise<Record<string, any>>;
|
||||
|
||||
export type QueryOpts = {
|
||||
url?: string;
|
||||
adapter?: typeof adapter;
|
||||
headers?: Record<string, string>;
|
||||
timeout?: number;
|
||||
};
|
||||
type Data = {
|
||||
path?: string;
|
||||
key?: string;
|
||||
payload?: Record<string, any>;
|
||||
[key: string]: any;
|
||||
};
|
||||
type Result<S = any> = {
|
||||
code: number;
|
||||
data?: S;
|
||||
message?: string;
|
||||
success: boolean;
|
||||
};
|
||||
// 额外功能
|
||||
type DataOpts = Partial<QueryOpts> & {
|
||||
beforeRequest?: Fn;
|
||||
afterResponse?: (result: Result) => Promise<any>;
|
||||
};
|
||||
/**
|
||||
* const query = new Query();
|
||||
* const res = await query.post({
|
||||
* path: 'demo',
|
||||
* key: '1',
|
||||
* });
|
||||
*
|
||||
* U是参数 V是返回值
|
||||
*/
|
||||
export class Query<U = any, V = any> {
|
||||
adapter: typeof adapter;
|
||||
url: string;
|
||||
beforeRequest?: Fn;
|
||||
afterResponse?: (result: Result) => Promise<any>;
|
||||
headers?: Record<string, string>;
|
||||
timeout?: number;
|
||||
constructor(opts?: QueryOpts) {
|
||||
this.adapter = opts?.adapter || adapter;
|
||||
this.url = opts?.url || '/api/router';
|
||||
this.headers = opts?.headers || {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
||||
}
|
||||
async get<T = any, S = any>(params: Record<string, any> & Data & U & T, options?: DataOpts): Promise<Result<V & S>> {
|
||||
return this.post(params, options);
|
||||
}
|
||||
async post<T = any, S = any>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>> {
|
||||
const url = options?.url || this.url;
|
||||
const headers = { ...this.headers, ...options?.headers };
|
||||
const adapter = options?.adapter || this.adapter;
|
||||
const beforeRequest = options?.beforeRequest || this.beforeRequest;
|
||||
const afterResponse = options?.afterResponse || this.afterResponse;
|
||||
const timeout = options?.timeout || this.timeout;
|
||||
const req = {
|
||||
url: url,
|
||||
headers: headers,
|
||||
body,
|
||||
timeout,
|
||||
};
|
||||
if (beforeRequest) {
|
||||
await beforeRequest(req);
|
||||
}
|
||||
return adapter(req).then(async (res) => {
|
||||
res.success = res.code === 200;
|
||||
if (afterResponse) {
|
||||
return await afterResponse(res);
|
||||
}
|
||||
return res;
|
||||
});
|
||||
}
|
||||
before(fn: Fn) {
|
||||
this.beforeRequest = fn;
|
||||
}
|
||||
after(fn: (result: Result) => Promise<any>) {
|
||||
this.afterResponse = fn;
|
||||
}
|
||||
}
|
||||
|
||||
export { adapter };
|
||||
Reference in New Issue
Block a user