UNPKG

2.91 kBJavaScriptView Raw
1'use strict';
2
3// local modules
4
5const auth = require('./auth');
6const httpUtils = require('./utils/http');
7
8// this module
9
10const TYPES = [ 'answerspaces', 'interactions' ];
11
12function assertIdOption (options) {
13 if (!options.id) {
14 throw new TypeError('"id" must be provided');
15 }
16}
17
18function assertOptionsMatchResult (options, result) {
19 const resultId = result[options.type].id;
20 if (options.id && options.id !== resultId) {
21 throw new Error(`request-result mismatch: id=${options.id}, result=${resultId}`);
22 }
23}
24
25function assertTypeOption (options) {
26 if (!options.type) {
27 throw new TypeError('"type" must be provided');
28 }
29}
30
31function assertKnownTypeOption (options) {
32 if (!~TYPES.indexOf(options.type)) {
33 throw new TypeError(`"${options.type}" is not a known type`);
34 }
35}
36
37function assertDataOption (options) {
38 if (!options.data || typeof options.data !== 'object') {
39 throw new TypeError(`"data" object is mandatory`);
40 }
41}
42
43function assertMatchingIdOption (options) {
44 if (options.id !== options.data.id) {
45 throw new Error('"id"s in resource and options do not match');
46 }
47}
48
49function getDashboard () {
50 return auth.read()
51 .then((a) => httpUtils.sendRequest({
52 credential: a.credential,
53 url: `${a.origin}/_api/v1/dashboard`
54 }));
55}
56
57function decorateWithOptionsAssertions (fn, assertions) {
58 return (options) => {
59 options = options || {};
60 assertions.forEach((assert) => assert(options));
61
62 return fn(options);
63 };
64}
65
66const deleteResource = decorateWithOptionsAssertions((options) => {
67 return auth.read()
68 .then((a) => httpUtils.sendRequest({
69 credential: a.credential,
70 method: 'DELETE',
71 url: `${a.origin}/_api/v1/${options.type}/${options.id}`
72 }));
73}, [
74 assertIdOption,
75 assertTypeOption,
76 assertKnownTypeOption
77]);
78
79const getResource = decorateWithOptionsAssertions((options) => {
80 return auth.read()
81 .then((a) => httpUtils.sendRequest({
82 credential: a.credential,
83 url: `${a.origin}/_api/v1/${options.type}/${options.id}`
84 }))
85 .then((result) => {
86 assertOptionsMatchResult(options, result);
87 return result;
88 });
89}, [
90 assertIdOption,
91 assertTypeOption,
92 assertKnownTypeOption
93]);
94
95const putResource = decorateWithOptionsAssertions((options) => {
96 const body = {};
97 body[options.type] = options.data;
98 let method;
99 let urlPath;
100 if (options.id) {
101 method = 'PUT';
102 urlPath = `/_api/v1/${options.type}/${options.id}`;
103 } else {
104 method = 'POST';
105 urlPath = `/_api/v1/${options.type}`;
106 }
107 return auth.read()
108 .then((a) => httpUtils.sendRequest({
109 body,
110 credential: a.credential,
111 method,
112 url: `${a.origin}${urlPath}`
113 }));
114}, [
115 assertTypeOption,
116 assertKnownTypeOption,
117 assertDataOption,
118 assertMatchingIdOption
119]);
120
121module.exports = {
122 deleteResource,
123 getDashboard,
124 getResource,
125 putResource,
126 TYPES
127};