kbfetch 0.0.2
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.md +0 -0
- package/bun.lockb +0 -0
- package/help.ts +30 -0
- package/index.ts +26 -0
- package/package.json +17 -0
- package/tsconfig.json +22 -0
package/README.md
ADDED
|
File without changes
|
package/bun.lockb
ADDED
|
Binary file
|
package/help.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
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/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kbfetch",
|
|
3
|
+
"module": "index.ts",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"version": "0.0.2",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"pub": " npm publish --registry https://registry.npmjs.org/ &&bun updatetaobao ",
|
|
9
|
+
"updatetaobao": "cnpm sync kbfetch"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"bun-types": "latest"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"typescript": "^5.0.0"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
}
|