Skip to content

Commit

Permalink
perf(Req): 支持配置默认 prefixUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Feb 1, 2024
1 parent ca16e24 commit 894b7f1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
36 changes: 26 additions & 10 deletions src/common/lib/ReqFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
* @Author: renxia
* @Date: 2024-01-15 11:26:52
* @LastEditors: renxia
* @LastEditTime: 2024-01-18 11:15:16
* @LastEditTime: 2024-02-01 10:10:05
* @Description:
*/
import type { OutgoingHttpHeaders } from 'node:http';
import { urlFormat } from '../url';
import { toLowcaseKeyObject } from '../objects';
import { assign, toLowcaseKeyObject } from '../objects';
import type { AnyObject } from '../../types';
import { cookieParse, cookieStringfiy } from '../cookie';

interface ReqOptions extends Omit<RequestInit, 'headers'> {
headers?: OutgoingHttpHeaders;
}

export interface ReqConfig {
cookie?: string;
headers?: OutgoingHttpHeaders;
prefixUrl?: string;
}

export class ReqBase {
protected cookies: Record<string, string> = {};
protected headers: OutgoingHttpHeaders = {
Expand All @@ -26,8 +32,15 @@ export class ReqBase {
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',
};
constructor(cookie?: string, headers?: OutgoingHttpHeaders) {
if (cookie) this.setCookie(cookie);
protected config: ReqConfig = {};
constructor(config?: string | ReqConfig, headers?: OutgoingHttpHeaders) {
if (config) {
if (typeof config === 'string') config = { cookie: config };
config = assign(this.config, config);
}

if (this.config.cookie) this.setCookie(this.config.cookie);
if (this.config.headers) this.setHeaders(this.config.headers);
if (headers) this.setHeaders(headers);
}
getHeaders(urlObject?: URL, headers?: OutgoingHttpHeaders) {
Expand All @@ -40,8 +53,7 @@ export class ReqBase {
if (!headers.host) headers.host = urlObject.host;
if (!headers.origin) headers.origin = urlObject.origin || `${urlObject.protocol}://${urlObject.hostname}`;
}

if (!headers.cookie && Object.keys(this.cookies).length > 0) headers.cookie = this.getCookie() as string;
if (!headers.cookie && Object.keys(this.cookies).length > 0) headers.cookie = this.getCookie();

return headers;
}
Expand All @@ -54,6 +66,8 @@ export class ReqBase {
Object.assign(this.cookies, cookieParse(cookie));
return this;
}
getCookie(isString?: true): string;
getCookie(isString: false): Record<string, string>;
getCookie(isString = true) {
return isString ? cookieStringfiy(this.cookies) : this.cookies;
}
Expand All @@ -69,17 +83,19 @@ export class ReqFetch extends ReqBase {
super(cookie, headers);
}
req(url: string | URL, parameters?: AnyObject, options: ReqOptions = {}) {
const urlObject = typeof url === 'string' ? new URL(url) : url;
options = { ...options, headers: this.getHeaders(urlObject, options.headers) };
if (typeof url === 'string') {
if (!url.startsWith('http') && this.config.prefixUrl) url = this.config.prefixUrl + url;
url = new URL(url);
}
options = { ...options, headers: this.getHeaders(url, options.headers) };

if (parameters) {
options.body = String(options.headers!['content-type']).includes('application/json')
? JSON.stringify(parameters)
: new URLSearchParams(parameters as Record<string, string>).toString();
options.headers!['content-length'] = Buffer.byteLength(options.body).toString();
}

return fetch(urlObject, options as never);
return fetch(url, options as never);
}
async request<T = AnyObject>(method: string, url: string | URL, parameters?: AnyObject, options?: ReqOptions) {
const response = await this.req(url, parameters, { ...options, method });
Expand Down
22 changes: 12 additions & 10 deletions src/node/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ export class Request extends ReqBase {
if (!this.instance) this.instance = new Request();
return this.instance;
}
constructor(cookie?: string, headers?: OutgoingHttpHeaders) {
super(cookie, headers);
}
req(url: string | URL, parameters?: AnyObject, options: RequestOptions = {}, autoRedirect = true) {
const urlObject = typeof url === 'string' ? new URL(url) : url;
if (typeof url === 'string') {
if (!url.startsWith('http')) url = this.config.prefixUrl + url;
url = new URL(url);
}

let postBody = '';
const { protocol, port } = url;
options = {
...options,
hostname: urlObject.host.split(':')[0],
port: urlObject.port,
path: urlObject.href.split(urlObject.host)[1],
headers: this.getHeaders(urlObject, options.headers),
hostname: url.host.split(':')[0],
port,
path: url.href.split(url.host)[1],
headers: this.getHeaders(url, options.headers),
};
let postBody = '';

if (parameters) {
postBody = String(options.headers!['content-type']).includes('application/json')
Expand All @@ -34,7 +36,7 @@ export class Request extends ReqBase {
}

return new Promise<{ req: http.ClientRequest; res: IncomingMessage }>((resolve, reject) => {
const h = urlObject.protocol === 'http:' ? http : https;
const h = protocol === 'http:' ? http : https;
let timer: NodeJS.Timeout;
const req: http.ClientRequest = h.request(options, res => {
globalThis.clearTimeout(timer);
Expand Down

0 comments on commit 894b7f1

Please sign in to comment.