nuxt-custom-fetch 1.1.1 → 1.1.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/README.md CHANGED
@@ -10,7 +10,6 @@
10
10
  {
11
11
  baseURL?: string
12
12
  paramsHandler?: (params: FetchOptions['params']) => FetchOptions & HTTPConfig
13
- extraParams?: string[]
14
13
  offline?: () => void
15
14
  }
16
15
  ```
@@ -23,11 +22,6 @@
23
22
 
24
23
  处理 params 的自定义函数
25
24
 
26
- ### extraParams
27
-
28
- 排除在 params 中的 key(用于生成\$fetch 所需要的唯一 key)
29
- 类型: string[]
30
-
31
25
  ### 请求-响应拦截
32
26
 
33
27
  ```ts
@@ -68,11 +62,7 @@
68
62
 
69
63
  ## 实例方法
70
64
 
71
- ### http
72
-
73
- ### get
74
-
75
- ### post
65
+ `http|get|post`
76
66
 
77
67
  ```js
78
68
  import { hash } from 'ohash'
@@ -119,20 +109,9 @@ interface fetchOptions {
119
109
  }
120
110
  ```
121
111
 
122
- ### AsyncDataOptions
112
+ ### Type
123
113
 
124
- ```ts
125
- // AsyncDataOptions<DataT>
126
- {
127
- server?: boolean;
128
- lazy?: boolean;
129
- default?: () => DataT | Ref<DataT> | null;
130
- transform?: Transform;
131
- pick?: PickKeys;
132
- watch?: MultiWatchSources;
133
- immediate?: boolean;
134
- }
135
- ```
114
+ [type](https://nuxt.com/docs/api/composables/use-async-data#type)
136
115
 
137
116
  ## 使用方式
138
117
 
@@ -0,0 +1,7 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+
3
+ interface ModuleOptions {
4
+ }
5
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
6
+
7
+ export { type ModuleOptions, _default as default };
package/dist/module.d.ts CHANGED
@@ -4,4 +4,4 @@ interface ModuleOptions {
4
4
  }
5
5
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
6
6
 
7
- export { ModuleOptions, _default as default };
7
+ export { type ModuleOptions, _default as default };
package/dist/module.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "nuxt-custom-fetch",
3
3
  "configKey": "customFetch",
4
- "version": "1.1.1"
4
+ "version": "1.1.3"
5
5
  }
@@ -1,6 +1,6 @@
1
- import type { _AsyncData, AsyncDataOptions } from 'nuxt/dist/app/composables/asyncData';
2
- import type { HTTPConfig, Interceptors, AjaxConfig } from './type';
3
- export declare const ajax: <DataT, ErrorT = Error | null>(url: string, key: string, config: AjaxConfig, options?: AsyncDataOptions<DataT, DataT, import("nuxt/dist/app/composables/asyncData").KeysOf<DataT>, null> | undefined) => Promise<_AsyncData<DataT, ErrorT>>;
1
+ import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app';
2
+ import type { HTTPConfig, Interceptors, AjaxConfig, KeysOf, PickFrom } from './type';
3
+ export declare const ajax: <DataT, ErrorT = Error | null>(url: string, key: string, config: AjaxConfig, options?: AsyncDataOptions<DataT, DataT, import("nuxt/app").KeysOf<DataT>, null> | undefined) => AsyncData<PickFrom<DataT, KeysOf<DataT>> | null, (ErrorT extends Error | NuxtError<unknown> ? ErrorT : NuxtError<ErrorT>) | null>;
4
4
  export declare class CustomFetch {
5
5
  baseURL: any;
6
6
  params: HTTPConfig;
@@ -11,11 +11,11 @@ export declare class CustomFetch {
11
11
  private baseConfig;
12
12
  http<DataT, ErrorT = Error | null>(url: string, config?: HTTPConfig & {
13
13
  key?: string;
14
- }, options?: AsyncDataOptions<DataT>): Promise<_AsyncData<DataT, ErrorT>>;
14
+ }, options?: AsyncDataOptions<DataT>): AsyncData<PickFrom<DataT, KeysOf<DataT>> | null, (ErrorT extends Error | NuxtError<unknown> ? ErrorT : NuxtError<ErrorT>) | null>;
15
15
  get<DataT, ErrorT = Error | null>(url: string, config?: HTTPConfig & {
16
16
  key?: string;
17
- }, options?: AsyncDataOptions<DataT>): Promise<_AsyncData<DataT, ErrorT>>;
17
+ }, options?: AsyncDataOptions<DataT>): AsyncData<PickFrom<DataT, KeysOf<DataT>> | null, (ErrorT extends Error | NuxtError<unknown> ? ErrorT : NuxtError<ErrorT>) | null>;
18
18
  post<DataT, ErrorT = Error | null>(url: string, config?: HTTPConfig & {
19
19
  key?: string;
20
- }, options?: AsyncDataOptions<DataT>): Promise<_AsyncData<DataT, ErrorT>>;
20
+ }, options?: AsyncDataOptions<DataT>): AsyncData<PickFrom<DataT, KeysOf<DataT>> | null, (ErrorT extends Error | NuxtError<unknown> ? ErrorT : NuxtError<ErrorT>) | null>;
21
21
  }
