forked from openedx/frontend-app-learner-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
60 lines (53 loc) · 1.9 KB
/
utils.js
File metadata and controls
60 lines (53 loc) · 1.9 KB
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
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
/**
* stringify(query, existingQuery)
* simple wrapper to convert an object to a query string
* @param {object} query - object to convert
* @param {string} existingQuery - existing query string
* @returns {string} - query string
*/
export const stringify = (query, existingQuery = '') => {
const searchParams = new URLSearchParams(existingQuery);
Object.entries(query).forEach(([key, value]) => {
if (value === undefined || value === null || value === '') {
searchParams.delete(key);
} else if (Array.isArray(value)) {
searchParams.delete(key);
value.forEach((val) => {
if (val !== undefined && val !== null && val !== '') {
searchParams.append(key, val);
}
});
} else {
searchParams.set(key, value);
}
});
return searchParams.toString();
};
/**
* get(url)
* simple wrapper providing an authenticated Http client get action
* @param {string} url - target url
*/
export const get = (...args) => getAuthenticatedHttpClient().get(...args);
/**
* post(url, data)
* simple wrapper providing an authenticated Http client post action
* stringify is used to convert the object to query string with = and &
* @param {string} url - target url
* @param {object|string} body - post payload
*/
export const post = (url, body = {}) => getAuthenticatedHttpClient().post(url, stringify(body));
export const client = getAuthenticatedHttpClient;
/**
* stringifyUrl(url, query)
* simple wrapper to convert a url and query object to a full url
* @param {string} url - base url string
* @param {object} query - query parameters
* @returns {string} - full url
*/
export const stringifyUrl = (url, query) => {
const [baseUrl, existingQuery = ''] = url.split('?');
const queryString = stringify(query, existingQuery);
return queryString ? `${baseUrl}?${queryString}` : baseUrl;
};