Skip to content

Commit

Permalink
Merge pull request #4 from Mavtak/fetch
Browse files Browse the repository at this point in the history
use window.fetch for API calls
  • Loading branch information
Mavtak committed Jan 13, 2018
2 parents 6714f7a + c57d9a3 commit 996b1b2
Show file tree
Hide file tree
Showing 26 changed files with 194 additions and 109 deletions.
@@ -1,6 +1,6 @@
function ComputerDetailController(
$http,
$state,
api,
pageMenuItems,
signInState,
wholePageStatus
Expand All @@ -11,14 +11,15 @@ function ComputerDetailController(
wholePageStatus.set('loading');
pageMenuItems.reset();

$http.post('/api/computer', {
api({
repository: 'computer',
action: 'read',
parameters: {
id: +$state.params.id
}
})
.then(function (result) {
controller.computer = result.data.data;
controller.computer = result.data;

signInState.set('signed-in');
wholePageStatus.set('ready');
Expand Down
@@ -1,5 +1,5 @@
function ComputerListController(
$http,
api,
pageMenuItems,
signInState,
wholePageStatus
Expand All @@ -10,11 +10,12 @@ function ComputerListController(
wholePageStatus.set('loading');
pageMenuItems.reset();

$http.post('/api/computer', {
api({
repository: 'computer',
action: 'list'
})
.then(function (result) {
controller.computers = result.data.data;
controller.computers = result.data;

signInState.set('signed-in');
wholePageStatus.set('ready');
Expand Down
@@ -1,7 +1,7 @@
import template from './template.html';

function addComputerWidget(
$http
api
) {

return {
Expand All @@ -16,7 +16,8 @@ function addComputerWidget(
scope.model = {};

function add() {
$http.post('/api/computer', {
api({
repository: 'computer',
action: 'create',
parameters: scope.model,
});
Expand Down
@@ -1,7 +1,7 @@
import template from './template.html';

function runScriptControls(
$http
api
) {

return {
Expand All @@ -26,7 +26,8 @@ function runScriptControls(
});

function runScript() {
$http.post('/api/computer', {
api({
repository: 'computer',
action: 'runScript',
parameters: Object.assign({}, {
id: scope.computer.id
Expand Down
@@ -1,9 +1,9 @@
import template from './template.html';

function webHookControls(
$http,
$state,
$window
$window,
api
) {

return {
Expand Down Expand Up @@ -32,7 +32,8 @@ function webHookControls(
}

function disable() {
$http.post('/api/computer', {
api({
repository: 'computer',
action: 'disableWebHook',
parameters: {
id: scope.computer.id,
Expand All @@ -44,7 +45,8 @@ function webHookControls(
}

function renew() {
$http.post('/api/computer', {
api({
repository: 'computer',
action: 'renewWebHookKeys',
parameters: {
id: scope.computer.id,
Expand Down
11 changes: 6 additions & 5 deletions Roomie.Web.Frontend/src/angular/data/ManualPoller/script.js
@@ -1,5 +1,5 @@
function ManualPoller(
$http
api
) {

return ManualPoller;
Expand All @@ -11,13 +11,14 @@
var selectItems = options.itemSelector || defaultItemSelector;

this.run = function () {
var result = $http.post('/api/' + repository, {
var result = api({
repository: repository,
action: 'list',
parameters: filter,
}).then(function (response) {
if (response.data.error) {
if (response.error) {
if (processErrors) {
return processErrors(response.data.error);
return processErrors(response.error);
} else {
return;
}
Expand All @@ -33,7 +34,7 @@
};

function selectHttpBody(response) {
return response.data.data;
return response.data;
}

function defaultItemSelector(page) {
Expand Down
69 changes: 37 additions & 32 deletions Roomie.Web.Frontend/src/angular/data/ManualPoller/test.spec.js
@@ -1,35 +1,43 @@
describe('angular roomie.data ManualPoller (factory)', function () {

var $httpBackend;
var $q;
var $timeout;
var api;
var ManualPoller;
var items;

beforeEach(angular.mock.module('roomie.data'));

beforeEach(angular.mock.module(function ($provide) {
api = jasmine.createSpy('api');

$provide.value('api', api);
}));

beforeEach(angular.mock.inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
$q = $injector.get('$q');
$timeout = $injector.get('$timeout');
ManualPoller = $injector.get('ManualPoller');
}));

beforeEach(function () {
api.and.returnValue($q.when({}));

items = [];
});

it('POSTs the provided resource', function () {
it('submits the API request', function () {
var manualPoller = new ManualPoller({
repository: 'derp'
});

$httpBackend.when('POST', '/api/derp')
.respond({
data: {}
});

$httpBackend.expectPOST('/api/derp');

manualPoller.run();

$httpBackend.flush();
expect(api).toHaveBeenCalledWith({
repository: 'derp',
action: 'list',
parameters: undefined,
})
});

describe('item selection', function () {
Expand All @@ -40,20 +48,19 @@
repository: 'derp'
});

$httpBackend.when('POST', '/api/derp')
.respond({
data: {
items: [{ id: 'a' }, { id: 'b' }],
}
});
api.and.returnValue($q.when({
data: {
items: [{ id: 'a' }, { id: 'b' }],
}
}));

manualPoller.run().then(function (x) {
actual = x;
});

$httpBackend.flush();
$timeout.flush();

expect(actual).toEqual([{ id: 'a' }, { id: 'b' }]);
expect(actual).toEqual([{ id: 'a' }, { id: 'b' }]);
});

it('selects the items property with an optional override when provided.', function () {
Expand All @@ -65,16 +72,15 @@
}
});

$httpBackend.when('POST', '/api/derp')
.respond({
data: [{ id: 'a' }, { id: 'b' }],
});
api.and.returnValue($q.when({
data: [{ id: 'a' }, { id: 'b' }],
}));

manualPoller.run().then(function (x) {
actual = x;
});

$httpBackend.flush();
$timeout.flush();

expect(actual).toEqual([{ id: 'a' }, { id: 'b' }]);
});
Expand All @@ -85,12 +91,11 @@
var theError;

beforeEach(function () {
$httpBackend.when('POST', '/api/derp')
.respond(200, {
error: {
something: 'a message maybe'
}
});
api.and.returnValue($q.when({
error: {
something: 'a message maybe'
}
}));
});

describe('when the processErrors option is not set', function () {
Expand All @@ -102,7 +107,7 @@

manualPoller.run()

$httpBackend.flush();
$timeout.flush();
});

});
Expand All @@ -119,7 +124,7 @@

manualPoller.run();

$httpBackend.flush();
$timeout.flush();

expect(processErrors.calls.count()).toEqual(1);
expect(processErrors.calls.mostRecent().args[0]).toEqual({
Expand Down
7 changes: 7 additions & 0 deletions Roomie.Web.Frontend/src/angular/data/api/index.js
@@ -0,0 +1,7 @@
import script from './script.js';

export default {
type: 'factory',
name: script.name,
value: script,
};
14 changes: 14 additions & 0 deletions Roomie.Web.Frontend/src/angular/data/api/script.js
@@ -0,0 +1,14 @@
import commonApi from '../../../api.js';

function api(
$q
) {
return function (request) {
let result = commonApi(request);
let wrappedResult = $q.when(result);

return wrappedResult;
}
}

export default api;
2 changes: 2 additions & 0 deletions Roomie.Web.Frontend/src/angular/data/module.js
@@ -1,4 +1,5 @@
import defineModule from '../defineModule.js';
import api from './api/index.js';
import AutomaticPollingUpdater from './AutomaticPollingUpdater/index.js';
import deviceTypes from './deviceTypes/index.js';
import ManualPoller from './ManualPoller/index.js';
Expand All @@ -8,6 +9,7 @@ import ManualUpdater from './ManualUpdater/index.js';
let module = defineModule({
name: 'roomie.data',
definitions: [
api,
AutomaticPollingUpdater,
deviceTypes,
ManualPoller,
Expand Down
@@ -1,5 +1,5 @@
function deviceUtilities(
$http
api
) {

return new DeviceUtilities();
Expand All @@ -21,7 +21,8 @@ function deviceUtilities(
}

function doAction(device, action, args) {
$http.post('/api/device', {
api({
repository: 'device',
action: action,
parameters: Object.assign({
id: device.id,
Expand Down

0 comments on commit 996b1b2

Please sign in to comment.