@@ -1,11 +1,18 @@
1
1
  import { hash } from "ohash";
2
2
  import { createError } from "h3";
3
- import { useAsyncData } from "#imports";
3
+ import { toValue, useAsyncData } from "#imports";
4
4
  export const ajax = (url, key, config, options) => {
5
5
  if (process.client && navigator && !navigator.onLine) {
6
6
  config.offline && config.offline();
7
7
  }
8
- const { onRequest, onRequestError, onResponse, onResponseError, interceptors, ...restAjaxConfig } = config;
8
+ const {
9
+ onRequest,
10
+ onRequestError,
11
+ onResponse,
12
+ onResponseError,
13
+ interceptors,
14
+ ...restAjaxConfig
15
+ } = config;
9
16
  const defaultOptions = {
10
17
  onRequest(ctx) {
11
18
  [interceptors.onRequest, onRequest].map((fn) => fn && fn(ctx));
@@ -24,7 +31,9 @@ export const ajax = (url, key, config, options) => {
24
31
  onResponse && onResponse(ctx);
25
32
  },
26
33
  onResponseError(ctx) {
27
- [interceptors.onResponseError, onResponseError].map((fn) => fn && fn(ctx));
34
+ [interceptors.onResponseError, onResponseError].map(
35
+ (fn) => fn && fn(ctx)
36
+ );
28
37
  throw createError({
29
38
  statusCode: ctx.response.status,
30
39
  statusMessage: ctx.request.toString(),
@@ -33,22 +42,33 @@ export const ajax = (url, key, config, options) => {
33
42
  });
34
43
  }
35
44
  };
36
- return useAsyncData(key, () => $fetch(url, {
37
- ...defaultOptions,
38
- ...restAjaxConfig,
39
- method: restAjaxConfig.method
40
- }), options);
45
+ return useAsyncData(
46
+ key,
47
+ () => $fetch(url, {
48
+ ...defaultOptions,
49
+ ...restAjaxConfig,
50
+ method: restAjaxConfig.method
51
+ }),
52
+ options
53
+ );
41
54
  };
42
- function getKey(url, config, extraParams = []) {
43
- const params = (config.query ? config.query : config.params) || {};
44
- const restConfig = {};
45
- for (const key2 in params) {
46
- if (Object.prototype.hasOwnProperty.call(params, key2) && !extraParams.includes(key2)) {
47
- restConfig[key2] = params[key2];
55
+ function generateOptionSegments(opts) {
56
+ const segments = [
57
+ toValue(opts.method)?.toUpperCase() || "GET",
58
+ toValue(opts.baseURL)
59
+ ];
60
+ for (const _obj of [opts.params || opts.query]) {
61
+ const obj = toValue(_obj);
62
+ if (!obj) {
63
+ continue;
48
64
  }
65
+ const unwrapped = {};
66
+ for (const [key, value] of Object.entries(obj)) {
67
+ unwrapped[toValue(key)] = toValue(value);
68
+ }
69
+ segments.push(unwrapped);
49
70
  }
50
- const key = hash(JSON.stringify(restConfig) + url);
51
- return key;
71
+ return segments;
52
72
  }
53
73
  const Noop = () => {
54
74
  };
@@ -84,22 +104,31 @@ export class CustomFetch {
84
104
  return { ...params };
85
105
  }
86
106
  http(url, config = {}, options) {
87
- const key = config.key ? config.key : getKey(url, config, this.params.extraParams);
107
+ const key = config.key ? config.key : hash([...generateOptionSegments(config)]);
88
108
  const baseURL = config?.baseURL || this.baseURL;
89
109
  const query = this.baseConfig(config);
90
110
  const offline = config?.offline || this.offline;
91
- return ajax(url, key, {
92
- baseURL,
93
- ...config,
94
- query,
95
- interceptors: this.interceptors,
96
- offline
97
- }, options);
111
+ return ajax(
112
+ url,
113
+ key,
114
+ {
115
+ baseURL,
116
+ ...config,
117
+ query,
118
+ interceptors: this.interceptors,
119
+ offline
120
+ },
121
+ options
122
+ );
98
123
  }
99
124
  get(url, config = {}, options) {
100
125
  return this.http(url, { ...config, method: "GET" }, options);
101
126
  }
102
127
  post(url, config = {}, options) {
103
- return this.http(url, { ...config, method: "POST" }, options);
128
+ return this.http(
129
+ url,
130
+ { ...config, method: "POST" },
131
+ options
132
+ );
104
133
  }
105
134
  }
@@ -1,4 +1,6 @@
1
1
  import type { FetchRequest, FetchOptions, FetchError, FetchResponse } from 'ofetch';
2
+ export type PickFrom<T, K extends Array<string>> = T extends Array<any> ? T : T extends Record<string, any> ? keyof T extends K[number] ? T : K[number] extends never ? T : Pick<T, K[number]> : T;
3
+ export type KeysOf<T> = Array<T extends T ? keyof T extends string ? keyof T : never : never>;
2
4
  export interface OnRequestType {
3
5
  request: FetchRequest;
4
6
  options: FetchOptions;
@@ -21,7 +23,6 @@ export interface OnResponseErrorType {
21
23
  export type FetchMethod = 'options' | 'GET' | 'POST' | 'get' | 'HEAD' | 'PATCH' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'post' | 'head' | 'patch' | 'put' | 'delete' | 'connect' | 'trace' | undefined;
22
24
  export interface HTTPConfig extends Omit<FetchOptions, 'method'> {
23
25
  baseURL?: string;
24
- extraParams?: string[];
25
26
  useParamsHandler?: boolean;
26
27
  paramsHandler?: (params: FetchOptions['params']) => HTTPConfig;
27
28
  offline?: () => void;
@@ -0,0 +1,16 @@
1
+
2
+ import type { ModuleOptions } from './module.js'
3
+
4
+
5
+ declare module '@nuxt/schema' {
6
+ interface NuxtConfig { ['customFetch']?: Partial<ModuleOptions> }
7
+ interface NuxtOptions { ['customFetch']?: ModuleOptions }
8
+ }
9
+
10
+ declare module 'nuxt/schema' {
11
+ interface NuxtConfig { ['customFetch']?: Partial<ModuleOptions> }
12
+ interface NuxtOptions { ['customFetch']?: ModuleOptions }
13
+ }
14
+
15
+
16
+ export type { ModuleOptions, default } from './module.js'
package/dist/types.d.ts CHANGED
@@ -1,10 +1,16 @@
1
1
 
2
- import { ModuleOptions } from './module'
2
+ import type { ModuleOptions } from './module'
3
+
3
4
 
4
5
  declare module '@nuxt/schema' {
5
6
  interface NuxtConfig { ['customFetch']?: Partial<ModuleOptions> }
6
7
  interface NuxtOptions { ['customFetch']?: ModuleOptions }
7
8
  }
8
9
 
10
+ declare module 'nuxt/schema' {
11
+ interface NuxtConfig { ['customFetch']?: Partial<ModuleOptions> }
12
+ interface NuxtOptions { ['customFetch']?: ModuleOptions }
13
+ }
14
+
9
15
 
10
- export { ModuleOptions, default } from './module'
16
+ export type { ModuleOptions, default } from './module'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-custom-fetch",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -19,19 +19,20 @@
19
19
  "url": "https://github.com/xjccc/nuxt-custom-fetch.git"
20
20
  },
21
21
  "scripts": {
22
- "prepack": "nuxt-module-build",
22
+ "prepack": "nuxt-module-build build",
23
23
  "dev": "nuxi dev playground",
24
24
  "dev:build": "nuxi build playground",
25
25
  "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground"
26
26
  },
27
27
  "dependencies": {
28
- "@nuxt/kit": "^3.8.1"
28
+ "@nuxt/kit": "^3.10.2",
29
+ "typescript": "^5.3.3"
29
30
  },
30
31
  "devDependencies": {
31
- "@nuxt/module-builder": "^0.2.1",
32
- "@nuxt/schema": "^3.8.1",
32
+ "@nuxt/module-builder": "^0.5.5",
33
+ "@nuxt/schema": "^3.10.2",
33
34
  "@nuxtjs/eslint-config-typescript": "^12.1.0",
34
- "eslint": "^8.53.0",
35
- "nuxt": "^3.8.1"
35
+ "eslint": "^8.56.0",
36
+ "nuxt": "^3.10.2"
36
37
  }
37
38
  }