mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-01 04:40:38 +08:00
got 替换为 uudici
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { request as undiciRequest, Dispatcher } from 'undici';
|
||||
|
||||
type RequestBaseOptions = {
|
||||
dispatcher?: Dispatcher;
|
||||
json?: Record<string, any>;
|
||||
form?: string;
|
||||
headers?: Record<string, string>;
|
||||
} & Omit<Dispatcher.RequestOptions<null>, 'origin' | 'path' | 'method'>;
|
||||
|
||||
type RequestOptionsWithOptions = RequestBaseOptions &
|
||||
Partial<Pick<Dispatcher.RequestOptions, 'method'>>;
|
||||
|
||||
type ResponseTypeMap = {
|
||||
json: Record<string, any>;
|
||||
text: string;
|
||||
};
|
||||
|
||||
type ResponseTypeKey = keyof ResponseTypeMap;
|
||||
|
||||
async function request(
|
||||
url: string,
|
||||
options?: RequestOptionsWithOptions,
|
||||
): Promise<Dispatcher.ResponseData<null>> {
|
||||
const { json, form, body, headers = {}, ...rest } = options || {};
|
||||
const finalHeaders = { ...headers } as Record<string, string>;
|
||||
let finalBody = body;
|
||||
|
||||
if (json) {
|
||||
finalHeaders['content-type'] = 'application/json';
|
||||
finalBody = JSON.stringify(json);
|
||||
} else if (form) {
|
||||
finalBody = form;
|
||||
delete finalHeaders['content-type'];
|
||||
}
|
||||
|
||||
const res = await undiciRequest(url, {
|
||||
method: 'POST',
|
||||
headers: finalHeaders,
|
||||
body: finalBody,
|
||||
...rest,
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
async function post<T extends ResponseTypeKey = 'json'>(
|
||||
url: string,
|
||||
options?: RequestBaseOptions & { responseType?: T },
|
||||
): Promise<ResponseTypeMap[T]> {
|
||||
const resp = await request(url, { ...options, method: 'POST' });
|
||||
|
||||
const rawText = await resp.body.text();
|
||||
|
||||
if (options?.responseType === 'text') {
|
||||
return rawText as ResponseTypeMap[T];
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(rawText) as ResponseTypeMap[T];
|
||||
} catch {
|
||||
return rawText as ResponseTypeMap[T];
|
||||
}
|
||||
}
|
||||
|
||||
export const httpClient = {
|
||||
post,
|
||||
request,
|
||||
};
|
||||
+1
-3
@@ -1,9 +1,6 @@
|
||||
import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import got from 'got';
|
||||
import iconv from 'iconv-lite';
|
||||
import { exec } from 'child_process';
|
||||
import FormData from 'form-data';
|
||||
import psTreeFun from 'ps-tree';
|
||||
import { promisify } from 'util';
|
||||
import { load } from 'js-yaml';
|
||||
@@ -12,6 +9,7 @@ import { PYTHON_INSTALL_DIR, TASK_COMMAND } from './const';
|
||||
import Logger from '../loaders/logger';
|
||||
import { writeFileWithLock } from '../shared/utils';
|
||||
import { DependenceTypes } from '../data/dependence';
|
||||
import { FormData } from 'undici';
|
||||
|
||||
export * from './share';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user