kbfetch 0.0.3 → 0.0.5
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 +69 -0
- package/dist/typings/help.d.ts +20 -0
- package/dist/typings/index.d.ts +98 -0
- package/package.json +13 -4
- package/bun.lockb +0 -0
- package/dprint.json +0 -22
- package/help.ts +0 -30
- package/index.ts +0 -48
- package/test.ts +0 -16
- package/tsconfig.json +0 -23
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
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kbfetch",
|
|
3
|
-
"
|
|
3
|
+
"main": "./dist/index.js",
|
|
4
|
+
"module": "./dist/index.js",
|
|
4
5
|
"type": "module",
|
|
5
|
-
"version": "0.0.
|
|
6
|
+
"version": "0.0.5",
|
|
6
7
|
"license": "MIT",
|
|
8
|
+
"types": "./dist/typings/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
7
12
|
"scripts": {
|
|
8
|
-
"pub": " npm publish --registry https://registry.npmjs.org/
|
|
13
|
+
"pub": "bun bld && bun tsc && bun patch && npm publish --registry https://registry.npmjs.org/ ; bun updatetaobao ",
|
|
14
|
+
"patch": "git stash && npm version patch && git stash pop",
|
|
15
|
+
"tsc": "tsc index.ts -d --emitDeclarationOnly --declarationDir ./dist/typings",
|
|
16
|
+
"bld": "bun build ./index.ts --outdir ./dist",
|
|
9
17
|
"updatetaobao": "cnpm sync kbfetch"
|
|
10
18
|
},
|
|
11
19
|
"devDependencies": {
|
|
12
|
-
"bun-types": "latest"
|
|
20
|
+
"bun-types": "latest",
|
|
21
|
+
"typescript": "^5.2.2"
|
|
13
22
|
},
|
|
14
23
|
"peerDependencies": {
|
|
15
24
|
"typescript": "^5.0.0"
|
package/bun.lockb
DELETED
|
Binary file
|
package/dprint.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"lineWidth": 180,
|
|
3
|
-
"indentWidth": 4,
|
|
4
|
-
"typescript": {
|
|
5
|
-
"semiColons": "asi",
|
|
6
|
-
"quoteStyle": "preferSingle",
|
|
7
|
-
"quoteProps": "asNeeded",
|
|
8
|
-
"useBraces": "maintain",
|
|
9
|
-
"trailingCommas": "never",
|
|
10
|
-
"operatorPosition": "maintain"
|
|
11
|
-
},
|
|
12
|
-
"json": {},
|
|
13
|
-
"markdown": {},
|
|
14
|
-
"toml": {},
|
|
15
|
-
"excludes": ["**/node_modules", "**/*-lock.json"],
|
|
16
|
-
"plugins": [
|
|
17
|
-
"https://plugins.dprint.dev/typescript-0.87.1.wasm",
|
|
18
|
-
"https://plugins.dprint.dev/json-0.17.4.wasm",
|
|
19
|
-
"https://plugins.dprint.dev/markdown-0.16.1.wasm",
|
|
20
|
-
"https://plugins.dprint.dev/toml-0.5.4.wasm"
|
|
21
|
-
]
|
|
22
|
-
}
|
package/help.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
type Before = (init: KbFetchInit) => KbFetchInit
|
|
2
|
-
type Parser = (res: Response) => any
|
|
3
|
-
type After = <T = any>(res: Promise<Response & { data: T }>) => any
|
|
4
|
-
type _KbFetchInit = { timeout?: number; before?: Before; parser?: Parser; after?: After; baseUrl?: string; flags?: Record<string, any> }
|
|
5
|
-
export type KbFetchInit = Omit<RequestInit, 'timeout'> & _KbFetchInit
|
|
6
|
-
const help = {
|
|
7
|
-
synthesizeUrlWithParams: (url: string, params?: Record<string, any>) => {
|
|
8
|
-
if (!params) return url
|
|
9
|
-
const [pre, originalQueryStr] = url.split('?')
|
|
10
|
-
const queryStr = Object.entries(params)
|
|
11
|
-
.map(([k, v]) => `${k}=${v}`)
|
|
12
|
-
.join('&')
|
|
13
|
-
const fullQueryStr = [originalQueryStr, queryStr].filter(Boolean).join('&')
|
|
14
|
-
return [pre, fullQueryStr].filter(Boolean).join('?')
|
|
15
|
-
},
|
|
16
|
-
createTimeoutSignal: (timeout: number) => {
|
|
17
|
-
const abortController = new AbortController()
|
|
18
|
-
setTimeout(() => {
|
|
19
|
-
abortController.abort()
|
|
20
|
-
}, timeout)
|
|
21
|
-
return abortController.signal
|
|
22
|
-
},
|
|
23
|
-
parseResBody: (res: Response) => {
|
|
24
|
-
const contentType = res.headers.get('content-type') || ''
|
|
25
|
-
if (contentType.includes('application/json')) return res.json()
|
|
26
|
-
return res.text()
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export default help
|
package/index.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import h, { KbFetchInit } from './help'
|
|
2
|
-
// type Res<T> = Omit<Response, 'body' | 'bodyUsed'> & { data: T }
|
|
3
|
-
// before after create
|
|
4
|
-
const defaultKbFetch = {
|
|
5
|
-
baseUrl: '',
|
|
6
|
-
before: (init: KbFetchInit) => init,
|
|
7
|
-
after: (res: Promise<Response & { data: any }>) => res.then(res => res.data) as any,
|
|
8
|
-
parser: h.parseResBody,
|
|
9
|
-
timeout: 0,
|
|
10
|
-
do: async function<T = any>(url: string, init?: KbFetchInit) {
|
|
11
|
-
const _t = this
|
|
12
|
-
let { before = _t.before, ..._init } = init || {}
|
|
13
|
-
_init = before(_init)
|
|
14
|
-
|
|
15
|
-
let { parser = _t.parser, after = _t.after, baseUrl = _t.baseUrl, timeout = _t.timeout, ...requestInit } = _init
|
|
16
|
-
timeout && Object.assign(requestInit, { signal: h.createTimeoutSignal(timeout) })
|
|
17
|
-
if (!url.startsWith('http://') && !url.startsWith('https://')) url = baseUrl + url
|
|
18
|
-
|
|
19
|
-
const response = fetch(url, requestInit as any as FetchRequestInit).then(async (respnse) => {
|
|
20
|
-
;(respnse as any).data = await parser(respnse)
|
|
21
|
-
return respnse as Response & { data: T }
|
|
22
|
-
})
|
|
23
|
-
return after(response)
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
get: function<T = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) {
|
|
27
|
-
return this.do<T>(h.synthesizeUrlWithParams(url, params), init).then(res => res.data)
|
|
28
|
-
},
|
|
29
|
-
|
|
30
|
-
g: function<T = any>(url: string, init?: KbFetchInit & { params?: Record<string, any> }) {
|
|
31
|
-
const { params, ..._init } = init || {}
|
|
32
|
-
return this.get<T>(url, params, _init)
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
post: function<T = any>(url: string, params?: Record<string, any>, init?: KbFetchInit) {
|
|
36
|
-
const _init = Object.assign({}, init, { method: 'POST', body: JSON.stringify(params) })
|
|
37
|
-
_init.headers = Object.assign({}, _init.headers, { 'Content-Type': 'application/json' })
|
|
38
|
-
return this.do<T>(url, _init)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export const createKbFetch = (config: Partial<typeof defaultKbFetch>) => ({ ...defaultKbFetch, ...config })
|
|
43
|
-
|
|
44
|
-
const kbFetch = defaultKbFetch
|
|
45
|
-
|
|
46
|
-
export default kbFetch
|
|
47
|
-
|
|
48
|
-
export const afetch = defaultKbFetch
|
package/test.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import kbFetch, { createKbFetch } from '.'
|
|
2
|
-
|
|
3
|
-
const fetch = createKbFetch({ baseUrl: 'https://api.xiaomark.com/v1/link/' })
|
|
4
|
-
fetch.post('create', {
|
|
5
|
-
apikey: '0da19eee6120a83930d77f281c262284',
|
|
6
|
-
domain: 't.otth.xyz',
|
|
7
|
-
origin_url: 'https://baidu.com'
|
|
8
|
-
}, {
|
|
9
|
-
after(res) {
|
|
10
|
-
return res.then(res => {
|
|
11
|
-
console.log(res, res.data, '===>')
|
|
12
|
-
if (!res.ok) return Promise.reject(res)
|
|
13
|
-
return res.data
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
}).then(res => console.log(res))
|
package/tsconfig.json
DELETED
|
@@ -1,23 +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
|
-
"noImplicitAny": false,
|
|
13
|
-
"downlevelIteration": true,
|
|
14
|
-
"skipLibCheck": true,
|
|
15
|
-
"jsx": "react-jsx",
|
|
16
|
-
"allowSyntheticDefaultImports": true,
|
|
17
|
-
"forceConsistentCasingInFileNames": true,
|
|
18
|
-
"allowJs": true,
|
|
19
|
-
"types": [
|
|
20
|
-
"bun-types" // add Bun global
|
|
21
|
-
]
|
|
22
|
-
}
|
|
23
|
-
}
|