nuxt-custom-fetch 2.1.4 → 2.2.0
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 +1 -0
- package/dist/module.json +1 -1
- package/dist/runtime/ajax.d.ts +2 -1
- package/dist/runtime/ajax.js +14 -6
- package/dist/runtime/type.d.ts +1 -0
- package/package.json +8 -7
package/README.md
CHANGED
package/dist/module.json
CHANGED
package/dist/runtime/ajax.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { NitroFetchRequest } from '
|
|
1
|
+
import type { NitroFetchRequest } from 'nitro';
|
|
2
2
|
import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app';
|
|
3
3
|
import type { FetchMethod, HTTPConfig, Interceptors, KeysOf, PickFrom } from './type.js';
|
|
4
4
|
type CustomFetchReturnValue<DataT, ErrorT> = AsyncData<PickFrom<DataT, KeysOf<DataT>> | null, (ErrorT extends Error | NuxtError<unknown> ? ErrorT : NuxtError<ErrorT>) | null>;
|
|
@@ -10,6 +10,7 @@ export declare class CustomFetch {
|
|
|
10
10
|
baseHandler: HTTPConfig['handler'];
|
|
11
11
|
interceptors: Interceptors;
|
|
12
12
|
offline: typeof Noop;
|
|
13
|
+
showLogs: boolean;
|
|
13
14
|
constructor(config?: HTTPConfig);
|
|
14
15
|
private baseConfig;
|
|
15
16
|
http<DataT, ErrorT = Error | null>(url: NitroFetchRequest, config: HTTPConfig & {
|
package/dist/runtime/ajax.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createError, reactive, ref, toValue, unref, useAsyncData, useNuxtApp } from "#imports";
|
|
2
|
-
import { hash } from "ohash";
|
|
2
|
+
import { hash, serialize } from "ohash";
|
|
3
3
|
function Noop() {
|
|
4
4
|
}
|
|
5
5
|
function generateOptionSegments(opts) {
|
|
@@ -32,10 +32,14 @@ export class CustomFetch {
|
|
|
32
32
|
baseHandler;
|
|
33
33
|
interceptors = {};
|
|
34
34
|
offline = Noop;
|
|
35
|
-
|
|
35
|
+
showLogs = false;
|
|
36
|
+
constructor(config = {
|
|
37
|
+
baseURL: ""
|
|
38
|
+
}) {
|
|
36
39
|
this.params = { ...config };
|
|
37
40
|
this.baseURL = config.baseURL;
|
|
38
41
|
this.immutableKey = config.immutableKey ?? false;
|
|
42
|
+
this.showLogs = config.showLogs ?? import.meta.dev;
|
|
39
43
|
if (config.handler) {
|
|
40
44
|
this.baseHandler = config.handler;
|
|
41
45
|
}
|
|
@@ -63,6 +67,10 @@ export class CustomFetch {
|
|
|
63
67
|
http(url, config, options = {}) {
|
|
64
68
|
config.baseURL = config?.baseURL || this.baseURL;
|
|
65
69
|
Object.assign(config, this.baseConfig(config));
|
|
70
|
+
const generateOptionSegmentsWithConfig = generateOptionSegments(config);
|
|
71
|
+
if (this.showLogs && import.meta.client) {
|
|
72
|
+
console.warn("[Custom Fetch] `Request:`", url, " `Query:`", serialize(generateOptionSegmentsWithConfig), " `Body:`", serialize(config.body));
|
|
73
|
+
}
|
|
66
74
|
const { onRequest, onRequestError, onResponse, onResponseError, offline, handler, useHandler, immutableKey, ...restAjaxConfig } = config;
|
|
67
75
|
if (import.meta.client && navigator && !navigator.onLine) {
|
|
68
76
|
if (offline) {
|
|
@@ -121,11 +129,11 @@ export class CustomFetch {
|
|
|
121
129
|
});
|
|
122
130
|
}
|
|
123
131
|
};
|
|
124
|
-
const hashValue = ["
|
|
132
|
+
const hashValue = ["custom_fetch:", url];
|
|
125
133
|
if (!this.immutableKey && !immutableKey) {
|
|
126
|
-
hashValue.push(...
|
|
134
|
+
hashValue.push(...generateOptionSegmentsWithConfig);
|
|
127
135
|
}
|
|
128
|
-
const key = hash(hashValue);
|
|
136
|
+
const key = hash(hashValue).replace(/[-_]/g, "").slice(0, 10);
|
|
129
137
|
options.default = options.default ?? (() => null);
|
|
130
138
|
const _handler = () => {
|
|
131
139
|
if (_cachedController.get(key)) {
|
|
@@ -137,8 +145,8 @@ export class CustomFetch {
|
|
|
137
145
|
..._config
|
|
138
146
|
});
|
|
139
147
|
};
|
|
140
|
-
const nuxtApp = useNuxtApp();
|
|
141
148
|
const controller = typeof AbortController !== "undefined" ? new AbortController() : {};
|
|
149
|
+
const nuxtApp = useNuxtApp();
|
|
142
150
|
if (import.meta.client && !nuxtApp.isHydrating) {
|
|
143
151
|
const asyncData = {
|
|
144
152
|
data: ref(null),
|
package/dist/runtime/type.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export type FetchMethod = 'options' | 'GET' | 'POST' | 'get' | 'HEAD' | 'PATCH'
|
|
|
6
6
|
export interface HTTPConfig extends Omit<FetchOptions, 'method'> {
|
|
7
7
|
key?: string;
|
|
8
8
|
immutableKey?: boolean;
|
|
9
|
+
showLogs?: boolean;
|
|
9
10
|
interceptors?: Interceptors;
|
|
10
11
|
baseURL?: string;
|
|
11
12
|
useHandler?: boolean;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-custom-fetch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.2.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -37,23 +37,24 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@nuxt/kit": "^3.15.4",
|
|
40
|
-
"ohash": "^
|
|
40
|
+
"ohash": "^2.0.11"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@antfu/eslint-config": "^4.
|
|
43
|
+
"@antfu/eslint-config": "^4.4.0",
|
|
44
44
|
"@commitlint/cli": "^19.7.1",
|
|
45
45
|
"@commitlint/config-conventional": "^19.7.1",
|
|
46
46
|
"@nuxt/module-builder": "^0.8.4",
|
|
47
47
|
"@nuxt/schema": "^3.15.4",
|
|
48
|
-
"@types/node": "^22.13.
|
|
49
|
-
"changelogen": "^0.
|
|
48
|
+
"@types/node": "^22.13.9",
|
|
49
|
+
"changelogen": "^0.6.1",
|
|
50
50
|
"commitizen": "^4.3.1",
|
|
51
|
-
"cz-git": "^1.11.
|
|
51
|
+
"cz-git": "^1.11.1",
|
|
52
52
|
"eslint": "^9.21.0",
|
|
53
53
|
"husky": "^9.1.7",
|
|
54
54
|
"lint-staged": "^15.4.3",
|
|
55
|
+
"nitro": "npm:nitropack",
|
|
55
56
|
"nuxt": "^3.15.4",
|
|
56
|
-
"typescript": "
|
|
57
|
+
"typescript": "5.7.3"
|
|
57
58
|
},
|
|
58
59
|
"husky": {
|
|
59
60
|
"hooks": {
|