@thepassle/app-tools 1.0.1 → 1.1.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/api/index.js +16 -16
- package/api/plugins/abort.js +9 -9
- package/api/plugins/cache.js +4 -4
- package/api/plugins/jsonPrefix.js +12 -12
- package/api/plugins/logger.js +17 -13
- package/package.json +1 -1
- package/types/api/index.d.ts +1 -1
- package/types/api/types.d.ts +4 -1
package/api/index.js
CHANGED
|
@@ -31,7 +31,7 @@ function handleStatus(response) {
|
|
|
31
31
|
* plugins: [
|
|
32
32
|
* {
|
|
33
33
|
* beforeFetch: ({url, method, opts, data}) => {},
|
|
34
|
-
* afterFetch: (
|
|
34
|
+
* afterFetch: ({response}) => response,
|
|
35
35
|
* }
|
|
36
36
|
* ]
|
|
37
37
|
*});
|
|
@@ -131,22 +131,22 @@ export class Api {
|
|
|
131
131
|
...(opts?.signal ? { signal: opts.signal } : {}),
|
|
132
132
|
})
|
|
133
133
|
/** [PLUGINS - AFTERFETCH] */
|
|
134
|
-
.then(async (
|
|
134
|
+
.then(async (response) => {
|
|
135
135
|
for (const plugin of plugins) {
|
|
136
136
|
try {
|
|
137
|
-
const afterFetchResult =
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
137
|
+
const afterFetchResult = await plugin?.afterFetch?.({
|
|
138
|
+
responseType,
|
|
139
|
+
headers,
|
|
140
|
+
fetchFn,
|
|
141
|
+
baseURL,
|
|
142
|
+
url,
|
|
143
|
+
method,
|
|
144
|
+
opts,
|
|
145
|
+
data,
|
|
146
|
+
response,
|
|
147
|
+
});
|
|
148
148
|
if (afterFetchResult) {
|
|
149
|
-
|
|
149
|
+
response = afterFetchResult;
|
|
150
150
|
}
|
|
151
151
|
} catch (e) {
|
|
152
152
|
log(`Plugin "${plugin.name}" error on afterFetch hook`);
|
|
@@ -154,12 +154,12 @@ export class Api {
|
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
return
|
|
157
|
+
return response;
|
|
158
158
|
})
|
|
159
159
|
/** [STATUS] */
|
|
160
160
|
.then(handleStatus)
|
|
161
161
|
/** [RESPONSETYPE] */
|
|
162
|
-
.then((
|
|
162
|
+
.then((response) => response[responseType]())
|
|
163
163
|
.then(async (data) => {
|
|
164
164
|
for (const plugin of plugins) {
|
|
165
165
|
try {
|
package/api/plugins/abort.js
CHANGED
|
@@ -6,12 +6,12 @@ export function abortPlugin() {
|
|
|
6
6
|
const requests = new Map();
|
|
7
7
|
|
|
8
8
|
return {
|
|
9
|
-
name:
|
|
9
|
+
name: "abort",
|
|
10
10
|
beforeFetch: (meta) => {
|
|
11
11
|
const { method, url } = meta;
|
|
12
12
|
requestId = `${method}:${url}`;
|
|
13
13
|
|
|
14
|
-
if(requests.has(requestId)) {
|
|
14
|
+
if (requests.has(requestId)) {
|
|
15
15
|
const request = requests.get(requestId);
|
|
16
16
|
request.abort();
|
|
17
17
|
}
|
|
@@ -21,17 +21,17 @@ export function abortPlugin() {
|
|
|
21
21
|
...meta,
|
|
22
22
|
opts: {
|
|
23
23
|
...meta.opts,
|
|
24
|
-
signal: requests.get(requestId).signal
|
|
25
|
-
}
|
|
24
|
+
signal: requests.get(requestId).signal,
|
|
25
|
+
},
|
|
26
26
|
};
|
|
27
27
|
},
|
|
28
|
-
afterFetch: (
|
|
28
|
+
afterFetch: ({ response }) => {
|
|
29
29
|
requests.delete(requestId);
|
|
30
|
-
return
|
|
30
|
+
return response;
|
|
31
31
|
},
|
|
32
32
|
// return true if an error should throw, return false if an error should be ignored
|
|
33
|
-
handleError: ({name}) => name !==
|
|
34
|
-
}
|
|
33
|
+
handleError: ({ name }) => name !== "AbortError",
|
|
34
|
+
};
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
export const abort = abortPlugin();
|
|
37
|
+
export const abort = abortPlugin();
|
package/api/plugins/cache.js
CHANGED
|
@@ -44,13 +44,13 @@ export function cachePlugin({
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
},
|
|
47
|
-
afterFetch: async (
|
|
48
|
-
const requestId = `${
|
|
49
|
-
const clone =
|
|
47
|
+
afterFetch: async ({ response, method, url }) => {
|
|
48
|
+
const requestId = `${method}:${url}`;
|
|
49
|
+
const clone = response.clone();
|
|
50
50
|
const data = await clone.json();
|
|
51
51
|
cache.set(requestId, { updatedAt: Date.now(), data });
|
|
52
52
|
evict();
|
|
53
|
-
return
|
|
53
|
+
return response;
|
|
54
54
|
},
|
|
55
55
|
};
|
|
56
56
|
}
|
|
@@ -5,23 +5,23 @@
|
|
|
5
5
|
export function jsonPrefixPlugin(jsonPrefix) {
|
|
6
6
|
let responseType;
|
|
7
7
|
return {
|
|
8
|
-
name:
|
|
9
|
-
beforeFetch: ({responseType: type}) => {
|
|
8
|
+
name: "jsonPrefix",
|
|
9
|
+
beforeFetch: ({ responseType: type }) => {
|
|
10
10
|
responseType = type;
|
|
11
11
|
},
|
|
12
|
-
afterFetch: async (
|
|
13
|
-
if(jsonPrefix && responseType ===
|
|
14
|
-
let responseAsText = await
|
|
15
|
-
|
|
16
|
-
if(responseAsText.startsWith(jsonPrefix)) {
|
|
12
|
+
afterFetch: async ({ response }) => {
|
|
13
|
+
if (jsonPrefix && responseType === "json") {
|
|
14
|
+
let responseAsText = await response.text();
|
|
15
|
+
|
|
16
|
+
if (responseAsText.startsWith(jsonPrefix)) {
|
|
17
17
|
responseAsText = responseAsText.substring(jsonPrefix.length);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
return new Response(responseAsText,
|
|
20
|
+
return new Response(responseAsText, response);
|
|
21
21
|
}
|
|
22
|
-
return
|
|
23
|
-
}
|
|
24
|
-
}
|
|
22
|
+
return response;
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export const jsonPrefix = jsonPrefixPlugin(`)]}',\n`);
|
|
27
|
+
export const jsonPrefix = jsonPrefixPlugin(`)]}',\n`);
|
package/api/plugins/logger.js
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
2
|
* @param {{
|
|
3
3
|
* collapsed?: boolean
|
|
4
4
|
* }} options
|
|
5
|
-
* @returns {import('../index.js').Plugin}
|
|
5
|
+
* @returns {import('../index.js').Plugin}
|
|
6
6
|
*/
|
|
7
|
-
export function loggerPlugin({collapsed = true} = {}) {
|
|
7
|
+
export function loggerPlugin({ collapsed = true } = {}) {
|
|
8
8
|
let m;
|
|
9
9
|
let start;
|
|
10
|
-
const group = collapsed ?
|
|
10
|
+
const group = collapsed ? "groupCollapsed" : "group";
|
|
11
11
|
return {
|
|
12
|
-
name:
|
|
12
|
+
name: "logger",
|
|
13
13
|
beforeFetch: (meta) => {
|
|
14
|
-
console[group](
|
|
14
|
+
console[group](
|
|
15
|
+
`[START] [${new Date().toLocaleTimeString()}] [${meta.method}] "${meta.url}"`,
|
|
16
|
+
);
|
|
15
17
|
console.table([meta]);
|
|
16
|
-
console.groupEnd()
|
|
18
|
+
console.groupEnd();
|
|
17
19
|
start = Date.now();
|
|
18
20
|
m = meta;
|
|
19
21
|
},
|
|
20
|
-
afterFetch: (
|
|
21
|
-
console.log(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
afterFetch: ({ response }) => {
|
|
23
|
+
console.log(
|
|
24
|
+
`[END] [${m.method}] "${m.url}" Request took ${Date.now() - start}ms`,
|
|
25
|
+
);
|
|
26
|
+
return response;
|
|
27
|
+
},
|
|
28
|
+
};
|
|
25
29
|
}
|
|
26
30
|
|
|
27
|
-
export const logger = loggerPlugin();
|
|
31
|
+
export const logger = loggerPlugin();
|
package/package.json
CHANGED
package/types/api/index.d.ts
CHANGED
package/types/api/types.d.ts
CHANGED
|
@@ -9,11 +9,14 @@ export type BodylessMethod = <R>(url: string, opts?: RequestOptions) => Promise<
|
|
|
9
9
|
export type Method = 'GET' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'POST' | 'PUT' | 'PATCH';
|
|
10
10
|
export interface Plugin {
|
|
11
11
|
beforeFetch?: (meta: MetaParams) => MetaParams | Promise<MetaParams> | void;
|
|
12
|
-
afterFetch?: (
|
|
12
|
+
afterFetch?: (meta: AfterFetchParams) => void | Promise<void> | Response | Promise<Response>;
|
|
13
13
|
transform?: (data: any) => any;
|
|
14
14
|
name: string;
|
|
15
15
|
handleError?: (e: Error) => boolean;
|
|
16
16
|
}
|
|
17
|
+
export interface AfterFetchParams extends MetaParams {
|
|
18
|
+
response: Response;
|
|
19
|
+
}
|
|
17
20
|
export interface CustomRequestOptions {
|
|
18
21
|
transform?: (data: object) => object;
|
|
19
22
|
responseType?: ResponseType;
|