forked from async-labs/saas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.note
29 lines (21 loc) · 1.17 KB
/
.note
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
makeQueryString when it is needed
Since `href` has to be URI-encoded, we convert `redirectUrlAfterLogin` string into URI-encoded string using `makeQueryString` method. We define this method in a new file `book/5-begin/app/lib/api/makeQueryString.ts`:
```
function makeQueryString(params) {
const query = Object.keys(params)
.filter((k) => !!params[k])
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
.join('&');
return query;
}
export { makeQueryString };
```
`makeQueryString` method relies on native (part of official API) JavaScript method `encodeURIComponent` that URI-encodes string:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
So if `redirectUrlAfterLogin` has value `?x=test1&y=test2` then `qs` will be `%3Fx%3Dtest1%26y%3Dtest2` and the last string is a valid format for URL:
```
console.log(makeQueryString('?x=test1&y=test2'));
// expected output: "%3Fx%3Dtest1%26y%3Dtest2"
```
We URI encode `qs` part of our API endpoint because entire string for `href` must be in valid, URI-encoded, format. You can read more on URL syntax here:
https://www.w3.org/Addressing/URL/url-spec.html