kbfetch 0.0.2 → 0.0.4

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 ADDED
@@ -0,0 +1,69 @@
1
+ // help.ts
2
+ var help = {
3
+ synthesizeUrlWithParams: (url, params) => {
4
+ if (!params)
5
+ return url;
6
+ const [pre, originalQueryStr] = url.split("?");
7
+ const queryStr = Object.entries(params).map(([k, v]) => `${k}=${v}`).join("&");
8
+ const fullQueryStr = [originalQueryStr, queryStr].filter(Boolean).join("&");
9
+ return [pre, fullQueryStr].filter(Boolean).join("?");
10
+ },
11
+ createTimeoutSignal: (timeout) => {
12
+ const abortController = new AbortController;
13
+ setTimeout(() => {
14
+ abortController.abort();
15
+ }, timeout);
16
+ return abortController.signal;
17
+ },
18
+ parseResBody: (res) => {
19
+ const contentType = res.headers.get("content-type") || "";
20
+ if (contentType.includes("application/json"))
21
+ return res.json();
22
+ return res.text();
23
+ }
24
+ };
25
+ var help_default = help;
26
+
27
+ // index.ts
28
+ var defaultKbFetch = {
29
+ baseUrl: "",
30
+ before: (init) => init,
31
+ after: (res) => res.then((res2) => res2.data),
32
+ parser: help_default.parseResBody,
33
+ timeout: 0,
34
+ do: async function(url, init) {
35
+ const _t = this;
36
+ let { before = _t.before, ..._init } = init || {};
37
+ _init = before(_init);
38
+ let { parser = _t.parser, after = _t.after, baseUrl = _t.baseUrl, timeout = _t.timeout, ...requestInit } = _init;
39
+ timeout && Object.assign(requestInit, { signal: help_default.createTimeoutSignal(timeout) });
40
+ if (!url.startsWith("http://") && !url.startsWith("https://"))
41
+ url = baseUrl + url;
42
+ const response = fetch(url, requestInit).then(async (respnse) => {
43
+ respnse.data = await parser(respnse);
44
+ return respnse;
45
+ });
46
+ return after(response);
47
+ },
48
+ get: function(url, params, init) {
49
+ return this.do(help_default.synthesizeUrlWithParams(url, params), init);
50
+ },
51
+ g: function(url, init) {
52
+ const { params, ..._init } = init || {};
53
+ return this.get(url, params, _init);
54
+ },
55
+ post: function(url, params, init) {
56
+ const _init = Object.assign({}, init, { method: "POST", body: JSON.stringify(params) });
57
+ _init.headers = Object.assign({}, _init.headers, { "Content-Type": "application/json" });
58
+ return this.do(url, _init);
59
+ }
60
+ };
61
+ var createKbFetch = (config) => ({ ...defaultKbFetch, ...config });
62
+ var kbFetch = defaultKbFetch;
63
+ var kbfetch_default = kbFetch;
64
+ var afetch = defaultKbFetch;
65
+ export {
66
+ kbfetch_default as default,
67
+ createKbFetch,
68
+ afetch
69
+ };
@@ -0,0 +1,20 @@
1
+ type Before = (init: KbFetchInit) => KbFetchInit;
2
+ type Parser = (res: Response) => any;
3
+ type After = <T = any>(res: Promise<Response & {
4
+ data: T;
5
+ }>) => any;
6
+ type _KbFetchInit = {
7
+ timeout?: number;
8
+ before?: Before;
9
+ parser?: Parser;
10
+ after?: After;
11
+ baseUrl?: string;
12
+ flags?: Record<string, any>;
13
+ };
14
+ export type KbFetchInit = Omit<RequestInit, 'timeout'> & _KbFetchInit;
15
+ declare const help: {
16
+ synthesizeUrlWithParams: (url: string, params?: Record<string, any>) => string;
17
+ createTimeoutSignal: (timeout: number) => AbortSignal;
18
+ parseResBody: (res: Response) => Promise<any>;
19
+ };
20
+ export default help;
@@ -0,0 +1,98 @@
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"> & {
13
+ timeout?: number;
14
+ before?: (init: KbFetchInit) => KbFetchInit;
15
+ parser?: (res: Response) => any;
16
+ after?: <T_3 = any>(res: Promise<Response & {
17
+ data: T_3;
18
+ }>) => any;
19
+ baseUrl?: string;
20
+ flags?: Record<string, any>;
21
+ } & {
22
+ params?: Record<string, any>;
23
+ }) => Promise<T_2>;
24
+ post: <T_4 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_4>;
25
+ };
26
+ export declare const createKbFetch: (config: Partial<typeof defaultKbFetch>) => {
27
+ baseUrl: string;
28
+ before: (init: KbFetchInit) => KbFetchInit;
29
+ after: (res: Promise<Response & {
30
+ data: any;
31
+ }>) => any;
32
+ parser: (res: Response) => Promise<any>;
33
+ timeout: number;
34
+ do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
35
+ get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
36
+ g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout"> & {
37
+ timeout?: number;
38
+ before?: (init: KbFetchInit) => KbFetchInit;
39
+ parser?: (res: Response) => any;
40
+ after?: <T_3 = any>(res: Promise<Response & {
41
+ data: T_3;
42
+ }>) => any;
43
+ baseUrl?: string;
44
+ flags?: Record<string, any>;
45
+ } & {
46
+ params?: Record<string, any>;
47
+ }) => Promise<T_2>;
48
+ post: <T_4 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_4>;
49
+ };
50
+ declare const kbFetch: {
51
+ baseUrl: string;
52
+ before: (init: KbFetchInit) => KbFetchInit;
53
+ after: (res: Promise<Response & {
54
+ data: any;
55
+ }>) => any;
56
+ parser: (res: Response) => Promise<any>;
57
+ timeout: number;
58
+ do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
59
+ get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
60
+ g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout"> & {
61
+ timeout?: number;
62
+ before?: (init: KbFetchInit) => KbFetchInit;
63
+ parser?: (res: Response) => any;
64
+ after?: <T_3 = any>(res: Promise<Response & {
65
+ data: T_3;
66
+ }>) => any;
67
+ baseUrl?: string;
68
+ flags?: Record<string, any>;
69
+ } & {
70
+ params?: Record<string, any>;
71
+ }) => Promise<T_2>;
72
+ post: <T_4 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_4>;
73
+ };
74
+ export default kbFetch;
75
+ export declare const afetch: {
76
+ baseUrl: string;
77
+ before: (init: KbFetchInit) => KbFetchInit;
78
+ after: (res: Promise<Response & {
79
+ data: any;
80
+ }>) => any;
81
+ parser: (res: Response) => Promise<any>;
82
+ timeout: number;
83
+ do: <T = any>(url: string, init?: KbFetchInit) => Promise<any>;
84
+ get: <T_1 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_1>;
85
+ g: <T_2 = any>(url: string, init?: Omit<RequestInit, "timeout"> & {
86
+ timeout?: number;
87
+ before?: (init: KbFetchInit) => KbFetchInit;
88
+ parser?: (res: Response) => any;
89
+ after?: <T_3 = any>(res: Promise<Response & {
90
+ data: T_3;
91
+ }>) => any;
92
+ baseUrl?: string;
93
+ flags?: Record<string, any>;
94
+ } & {
95
+ params?: Record<string, any>;
96
+ }) => Promise<T_2>;
97
+ post: <T_4 = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) => Promise<T_4>;
98
+ };
package/package.json CHANGED
@@ -2,14 +2,22 @@
2
2
  "name": "kbfetch",
