nuxt-custom-fetch 1.1.0 → 1.1.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 CHANGED
@@ -68,11 +68,7 @@
68
68
 
69
69
  ## 实例方法
70
70
 
71
- ### http
72
-
73
- ### get
74
-
75
- ### post
71
+ `http|get|post`
76
72
 
77
73
  ```js
78
74
  import { hash } from 'ohash'
@@ -119,20 +115,9 @@ interface fetchOptions {
119
115
  }
120
116
  ```
121
117
 
122
- ### AsyncDataOptions
118
+ ### Type
123
119
 
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
- ```
120
+ [type](https://nuxt.com/docs/api/composables/use-async-data#type)
136
121
 
137
122
  ## 使用方式
138
123
 
@@ -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.0"
4
+ "version": "1.1.2"
5
5
  }
@@ -1,6 +1,6 @@
1
- import type { _AsyncData, AsyncDataOptions } from 'nuxt/dist/app/composables/asyncData';
2
- import { 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
  }
@@ -5,7 +5,14 @@ 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,11 +42,15 @@ 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
55
  function getKey(url, config, extraParams = []) {
43
56
  const params = (config.query ? config.query : config.params) || {};
@@ -88,18 +101,27 @@ export class CustomFetch {
88
101
  const baseURL = config?.baseURL || this.baseURL;
89
102
  const query = this.baseConfig(config);
90
103
  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);
104
+ return ajax(
105
+ url,
106
+ key,
107
+ {
108
+ baseURL,
109
+ ...config,
110
+ query,
111
+ interceptors: this.interceptors,
112
+ offline
113
+ },
114
+ options
115
+ );
98
116
  }
99
117
  get(url, config = {}, options) {
100
118
  return this.http(url, { ...config, method: "GET" }, options);
101
119
  }
102
120
  post(url, config = {}, options) {
103
- return this.http(url, { ...config, method: "POST" }, options);
121
+ return this.http(
122
+ url,
123
+ { ...config, method: "POST" },
124
+ options
125
+ );
104
126
  }
105
127
  }
@@ -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;
@@ -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.0",
3
+ "version": "1.1.2",
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
  }