Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复 requestType 类型声明缺少自定义类型 #300

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ coverage
.vscode
.DS_Store
/yarn.lock

.idea
22 changes: 17 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type ResponseType = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData';

export interface ResponseError<D = any> extends Error {
name: string;
data: D;
Expand All @@ -9,6 +10,7 @@ export interface ResponseError<D = any> extends Error {
};
type: string;
}

/**
* 增加的参数
* @param {string} requestType post类型, 用来简化写content-Type, 默认json
Expand All @@ -26,7 +28,7 @@ export interface ResponseError<D = any> extends Error {
*/
export interface RequestOptionsInit extends RequestInit {
charset?: 'utf8' | 'gbk';
requestType?: 'json' | 'form';
requestType?: 'json' | 'form' | string;
data?: any;
params?: object | URLSearchParams;
paramsSerializer?: (params: object) => string;
Expand All @@ -44,6 +46,7 @@ export interface RequestOptionsInit extends RequestInit {
getResponse?: boolean;
validateCache?: (url: string, options: RequestOptionsInit) => boolean;
__umiRequestCoreType__?: string;

[key: string]: any;
}

Expand Down Expand Up @@ -83,8 +86,11 @@ export type OnionOptions = { global?: boolean; core?: boolean; defaultInstance?:

export interface RequestMethod<R = false> {
<T = any>(url: string, options: RequestOptionsWithResponse): Promise<RequestResponse<T>>;

<T = any>(url: string, options: RequestOptionsWithoutResponse): Promise<T>;

<T = any>(url: string, options?: RequestOptionsInit): R extends true ? Promise<RequestResponse<T>> : Promise<T>;

get: RequestMethod<R>;
post: RequestMethod<R>;
delete: RequestMethod<R>;
Expand All @@ -105,7 +111,9 @@ export interface RequestMethod<R = false> {
fetchIndex: number;
Cancel: CancelStatic;
CancelToken: CancelTokenStatic;

isCancel(value: any): boolean;

extendOptions: (options: RequestOptionsInit) => void;
middlewares: {
instance: OnionMiddleware[];
Expand All @@ -127,14 +135,16 @@ export type ExtendOptionsWithResponse = RequestOptionsWithResponse & ExtendOnlyO

export interface Extend {
(options: ExtendOptionsWithoutResponse): RequestMethod<false>;

(options: ExtendOptionsWithResponse): RequestMethod<true>;

(options: ExtendOptionsInit): RequestMethod;
}

export declare var extend: Extend;

export interface CancelStatic {
new (message?: string): Cancel;
new(message?: string): Cancel;
}

export interface Cancel {
Expand All @@ -146,13 +156,15 @@ export interface Canceler {
}

export interface CancelTokenStatic {
new (executor: (cancel: Canceler) => void): CancelToken;
new(executor: (cancel: Canceler) => void): CancelToken;

source(): CancelTokenSource;
}

export interface CancelToken {
promise: Promise<Cancel>;
reason?: Cancel;

throwIfRequested(): void;
}

Expand All @@ -165,7 +177,7 @@ declare var request: RequestMethod;

export declare var fetch: RequestMethod;

export declare var AbortController: { prototype: AbortController; new (): AbortController };
export declare var AbortSignal: { prototype: AbortSignal; new (): AbortSignal };
export declare var AbortController: { prototype: AbortController; new(): AbortController };
export declare var AbortSignal: { prototype: AbortSignal; new(): AbortSignal };

export default request;