]>
Commit | Line | Data |
---|---|---|
1 | import { environment } from '../../../environments/environment' | |
2 | ||
3 | function getAbsoluteAPIUrl () { | |
4 | let absoluteAPIUrl = environment.hmr === true | |
5 | ? 'http://localhost:9000' | |
6 | : environment.apiUrl | |
7 | ||
8 | if (!absoluteAPIUrl) { | |
9 | // The API is on the same domain | |
10 | absoluteAPIUrl = window.location.origin | |
11 | } | |
12 | ||
13 | return absoluteAPIUrl | |
14 | } | |
15 | ||
16 | function getAPIHost () { | |
17 | return new URL(getAbsoluteAPIUrl()).host | |
18 | } | |
19 | ||
20 | function getAbsoluteEmbedUrl () { | |
21 | let absoluteEmbedUrl = environment.originServerUrl | |
22 | if (!absoluteEmbedUrl) { | |
23 | // The Embed is on the same domain | |
24 | absoluteEmbedUrl = window.location.origin | |
25 | } | |
26 | ||
27 | return absoluteEmbedUrl | |
28 | } | |
29 | ||
30 | // Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34 | |
31 | function objectToFormData (obj: any, form?: FormData, namespace?: string) { | |
32 | const fd = form || new FormData() | |
33 | let formKey | |
34 | ||
35 | for (const key of Object.keys(obj)) { | |
36 | if (namespace) formKey = `${namespace}[${key}]` | |
37 | else formKey = key | |
38 | ||
39 | if (obj[key] === undefined) continue | |
40 | ||
41 | if (Array.isArray(obj[key]) && obj[key].length === 0) { | |
42 | fd.append(key, null) | |
43 | continue | |
44 | } | |
45 | ||
46 | if (obj[key] !== null && typeof obj[key] === 'object' && !(obj[key] instanceof File)) { | |
47 | objectToFormData(obj[key], fd, formKey) | |
48 | } else { | |
49 | fd.append(formKey, obj[key]) | |
50 | } | |
51 | } | |
52 | ||
53 | return fd | |
54 | } | |
55 | ||
56 | export { | |
57 | getAbsoluteAPIUrl, | |
58 | getAPIHost, | |
59 | getAbsoluteEmbedUrl, | |
60 | ||
61 | objectToFormData | |
62 | } |