hook-fetch 2.0.5 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +23 -9
- package/README.md +24 -10
- package/dist/cjs/index.cjs +3 -2
- package/dist/cjs/plugins/index.cjs +1 -1
- package/dist/cjs/plugins/sse.cjs +1 -1
- package/dist/cjs/react/index.cjs +2 -1
- package/dist/{sse-BcLGP_P_.cjs → cjs/sse.cjs} +2 -1
- package/dist/cjs/vue/index.cjs +2 -1
- package/dist/es/index.mjs +3 -2
- package/dist/es/plugins/index.mjs +1 -1
- package/dist/es/plugins/sse.mjs +1 -1
- package/dist/es/react/index.mjs +2 -1
- package/dist/{sse-D_UhnGyk.js → es/sse.mjs} +2 -1
- package/dist/es/vue/index.mjs +2 -1
- package/dist/umd/index.js +5 -4
- package/package.json +19 -14
- package/types/index-n7kSnbyZ.d.ts +108 -0
- package/types/index.d.ts +3 -6
- package/types/plugins/index.d.ts +3 -1
- package/types/plugins/sse.d.ts +3 -45
- package/types/react/index.d.ts +23 -31
- package/types/sse-DPRkQwhR.d.ts +23 -0
- package/types/types-CAGBd7wv.d.ts +110 -0
- package/types/vue/index.d.ts +22 -29
- package/types/base.d.ts +0 -41
- package/types/enum.d.ts +0 -26
- package/types/error.d.ts +0 -19
- package/types/types.d.ts +0 -92
- package/types/umd.d.ts +0 -7
- package/types/utils.d.ts +0 -38
- /package/dist/{chunk-mbd8xe7s.cjs → cjs/chunk.cjs} +0 -0
package/package.json
CHANGED
|
@@ -18,7 +18,18 @@
|
|
|
18
18
|
"hook-fetch-plugin-response-transform",
|
|
19
19
|
"hook-fetch-plugin-request-transform"
|
|
20
20
|
],
|
|
21
|
-
"version": "2.0
|
|
21
|
+
"version": "2.1.0",
|
|
22
|
+
"scripts": {
|
|
23
|
+
"dev": "vite",
|
|
24
|
+
"build:rolldown": "rolldown -c ./rolldown.config.ts",
|
|
25
|
+
"build:umd": "vite build -c ./vite.config.umd.ts",
|
|
26
|
+
"build:base": "rimraf types && vite build",
|
|
27
|
+
"build": "rimraf types && rimraf dist && pnpm build:rolldown",
|
|
28
|
+
"publish:release": "npm publish",
|
|
29
|
+
"publish:beta": "npm publish --tag beta",
|
|
30
|
+
"test": "vitest",
|
|
31
|
+
"lint-staged": "lint-staged"
|
|
32
|
+
},
|
|
22
33
|
"type": "module",
|
|
23
34
|
"files": [
|
|
24
35
|
"dist",
|
|
@@ -31,7 +42,9 @@
|
|
|
31
42
|
"main": "./dist/cjs/index.cjs",
|
|
32
43
|
"module": "./dist/es/index.mjs",
|
|
33
44
|
"types": "./types/index.d.ts",
|
|
45
|
+
"browser": "./dist/umd/index.js",
|
|
34
46
|
"unpkg": "./dist/umd/index.js",
|
|
47
|
+
"jsdelivr": "./dist/umd/index.js",
|
|
35
48
|
"exports": {
|
|
36
49
|
".": {
|
|
37
50
|
"require": "./dist/cjs/index.cjs",
|
|
@@ -82,12 +95,14 @@
|
|
|
82
95
|
"oxlint": "^0.16.3",
|
|
83
96
|
"react": "^19.1.0",
|
|
84
97
|
"rimraf": "^6.0.1",
|
|
85
|
-
"rolldown": "1.0.0-beta.
|
|
98
|
+
"rolldown": "1.0.0-beta.28",
|
|
99
|
+
"rolldown-plugin-dts": "^0.13.6",
|
|
86
100
|
"typescript": "^5.7.3",
|
|
87
101
|
"vite": "npm:rolldown-vite@latest",
|
|
88
102
|
"vite-plugin-dts": "^4.5.3",
|
|
89
103
|
"vitest": "^3.0.8",
|
|
90
|
-
"vue": "^3.5.16"
|
|
104
|
+
"vue": "^3.5.16",
|
|
105
|
+
"@jsonlee/tsconfig": "workspace:^"
|
|
91
106
|
},
|
|
92
107
|
"peerDependencies": {
|
|
93
108
|
"react": "^19.1.0",
|
|
@@ -105,15 +120,5 @@
|
|
|
105
120
|
"commitizen": {
|
|
106
121
|
"path": "node_modules/cz-git"
|
|
107
122
|
}
|
|
108
|
-
},
|
|
109
|
-
"scripts": {
|
|
110
|
-
"dev": "vite",
|
|
111
|
-
"build:umd": "vite build -c ./vite.config.umd.ts",
|
|
112
|
-
"build:base": "rimraf types && vite build",
|
|
113
|
-
"build": "rimraf types && pnpm build:base && pnpm build:umd",
|
|
114
|
-
"ci:publish": "pnpm changeset publish -r",
|
|
115
|
-
"ci:beta": "pnpm changeset publish --tag beta -r",
|
|
116
|
-
"test": "vitest",
|
|
117
|
-
"lint-staged": "lint-staged"
|
|
118
123
|
}
|
|
119
|
-
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { BaseOptions, BaseRequestOptions, DeleteOptions, GetOptions, HeadOptions, HookFetchPlugin, OptionsOptions, PatchOptions, PostOptions, PutOptions, RequestOptions, ResponseError, StreamContext } from "./types-CAGBd7wv.js";
|
|
2
|
+
import { AnyObject, Generic } from "typescript-api-pro";
|
|
3
|
+
import QueryString from "qs";
|
|
4
|
+
|
|
5
|
+
//#region src/utils.d.ts
|
|
6
|
+
|
|
7
|
+
declare class HookFetchRequest<T, E> implements PromiseLike<T> {
|
|
8
|
+
#private;
|
|
9
|
+
constructor(options: BaseRequestOptions<unknown, unknown, E>);
|
|
10
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
|
|
11
|
+
catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null | undefined): Promise<T | TResult>;
|
|
12
|
+
finally(onfinally?: (() => void) | null | undefined): void;
|
|
13
|
+
abort(): void;
|
|
14
|
+
json(): Promise<T>;
|
|
15
|
+
blob(): Promise<Blob>;
|
|
16
|
+
text(): Promise<string>;
|
|
17
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
18
|
+
formData(): Promise<FormData>;
|
|
19
|
+
bytes(): Promise<Uint8Array<ArrayBufferLike>>;
|
|
20
|
+
stream<T>(): AsyncGenerator<StreamContext<T> | StreamContext<null>, ResponseError<unknown> | undefined, unknown>;
|
|
21
|
+
retry(): HookFetchRequest<unknown, E>;
|
|
22
|
+
get response(): Promise<Response>;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/base.d.ts
|
|
26
|
+
type GenericWithNull<T, R extends AnyObject | null, K extends keyof R> = R extends null ? T : Generic<Exclude<R, null>, K, T>;
|
|
27
|
+
declare class HookFetch<R extends AnyObject | null = null, K extends keyof R = never, E = AnyObject> {
|
|
28
|
+
#private;
|
|
29
|
+
constructor({
|
|
30
|
+
timeout,
|
|
31
|
+
baseURL,
|
|
32
|
+
headers,
|
|
33
|
+
plugins,
|
|
34
|
+
withCredentials
|
|
35
|
+
}: BaseOptions);
|
|
36
|
+
use(plugin: HookFetchPlugin<any, any, any, any>): this;
|
|
37
|
+
request<T = AnyObject, P = AnyObject, D = AnyObject>(url: string, {
|
|
38
|
+
timeout,
|
|
39
|
+
headers,
|
|
40
|
+
method,
|
|
41
|
+
params,
|
|
42
|
+
data,
|
|
43
|
+
qsArrayFormat,
|
|
44
|
+
withCredentials,
|
|
45
|
+
extra
|
|
46
|
+
}?: RequestOptions<P, D, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>;
|
|
47
|
+
get<T = AnyObject, P = AnyObject>(url: string, params?: P, options?: GetOptions<P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>;
|
|
48
|
+
head<T = AnyObject, P = AnyObject>(url: string, params?: P, options?: HeadOptions<P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>;
|
|
49
|
+
options<T = AnyObject, P = AnyObject, D = AnyObject>(url: string, params?: P, options?: OptionsOptions<P, D, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>;
|
|
50
|
+
delete<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, options?: DeleteOptions<P, D, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>;
|
|
51
|
+
post<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>;
|
|
52
|
+
upload<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>;
|
|
53
|
+
put<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PutOptions<D, P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>;
|
|
54
|
+
patch<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PatchOptions<D, P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>;
|
|
55
|
+
abortAll(): void;
|
|
56
|
+
}
|
|
57
|
+
declare const useRequest: <R = AnyObject, P = AnyObject, D = AnyObject, E = AnyObject>(url: string, options?: RequestOptions<P, D, E>) => HookFetchRequest<R, E>;
|
|
58
|
+
declare const request: <R = AnyObject, P = AnyObject, D = AnyObject, E = AnyObject>(url: string, options?: RequestOptions<P, D, E>) => HookFetchRequest<R, E>;
|
|
59
|
+
declare const get: <R = AnyObject, P = AnyObject, E = AnyObject>(url: string, params?: P, options?: GetOptions<P, E>) => HookFetchRequest<R, E>;
|
|
60
|
+
declare const head: <R = AnyObject, P = AnyObject, E = AnyObject>(url: string, params?: P, options?: HeadOptions<P, E>) => HookFetchRequest<R, E>;
|
|
61
|
+
declare const options: <R = AnyObject, P = AnyObject, D = AnyObject, E = AnyObject>(url: string, params?: P, options?: OptionsOptions<P, D, E>) => HookFetchRequest<R, E>;
|
|
62
|
+
declare const del: <R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, options?: DeleteOptions<P, D, E>) => HookFetchRequest<R, E>;
|
|
63
|
+
declare const post: <R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>) => HookFetchRequest<R, E>;
|
|
64
|
+
declare const upload: <R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>) => HookFetchRequest<R, E>;
|
|
65
|
+
declare const put: <R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PutOptions<D, P, E>) => HookFetchRequest<R, E>;
|
|
66
|
+
declare const patch: <R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PatchOptions<D, P, E>) => HookFetchRequest<R, E>;
|
|
67
|
+
type ExportDefault = typeof useRequest & {
|
|
68
|
+
create: <R extends AnyObject | null = null, K extends keyof R = never, E = AnyObject>(options: BaseOptions) => (HookFetch<R, K, E>['request'] & HookFetch<R, K, E>);
|
|
69
|
+
get: typeof get;
|
|
70
|
+
head: typeof head;
|
|
71
|
+
options: typeof options;
|
|
72
|
+
delete: typeof del;
|
|
73
|
+
post: typeof post;
|
|
74
|
+
put: typeof put;
|
|
75
|
+
patch: typeof patch;
|
|
76
|
+
upload: typeof upload;
|
|
77
|
+
};
|
|
78
|
+
declare const hookFetch: ExportDefault;
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/enum.d.ts
|
|
81
|
+
declare enum ContentType {
|
|
82
|
+
JSON = "application/json",
|
|
83
|
+
FORM_URLENCODED = "application/x-www-form-urlencoded",
|
|
84
|
+
FORM_DATA = "multipart/form-data",
|
|
85
|
+
TEXT = "text/plain",
|
|
86
|
+
HTML = "text/html",
|
|
87
|
+
XML = "text/xml",
|
|
88
|
+
CSV = "text/csv",
|
|
89
|
+
STREAM = "application/octet-stream",
|
|
90
|
+
}
|
|
91
|
+
declare enum StatusCode {
|
|
92
|
+
TIME_OUT = 408,
|
|
93
|
+
ABORTED = 499,
|
|
94
|
+
NETWORK_ERROR = 599,
|
|
95
|
+
BODY_NULL = 502,
|
|
96
|
+
UNKNOWN = 601,
|
|
97
|
+
}
|
|
98
|
+
declare enum Method {
|
|
99
|
+
GET = "GET",
|
|
100
|
+
POST = "POST",
|
|
101
|
+
PUT = "PUT",
|
|
102
|
+
DELETE = "DELETE",
|
|
103
|
+
PATCH = "PATCH",
|
|
104
|
+
HEAD = "HEAD",
|
|
105
|
+
OPTIONS = "OPTIONS",
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
export { ContentType, HookFetchRequest, Method, StatusCode, del, get, head, hookFetch, options, patch, post, put, request, upload };
|
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
export * from './types';
|
|
5
|
-
export * from './error';
|
|
6
|
-
export default hookFetch;
|
|
1
|
+
import { AfterResponseHandler, BaseOptions, BaseRequestOptions, BeforeRequestHandler, BeforeStreamHandler, DeleteOptions, FetchPluginContext, FetchResponseType, GetOptions, HeadOptions, HookFetchPlugin, OnErrorHandler, OnFinallyHandler, OptionProps, OptionsOptions, PatchOptions, PostOptions, PutOptions, RequestConfig, RequestMethod, RequestMethodWithBody, RequestMethodWithParams, RequestOptions, RequestUseOptions, RequestWithBodyFnOptions, RequestWithBodyOptions, RequestWithParamsFnOptions, RequestWithParamsOptions, ResponseError, ResponseErrorOptions, StreamContext, TransformStreamChunkHandler } from "./types-CAGBd7wv.js";
|
|
2
|
+
import { ContentType, Method, StatusCode, del, get, head, hookFetch, options, patch, post, put, request, upload } from "./index-n7kSnbyZ.js";
|
|
3
|
+
export { AfterResponseHandler, BaseOptions, BaseRequestOptions, BeforeRequestHandler, BeforeStreamHandler, ContentType, DeleteOptions, FetchPluginContext, FetchResponseType, GetOptions, HeadOptions, HookFetchPlugin, Method, OnErrorHandler, OnFinallyHandler, OptionProps, OptionsOptions, PatchOptions, PostOptions, PutOptions, RequestConfig, RequestMethod, RequestMethodWithBody, RequestMethodWithParams, RequestOptions, RequestUseOptions, RequestWithBodyFnOptions, RequestWithBodyOptions, RequestWithParamsFnOptions, RequestWithParamsOptions, ResponseError, ResponseErrorOptions, StatusCode, StreamContext, TransformStreamChunkHandler, hookFetch as default, del, get, head, options, patch, post, put, request, upload };
|
package/types/plugins/index.d.ts
CHANGED
package/types/plugins/sse.d.ts
CHANGED
|
@@ -1,45 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
lineSeparator: string | undefined;
|
|
5
|
-
trim: boolean;
|
|
6
|
-
json: boolean;
|
|
7
|
-
prefix: string;
|
|
8
|
-
doneSymbol: string;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* SSE 文本解码插件 | A SSE (Server-Sent Events) response text decoder plugin.
|
|
12
|
-
*
|
|
13
|
-
* 用于处理服务端推送事件(SSE)的响应流,提供以下功能:
|
|
14
|
-
* - 将二进制Buffer解码为文本
|
|
15
|
-
* - 按指定分隔符拆分事件块
|
|
16
|
-
* - 去除首尾空白字符
|
|
17
|
-
* - 自动JSON解析
|
|
18
|
-
* - 移除特定前缀
|
|
19
|
-
*
|
|
20
|
-
* Processes Server-Sent Events (SSE) response streams with the following features:
|
|
21
|
-
* - Decodes binary buffer to text
|
|
22
|
-
* - Splits event chunks by specified separator
|
|
23
|
-
* - Trims whitespace
|
|
24
|
-
* - Automatic JSON parsing
|
|
25
|
-
* - Removes specified prefix
|
|
26
|
-
*
|
|
27
|
-
* @param {Object} [options] 配置选项 | Plugin options
|
|
28
|
-
* @param {string} [options.splitSeparator='\n\n'] 分割符,用于拆分事件块(默认 '\n\n') | Separator for splitting events (default '\n\n')
|
|
29
|
-
* @param {string} [options.lineSeparator] 行分割符,用于拆分每行(可选) | Line separator for splitting each line (optional)
|
|
30
|
-
* @param {boolean} [options.trim=true] 是否去除首尾空白(默认 true) | Whether to trim whitespace (default true)
|
|
31
|
-
* @param {boolean} [options.json=false] 是否解析JSON(默认 false) | Whether to parse JSON (default false)
|
|
32
|
-
* @param {string} [options.prefix=''] 要移除的前缀,如 "data: "(默认为空) | Prefix to remove, e.g. "data: " (default '')
|
|
33
|
-
* @param {string} [options.doneSymbol] 结束标记,收到此标记时结束流(可选) | Symbol indicating stream end (optional)
|
|
34
|
-
* @returns {HookFetchPlugin} 返回 HookFetch 插件实例 | Returns a HookFetch plugin instance
|
|
35
|
-
* @example
|
|
36
|
-
* request.use(sseTextDecoderPlugin({
|
|
37
|
-
* json: true,
|
|
38
|
-
* prefix: 'data:',
|
|
39
|
-
* splitSeparator: '\n\n',
|
|
40
|
-
* doneSymbol: '[DONE]'
|
|
41
|
-
* }));
|
|
42
|
-
*/
|
|
43
|
-
export declare const sseTextDecoderPlugin: ({ splitSeparator, lineSeparator, trim, json, prefix, doneSymbol }?: Partial<SSETextDecoderPluginOptions>) => HookFetchPlugin<unknown, {
|
|
44
|
-
sseAble: boolean;
|
|
45
|
-
}>;
|
|
1
|
+
import "../types-CAGBd7wv.js";
|
|
2
|
+
import { SSETextDecoderPluginOptions, sseTextDecoderPlugin } from "../sse-DPRkQwhR.js";
|
|
3
|
+
export { SSETextDecoderPluginOptions, sseTextDecoderPlugin };
|
package/types/react/index.d.ts
CHANGED
|
@@ -1,34 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ResponseError, StreamContext } from "../types-CAGBd7wv.js";
|
|
2
|
+
import { HookFetchRequest } from "../index-n7kSnbyZ.js";
|
|
3
|
+
import * as react0 from "react";
|
|
4
|
+
|
|
5
|
+
//#region src/react/index.d.ts
|
|
2
6
|
interface UseHookFetchOptions<Q extends (...args: any[]) => any> {
|
|
3
|
-
|
|
4
|
-
|
|
7
|
+
request: Q;
|
|
8
|
+
onError?: (e: Error) => any;
|
|
5
9
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
* @returns {Function} bytesData - Get response as bytes | 获取字节数据响应
|
|
21
|
-
*/
|
|
22
|
-
export declare const useHookFetch: <Q extends (...args: any[]) => any>({ request, onError }: UseHookFetchOptions<Q>) => {
|
|
23
|
-
request: (...args: any[]) => import('react').RefObject<HookFetchRequest<any, any> | null>;
|
|
24
|
-
stream: (...args: Parameters<Q>) => AsyncGenerator<import('..').StreamContext<unknown> | import('..').StreamContext<null>, import('../utils').ResponseError<unknown> | undefined, unknown>;
|
|
25
|
-
text: (...args: Parameters<Q>) => Promise<any>;
|
|
26
|
-
blob: (...args: Parameters<Q>) => Promise<any>;
|
|
27
|
-
arrayBufferData: (...args: Parameters<Q>) => Promise<any>;
|
|
28
|
-
formDataResult: (...args: Parameters<Q>) => Promise<any>;
|
|
29
|
-
bytesData: (...args: Parameters<Q>) => Promise<any>;
|
|
30
|
-
cancel: () => void;
|
|
31
|
-
loading: boolean;
|
|
32
|
-
setLoading: import('react').Dispatch<import('react').SetStateAction<boolean>>;
|
|
10
|
+
declare const useHookFetch: <Q extends (...args: any[]) => any>({
|
|
11
|
+
request,
|
|
12
|
+
onError
|
|
13
|
+
}: UseHookFetchOptions<Q>) => {
|
|
14
|
+
request: (...args: any[]) => react0.RefObject<HookFetchRequest<any, any> | null>;
|
|
15
|
+
stream: (...args: Parameters<Q>) => AsyncGenerator<StreamContext<unknown> | StreamContext<null>, ResponseError<unknown> | undefined, unknown>;
|
|
16
|
+
text: (...args: Parameters<Q>) => Promise<string>;
|
|
17
|
+
blob: (...args: Parameters<Q>) => Promise<Blob>;
|
|
18
|
+
arrayBufferData: (...args: Parameters<Q>) => Promise<ArrayBuffer>;
|
|
19
|
+
formDataResult: (...args: Parameters<Q>) => Promise<FormData>;
|
|
20
|
+
bytesData: (...args: Parameters<Q>) => Promise<Uint8Array<ArrayBufferLike>>;
|
|
21
|
+
cancel: () => void;
|
|
22
|
+
loading: boolean;
|
|
23
|
+
setLoading: react0.Dispatch<react0.SetStateAction<boolean>>;
|
|
33
24
|
};
|
|
34
|
-
|
|
25
|
+
//#endregion
|
|
26
|
+
export { useHookFetch };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { HookFetchPlugin } from "./types-CAGBd7wv.js";
|
|
2
|
+
|
|
3
|
+
//#region src/plugins/sse.d.ts
|
|
4
|
+
interface SSETextDecoderPluginOptions {
|
|
5
|
+
splitSeparator: string;
|
|
6
|
+
lineSeparator: string | undefined;
|
|
7
|
+
trim: boolean;
|
|
8
|
+
json: boolean;
|
|
9
|
+
prefix: string;
|
|
10
|
+
doneSymbol: string;
|
|
11
|
+
}
|
|
12
|
+
declare const sseTextDecoderPlugin: ({
|
|
13
|
+
splitSeparator,
|
|
14
|
+
lineSeparator,
|
|
15
|
+
trim,
|
|
16
|
+
json,
|
|
17
|
+
prefix,
|
|
18
|
+
doneSymbol
|
|
19
|
+
}?: Partial<SSETextDecoderPluginOptions>) => HookFetchPlugin<unknown, {
|
|
20
|
+
sseAble: boolean;
|
|
21
|
+
}>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { SSETextDecoderPluginOptions, sseTextDecoderPlugin };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { AnyObject } from "typescript-api-pro";
|
|
2
|
+
import QueryString from "qs";
|
|
3
|
+
|
|
4
|
+
//#region src/error.d.ts
|
|
5
|
+
interface ResponseErrorOptions<E = unknown> {
|
|
6
|
+
name?: string;
|
|
7
|
+
message: string;
|
|
8
|
+
status?: number;
|
|
9
|
+
statusText?: string;
|
|
10
|
+
response?: Response;
|
|
11
|
+
config?: RequestConfig<unknown, unknown, E>;
|
|
12
|
+
}
|
|
13
|
+
declare class ResponseError<E = unknown> extends Error {
|
|
14
|
+
#private;
|
|
15
|
+
constructor({
|
|
16
|
+
message,
|
|
17
|
+
status,
|
|
18
|
+
statusText,
|
|
19
|
+
response,
|
|
20
|
+
config,
|
|
21
|
+
name
|
|
22
|
+
}: ResponseErrorOptions<E>);
|
|
23
|
+
get message(): string;
|
|
24
|
+
get status(): number | undefined;
|
|
25
|
+
get statusText(): string | undefined;
|
|
26
|
+
get response(): Response | undefined;
|
|
27
|
+
get config(): RequestConfig<unknown, unknown, E> | undefined;
|
|
28
|
+
get name(): string;
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/types.d.ts
|
|
32
|
+
type FetchResponseType = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData' | 'bytes';
|
|
33
|
+
type RequestMethodWithParams = 'GET' | 'HEAD';
|
|
34
|
+
type RequestMethodWithBody = 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS';
|
|
35
|
+
type RequestMethod = RequestMethodWithParams | RequestMethodWithBody;
|
|
36
|
+
interface RequestConfig<P, D, E = AnyObject> extends Omit<RequestInit, 'body' | 'signal' | 'credentials' | 'method'> {
|
|
37
|
+
url: string;
|
|
38
|
+
baseURL: string;
|
|
39
|
+
params?: P;
|
|
40
|
+
data?: D;
|
|
41
|
+
withCredentials?: boolean;
|
|
42
|
+
extra?: E;
|
|
43
|
+
method: RequestMethod;
|
|
44
|
+
qsArrayFormat?: QueryString.IStringifyOptions['arrayFormat'];
|
|
45
|
+
}
|
|
46
|
+
type BaseRequestOptions<P, D, E = AnyObject> = Partial<{
|
|
47
|
+
plugins: Array<HookFetchPlugin>;
|
|
48
|
+
timeout: number;
|
|
49
|
+
params: P;
|
|
50
|
+
data: D;
|
|
51
|
+
controller: AbortController;
|
|
52
|
+
extra: E;
|
|
53
|
+
qsArrayFormat: QueryString.IStringifyOptions['arrayFormat'];
|
|
54
|
+
withCredentials: boolean;
|
|
55
|
+
method: RequestMethod;
|
|
56
|
+
}> & Omit<RequestInit, 'body' | 'method'> & {
|
|
57
|
+
url: string;
|
|
58
|
+
baseURL: string;
|
|
59
|
+
};
|
|
60
|
+
interface FetchPluginContext<T = unknown, E = unknown, P = unknown, D = unknown> {
|
|
61
|
+
config: RequestConfig<P, D, E>;
|
|
62
|
+
response: Response;
|
|
63
|
+
responseType: FetchResponseType;
|
|
64
|
+
result?: T;
|
|
65
|
+
controller: AbortController;
|
|
66
|
+
}
|
|
67
|
+
interface StreamContext<T = unknown> {
|
|
68
|
+
result: T;
|
|
69
|
+
source: Uint8Array<ArrayBufferLike>;
|
|
70
|
+
error: unknown | null;
|
|
71
|
+
}
|
|
72
|
+
type BeforeRequestHandler<E = unknown, P = unknown, D = unknown> = (config: RequestConfig<P, D, E>) => RequestConfig<P, D, E> | PromiseLike<RequestConfig<P, D, E>>;
|
|
73
|
+
type AfterResponseHandler<T = unknown, E = unknown, P = unknown, D = unknown> = (context: FetchPluginContext<T>, config: RequestConfig<P, D, E>) => FetchPluginContext<T> | PromiseLike<FetchPluginContext<T>>;
|
|
74
|
+
type BeforeStreamHandler<E = unknown, P = unknown, D = unknown> = (body: ReadableStream<any>, config: RequestConfig<P, D, E>) => ReadableStream<any> | PromiseLike<ReadableStream<any>>;
|
|
75
|
+
type TransformStreamChunkHandler<E = unknown, P = unknown, D = unknown> = (chunk: StreamContext<any>, config: RequestConfig<P, D, E>) => StreamContext | PromiseLike<StreamContext>;
|
|
76
|
+
type OnFinallyHandler<E = unknown, P = unknown, D = unknown> = (res: Pick<FetchPluginContext<unknown, E, P, D>, 'config' | 'response'>) => void | PromiseLike<void>;
|
|
77
|
+
type OnErrorHandler<E = unknown, P = unknown, D = unknown> = (error: ResponseError, config: RequestConfig<P, D, E>) => PromiseLike<Error | void | ResponseError<E>> | Error | void | ResponseError<E>;
|
|
78
|
+
type HookFetchPlugin<T = unknown, E = unknown, P = unknown, D = unknown> = {
|
|
79
|
+
name: string;
|
|
80
|
+
priority?: number;
|
|
81
|
+
beforeRequest?: BeforeRequestHandler<E, P, D>;
|
|
82
|
+
afterResponse?: AfterResponseHandler<T, E, P, D>;
|
|
83
|
+
beforeStream?: BeforeStreamHandler<E, P, D>;
|
|
84
|
+
transformStreamChunk?: TransformStreamChunkHandler<E, P, D>;
|
|
85
|
+
onError?: OnErrorHandler<E, P, D>;
|
|
86
|
+
onFinally?: OnFinallyHandler<E, P, D>;
|
|
87
|
+
};
|
|
88
|
+
interface OptionProps {
|
|
89
|
+
baseURL: string;
|
|
90
|
+
timeout: number;
|
|
91
|
+
headers: HeadersInit;
|
|
92
|
+
plugins: Array<HookFetchPlugin<any, any, any, any>>;
|
|
93
|
+
withCredentials: boolean;
|
|
94
|
+
}
|
|
95
|
+
type BaseOptions = Partial<OptionProps>;
|
|
96
|
+
type RequestOptions<P = AnyObject, D = AnyObject, E = AnyObject> = Omit<BaseRequestOptions<P, D, E>, 'url' | 'plugins' | 'baseURL' | 'controller'>;
|
|
97
|
+
type RequestUseOptions<P = AnyObject, D = AnyObject, E = AnyObject> = RequestOptions<P, D, E>;
|
|
98
|
+
type RequestWithBodyOptions<D = AnyObject, P = AnyObject, E = AnyObject> = Omit<RequestOptions<P, D, E>, 'data'>;
|
|
99
|
+
type RequestWithParamsOptions<P = AnyObject, E = AnyObject> = Omit<RequestOptions<P, null, E>, 'params' | 'data'>;
|
|
100
|
+
type RequestWithBodyFnOptions<D = AnyObject, P = AnyObject, E = AnyObject> = Omit<RequestOptions<P, D, E>, 'data' | 'method'>;
|
|
101
|
+
type RequestWithParamsFnOptions<P = AnyObject, E = AnyObject> = Omit<RequestOptions<P, null, E>, 'params' | 'data' | 'method'>;
|
|
102
|
+
type PostOptions<D = AnyObject, P = AnyObject, E = AnyObject> = RequestWithBodyFnOptions<D, P, E>;
|
|
103
|
+
type PutOptions<D = AnyObject, P = AnyObject, E = AnyObject> = RequestWithBodyFnOptions<D, P, E>;
|
|
104
|
+
type PatchOptions<D = AnyObject, P = AnyObject, E = AnyObject> = RequestWithBodyFnOptions<D, P, E>;
|
|
105
|
+
type GetOptions<P = AnyObject, E = AnyObject> = RequestWithParamsFnOptions<P, E>;
|
|
106
|
+
type HeadOptions<P = AnyObject, E = AnyObject> = RequestWithParamsFnOptions<P, E>;
|
|
107
|
+
type OptionsOptions<P = AnyObject, D = AnyObject, E = AnyObject> = RequestUseOptions<P, D, E>;
|
|
108
|
+
type DeleteOptions<P = AnyObject, D = AnyObject, E = AnyObject> = RequestUseOptions<P, D, E>;
|
|
109
|
+
//#endregion
|
|
110
|
+
export { AfterResponseHandler, BaseOptions, BaseRequestOptions, BeforeRequestHandler, BeforeStreamHandler, DeleteOptions, FetchPluginContext, FetchResponseType, GetOptions, HeadOptions, HookFetchPlugin, OnErrorHandler, OnFinallyHandler, OptionProps, OptionsOptions, PatchOptions, PostOptions, PutOptions, RequestConfig, RequestMethod, RequestMethodWithBody, RequestMethodWithParams, RequestOptions, RequestUseOptions, RequestWithBodyFnOptions, RequestWithBodyOptions, RequestWithParamsFnOptions, RequestWithParamsOptions, ResponseError, ResponseErrorOptions, StreamContext, TransformStreamChunkHandler };
|
package/types/vue/index.d.ts
CHANGED
|
@@ -1,32 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ResponseError, StreamContext } from "../types-CAGBd7wv.js";
|
|
2
|
+
import { HookFetchRequest } from "../index-n7kSnbyZ.js";
|
|
3
|
+
import * as vue0 from "vue";
|
|
4
|
+
|
|
5
|
+
//#region src/vue/index.d.ts
|
|
2
6
|
interface UseHookFetchOptions<Q extends (...args: any[]) => any> {
|
|
3
|
-
|
|
4
|
-
|
|
7
|
+
request: Q;
|
|
8
|
+
onError?: (e: Error) => any;
|
|
5
9
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
* @returns {Function} bytesData - Get response as bytes | 获取字节数据响应
|
|
20
|
-
*/
|
|
21
|
-
export declare const useHookFetch: <Q extends (...args: any[]) => any>({ request, onError }: UseHookFetchOptions<Q>) => {
|
|
22
|
-
request: (...args: any[]) => HookFetchRequest<any, any> | null;
|
|
23
|
-
stream: (...args: Parameters<Q>) => AsyncGenerator<import('..').StreamContext<unknown> | import('..').StreamContext<null>, import('../utils').ResponseError<unknown> | undefined, unknown>;
|
|
24
|
-
text: (...args: Parameters<Q>) => Promise<any>;
|
|
25
|
-
blob: (...args: Parameters<Q>) => Promise<any>;
|
|
26
|
-
arrayBufferData: (...args: Parameters<Q>) => Promise<any>;
|
|
27
|
-
formDataResult: (...args: Parameters<Q>) => Promise<any>;
|
|
28
|
-
bytesData: (...args: Parameters<Q>) => Promise<any>;
|
|
29
|
-
cancel: () => void;
|
|
30
|
-
loading: import('vue').Ref<boolean, boolean>;
|
|
10
|
+
declare const useHookFetch: <Q extends (...args: any[]) => any>({
|
|
11
|
+
request,
|
|
12
|
+
onError
|
|
13
|
+
}: UseHookFetchOptions<Q>) => {
|
|
14
|
+
request: (...args: any[]) => HookFetchRequest<any, any> | null;
|
|
15
|
+
stream: (...args: Parameters<Q>) => AsyncGenerator<StreamContext<unknown> | StreamContext<null>, ResponseError<unknown> | undefined, unknown>;
|
|
16
|
+
text: (...args: Parameters<Q>) => Promise<string>;
|
|
17
|
+
blob: (...args: Parameters<Q>) => Promise<Blob>;
|
|
18
|
+
arrayBufferData: (...args: Parameters<Q>) => Promise<ArrayBuffer>;
|
|
19
|
+
formDataResult: (...args: Parameters<Q>) => Promise<FormData>;
|
|
20
|
+
bytesData: (...args: Parameters<Q>) => Promise<Uint8Array<ArrayBufferLike>>;
|
|
21
|
+
cancel: () => void;
|
|
22
|
+
loading: vue0.Ref<boolean, boolean>;
|
|
31
23
|
};
|
|
32
|
-
|
|
24
|
+
//#endregion
|
|
25
|
+
export { useHookFetch };
|
package/types/base.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { AnyObject, Generic } from 'typescript-api-pro';
|
|
2
|
-
import { BaseOptions, DeleteOptions, GetOptions, HeadOptions, HookFetchPlugin, OptionsOptions, PatchOptions, PostOptions, PutOptions, RequestOptions } from './types';
|
|
3
|
-
import { HookFetchRequest } from './utils';
|
|
4
|
-
declare class HookFetch<R extends AnyObject = AnyObject, K extends keyof R = 'data', E = AnyObject> {
|
|
5
|
-
#private;
|
|
6
|
-
constructor({ timeout, baseURL, headers, plugins, withCredentials }: BaseOptions);
|
|
7
|
-
use(plugin: HookFetchPlugin<any, any, any, any>): this;
|
|
8
|
-
request<T = AnyObject, P = AnyObject, D = AnyObject>(url: string, { timeout, headers, method, params, data, qsArrayFormat, withCredentials, extra }?: RequestOptions<P, D, E>): HookFetchRequest<Generic<R, K, T>, E>;
|
|
9
|
-
get<T = AnyObject, P = AnyObject>(url: string, params?: P, options?: GetOptions<P, E>): HookFetchRequest<Generic<R, K, T>, E>;
|
|
10
|
-
head<T = AnyObject, P = AnyObject>(url: string, params?: P, options?: HeadOptions<P, E>): HookFetchRequest<Generic<R, K, T>, E>;
|
|
11
|
-
options<T = AnyObject, P = AnyObject, D = AnyObject>(url: string, params?: P, options?: OptionsOptions<P, D, E>): HookFetchRequest<Generic<R, K, T>, E>;
|
|
12
|
-
delete<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, options?: DeleteOptions<P, D, E>): HookFetchRequest<Generic<R, K, T>, E>;
|
|
13
|
-
post<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>): HookFetchRequest<Generic<R, K, T>, E>;
|
|
14
|
-
upload<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>): HookFetchRequest<Generic<R, K, T>, E>;
|
|
15
|
-
put<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PutOptions<D, P, E>): HookFetchRequest<Generic<R, K, T>, E>;
|
|
16
|
-
patch<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PatchOptions<D, P, E>): HookFetchRequest<Generic<R, K, T>, E>;
|
|
17
|
-
abortAll(): void;
|
|
18
|
-
}
|
|
19
|
-
declare const useRequest: <R = AnyObject, P = AnyObject, D = AnyObject, E = AnyObject>(url: string, options?: RequestOptions<P, D, E>) => HookFetchRequest<R, E>;
|
|
20
|
-
export declare const request: <R = AnyObject, P = AnyObject, D = AnyObject, E = AnyObject>(url: string, options?: RequestOptions<P, D, E>) => HookFetchRequest<R, E>;
|
|
21
|
-
export declare const get: <R = AnyObject, P = AnyObject, E = AnyObject>(url: string, params?: P, options?: GetOptions<P, E>) => HookFetchRequest<R, E>;
|
|
22
|
-
export declare const head: <R = AnyObject, P = AnyObject, E = AnyObject>(url: string, params?: P, options?: HeadOptions<P, E>) => HookFetchRequest<R, E>;
|
|
23
|
-
export declare const options: <R = AnyObject, P = AnyObject, D = AnyObject, E = AnyObject>(url: string, params?: P, options?: OptionsOptions<P, D, E>) => HookFetchRequest<R, E>;
|
|
24
|
-
export declare const del: <R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, options?: DeleteOptions<P, D, E>) => HookFetchRequest<R, E>;
|
|
25
|
-
export declare const post: <R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>) => HookFetchRequest<R, E>;
|
|
26
|
-
export declare const upload: <R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>) => HookFetchRequest<R, E>;
|
|
27
|
-
export declare const put: <R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PutOptions<D, P, E>) => HookFetchRequest<R, E>;
|
|
28
|
-
export declare const patch: <R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PatchOptions<D, P, E>) => HookFetchRequest<R, E>;
|
|
29
|
-
type ExportDefault = typeof useRequest & {
|
|
30
|
-
create: <R extends AnyObject = AnyObject, K extends keyof R = 'data', E = AnyObject>(options: BaseOptions) => (HookFetch<R, K, E>['request'] & HookFetch<R, K, E>);
|
|
31
|
-
get: typeof get;
|
|
32
|
-
head: typeof head;
|
|
33
|
-
options: typeof options;
|
|
34
|
-
delete: typeof del;
|
|
35
|
-
post: typeof post;
|
|
36
|
-
put: typeof put;
|
|
37
|
-
patch: typeof patch;
|
|
38
|
-
upload: typeof upload;
|
|
39
|
-
};
|
|
40
|
-
declare const hookFetch: ExportDefault;
|
|
41
|
-
export default hookFetch;
|
package/types/enum.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export declare enum ContentType {
|
|
2
|
-
JSON = "application/json",
|
|
3
|
-
FORM_URLENCODED = "application/x-www-form-urlencoded",
|
|
4
|
-
FORM_DATA = "multipart/form-data",
|
|
5
|
-
TEXT = "text/plain",
|
|
6
|
-
HTML = "text/html",
|
|
7
|
-
XML = "text/xml",
|
|
8
|
-
CSV = "text/csv",
|
|
9
|
-
STREAM = "application/octet-stream"
|
|
10
|
-
}
|
|
11
|
-
export declare enum StatusCode {
|
|
12
|
-
TIME_OUT = 408,
|
|
13
|
-
ABORTED = 499,
|
|
14
|
-
NETWORK_ERROR = 599,
|
|
15
|
-
BODY_NULL = 502,
|
|
16
|
-
UNKNOWN = 601
|
|
17
|
-
}
|
|
18
|
-
export declare enum Method {
|
|
19
|
-
GET = "GET",
|
|
20
|
-
POST = "POST",
|
|
21
|
-
PUT = "PUT",
|
|
22
|
-
DELETE = "DELETE",
|
|
23
|
-
PATCH = "PATCH",
|
|
24
|
-
HEAD = "HEAD",
|
|
25
|
-
OPTIONS = "OPTIONS"
|
|
26
|
-
}
|
package/types/error.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { RequestConfig } from './types';
|
|
2
|
-
export interface ResponseErrorOptions<E = unknown> {
|
|
3
|
-
name?: string;
|
|
4
|
-
message: string;
|
|
5
|
-
status?: number;
|
|
6
|
-
statusText?: string;
|
|
7
|
-
response?: Response;
|
|
8
|
-
config?: RequestConfig<unknown, unknown, E>;
|
|
9
|
-
}
|
|
10
|
-
export declare class ResponseError<E = unknown> extends Error {
|
|
11
|
-
#private;
|
|
12
|
-
constructor({ message, status, statusText, response, config, name }: ResponseErrorOptions<E>);
|
|
13
|
-
get message(): string;
|
|
14
|
-
get status(): number | undefined;
|
|
15
|
-
get statusText(): string | undefined;
|
|
16
|
-
get response(): Response | undefined;
|
|
17
|
-
get config(): RequestConfig<unknown, unknown, E> | undefined;
|
|
18
|
-
get name(): string;
|
|
19
|
-
}
|