1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | const auth = require('./auth');
|
6 | const httpUtils = require('./utils/http');
|
7 |
|
8 |
|
9 |
|
10 | const TYPES = [ 'answerspaces', 'interactions' ];
|
11 |
|
12 | function assertIdOption (options) {
|
13 | if (!options.id) {
|
14 | throw new TypeError('"id" must be provided');
|
15 | }
|
16 | }
|
17 |
|
18 | function 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 |
|
25 | function assertTypeOption (options) {
|
26 | if (!options.type) {
|
27 | throw new TypeError('"type" must be provided');
|
28 | }
|
29 | }
|
30 |
|
31 | function assertKnownTypeOption (options) {
|
32 | if (!~TYPES.indexOf(options.type)) {
|
33 | throw new TypeError(`"${options.type}" is not a known type`);
|
34 | }
|
35 | }
|
36 |
|
37 | function assertDataOption (options) {
|
38 | if (!options.data || typeof options.data !== 'object') {
|
39 | throw new TypeError(`"data" object is mandatory`);
|
40 | }
|
41 | }
|
42 |
|
43 | function 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 |
|
49 | function getDashboard () {
|
50 | return auth.read()
|
51 | .then((a) => httpUtils.sendRequest({
|
52 | credential: a.credential,
|
53 | url: `${a.origin}/_api/v1/dashboard`
|
54 | }));
|
55 | }
|
56 |
|
57 | function decorateWithOptionsAssertions (fn, assertions) {
|
58 | return (options) => {
|
59 | options = options || {};
|
60 | assertions.forEach((assert) => assert(options));
|
61 |
|
62 | return fn(options);
|
63 | };
|
64 | }
|
65 |
|
66 | const 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 |
|
79 | const 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 |
|
95 | const 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 |
|
121 | module.exports = {
|
122 | deleteResource,
|
123 | getDashboard,
|
124 | getResource,
|
125 | putResource,
|
126 | TYPES
|
127 | };
|