This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
/
requests.js
131 lines (120 loc) · 3.9 KB
/
requests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/***
* Copyright (c) 2016 - 2018 Alex Grant (@localnerve), LocalNerve LLC
* Copyrights licensed under the BSD License. See the accompanying LICENSE file
* for terms.
*
* Module to contain request handling operations.
*/
/* global Request, Blob */
import {
getCSRFTokenFromContext,
CSRFTokenPropertyName,
replaceCSRFTokenInRequestBody
} from './api';
/**
* Adds or replaces a query string parameter to the given url.
*
* @param {String} url - The url to operate on.
* @param {String} name - The name of the search parameter.
* @param {String} value - The value of the search parameter.
* @return {String} The updated url with the new name, value search parameter.
*/
export function addOrReplaceUrlSearchParameter(url, name, value) {
let search, newUrl;
const reName = new RegExp('('+name+'=)([^&]+)');
// replace/add the name/value parameter in the url.
if (reName.test(url)) {
newUrl = url.replace(reName, '$1'+value);
} else {
search = url.split('?');
url = url.replace(/\?$/, '');
newUrl = url + ( search[1] ? '&' : '?' );
newUrl += [name, value].join('=');
}
return newUrl;
}
/**
* strip search!
*
* Removes the search/query portion from a URL.
* E.g. stripSearchParameters("https://example.com/index.html?a=b&c=d")
* ➔ "https://example.com/index.html"
*
* @param {String} url - the url of a request.
* @return {String} A url with the entire query string removed.
*/
export function stripSearchParameters(url) {
return url.replace(/\?(?:.*)$/, '');
}
/**
* Add or update a cache busting parameter to the given URL.
*
* @param {String} url - The url to add or update the cache busting parameter.
* @return {String} A url with the cache busting parameter added or updated.
*/
export function cacheBustRequest (url) {
return addOrReplaceUrlSearchParameter(url, 'sw-cache', Date.now());
}
/**
* Utility method to dehydrate a Request to a plain object for IDBObjectStore.
*
* @private
*
* @param {Request} request - The input Request object.
* @param {String} bodyType - One of the Request Body methods: arrayBuffer,
* blob, json, text, or formData
* @return {Promise} Promise resolves to dehydrated request state.
*/
export function dehydrateRequest (request, bodyType) {
const bodyGetter = request[bodyType];
return bodyGetter.call(request).then(function (body) {
return {
method: request.method,
url: request.url,
bodyType: bodyType,
body: body
};
});
}
/**
* Utility method to rehydrate a Request from a plain object from IDBObjectStore.
*
* @private
*
* @param {Object} state - The dehydrated request state.
* @param {String} state.url - The request url.
* @param {String} state.method - The request method.
* @param {String} state.bodyType - Must be 'json'.
* @param {Object} state.body - The request body.
* @param {Object} [state.body.context] - The request body context.
* @param {Object} [apiInfo] - contains timeout, cors, and CSRF token to use.
* If specified, any existing csrf value in the request state url is replaced,
* or a new key/value is added to the end of the url.
* @param {Object} [apiInfo.xhrContext] - contains csrfToken.
* @param {String} [apiInfo.xhrContext._csrf] - The csrfToken.
* @return {Request} A new Request object from state.
*/
export function rehydrateRequest (state, apiInfo) {
let body, url;
const csrfToken = getCSRFTokenFromContext(apiInfo.xhrContext);
// If csrfToken specified, add/replace one in the body and url
if (csrfToken) {
url = addOrReplaceUrlSearchParameter(
state.url,
CSRFTokenPropertyName,
csrfToken
);
replaceCSRFTokenInRequestBody(state.body, csrfToken);
}
// Only supporting json bodyType for this app.
if (state.bodyType === 'json') {
body = new Blob([JSON.stringify(state.body)], {
type: 'application/json'
});
}
return new Request (url, {
method: state.method,
body: body,
credentials: 'include'
});
}