kbfetch 0.0.2 → 0.0.3
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/dprint.json +22 -0
- package/help.ts +7 -7
- package/index.ts +41 -19
- package/package.json +1 -1
- package/test.ts +16 -0
- package/tsconfig.json +21 -20
package/dprint.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
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
|
|
2
6
|
const help = {
|
|
3
7
|
synthesizeUrlWithParams: (url: string, params?: Record<string, any>) => {
|
|
4
8
|
if (!params) return url
|
|
@@ -20,11 +24,7 @@ const help = {
|
|
|
20
24
|
const contentType = res.headers.get('content-type') || ''
|
|
21
25
|
if (contentType.includes('application/json')) return res.json()
|
|
22
26
|
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
|
-
},
|
|
27
|
+
}
|
|
29
28
|
}
|
|
29
|
+
|
|
30
30
|
export default help
|
package/index.ts
CHANGED
|
@@ -1,26 +1,48 @@
|
|
|
1
|
-
import h, {
|
|
2
|
-
type Res<T> = Omit<Response, 'body' | 'bodyUsed'> & { data: T }
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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 }
|
|
9
22
|
})
|
|
23
|
+
return after(response)
|
|
10
24
|
},
|
|
11
|
-
|
|
12
|
-
|
|
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)
|
|
13
28
|
},
|
|
14
|
-
|
|
29
|
+
|
|
30
|
+
g: function<T = any>(url: string, init?: KbFetchInit & { params?: Record<string, any> }) {
|
|
15
31
|
const { params, ..._init } = init || {}
|
|
16
|
-
return
|
|
32
|
+
return this.get<T>(url, params, _init)
|
|
17
33
|
},
|
|
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
34
|
|
|
23
|
-
|
|
24
|
-
|
|
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
|
+
}
|
|
25
40
|
}
|
|
26
|
-
|
|
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/package.json
CHANGED
package/test.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
+
}
|
|
22
23
|
}
|