floppy-disk 2.10.0-beta.3 → 2.10.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/esm/utils/fetcher.d.ts +2 -0
- package/esm/utils/fetcher.js +26 -7
- package/esm/utils/index.d.ts +1 -0
- package/esm/utils/index.js +4 -0
- package/lib/utils/fetcher.d.ts +2 -0
- package/lib/utils/fetcher.js +25 -6
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +6 -1
- package/package.json +1 -1
package/esm/utils/fetcher.d.ts
CHANGED
package/esm/utils/fetcher.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import { getValueOrComputedValue, identityFn } from '.';
|
|
1
|
+
import { createError, getValueOrComputedValue, identityFn } from '.';
|
|
3
2
|
const encodeParams = (params) => Object.entries(params)
|
|
3
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
4
4
|
.map((kv) => kv.map(encodeURIComponent).join('='))
|
|
5
5
|
.join('&');
|
|
6
6
|
/**
|
|
@@ -8,6 +8,8 @@ const encodeParams = (params) => Object.entries(params)
|
|
|
8
8
|
*
|
|
9
9
|
* Can be used for REST or GraphQL.
|
|
10
10
|
*
|
|
11
|
+
* Only work for JSON response only.
|
|
12
|
+
*
|
|
11
13
|
* @see https://floppy-disk.vercel.app/docs/experimental
|
|
12
14
|
*
|
|
13
15
|
* @returns A function to fetch data
|
|
@@ -47,7 +49,12 @@ export const fetcher = (options) => async (...args) => {
|
|
|
47
49
|
let resJson = await res.json();
|
|
48
50
|
if (query) {
|
|
49
51
|
if (resJson.errors) {
|
|
50
|
-
throw
|
|
52
|
+
throw createError('Error GraphQL response', {
|
|
53
|
+
status: res.status,
|
|
54
|
+
statusText: res.statusText,
|
|
55
|
+
response: resJson.errors,
|
|
56
|
+
request: interceptedOptions,
|
|
57
|
+
});
|
|
51
58
|
}
|
|
52
59
|
resJson = resJson.data;
|
|
53
60
|
}
|
|
@@ -58,18 +65,30 @@ export const fetcher = (options) => async (...args) => {
|
|
|
58
65
|
return finalResponse;
|
|
59
66
|
}
|
|
60
67
|
catch (error) {
|
|
61
|
-
throw {
|
|
68
|
+
throw createError('Error intercept response', {
|
|
62
69
|
status: res.status,
|
|
63
70
|
statusText: res.statusText,
|
|
64
71
|
response: resJson,
|
|
65
72
|
error,
|
|
66
|
-
|
|
73
|
+
request: interceptedOptions,
|
|
74
|
+
});
|
|
67
75
|
}
|
|
68
76
|
}
|
|
69
77
|
return resJson;
|
|
70
78
|
}
|
|
71
|
-
throw
|
|
79
|
+
throw createError('Fetch error', {
|
|
80
|
+
status: res.status,
|
|
81
|
+
statusText: res.statusText,
|
|
82
|
+
response: resJson,
|
|
83
|
+
request: interceptedOptions,
|
|
84
|
+
});
|
|
72
85
|
}
|
|
73
86
|
const resText = await res.text().catch(() => undefined);
|
|
74
|
-
throw
|
|
87
|
+
throw createError('Response type is not JSON', {
|
|
88
|
+
status: res.status,
|
|
89
|
+
statusText: res.statusText,
|
|
90
|
+
response: resText,
|
|
91
|
+
contentType,
|
|
92
|
+
request: interceptedOptions,
|
|
93
|
+
});
|
|
75
94
|
};
|
package/esm/utils/index.d.ts
CHANGED
|
@@ -5,3 +5,4 @@ export declare const hashStoreKey: (obj?: any) => string;
|
|
|
5
5
|
export declare const getValueOrComputedValue: <T, P extends any[]>(valueOrComputeValueFn: T | ((...params: P) => T), ...params: P) => T;
|
|
6
6
|
export type Maybe<T> = T | null | undefined;
|
|
7
7
|
export declare const isClient: boolean;
|
|
8
|
+
export declare const createError: (message: string, props: Record<string, any>) => Error & Record<string, any>;
|
package/esm/utils/index.js
CHANGED
|
@@ -9,3 +9,7 @@ export const getValueOrComputedValue = (valueOrComputeValueFn, ...params) => {
|
|
|
9
9
|
return valueOrComputeValueFn;
|
|
10
10
|
};
|
|
11
11
|
export const isClient = typeof window !== 'undefined' && !('Deno' in window);
|
|
12
|
+
export const createError = (message, props) => {
|
|
13
|
+
const error = Object.assign(new Error(message), props);
|
|
14
|
+
return error;
|
|
15
|
+
};
|
package/lib/utils/fetcher.d.ts
CHANGED
package/lib/utils/fetcher.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fetcher = void 0;
|
|
4
|
-
/* eslint-disable no-throw-literal */
|
|
5
4
|
const _1 = require(".");
|
|
6
5
|
const encodeParams = (params) => Object.entries(params)
|
|
6
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
7
7
|
.map((kv) => kv.map(encodeURIComponent).join('='))
|
|
8
8
|
.join('&');
|
|
9
9
|
/**
|
|
@@ -11,6 +11,8 @@ const encodeParams = (params) => Object.entries(params)
|
|
|
11
11
|
*
|
|
12
12
|
* Can be used for REST or GraphQL.
|
|
13
13
|
*
|
|
14
|
+
* Only work for JSON response only.
|
|
15
|
+
*
|
|
14
16
|
* @see https://floppy-disk.vercel.app/docs/experimental
|
|
15
17
|
*
|
|
16
18
|
* @returns A function to fetch data
|
|
@@ -50,7 +52,12 @@ const fetcher = (options) => async (...args) => {
|
|
|
50
52
|
let resJson = await res.json();
|
|
51
53
|
if (query) {
|
|
52
54
|
if (resJson.errors) {
|
|
53
|
-
throw
|
|
55
|
+
throw (0, _1.createError)('Error GraphQL response', {
|
|
56
|
+
status: res.status,
|
|
57
|
+
statusText: res.statusText,
|
|
58
|
+
response: resJson.errors,
|
|
59
|
+
request: interceptedOptions,
|
|
60
|
+
});
|
|
54
61
|
}
|
|
55
62
|
resJson = resJson.data;
|
|
56
63
|
}
|
|
@@ -61,19 +68,31 @@ const fetcher = (options) => async (...args) => {
|
|
|
61
68
|
return finalResponse;
|
|
62
69
|
}
|
|
63
70
|
catch (error) {
|
|
64
|
-
throw {
|
|
71
|
+
throw (0, _1.createError)('Error intercept response', {
|
|
65
72
|
status: res.status,
|
|
66
73
|
statusText: res.statusText,
|
|
67
74
|
response: resJson,
|
|
68
75
|
error,
|
|
69
|
-
|
|
76
|
+
request: interceptedOptions,
|
|
77
|
+
});
|
|
70
78
|
}
|
|
71
79
|
}
|
|
72
80
|
return resJson;
|
|
73
81
|
}
|
|
74
|
-
throw
|
|
82
|
+
throw (0, _1.createError)('Fetch error', {
|
|
83
|
+
status: res.status,
|
|
84
|
+
statusText: res.statusText,
|
|
85
|
+
response: resJson,
|
|
86
|
+
request: interceptedOptions,
|
|
87
|
+
});
|
|
75
88
|
}
|
|
76
89
|
const resText = await res.text().catch(() => undefined);
|
|
77
|
-
throw
|
|
90
|
+
throw (0, _1.createError)('Response type is not JSON', {
|
|
91
|
+
status: res.status,
|
|
92
|
+
statusText: res.statusText,
|
|
93
|
+
response: resText,
|
|
94
|
+
contentType,
|
|
95
|
+
request: interceptedOptions,
|
|
96
|
+
});
|
|
78
97
|
};
|
|
79
98
|
exports.fetcher = fetcher;
|
package/lib/utils/index.d.ts
CHANGED
|
@@ -5,3 +5,4 @@ export declare const hashStoreKey: (obj?: any) => string;
|
|
|
5
5
|
export declare const getValueOrComputedValue: <T, P extends any[]>(valueOrComputeValueFn: T | ((...params: P) => T), ...params: P) => T;
|
|
6
6
|
export type Maybe<T> = T | null | undefined;
|
|
7
7
|
export declare const isClient: boolean;
|
|
8
|
+
export declare const createError: (message: string, props: Record<string, any>) => Error & Record<string, any>;
|
package/lib/utils/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isClient = exports.getValueOrComputedValue = exports.hashStoreKey = exports.hasValue = exports.identityFn = exports.noop = void 0;
|
|
3
|
+
exports.createError = exports.isClient = exports.getValueOrComputedValue = exports.hashStoreKey = exports.hasValue = exports.identityFn = exports.noop = void 0;
|
|
4
4
|
const noop = () => { };
|
|
5
5
|
exports.noop = noop;
|
|
6
6
|
const identityFn = (value) => value;
|
|
@@ -17,3 +17,8 @@ const getValueOrComputedValue = (valueOrComputeValueFn, ...params) => {
|
|
|
17
17
|
};
|
|
18
18
|
exports.getValueOrComputedValue = getValueOrComputedValue;
|
|
19
19
|
exports.isClient = typeof window !== 'undefined' && !('Deno' in window);
|
|
20
|
+
const createError = (message, props) => {
|
|
21
|
+
const error = Object.assign(new Error(message), props);
|
|
22
|
+
return error;
|
|
23
|
+
};
|
|
24
|
+
exports.createError = createError;
|