3
3
  "module": "index.ts",
4
4
  "type": "module",
5
- "version": "0.0.2",
5
+ "version": "0.0.4",
6
6
  "license": "MIT",
7
+ "types": "./dist/typings/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
7
11
  "scripts": {
8
- "pub": " npm publish --registry https://registry.npmjs.org/ &&bun updatetaobao ",
12
+ "patch": "git stash && npm version patch && git stash pop",
13
+ "pub": "bun bld && bun tsc && bun patch && npm publish --registry https://registry.npmjs.org/ ; bun updatetaobao ",
14
+ "tsc": "tsc index.ts -d --emitDeclarationOnly --declarationDir ./dist/typings",
15
+ "bld": "bun build ./index.ts --outdir ./dist",
9
16
  "updatetaobao": "cnpm sync kbfetch"
10
17
  },
11
18
  "devDependencies": {
12
- "bun-types": "latest"
19
+ "bun-types": "latest",
20
+ "typescript": "^5.2.2"
13
21
  },
14
22
  "peerDependencies": {
15
23
  "typescript": "^5.0.0"
package/bun.lockb DELETED
Binary file
package/help.ts DELETED
@@ -1,30 +0,0 @@
1
- export type AFetchInit = Omit<RequestInit, 'timeout'> & { timeout?: number }
2
- const help = {
3
- synthesizeUrlWithParams: (url: string, params?: Record<string, any>) => {
4
- if (!params) return url
5
- const [pre, originalQueryStr] = url.split('?')
6
- const queryStr = Object.entries(params)
7
- .map(([k, v]) => `${k}=${v}`)
8
- .join('&')
9
- const fullQueryStr = [originalQueryStr, queryStr].filter(Boolean).join('&')
10
- return [pre, fullQueryStr].filter(Boolean).join('?')
11
- },
12
- createTimeoutSignal: (timeout: number) => {
13
- const abortController = new AbortController()
14
- setTimeout(() => {
15
- abortController.abort()
16
- }, timeout)
17
- return abortController.signal
18
- },
19
- parseResBody: (res: Response) => {
20
- const contentType = res.headers.get('content-type') || ''
21
- if (contentType.includes('application/json')) return res.json()
22
- return res.text()
23
- },
24
- preprocessRequestInit: (init?: AFetchInit) => {
25
- const { timeout, ..._init } = init || {}
26
- if (timeout) return { ..._init, signal: help.createTimeoutSignal(timeout) }
27
- return _init
28
- },
29
- }
30
- export default help
package/index.ts DELETED
@@ -1,26 +0,0 @@
1
- import h, { AFetchInit } from './help'
2
- type Res<T> = Omit<Response, 'body' | 'bodyUsed'> & { data: T }
3
- const afetch = {
4
- getResponse: <T = any>(url: string, params?: Record<string, any>, init?: AFetchInit) => {
5
- url = h.synthesizeUrlWithParams(url, params)
6
- return fetch(url, h.preprocessRequestInit(init)).then(async (respnse: any) => {
7
- respnse.data = await h.parseResBody(respnse)
8
- return respnse as unknown as Res<T>
9
- })
10
- },
11
- get: <T = any>(url: string, params?: Record<string, any>, init?: AFetchInit) => {
12
- return afetch.getResponse<T>(url, params, init).then(res => res.data)
13
- },
14
- g: <T = any>(url: string, init?: AFetchInit & { params?: Record<string, any> }) => {
15
- const { params, ..._init } = init || {}
16
- return afetch.get<T>(url, params, _init)
17
- },
18
- post: <T = any>(url: string, params?: Record<string, any>, init?: AFetchInit) => {
19
- let _init = h.preprocessRequestInit(init)
20
- _init = Object.assign({}, _init, { method: 'POST', body: JSON.stringify(params) })
21
- _init.headers = Object.assign({}, _init.headers, { 'Content-Type': 'application/json' })
22
-
23
- return fetch(url, _init).then(h.parseResBody) as Promise<T>
24
- },
25
- }
26
- export default afetch
package/tsconfig.json DELETED
@@ -1,22 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "lib": ["ESNext"],
4
- "module": "esnext",
5
- "target": "esnext",
6
- "moduleResolution": "bundler",
7
- "moduleDetection": "force",
8
- "allowImportingTsExtensions": true,
9
- "noEmit": true,
10
- "composite": true,
11
- "strict": true,
12
- "downlevelIteration": true,
13
- "skipLibCheck": true,
14
- "jsx": "react-jsx",
15
- "allowSyntheticDefaultImports": true,
16
- "forceConsistentCasingInFileNames": true,
17
- "allowJs": true,
18
- "types": [
19
- "bun-types" // add Bun global
20
- ]
21
- }
22
- }