nuxt-custom-fetch 1.0.1 → 1.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/README.md CHANGED
@@ -26,6 +26,42 @@ offline?: () => void
26
26
  排除在 params 中的 key(用于生成\$fetch 所需要的唯一 key)
27
27
  类型: string[]
28
28
 
29
+ ### 请求-响应拦截
30
+
31
+ ```
32
+ onRequest?: ({ request, options }: {
33
+ request: FetchRequest
34
+ options: FetchOptions
35
+ }) => void
36
+ onRequestError?: ({
37
+ request,
38
+ options,
39
+ error
40
+ }: {
41
+ request: FetchRequest
42
+ options: FetchOptions
43
+ error: FetchError
44
+ }) => void
45
+ onResponse?: ({
46
+ request,
47
+ response,
48
+ options
49
+ }: {
50
+ request: FetchRequest
51
+ options: FetchOptions
52
+ response: FetchResponse<any>
53
+ }) => Promise<void> | void
54
+ onResponseError?: ({
55
+ request,
56
+ response,
57
+ options
58
+ }: {
59
+ request: FetchRequest
60
+ options: FetchOptions
61
+ response: FetchResponse<any>
62
+ }) => void
63
+ ```
64
+
29
65
  ```js
30
66
  import { hash } from 'ohash'
31
67
  const key = hash(JSON.stringify(restConfig) + url)
@@ -98,6 +134,10 @@ const { data, error, pending } = await getInfo({
98
134
  console.log(data.value)
99
135
  ```
100
136
 
137
+ ## 注意
138
+
139
+ 客户端请求,需要设置{server: false},在 onMounted 中请求无效(version > 3.0.0)
140
+
101
141
  ## Development
102
142
 
103
143
  - Run `npm run dev:prepare` to generate type stubs.
package/dist/module.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "nuxt-http-fetch",
3
3
  "configKey": "httpFetch",
4
- "version": "1.0.1"
4
+ "version": "1.0.3"
5
5
  }
@@ -1,20 +1,12 @@
1
1
  import type { FetchOptions } from 'ofetch';
2
2
  import type { _AsyncData, AsyncDataOptions } 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
- }, options?: AsyncDataOptions<DataT, import("nuxt/dist/app/composables/asyncData")._Transform<DataT, DataT>, import("nuxt/dist/app/composables/asyncData").KeyOfRes<import("nuxt/dist/app/composables/asyncData")._Transform<DataT, DataT>>> | undefined) => Promise<_AsyncData<DataT, ErrorT>>;
7
- type HTTPConfig = {
8
- baseURL?: string;
9
- extraParams?: string[];
10
- paramsHandler?: (params: FetchOptions['params']) => FetchOptions & HTTPConfig;
11
- offline?: () => void;
12
- };
3
+ import { HTTPConfig, Interceptors, AjaxConfig } from './type';
4
+ export declare const ajax: <DataT, ErrorT = Error | null>(url: string, key: string, config: AjaxConfig, options?: AsyncDataOptions<DataT, import("nuxt/dist/app/composables/asyncData")._Transform<DataT, DataT>, import("nuxt/dist/app/composables/asyncData").KeyOfRes<import("nuxt/dist/app/composables/asyncData")._Transform<DataT, DataT>>> | undefined) => Promise<_AsyncData<DataT, ErrorT>>;
13
5
  export declare class HTTP {
14
6
  params: HTTPConfig;
7
+ interceptors: Interceptors;
15
8
  constructor(config?: HTTPConfig);
16
- baseConfig(config: FetchOptions): FetchOptions;
9
+ private baseConfig;
17
10
  get<DataT, ErrorT = Error | null>(url: string, config?: FetchOptions, options?: AsyncDataOptions<DataT>): Promise<_AsyncData<DataT, ErrorT>>;
18
11
  post<DataT, ErrorT = Error | null>(url: string, config?: FetchOptions, options?: AsyncDataOptions<DataT>): Promise<_AsyncData<DataT, ErrorT>>;
19
12
  }
20
- export {};
@@ -1,45 +1,42 @@
1
1
  import { hash } from "ohash";
2
2
  import { createError } from "h3";
3
3
  import { useAsyncData } from "#imports";
