-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathutil.js
57 lines (49 loc) · 1.49 KB
/
util.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
/**
* @desc Utility functions for the API Wrapper
*/
import fetch from 'node-fetch'; // Ensure this is installed: `npm install node-fetch`
import { Buffer } from 'buffer'; // Built-in node.js Buffer
// Replace `querystring` with URLSearchParams which is natively supported in modern Node.js
export const encode = (apiKey) => {
// Encoder for API key
const buff = Buffer.from(apiKey);
const base64data = buff.toString('base64');
return base64data;
};
export const authenticate = async (api) => {
// Authentication checker, returns a boolean
const url = `${api.baseUrl}/auth/test`;
try {
const res = await fetch(url, {
method: 'GET',
headers: api.headers,
});
return res.ok;
} catch (error) {
return false;
}
};
export const isBase64Encoded = (str) => {
const result = str.length === 24 && /^[a-zA-Z\d*~]{24}/.test(str);
return result;
};
export const replaceWithId = (url, id) => {
const path = url.replace(/:[a-z]*Id/, id);
return path;
};
export const replaceWithEndpointAndParam = (url, endpoint, id) => {
let path = url;
if (['workers', 'teams', 'organizations'].includes(endpoint)) {
path = url.replace(/\/:param/, '');
}
path = path.replace(/:[a-z]*Id$/, `${endpoint}/${id}`);
return path;
};
export const appendQueryParameters = (url, queryObj) => {
const params = new URLSearchParams(queryObj);
const path = `${url}?${params.toString()}`;
return path;
};
export const isQueryParam = (obj) => {
return typeof obj === 'object';
};