kbfetch 0.0.8 → 0.0.12

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/dist/index.js CHANGED
@@ -8,12 +8,12 @@ var help = {
8
8
  const fullQueryStr = [originalQueryStr, queryStr].filter(Boolean).join("&");
9
9
  return [pre, fullQueryStr].filter(Boolean).join("?");
10
10
  },
11
- createTimeoutSignal: (timeout) => {
11
+ createTimeoutAbortController: (timeout) => {
12
12
  const abortController = new AbortController;
13
- setTimeout(() => {
13
+ timeout && setTimeout(() => {
14
14
  abortController.abort();
15
15
  }, timeout);
16
- return abortController.signal;
16
+ return abortController;
17
17
  },
18
18
  parseResBody: (res) => {
19
19
  const contentType = res.headers.get("content-type") || "";
@@ -36,20 +36,22 @@ var defaultKbFetch = {
36
36
  after: (res) => res.then((res2) => res2.data),
37
37
  parser: help_default.parseResBody,
38
38
  timeout: 0,
39
- do: async function(url, init) {
39
+ do: function(url, init) {
40
40
  const _t = this;
41
41
  let { before = _t.before, ..._init } = init || {};
42
42
  _init = before(_init);
43
- let { parser = _t.parser, after = _t.after, baseUrl = _t.baseUrl, timeout = _t.timeout, headers, ...requestInit } = _init;
44
- timeout && Object.assign(requestInit, { signal: help_default.createTimeoutSignal(timeout) });
43
+ let { parser = _t.parser, after = _t.after, baseUrl = _t.baseUrl, timeout = _t.timeout, canAbort, headers, ...requestInit } = _init;
44
+ let abortController = null;
45
+ (timeout || canAbort) && (abortController = help_default.createTimeoutAbortController(timeout));
46
+ abortController && Object.assign(requestInit, { signal: abortController.signal });
45
47
  headers && Object.assign(requestInit, { headers: help_default.obj2header(headers) });
46
48
  if (!url.startsWith("http://") && !url.startsWith("https://"))
47
49
  url = baseUrl + url;
48
- const response = fetch(url, requestInit).then(async (respnse) => {
49
- respnse.data = await parser(respnse);
50
- return respnse;
50
+ const response = fetch(url, requestInit).then(async (respnse) => Object.assign(respnse, { data: await parser(respnse) }));
51
+ const res = after(response);
52
+ return Object.assign(res, {
53
+ abort: () => abortController?.abort()
51
54
  });
52
- return after(response);
53
55
  },
54
56
  get: function(url, params, init) {
55
57
  return this.do(help_default.synthesizeUrlWithParams(url, params), init);
@@ -2,12 +2,13 @@ type Before = (init: KbFetchInit) => KbFetchInit;
2
2
  type Parser = (res: Response) => any;
3
3
  type After = <T = any>(res: Promise<Response & {
4
4
  data: T;
5
- }>) => any;
5
+ }>) => Promise<T>;
6
6
  type _KbFetchInit = {
7
7
  before?: Before;
8
8
  parser?: Parser;
9
9
  after?: After;
10
10
  baseUrl?: string;
11
+ canAbort?: boolean;
11
12
  flags?: Record<string, any>;
12
13
  };
13
14
  export type KbFetchInit = Omit<RequestInit, 'timeout' | 'headers'> & {
@@ -16,7 +17,7 @@ export type KbFetchInit = Omit<RequestInit, 'timeout' | 'headers'> & {
16
17
  } & _KbFetchInit;
17
18
  declare const help: {
18
19
  synthesizeUrlWithParams: (url: string, params?: Record<string, any>) => string;
19
- createTimeoutSignal: (timeout: number) => AbortSignal;
20
+ createTimeoutAbortController: (timeout: number) => AbortController;
20
21
  parseResBody: (res: Response) => Promise<any>;
21
22
  obj2header: (obj: Record<string, string>) => Headers;
22
23
  };
@@ -1,106 +1,38 @@
1
1
  import { KbFetchInit } from './help';
2
- declare const defaultKbFetch: {
3
- baseUrl: string;
4
- before: (init: KbFetchInit) => KbFetchInit;
5
- after: (res: Promise<Response & {
6
- data: any;
7
- }>) => any;
8
- parser: (res: Response) => Promise<any>;
9
- timeout: number;
10
- do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
11
- get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
12
- g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout" | "headers"> & {
13
- timeout?: number;
14
- headers?: Record<string, string>;
15
- } & {
16
- before?: (init: KbFetchInit) => KbFetchInit;
17
- parser?: (res: Response) => any;
18
- after?: <T_3 = any>(res: Promise<Response & {
19
- data: T_3;
20
- }>) => any;
21
- baseUrl?: string;
22
- flags?: Record<string, any>;
23
- } & {
24
- params?: Record<string, any>;
25
- }) => Promise<T_2>;
26
- post: <T_4 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_4>;
2
+ type Res<T> = Promise<T> & {
3
+ abort: () => void;
27
4
  };
28
- export declare const createKbFetch: (config: Partial<typeof defaultKbFetch>) => {
5
+ interface DefaultKbFetch {
29
6
  baseUrl: string;
30
7
  before: (init: KbFetchInit) => KbFetchInit;
31
- after: (res: Promise<Response & {
32
- data: any;
33
- }>) => any;
8
+ after: <T = any>(res: Promise<Response & {
9
+ data: T;
10
+ }>) => Promise<T>;
34
11
  parser: (res: Response) => Promise<any>;
35
12
  timeout: number;
36
- do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
37
- get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
38
- g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout" | "headers"> & {
39
- timeout?: number;
40
- headers?: Record<string, string>;
41
- } & {
42
- before?: (init: KbFetchInit) => KbFetchInit;
43
- parser?: (res: Response) => any;
44
- after?: <T_3 = any>(res: Promise<Response & {
45
- data: T_3;
46
- }>) => any;
47
- baseUrl?: string;
48
- flags?: Record<string, any>;
49
- } & {
13
+ do: <T>(url: string, init?: KbFetchInit) => Res<T>;
14
+ get: <T = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Res<T>;
15
+ g: <T = any>(url: string, init?: KbFetchInit & {
50
16
  params?: Record<string, any>;
51
- }) => Promise<T_2>;
52
- post: <T_4 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_4>;
53
- };
54
- declare const kbFetch: {
17
+ }) => Res<T>;
18
+ post: <T = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Res<T>;
19
+ }
20
+ declare const defaultKbFetch: DefaultKbFetch;
21
+ export declare const createKbFetch: (config: Partial<typeof defaultKbFetch>) => {
55
22
  baseUrl: string;
56
23
  before: (init: KbFetchInit) => KbFetchInit;
57
- after: (res: Promise<Response & {
58
- data: any;
59
- }>) => any;
24
+ after: <T = any>(res: Promise<Response & {
25
+ data: T;
26
+ }>) => Promise<T>;
60
27
  parser: (res: Response) => Promise<any>;
61
28
  timeout: number;
62
- do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
63
- get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
64
- g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout" | "headers"> & {
65
- timeout?: number;
66
- headers?: Record<string, string>;
67
- } & {
68
- before?: (init: KbFetchInit) => KbFetchInit;
69
- parser?: (res: Response) => any;
70
- after?: <T_3 = any>(res: Promise<Response & {
71
- data: T_3;
72
- }>) => any;
73
- baseUrl?: string;
74
- flags?: Record<string, any>;
75
- } & {
29
+ do: <T_1>(url: string, init?: KbFetchInit) => Res<T_1>;
30
+ get: <T_2 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Res<T_2>;
31
+ g: <T_3 = any>(url: string, init?: KbFetchInit & {
76
32
  params?: Record<string, any>;
77
- }) => Promise<T_2>;
78
- post: <T_4 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_4>;
33
+ }) => Res<T_3>;
34
+ post: <T_4 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Res<T_4>;
79
35
  };
36
+ declare const kbFetch: DefaultKbFetch;
80
37
  export default kbFetch;
81
- export declare const afetch: {
82
- baseUrl: string;
83
- before: (init: KbFetchInit) => KbFetchInit;
84
- after: (res: Promise<Response & {
85
- data: any;
86
- }>) => any;
87
- parser: (res: Response) => Promise<any>;
88
- timeout: number;
89
- do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
90
- get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
91
- g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout" | "headers"> & {
92
- timeout?: number;
93
- headers?: Record<string, string>;
94
- } & {
95
- before?: (init: KbFetchInit) => KbFetchInit;
96
- parser?: (res: Response) => any;
97
- after?: <T_3 = any>(res: Promise<Response & {
98
- data: T_3;
99
- }>) => any;
100
- baseUrl?: string;
101
- flags?: Record<string, any>;
102
- } & {
103
- params?: Record<string, any>;
104
- }) => Promise<T_2>;
105
- post: <T_4 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_4>;
106
- };
38
+ export declare const afetch: DefaultKbFetch;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "./dist/index.js",
4
4
  "module": "./dist/index.js",
5
5
  "type": "module",
6
- "version": "0.0.8",
6
+ "version": "0.0.12",
7
7
  "license": "MIT",
8
8
  "types": "./dist/typings/index.d.ts",
9
9
  "files": [