-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathbuild-url.js
More file actions
45 lines (39 loc) · 1.3 KB
/
build-url.js
File metadata and controls
45 lines (39 loc) · 1.3 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
import { transformUrl } from './build-url-transforms.js';
import { ErrorResponse, SuccessResponse } from '../types.js';
/**
* This builds the proper URL given the URL template and userData.
*
* @param action
* @param {Record<string, any>} userData
* @return {import('../types.js').SuccessResponse<{url: string}> | import('../types.js').ErrorResponse}
*/
export function buildUrl(action, userData) {
const result = replaceTemplatedUrl(action, userData);
if ('error' in result) {
return new ErrorResponse({ actionID: action.id, message: result.error });
}
return new SuccessResponse({ actionID: action.id, actionType: action.actionType, response: { url: result.url } });
}
/**
* Perform some basic validations before we continue into the templating.
*
* @param action
* @param userData
* @return {{url: string} | {error: string}}
*/
export function replaceTemplatedUrl(action, userData) {
const url = action?.url;
if (!url) {
return { error: 'Error: No url provided.' };
}
try {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _ = new URL(action.url);
} catch (e) {
return { error: 'Error: Invalid URL provided.' };
}
if (!userData) {
return { url };
}
return transformUrl(action, userData);
}