@fleetbase/ember-core 0.0.2 → 0.0.4
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.
- package/addon/adapters/application.js +4 -0
- package/addon/exports/host-services.js +1 -0
- package/addon/exports/services.js +1 -0
- package/addon/services/fetch.js +8 -2
- package/addon/services/socket.js +55 -0
- package/addon/utils/to-boolean.js +19 -0
- package/app/services/socket.js +1 -0
- package/app/utils/to-boolean.js +1 -0
- package/package.json +1 -1
|
@@ -9,6 +9,10 @@ import { pluralize } from 'ember-inflector';
|
|
|
9
9
|
import getUserOptions from '../utils/get-user-options';
|
|
10
10
|
import config from 'ember-get-config';
|
|
11
11
|
|
|
12
|
+
if (isBlank(config.API.host)) {
|
|
13
|
+
config.API.host = `${window.location.protocol}//${window.location.hostname}:8000`;
|
|
14
|
+
}
|
|
15
|
+
|
|
12
16
|
export default class ApplicationAdapter extends RESTAdapter {
|
|
13
17
|
/**
|
|
14
18
|
* Inject the `session` service
|
package/addon/services/fetch.js
CHANGED
|
@@ -17,6 +17,10 @@ import download from '../utils/download';
|
|
|
17
17
|
import getUserOptions from '../utils/get-user-options';
|
|
18
18
|
import fetch from 'fetch';
|
|
19
19
|
|
|
20
|
+
if (isBlank(config.API.host)) {
|
|
21
|
+
config.API.host = `${window.location.protocol}//${window.location.hostname}:8000`;
|
|
22
|
+
}
|
|
23
|
+
|
|
20
24
|
export default class FetchService extends Service {
|
|
21
25
|
/**
|
|
22
26
|
* Creates an instance of FetchService.
|
|
@@ -253,9 +257,12 @@ export default class FetchService extends Service {
|
|
|
253
257
|
*/
|
|
254
258
|
request(path, method = 'GET', data = {}, options = {}) {
|
|
255
259
|
const headers = assign(this.getHeaders(), options.headers ?? {});
|
|
260
|
+
const host = options.host ?? this.host;
|
|
261
|
+
const namespace = options.namespace ?? this.namespace;
|
|
262
|
+
const url = options.externalRequest === true ? path : [host, namespace, path].filter(Boolean).join('/');
|
|
256
263
|
|
|
257
264
|
return new Promise((resolve, reject) => {
|
|
258
|
-
return fetch(
|
|
265
|
+
return fetch(url, {
|
|
259
266
|
method,
|
|
260
267
|
mode: options.mode || 'cors',
|
|
261
268
|
credentials: options.credentials || this.credentials,
|
|
@@ -264,7 +271,6 @@ export default class FetchService extends Service {
|
|
|
264
271
|
})
|
|
265
272
|
.then(this.parseJSON)
|
|
266
273
|
.then((response) => {
|
|
267
|
-
// console.log('[fetch:response]', response);
|
|
268
274
|
if (response.ok) {
|
|
269
275
|
if (options.normalizeToEmberData) {
|
|
270
276
|
const normalized = this.normalizeModel(response.json, options.normalizeModelType);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import Service from '@ember/service';
|
|
2
|
+
import { tracked } from '@glimmer/tracking';
|
|
3
|
+
import { isBlank } from '@ember/utils';
|
|
4
|
+
import toBoolean from '../utils/to-boolean';
|
|
5
|
+
import config from 'ember-get-config';
|
|
6
|
+
|
|
7
|
+
export default class SocketService extends Service {
|
|
8
|
+
@tracked channels = [];
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
super(...arguments);
|
|
12
|
+
this.socket = this.createSocketClusterClient();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
instance() {
|
|
16
|
+
return this.socket;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
createSocketClusterClient() {
|
|
20
|
+
const socketConfig = { ...config.socket };
|
|
21
|
+
|
|
22
|
+
if (isBlank(socketConfig.hostname)) {
|
|
23
|
+
socketConfig.hostname = window.location.hostname;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
socketConfig.secure = toBoolean(socketConfig.secure);
|
|
27
|
+
|
|
28
|
+
return socketClusterClient.create(socketConfig);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async listen(channelId, callback) {
|
|
32
|
+
const channel = this.socket.subscribe(channelId);
|
|
33
|
+
|
|
34
|
+
// track channel
|
|
35
|
+
this.channels.pushObject(channel);
|
|
36
|
+
|
|
37
|
+
// listen to channel for events
|
|
38
|
+
await channel.listener('subscribe').once();
|
|
39
|
+
|
|
40
|
+
// get incoming data and console out
|
|
41
|
+
for await (let output of channel) {
|
|
42
|
+
if (typeof callback === 'function') {
|
|
43
|
+
callback(output);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
closeChannels() {
|
|
49
|
+
for (let i = 0; i < this.channels.length; i++) {
|
|
50
|
+
const channel = this.channels.objectAt(i);
|
|
51
|
+
|
|
52
|
+
channel.close();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default function toBoolean(value) {
|
|
2
|
+
switch (value) {
|
|
3
|
+
case 'true':
|
|
4
|
+
case '1':
|
|
5
|
+
case 1:
|
|
6
|
+
case true:
|
|
7
|
+
return true;
|
|
8
|
+
case 'false':
|
|
9
|
+
case '0':
|
|
10
|
+
case 0:
|
|
11
|
+
case false:
|
|
12
|
+
case null:
|
|
13
|
+
case undefined:
|
|
14
|
+
case '':
|
|
15
|
+
return false;
|
|
16
|
+
default:
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from '@fleetbase/ember-core/services/socket';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from '@fleetbase/ember-core/utils/to-boolean';
|
package/package.json
CHANGED