Skip to content

Commit

Permalink
perf(req): 增加 reqOptions 参数,支持初始化时设置更多的默认请求参数
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Apr 2, 2024
1 parent 70701e8 commit 8d9c978
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/common/lib/ReqFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* @Author: renxia
* @Date: 2024-01-15 11:26:52
* @LastEditors: renxia
* @LastEditTime: 2024-03-14 09:46:01
* @LastEditTime: 2024-04-02 13:44:22
* @Description:
*/
import type { OutgoingHttpHeaders } from 'node:http';
import type { RequestOptions } from 'node:https';
import { urlFormat } from '../url';
import { assign, toLowcaseKeyObject } from '../objects';
import type { AnyObject } from '../../types';
Expand All @@ -19,6 +20,7 @@ export interface ReqConfig {
cookie?: string;
headers?: OutgoingHttpHeaders;
prefixUrl?: string;
reqOptions?: ReqOptions | RequestOptions;
}

export class ReqBase {
Expand All @@ -30,7 +32,7 @@ export class ReqBase {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'accept-language': 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,es;q=0.2',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
};
protected isBrowser = typeof document !== 'undefined' && typeof window !== 'undefined';
protected config: ReqConfig = {};
Expand All @@ -44,6 +46,7 @@ export class ReqBase {
if (this.config.cookie) this.setCookie(this.config.cookie);
if (this.config.headers) this.setHeaders(this.config.headers);
if (headers) this.setHeaders(headers);
if (this.config.reqOptions?.headers) this.setHeaders(this.config.reqOptions.headers);
}
getHeaders(urlObject?: URL, headers?: OutgoingHttpHeaders) {
headers = {
Expand Down Expand Up @@ -81,15 +84,15 @@ export class ReqFetch extends ReqBase {
if (!this.instance) this.instance = new ReqFetch();
return this.instance;
}
constructor(cookie?: string, headers?: OutgoingHttpHeaders) {
constructor(cookie?: string | (Omit<ReqConfig, 'reqOptions'> & { reqOptions?: ReqOptions }), headers?: OutgoingHttpHeaders) {
super(cookie, headers);
}
req(url: string | URL, parameters?: AnyObject, options: ReqOptions = {}) {
if (typeof url === 'string') {
if (!/^[A-Za-z]+:\/\//.test(url) && this.config.prefixUrl) url = this.config.prefixUrl + url;
url = new URL(url);
}
options = { ...options, headers: this.getHeaders(url, options.headers) };
options = { ...this.config.reqOptions, ...options, headers: this.getHeaders(url, options.headers) };

if (parameters) {
options.body = String(options.headers!['content-type']).includes('application/json')
Expand Down Expand Up @@ -119,4 +122,4 @@ export class ReqFetch extends ReqBase {
}
}

// new ReqFetch().get('https://www.baidu.com').then(d => console.log(d.response.status, d.data.length));
// new ReqFetch({}).get('https://www.baidu.com').then(d => console.log(d.response.status, d.data.length));
5 changes: 3 additions & 2 deletions src/common/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: renxia
* @Date: 2023-03-23 23:05:16
* @LastEditors: renxia
* @LastEditTime: 2024-01-11 10:58:35
* @LastEditTime: 2024-04-01 11:23:08
* @Description:
*/
/**
Expand Down Expand Up @@ -52,7 +52,8 @@ export function toQueryString(params: Record<string, unknown>) {
export function getUrlParams(query = location.search) {
const ret: Record<string, string> = {};
if (query) {
const parts = query.slice(1).split('&');
if (query.includes('?')) query = query.slice(query.indexOf('?') + 1);
const parts = query.split('&');
for (const line of parts) {
const kv = line.split('=').map(d => decodeURIComponent(d));
ret[kv[0]] = kv[1] || '';
Expand Down
6 changes: 5 additions & 1 deletion src/node/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import zlib from 'node:zlib';
import http, { type IncomingMessage, type IncomingHttpHeaders, type OutgoingHttpHeaders } from 'node:http';
import https, { type RequestOptions } from 'node:https';
import { urlFormat } from '../../common/url';
import { ReqBase } from '../../common/lib/ReqFetch';
import { ReqBase, type ReqConfig } from '../../common/lib/ReqFetch';
import type { AnyObject } from '../../types';

export class Request extends ReqBase {
Expand All @@ -12,6 +12,9 @@ export class Request extends ReqBase {
if (!this.instance) this.instance = new Request();
return this.instance;
}
constructor(cookie?: string | (Omit<ReqConfig, 'reqOptions'> & { reqOptions?: RequestOptions }), headers?: OutgoingHttpHeaders) {
super(cookie, headers);
}
req(url: string | URL, parameters?: AnyObject, options: RequestOptions = {}, autoRedirect = true) {
if (typeof url === 'string') {
if (!url.startsWith('http')) url = this.config.prefixUrl + url;
Expand All @@ -21,6 +24,7 @@ export class Request extends ReqBase {
let postBody = '';
const { protocol, port } = url;
options = {
...(this.config.reqOptions as RequestOptions),
...options,
hostname: url.host.split(':')[0],
port,
Expand Down

0 comments on commit 8d9c978

Please sign in to comment.