1 | 'use strict';
|
2 | const URL = require('url').URL;
|
3 | const utils = require('handlebars-utils');
|
4 | const common = require('./../lib/common');
|
5 | const SafeString = require('handlebars').SafeString;
|
6 |
|
7 | function helper(paper) {
|
8 | paper.handlebars.registerHelper('setURLQueryParam', function(string, key, value) {
|
9 | string = common.unwrapIfSafeString(string);
|
10 | key = common.unwrapIfSafeString(key);
|
11 | value = common.unwrapIfSafeString(value);
|
12 | if (!utils.isString(string) || !common.isValidURL(string)){
|
13 | throw new TypeError("Invalid URL passed to setURLQueryParam");
|
14 | } else if (!utils.isString(key)){
|
15 | throw new TypeError("Invalid query parameter key passed to setURLQueryParam");
|
16 | } else if(!utils.isString(value)) {
|
17 | throw new TypeError("Invalid query parameter value passed to setURLQueryParam");
|
18 | }
|
19 |
|
20 | const url = new URL(string);
|
21 |
|
22 | url.searchParams.set(encodeURIComponent(key), encodeURIComponent(value));
|
23 |
|
24 | return new SafeString(url.toString());
|
25 | });
|
26 | }
|
27 |
|
28 | module.exports = helper;
|