hostr/web/public/src/app/services.js

94 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-07-09 23:01:43 +01:00
export class FileService {
2015-08-23 22:12:32 +01:00
constructor($resource, $cacheFactory) {
const cache = $cacheFactory('files-cache');
return $resource(window.settings.apiURL + '/file/:id', {
id: '@id',
}, {
query: {
method: 'GET',
isArray: true,
cache: cache,
params: {
perpage: 0,
all: true,
},
},
delete: {
method: 'DELETE',
isArray: true,
cache: cache,
2015-07-09 23:01:43 +01:00
},
});
2015-08-23 22:12:32 +01:00
}
2015-07-09 23:01:43 +01:00
2015-08-23 22:12:32 +01:00
static factory($resource, $cacheFactory) {
2015-07-09 23:01:43 +01:00
return new FileService($resource, $cacheFactory);
}
}
export class UserService {
2015-08-23 22:12:32 +01:00
constructor($resource) {
return $resource(window.settings.apiURL + '/user');
}
2015-07-09 23:01:43 +01:00
2015-08-23 22:12:32 +01:00
static factory($resource) {
2015-07-09 23:01:43 +01:00
return new UserService($resource);
}
}
export class EventService {
2015-08-23 22:12:32 +01:00
constructor($rootScope, ReconnectingWebSocket) {
if (window.user && WebSocket) {
2018-06-02 15:50:39 +00:00
const apiURL = new URL(window.settings.apiURL);
const ws = new ReconnectingWebSocket((apiURL.protocol === 'http:' ? 'ws' : 'wss') + window.settings.apiURL.replace('https', '').replace('http', '') + '/user');
2015-08-23 22:12:32 +01:00
ws.onmessage = (msg) => {
const evt = JSON.parse(msg.data);
2015-07-09 23:01:43 +01:00
$rootScope.$broadcast(evt.type, evt.data);
};
2015-08-23 22:12:32 +01:00
ws.onopen = () => {
ws.send(JSON.stringify({
authorization: window.user.token,
}));
2015-07-09 23:01:43 +01:00
};
}
return true;
2015-08-23 22:12:32 +01:00
}
2015-07-09 23:01:43 +01:00
2015-08-23 22:12:32 +01:00
static factory($rootScope, ReconnectingWebSocket) {
return new EventService($rootScope, ReconnectingWebSocket);
}
2015-07-09 23:01:43 +01:00
}
export class TransactionService {
2015-08-23 22:12:32 +01:00
constructor($resource, $cacheFactory) {
const cache = $cacheFactory('transaction-cache');
return $resource(window.settings.apiURL + '/user/transaction/:id', {
id: '@id',
}, {
query: {
method: 'GET',
isArray: true,
cache: cache,
},
2015-07-09 23:01:43 +01:00
});
}
2015-08-23 22:12:32 +01:00
static factory($resource, $cacheFactory) {
2015-07-09 23:01:43 +01:00
return new TransactionService($resource, $cacheFactory);
}
}
export class SettingService {
2015-08-23 22:12:32 +01:00
constructor($http) {
const service = {};
service.update = (data) => {
2015-07-09 23:01:43 +01:00
return $http.post(window.settings.apiURL + '/user/settings', data);
};
return service;
}
2015-08-23 22:12:32 +01:00
static factory($http) {
2015-07-09 23:01:43 +01:00
return new SettingService($http);
}
}