nuxt-custom-fetch 0.0.1
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 +97 -0
- package/dist/module.cjs +5 -0
- package/dist/module.d.ts +7 -0
- package/dist/module.json +5 -0
- package/dist/module.mjs +21 -0
- package/dist/runtime/ajax.d.ts +21 -0
- package/dist/runtime/ajax.mjs +92 -0
- package/dist/types.d.ts +10 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Nuxt Module
|
|
2
|
+
|
|
3
|
+
简单封装 useAsyncData
|
|
4
|
+
|
|
5
|
+
## HTTPOptions
|
|
6
|
+
|
|
7
|
+
实例化全局 HTTP
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
baseURL?: string
|
|
11
|
+
useHandler?: boolean
|
|
12
|
+
paramsHandler?: (params: FetchOptions['params']) => FetchOptions & HTTPConfig
|
|
13
|
+
extraParams?: string[]
|
|
14
|
+
offline?: () => void
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### baseURL
|
|
18
|
+
|
|
19
|
+
当前实例下的 baseURL
|
|
20
|
+
|
|
21
|
+
### useHandler
|
|
22
|
+
|
|
23
|
+
是否使用 paramsHandler
|
|
24
|
+
|
|
25
|
+
### paramsHandler
|
|
26
|
+
|
|
27
|
+
处理 params 的自定义函数
|
|
28
|
+
|
|
29
|
+
### extraParams
|
|
30
|
+
|
|
31
|
+
排除在 params 中的 key(用于生成\$fetch 所需要的唯一 key)
|
|
32
|
+
类型: string[]
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { hash } from 'ohash'
|
|
36
|
+
const key = hash(JSON.stringify(restConfig) + url)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### offline
|
|
40
|
+
|
|
41
|
+
客户端中,离线时调用(判断 navigator.onLine)
|
|
42
|
+
|
|
43
|
+
## ajax - 传参
|
|
44
|
+
|
|
45
|
+
### fetchOptions
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
baseURL?: string;
|
|
49
|
+
body?: RequestInit["body"] | Record<string, any>;
|
|
50
|
+
params?: SearchParameters;
|
|
51
|
+
query?: SearchParameters;
|
|
52
|
+
parseResponse?: (responseText: string) => any;
|
|
53
|
+
responseType?: R;
|
|
54
|
+
response?: boolean;
|
|
55
|
+
retry?: number | false;
|
|
56
|
+
onRequest?(context: FetchContext): Promise<void> | void;
|
|
57
|
+
onRequestError?(context: FetchContext & {
|
|
58
|
+
error: Error;
|
|
59
|
+
}): Promise<void> | void;
|
|
60
|
+
onResponse?(context: FetchContext & {
|
|
61
|
+
response: FetchResponse<R>;
|
|
62
|
+
}): Promise<void> | void;
|
|
63
|
+
onResponseError?(context: FetchContext & {
|
|
64
|
+
response: FetchResponse<R>;
|
|
65
|
+
}): Promise<void> | void;
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 使用方式
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
// ajax.ts文件
|
|
72
|
+
import type { FetchOptions } from 'ofetch'
|
|
73
|
+
const ajax = new HTTP({
|
|
74
|
+
baseURL: '',
|
|
75
|
+
paramsHandler: (params = {}) => {
|
|
76
|
+
params.aa = 111
|
|
77
|
+
return params
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
export const getInfo = <DataT>(params: FetchOptions['params']) =>
|
|
82
|
+
ajax.get<DataT>('/api/get-ip', { params })
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { getInfo } from './ajax'
|
|
87
|
+
|
|
88
|
+
const { data, error, pending } = await getInfo<T>({
|
|
89
|
+
sign: 123
|
|
90
|
+
})
|
|
91
|
+
console.log(data.value)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Development
|
|
95
|
+
|
|
96
|
+
- Run `npm run dev:prepare` to generate type stubs.
|
|
97
|
+
- Use `npm run dev` to start [playground](./playground) in development mode.
|
package/dist/module.cjs
ADDED
package/dist/module.d.ts
ADDED
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { fileURLToPath } from 'url';
|
|
2
|
+
import { defineNuxtModule, createResolver, addImports } from '@nuxt/kit';
|
|
3
|
+
|
|
4
|
+
const module = defineNuxtModule({
|
|
5
|
+
meta: {
|
|
6
|
+
name: "nuxt-http-fetch",
|
|
7
|
+
configKey: "httpFetch"
|
|
8
|
+
},
|
|
9
|
+
defaults: {},
|
|
10
|
+
setup(options, nuxt) {
|
|
11
|
+
const { resolve } = createResolver(import.meta.url);
|
|
12
|
+
const runtimeDir = fileURLToPath(new URL("./runtime", import.meta.url));
|
|
13
|
+
nuxt.options.build.transpile.push(runtimeDir);
|
|
14
|
+
addImports({
|
|
15
|
+
from: resolve(runtimeDir, "ajax"),
|
|
16
|
+
name: "HTTP"
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export { module as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { FetchOptions } from 'ofetch';
|
|
2
|
+
import type { _AsyncData } from 'nuxt/dist/app/composables/asyncData';
|
|
3
|
+
export declare const ajax: <DataT, ErrorT = Error | null>(url: string, key: string, config?: FetchOptions & {
|
|
4
|
+
extraParams?: string[];
|
|
5
|
+
offline?: () => void;
|
|
6
|
+
}) => Promise<_AsyncData<DataT, ErrorT>>;
|
|
7
|
+
type HTTPConfig = {
|
|
8
|
+
baseURL?: string;
|
|
9
|
+
useHandler?: boolean;
|
|
10
|
+
extraParams?: string[];
|
|
11
|
+
paramsHandler?: (params: FetchOptions['params']) => FetchOptions & HTTPConfig;
|
|
12
|
+
offline?: () => void;
|
|
13
|
+
};
|
|
14
|
+
export declare class HTTP {
|
|
15
|
+
params: HTTPConfig;
|
|
16
|
+
constructor(config?: HTTPConfig);
|
|
17
|
+
baseConfig(config: FetchOptions): FetchOptions;
|
|
18
|
+
get<DataT, ErrorT = Error | null>(url: string, config?: FetchOptions): Promise<_AsyncData<DataT, ErrorT>>;
|
|
19
|
+
post<DataT, ErrorT = Error | null>(url: string, config?: FetchOptions): Promise<_AsyncData<DataT, ErrorT>>;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { hash } from "ohash";
|
|
2
|
+
import { createError } from "h3";
|
|
3
|
+
import { useAsyncData } from "#imports";
|
|
4
|
+
export const ajax = (url, key, config = {}) => {
|
|
5
|
+
if (process.client && navigator && !navigator.onLine) {
|
|
6
|
+
config.offline && config.offline();
|
|
7
|
+
}
|
|
8
|
+
const defaultOptions = {
|
|
9
|
+
key,
|
|
10
|
+
onRequest({
|
|
11
|
+
options
|
|
12
|
+
}) {
|
|
13
|
+
options.headers = options.headers || {};
|
|
14
|
+
},
|
|
15
|
+
onRequestError({
|
|
16
|
+
error
|
|
17
|
+
}) {
|
|
18
|
+
throw createError({
|
|
19
|
+
statusCode: error.statusCode,
|
|
20
|
+
statusMessage: error.statusMessage,
|
|
21
|
+
message: error.message
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
onResponse({
|
|
25
|
+
response
|
|
26
|
+
}) {
|
|
27
|
+
return response._data;
|
|
28
|
+
},
|
|
29
|
+
onResponseError({
|
|
30
|
+
request,
|
|
31
|
+
response
|
|
32
|
+
}) {
|
|
33
|
+
throw createError({
|
|
34
|
+
statusCode: 500,
|
|
35
|
+
statusMessage: request.toString(),
|
|
36
|
+
message: response._data.messages
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
return useAsyncData(key, () => $fetch(url, {
|
|
41
|
+
...defaultOptions,
|
|
42
|
+
...config
|
|
43
|
+
}));
|
|
44
|
+
};
|
|
45
|
+
function getKey(url, params = {}, extraParams = []) {
|
|
46
|
+
const restConfig = {};
|
|
47
|
+
for (const key2 in params) {
|
|
48
|
+
if (Object.prototype.hasOwnProperty.call(params, key2) && !extraParams.includes(key2)) {
|
|
49
|
+
restConfig[key2] = params[key2];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const key = hash(JSON.stringify(restConfig) + url);
|
|
53
|
+
return key;
|
|
54
|
+
}
|
|
55
|
+
export class HTTP {
|
|
56
|
+
constructor(config = { baseURL: "" }) {
|
|
57
|
+
this.params = {};
|
|
58
|
+
this.params = {
|
|
59
|
+
useHandler: true,
|
|
60
|
+
...config
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
baseConfig(config) {
|
|
64
|
+
const { paramsHandler } = this.params;
|
|
65
|
+
const params = config.params || {};
|
|
66
|
+
if (paramsHandler && typeof paramsHandler === "function") {
|
|
67
|
+
return paramsHandler({ ...params });
|
|
68
|
+
}
|
|
69
|
+
return { ...params };
|
|
70
|
+
}
|
|
71
|
+
get(url, config = {}) {
|
|
72
|
+
const key = getKey(url, config.params, this.params.extraParams);
|
|
73
|
+
const params = this.params.useHandler ? this.baseConfig(config) : config.params;
|
|
74
|
+
return ajax(url, key, {
|
|
75
|
+
baseURL: this.params.baseURL,
|
|
76
|
+
method: "GET",
|
|
77
|
+
...config,
|
|
78
|
+
params
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
post(url, config = {}) {
|
|
82
|
+
const key = getKey(url, config.params, this.params.extraParams);
|
|
83
|
+
const params = this.params.useHandler ? this.baseConfig(config) : config.params;
|
|
84
|
+
return ajax(url, key, {
|
|
85
|
+
baseURL: this.params.baseURL,
|
|
86
|
+
method: "POST",
|
|
87
|
+
...config,
|
|
88
|
+
params,
|
|
89
|
+
offline: this.params.offline
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
import { ModuleOptions } from './module'
|
|
3
|
+
|
|
4
|
+
declare module '@nuxt/schema' {
|
|
5
|
+
interface NuxtConfig { ['httpFetch']?: Partial<ModuleOptions> }
|
|
6
|
+
interface NuxtOptions { ['httpFetch']?: ModuleOptions }
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export { ModuleOptions, default } from './module'
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nuxt-custom-fetch",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/module.mjs",
|
|
9
|
+
"require": "./dist/module.cjs"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"main": "./dist/module.cjs",
|
|
13
|
+
"types": "./dist/types.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@nuxt/kit": "^3.0.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@nuxt/module-builder": "^0.2.1",
|
|
22
|
+
"@nuxt/schema": "^3.0.0",
|
|
23
|
+
"@nuxtjs/eslint-config-typescript": "^12.0.0",
|
|
24
|
+
"eslint": "^8.29.0",
|
|
25
|
+
"nuxt": "^3.0.0"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"dev": "nuxi dev playground",
|
|
29
|
+
"dev:build": "nuxi build playground",
|
|
30
|
+
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground"
|
|
31
|
+
}
|
|
32
|
+
}
|