@fleetbase/ember-core 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,55 +2,39 @@ import config from '@fleetbase/console/config/environment';
2
2
  import { isBlank } from '@ember/utils';
3
3
 
4
4
  const isDevelopment = ['local', 'development'].includes(config.environment);
5
- const isProduction = ['production'].includes(config.environment);
6
5
 
7
- function queryString(params) {
6
+ export function queryString(params) {
8
7
  return Object.keys(params)
9
- .map((key) => `${key}=${params[key]}`)
8
+ .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
10
9
  .join('&');
11
10
  }
12
11
 
13
- function extractHostAndPort(url) {
12
+ export function extractHostAndPort(url) {
14
13
  try {
15
- const parsedUrl = new URL(url);
16
- const host = parsedUrl.hostname;
17
- const port = parsedUrl.port;
18
-
19
- return {
20
- host: host,
21
- port: port || null,
22
- };
14
+ const { hostname: host, port = null } = new URL(url);
15
+ return { host, port };
23
16
  } catch (error) {
24
- console.error('Invalid URL:', error);
25
- return null;
17
+ return { host: null, port: null };
26
18
  }
27
19
  }
28
20
 
29
- export default function consoleUrl(path = '', queryParams = {}, subdomain = 'console', host = 'fleetbase.io') {
30
- let parsedHost = extractHostAndPort(host);
31
- let url = isDevelopment ? 'http://' : 'https://';
32
-
33
- if (subdomain) {
34
- url += subdomain + '.';
35
- }
36
-
37
- let urlParams = !isBlank(queryParams) ? queryString(queryParams) : '';
38
-
39
- if (!isProduction && !isDevelopment) {
40
- url += `${config.environment}.`;
21
+ export default function consoleUrl(path = '', queryParams = {}, subdomain = null, host = null) {
22
+ if (subdomain === null || host === null) {
23
+ const { hostname, host: currentHost } = window.location;
24
+ if (subdomain === null) {
25
+ const parts = hostname.split('.');
26
+ subdomain = parts.length > 2 ? parts[0] : null;
27
+ }
28
+ if (host === null) {
29
+ host = currentHost;
30
+ }
41
31
  }
42
32
 
43
- url += parsedHost.host;
44
-
45
- if (parsedHost.port) {
46
- url += `:${parsedHost.port}`;
47
- }
48
-
49
- url += `/${path}`;
50
-
51
- if (urlParams) {
52
- url += `?${urlParams}`;
53
- }
33
+ const { host: parsedHost, port } = extractHostAndPort(host);
34
+ const protocol = isDevelopment ? 'http://' : 'https://';
35
+ const urlParams = !isBlank(queryParams) ? queryString(queryParams) : '';
36
+ const portSegment = port ? `:${port}` : '';
37
+ const pathSegment = path.startsWith('/') ? path : `/${path}`;
54
38
 
55
- return url;
39
+ return `${protocol}${subdomain ? subdomain + '.' : ''}${parsedHost}${portSegment}${pathSegment}${urlParams ? '?' + urlParams : ''}`;
56
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fleetbase/ember-core",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
5
5
  "keywords": [
6
6
  "fleetbase-core",