4
- export const ajax = (url, key, config = {}, options) => {
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
9
  const defaultOptions = {
9
10
  key,
10
- onRequest({
11
- options: options2
12
- }) {
13
- options2.headers = options2.headers || {};
11
+ onRequest(ctx) {
12
+ [interceptors.onRequest, onRequest].map((fn) => fn && fn(ctx));
14
13
  },
15
- onRequestError({
16
- error
17
- }) {
14
+ onRequestError(ctx) {
15
+ [interceptors.onRequestError, onRequestError].map((fn) => fn && fn(ctx));
18
16
  throw createError({
19
- statusCode: error.statusCode,
20
- statusMessage: error.statusMessage,
21
- message: error.message
17
+ statusCode: ctx.error.statusCode,
18
+ statusMessage: ctx.error.message,
19
+ message: ctx.error.message,
20
+ fatal: true
22
21
  });
23
22
  },
24
- onResponse({
25
- response
26
- }) {
27
- return response._data;
23
+ onResponse(ctx) {
24
+ interceptors.onResponse && interceptors.onResponse(ctx);
25
+ onResponse && onResponse(ctx);
28
26
  },
29
- onResponseError({
30
- request,
31
- response
32
- }) {
27
+ onResponseError(ctx) {
28
+ [interceptors.onResponseError, onResponseError].map((fn) => fn && fn(ctx));
33
29
  throw createError({
34
- statusCode: 500,
35
- statusMessage: request.toString(),
36
- message: response._data.messages
30
+ statusCode: ctx.response.status,
31
+ statusMessage: ctx.request.toString(),
32
+ message: ctx.response._data,
33
+ fatal: true
37
34
  });
38
35
  }
39
36
  };
40
37
  return useAsyncData(key, () => $fetch(url, {
41
38
  ...defaultOptions,
42
- ...config
39
+ ...restAjaxConfig
43
40
  }), options);
44
41
  };
45
42
  function getKey(url, config, extraParams = []) {
@@ -56,9 +53,20 @@ function getKey(url, config, extraParams = []) {
56
53
  export class HTTP {
57
54
  constructor(config = { baseURL: "" }) {
58
55
  this.params = {};
56
+ this.interceptors = {};
59
57
  this.params = {
60
58
  ...config
61
59
  };
60
+ this.interceptors = {
61
+ onRequest: config.onRequest || function() {
62
+ },
63
+ onRequestError: config.onRequestError || function() {
64
+ },
65
+ onResponse: config.onResponse || function() {
66
+ },
67
+ onResponseError: config.onResponseError || function() {
68
+ }
69
+ };
62
70
  }
63
71
  baseConfig(config) {
64
72
  const { paramsHandler } = this.params;
@@ -75,7 +83,9 @@ export class HTTP {
75
83
  baseURL: this.params.baseURL,
76
84
  method: "GET",
77
85
  ...config,
78
- query
86
+ query,
87
+ interceptors: this.interceptors,
88
+ offline: this.params.offline
79
89
  }, options);
80
90
  }
81
91
  post(url, config = {}, options) {
@@ -86,6 +96,7 @@ export class HTTP {
86
96
  method: "POST",
87
97
  ...config,
88
98
  query,
99
+ interceptors: this.interceptors,
89
100
  offline: this.params.offline
90
101
  }, options);
91
102
  }
@@ -0,0 +1,41 @@
1
+ import type { FetchRequest, FetchOptions, FetchError, FetchResponse } from 'ofetch';
2
+ export interface OnRequestType {
3
+ request: FetchRequest;
4
+ options: FetchOptions;
5
+ }
6
+ export interface OnRequestErrorType {
7
+ request: FetchRequest;
8
+ options: FetchOptions;
9
+ error: FetchError;
10
+ }
11
+ export interface OnResponseType {
12
+ request: FetchRequest;
13
+ options: FetchOptions;
14
+ response: FetchResponse<any>;
15
+ }
16
+ export interface OnResponseErrorType {
17
+ request: FetchRequest;
18
+ options: FetchOptions;
19
+ response: FetchResponse<any>;
20
+ }
21
+ export type HTTPConfig = {
22
+ baseURL?: string;
23
+ extraParams?: string[];
24
+ paramsHandler?: (params: FetchOptions['params']) => FetchOptions & HTTPConfig;
25
+ offline?: () => void;
26
+ onRequest?: ({ request, options }: OnRequestType) => void;
27
+ onRequestError?: ({ request, options, error }: OnRequestErrorType) => void;
28
+ onResponse?: ({ request, response, options }: OnResponseType) => Promise<void> | void;
29
+ onResponseError?: ({ request, response, options }: OnResponseErrorType) => void;
30
+ };
31
+ export interface Interceptors {
32
+ onRequest?: ({ request, options }: OnRequestType) => void;
33
+ onRequestError?: ({ request, options, error }: OnRequestErrorType) => void;
34
+ onResponse?: ({ request, response, options }: OnResponseType) => Promise<void> | void;
35
+ onResponseError?: ({ request, response, options }: OnResponseErrorType) => void;
36
+ }
37
+ export type AjaxConfig = FetchOptions & {
38
+ extraParams?: string[];
39
+ offline?: () => void;
40
+ interceptors: Interceptors;
41
+ };
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-custom-fetch",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {