@hkdigital/lib-sveltekit 0.1.67 → 0.1.68
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.
@@ -20,6 +20,8 @@
|
|
20
20
|
*
|
21
21
|
* e.g. options.headers = { "content-type": "application/json" }
|
22
22
|
*
|
23
|
+
* @param {boolean} [_.withCredentials=false]
|
24
|
+
*
|
23
25
|
* @param {requestHandler} [_.requestHandler]
|
24
26
|
*
|
25
27
|
* @param {number} [_.timeoutMs]
|
@@ -28,10 +30,11 @@
|
|
28
30
|
*
|
29
31
|
* @returns {Promise<Response>} responsePromise
|
30
32
|
*/
|
31
|
-
export function httpGet({ url, urlSearchParams, headers, requestHandler, timeoutMs }: {
|
33
|
+
export function httpGet({ url, urlSearchParams, headers, withCredentials, requestHandler, timeoutMs }: {
|
32
34
|
url: string | URL;
|
33
35
|
urlSearchParams?: object;
|
34
36
|
headers?: object;
|
37
|
+
withCredentials?: boolean;
|
35
38
|
requestHandler?: requestHandler;
|
36
39
|
timeoutMs?: number;
|
37
40
|
}): Promise<Response>;
|
@@ -49,6 +52,8 @@ export function httpGet({ url, urlSearchParams, headers, requestHandler, timeout
|
|
49
52
|
*
|
50
53
|
* e.g. options.headers = { "content-type": "application/json" }
|
51
54
|
*
|
55
|
+
* @param {boolean} [_.withCredentials=false]
|
56
|
+
*
|
52
57
|
* @param {requestHandler} [_.requestHandler]
|
53
58
|
*
|
54
59
|
* @param {number} [_.timeoutMs]
|
@@ -57,10 +62,11 @@ export function httpGet({ url, urlSearchParams, headers, requestHandler, timeout
|
|
57
62
|
*
|
58
63
|
* @returns {Promise<Response>} responsePromise
|
59
64
|
*/
|
60
|
-
export function httpPost({ url, body, headers, requestHandler, timeoutMs }: {
|
65
|
+
export function httpPost({ url, body, headers, withCredentials, requestHandler, timeoutMs }: {
|
61
66
|
url: string | URL;
|
62
67
|
body?: any;
|
63
68
|
headers?: object;
|
69
|
+
withCredentials?: boolean;
|
64
70
|
requestHandler?: requestHandler;
|
65
71
|
timeoutMs?: number;
|
66
72
|
}): Promise<Response>;
|
@@ -83,6 +89,11 @@ export function httpPost({ url, body, headers, requestHandler, timeoutMs }: {
|
|
83
89
|
* Object that contains custom headers. A header is a name, value pair.
|
84
90
|
*
|
85
91
|
* e.g. options.headers = { "content-type": "application/json" }
|
92
|
+
*
|
93
|
+
* @param {boolean} [_.withCredentials=false]
|
94
|
+
* Whether to include credentials (cookies, HTTP authentication, and client
|
95
|
+
* SSL certificates) with cross-origin requests. When true, sets fetch
|
96
|
+
* credentials to 'include', otherwise to 'omit'.
|
86
97
|
*
|
87
98
|
* @param {requestHandler} [_.requestHandler]
|
88
99
|
*
|
@@ -97,12 +108,13 @@ export function httpPost({ url, body, headers, requestHandler, timeoutMs }: {
|
|
97
108
|
*
|
98
109
|
* @returns {Promise<Response>} responsePromise
|
99
110
|
*/
|
100
|
-
export function httpRequest({ method, url, urlSearchParams, body, headers, requestHandler, timeoutMs }: {
|
111
|
+
export function httpRequest({ method, url, urlSearchParams, body, headers, withCredentials, requestHandler, timeoutMs }: {
|
101
112
|
url: string | URL;
|
102
113
|
method: string;
|
103
114
|
urlSearchParams?: object;
|
104
115
|
body?: any;
|
105
116
|
headers?: object;
|
117
|
+
withCredentials?: boolean;
|
106
118
|
requestHandler?: requestHandler;
|
107
119
|
timeoutMs?: number;
|
108
120
|
}): Promise<Response>;
|
@@ -34,6 +34,8 @@ import { waitForAndCheckResponse } from './response.js';
|
|
34
34
|
*
|
35
35
|
* e.g. options.headers = { "content-type": "application/json" }
|
36
36
|
*
|
37
|
+
* @param {boolean} [_.withCredentials=false]
|
38
|
+
*
|
37
39
|
* @param {requestHandler} [_.requestHandler]
|
38
40
|
*
|
39
41
|
* @param {number} [_.timeoutMs]
|
@@ -46,6 +48,7 @@ export async function httpGet({
|
|
46
48
|
url,
|
47
49
|
urlSearchParams,
|
48
50
|
headers,
|
51
|
+
withCredentials,
|
49
52
|
requestHandler,
|
50
53
|
timeoutMs
|
51
54
|
}) {
|
@@ -75,6 +78,8 @@ export async function httpGet({
|
|
75
78
|
*
|
76
79
|
* e.g. options.headers = { "content-type": "application/json" }
|
77
80
|
*
|
81
|
+
* @param {boolean} [_.withCredentials=false]
|
82
|
+
*
|
78
83
|
* @param {requestHandler} [_.requestHandler]
|
79
84
|
*
|
80
85
|
* @param {number} [_.timeoutMs]
|
@@ -87,6 +92,7 @@ export async function httpPost({
|
|
87
92
|
url,
|
88
93
|
body = null,
|
89
94
|
headers,
|
95
|
+
withCredentials,
|
90
96
|
requestHandler,
|
91
97
|
timeoutMs
|
92
98
|
}) {
|
@@ -95,6 +101,7 @@ export async function httpPost({
|
|
95
101
|
url,
|
96
102
|
body,
|
97
103
|
headers,
|
104
|
+
withCredentials,
|
98
105
|
requestHandler,
|
99
106
|
timeoutMs
|
100
107
|
});
|
@@ -123,6 +130,11 @@ export async function httpPost({
|
|
123
130
|
* Object that contains custom headers. A header is a name, value pair.
|
124
131
|
*
|
125
132
|
* e.g. options.headers = { "content-type": "application/json" }
|
133
|
+
*
|
134
|
+
* @param {boolean} [_.withCredentials=false]
|
135
|
+
* Whether to include credentials (cookies, HTTP authentication, and client
|
136
|
+
* SSL certificates) with cross-origin requests. When true, sets fetch
|
137
|
+
* credentials to 'include', otherwise to 'omit'.
|
126
138
|
*
|
127
139
|
* @param {requestHandler} [_.requestHandler]
|
128
140
|
*
|
@@ -143,6 +155,7 @@ export async function httpRequest({
|
|
143
155
|
urlSearchParams = null,
|
144
156
|
body = null,
|
145
157
|
headers,
|
158
|
+
withCredentials,
|
146
159
|
requestHandler,
|
147
160
|
timeoutMs
|
148
161
|
}) {
|
@@ -171,7 +184,7 @@ export async function httpRequest({
|
|
171
184
|
const init = {
|
172
185
|
mode: 'cors',
|
173
186
|
cache: 'no-cache',
|
174
|
-
credentials: 'omit',
|
187
|
+
credentials: withCredentials ? 'include': 'omit',
|
175
188
|
redirect: 'follow',
|
176
189
|
referrerPolicy: 'no-referrer',
|
177
190
|
headers: requestHeaders
|
@@ -13,15 +13,18 @@
|
|
13
13
|
* the header name and the header value.
|
14
14
|
* E.g. [ "content-type", "application/json" ]
|
15
15
|
*
|
16
|
+
* @param {boolean} [_.withCredentials=false]
|
17
|
+
*
|
16
18
|
* @throws ResponseError
|
17
19
|
* If a network error occurred or the response was not ok
|
18
20
|
*
|
19
21
|
* @returns {Promise<any>} parsed JSON data
|
20
22
|
*/
|
21
|
-
export function jsonGet({ url, urlSearchParams, headers }: {
|
23
|
+
export function jsonGet({ url, urlSearchParams, headers, withCredentials }: {
|
22
24
|
url: string | URL;
|
23
25
|
urlSearchParams?: object;
|
24
26
|
headers?: [string, string][];
|
27
|
+
withCredentials?: boolean;
|
25
28
|
}): Promise<any>;
|
26
29
|
/**
|
27
30
|
* Make a POST request to fetch JSON encoded data
|
@@ -45,14 +48,17 @@ export function jsonGet({ url, urlSearchParams, headers }: {
|
|
45
48
|
* the header name and the header value.
|
46
49
|
* E.g. [ "content-type", "application/json" ]
|
47
50
|
*
|
51
|
+
* @param {boolean} [_.withCredentials=false]
|
52
|
+
*
|
48
53
|
* @throws ResponseError
|
49
54
|
* If a network error occurred or the response was not ok
|
50
55
|
*
|
51
56
|
* @returns {Promise<any>} parsed JSON data
|
52
57
|
*/
|
53
|
-
export function jsonPost({ url, body, urlSearchParams, headers }: {
|
58
|
+
export function jsonPost({ url, body, urlSearchParams, headers, withCredentials }: {
|
54
59
|
url: string | URL;
|
55
60
|
body: any;
|
56
61
|
urlSearchParams?: object;
|
57
62
|
headers?: [string, string][];
|
63
|
+
withCredentials?: boolean;
|
58
64
|
}): Promise<any>;
|
@@ -27,12 +27,19 @@ const ACCEPT = 'accept';
|
|
27
27
|
* the header name and the header value.
|
28
28
|
* E.g. [ "content-type", "application/json" ]
|
29
29
|
*
|
30
|
+
* @param {boolean} [_.withCredentials=false]
|
31
|
+
*
|
30
32
|
* @throws ResponseError
|
31
33
|
* If a network error occurred or the response was not ok
|
32
34
|
*
|
33
35
|
* @returns {Promise<any>} parsed JSON data
|
34
36
|
*/
|
35
|
-
export async function jsonGet({
|
37
|
+
export async function jsonGet({
|
38
|
+
url,
|
39
|
+
urlSearchParams,
|
40
|
+
headers,
|
41
|
+
withCredentials
|
42
|
+
}) {
|
36
43
|
url = toURL(url);
|
37
44
|
|
38
45
|
if (!headers) {
|
@@ -45,7 +52,8 @@ export async function jsonGet({ url, urlSearchParams, headers }) {
|
|
45
52
|
method: METHOD_GET,
|
46
53
|
url,
|
47
54
|
urlSearchParams,
|
48
|
-
headers
|
55
|
+
headers,
|
56
|
+
withCredentials
|
49
57
|
});
|
50
58
|
|
51
59
|
const response = await waitForAndCheckResponse(responsePromise, url);
|
@@ -60,13 +68,18 @@ export async function jsonGet({ url, urlSearchParams, headers }) {
|
|
60
68
|
//
|
61
69
|
parsedResponse = await response.json();
|
62
70
|
} catch (e) {
|
63
|
-
throw new ResponseError(
|
64
|
-
|
65
|
-
|
71
|
+
throw new ResponseError(
|
72
|
+
`Failed to JSON decode server response from [${decodeURI(url.href)}]`,
|
73
|
+
{
|
74
|
+
cause: e
|
75
|
+
}
|
76
|
+
);
|
66
77
|
}
|
67
78
|
|
68
79
|
if (parsedResponse.error) {
|
69
|
-
throw new ResponseError(
|
80
|
+
throw new ResponseError(
|
81
|
+
`Server returned response error message [${parsedResponse.error}]`
|
82
|
+
);
|
70
83
|
}
|
71
84
|
|
72
85
|
return parsedResponse;
|
@@ -94,12 +107,20 @@ export async function jsonGet({ url, urlSearchParams, headers }) {
|
|
94
107
|
* the header name and the header value.
|
95
108
|
* E.g. [ "content-type", "application/json" ]
|
96
109
|
*
|
110
|
+
* @param {boolean} [_.withCredentials=false]
|
111
|
+
*
|
97
112
|
* @throws ResponseError
|
98
113
|
* If a network error occurred or the response was not ok
|
99
114
|
*
|
100
115
|
* @returns {Promise<any>} parsed JSON data
|
101
116
|
*/
|
102
|
-
export async function jsonPost({
|
117
|
+
export async function jsonPost({
|
118
|
+
url,
|
119
|
+
body,
|
120
|
+
urlSearchParams,
|
121
|
+
headers,
|
122
|
+
withCredentials
|
123
|
+
}) {
|
103
124
|
url = toURL(url);
|
104
125
|
|
105
126
|
if (!headers) {
|
@@ -113,7 +134,13 @@ export async function jsonPost({ url, body, urlSearchParams, headers }) {
|
|
113
134
|
headers[ACCEPT] = APPLICATION_JSON;
|
114
135
|
headers[CONTENT_TYPE] = APPLICATION_JSON;
|
115
136
|
|
116
|
-
const responsePromise = httpRequest({
|
137
|
+
const responsePromise = httpRequest({
|
138
|
+
method: METHOD_POST,
|
139
|
+
url,
|
140
|
+
body,
|
141
|
+
urlSearchParams,
|
142
|
+
headers
|
143
|
+
});
|
117
144
|
|
118
145
|
const response = await waitForAndCheckResponse(responsePromise, url);
|
119
146
|
|
@@ -128,7 +155,9 @@ export async function jsonPost({ url, body, urlSearchParams, headers }) {
|
|
128
155
|
parsedResponse = await response.json();
|
129
156
|
} catch (e) {
|
130
157
|
// console.log( response );
|
131
|
-
throw new ResponseError(
|
158
|
+
throw new ResponseError(
|
159
|
+
`Failed to JSON decode server response from [${decodeURI(url.href)}]`
|
160
|
+
);
|
132
161
|
}
|
133
162
|
|
134
163
|
if (parsedResponse.error) {
|
@@ -136,7 +165,9 @@ export async function jsonPost({ url, body, urlSearchParams, headers }) {
|
|
136
165
|
// @note this is API specific, but it's quite logical
|
137
166
|
//
|
138
167
|
//
|
139
|
-
throw new ResponseError(
|
168
|
+
throw new ResponseError(
|
169
|
+
`Server returned response error message [${parsedResponse.error}]`
|
170
|
+
);
|
140
171
|
}
|
141
172
|
|
142
173
|
return parsedResponse;
|