@thepassle/app-tools 0.0.2 → 0.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 +1 -0
- package/api/index.js +7 -19
- package/package.json +2 -1
package/README.md
CHANGED
package/api/index.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
function getCookie(name, _document = document) {
|
|
2
|
-
const match = _document.cookie.match(new RegExp(`(^|;\\s*)(${name})=([^;]*)`));
|
|
3
|
-
return match ? decodeURIComponent(match[3]) : null;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
1
|
function handleStatus(response) {
|
|
7
2
|
if (!response.ok) {
|
|
8
3
|
throw new Error(response.statusText);
|
|
@@ -12,8 +7,6 @@ function handleStatus(response) {
|
|
|
12
7
|
|
|
13
8
|
/**
|
|
14
9
|
* @typedef {object} Config
|
|
15
|
-
* @property {string} [xsrfHeaderName=X-CSRF-TOKEN]
|
|
16
|
-
* @property {string} [xsrfCookieName=XSRF-TOKEN]
|
|
17
10
|
* @property {Plugin[]} [plugins=[]]
|
|
18
11
|
* @property {'text'|'json'|'stream'|'blob'|'arrayBuffer'|'formData'|'stream'} [responseType=json]
|
|
19
12
|
* @property {string} [baseURL]
|
|
@@ -44,16 +37,16 @@ function handleStatus(response) {
|
|
|
44
37
|
* baseURL: string,
|
|
45
38
|
* url: string,
|
|
46
39
|
* method: Method,
|
|
40
|
+
* headers: Headers,
|
|
47
41
|
* opts?: RequestOptions,
|
|
48
42
|
* data?: any,
|
|
49
|
-
* fetchFn
|
|
43
|
+
* fetchFn: typeof globalThis.fetch
|
|
50
44
|
* }} MetaParams
|
|
51
45
|
*/
|
|
52
46
|
|
|
53
47
|
/**
|
|
54
48
|
* @example
|
|
55
49
|
* const api = new Api({
|
|
56
|
-
* xsrfCookieName: 'XSRF-COOKIE',
|
|
57
50
|
* baseURL: 'https://api.foo.com/',
|
|
58
51
|
* responseType: 'text',
|
|
59
52
|
* plugins: [
|
|
@@ -68,7 +61,6 @@ export class Api {
|
|
|
68
61
|
/** @param {Config} config */
|
|
69
62
|
constructor(config = {}) {
|
|
70
63
|
this.config = {
|
|
71
|
-
xsrfCookieName: 'XSRF-TOKEN',
|
|
72
64
|
plugins: [],
|
|
73
65
|
responseType: 'json',
|
|
74
66
|
...config
|
|
@@ -89,16 +81,12 @@ export class Api {
|
|
|
89
81
|
*/
|
|
90
82
|
async fetch(url, method, opts, data) {
|
|
91
83
|
const plugins = [...this.config.plugins, ...(opts?.plugins || [])];
|
|
92
|
-
const csrfToken = getCookie(this.config.xsrfCookieName);
|
|
93
|
-
const xsrfHeaderName = this.config.xsrfHeaderName ?? 'X-CSRF-TOKEN';
|
|
94
84
|
|
|
95
|
-
let fetchFn =
|
|
85
|
+
let fetchFn = globalThis.fetch;
|
|
96
86
|
let baseURL = opts?.baseURL ?? this.config?.baseURL ?? '';
|
|
97
87
|
let responseType = opts?.responseType ?? this.config.responseType;
|
|
98
|
-
|
|
99
|
-
const headers = new Headers({
|
|
88
|
+
let headers = new Headers({
|
|
100
89
|
'Content-Type': 'application/json',
|
|
101
|
-
...(csrfToken ? { [xsrfHeaderName]: csrfToken } : {}),
|
|
102
90
|
...opts?.headers
|
|
103
91
|
});
|
|
104
92
|
|
|
@@ -111,9 +99,9 @@ export class Api {
|
|
|
111
99
|
}
|
|
112
100
|
|
|
113
101
|
for(const { beforeFetch } of plugins) {
|
|
114
|
-
const overrides = await beforeFetch?.({ responseType, fetchFn, baseURL, url, method, opts, data });
|
|
102
|
+
const overrides = await beforeFetch?.({ responseType, headers, fetchFn, baseURL, url, method, opts, data });
|
|
115
103
|
if(overrides) {
|
|
116
|
-
({ responseType, fetchFn, baseURL, url, method, opts, data } = {...overrides});
|
|
104
|
+
({ responseType, headers, fetchFn, baseURL, url, method, opts, data } = {...overrides});
|
|
117
105
|
}
|
|
118
106
|
}
|
|
119
107
|
|
|
@@ -173,4 +161,4 @@ export class Api {
|
|
|
173
161
|
patch = (url, data, opts) => this.fetch(url, 'PATCH', opts, data);
|
|
174
162
|
}
|
|
175
163
|
|
|
176
|
-
export const api = new Api();
|
|
164
|
+
export const api = new Api();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thepassle/app-tools",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"exports": {
|
|
12
12
|
"./state.js": "./state.js",
|
|
13
|
+
"./pwa.js": "./pwa.js",
|
|
13
14
|
"./api.js": "./api.js",
|
|
14
15
|
"./api/plugins/abort.js": "./api/plugins/abort.js",
|
|
15
16
|
"./api/plugins/cache.js": "./api/plugins/cache.js",
|