Repository navigation
Expand file tree
/
Copy pathcreateMeta.js
More file actions
80 lines (67 loc) · 3.66 KB
/
Copy pathcreateMeta.js
File metadata and controls
80 lines (67 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import invariant from 'invariant';
import transformDataToAsset from './transformDataToAsset';
import getV1Urls from './getV1Urls';
import Oid from './Oid';
export default ({hostname, instance, protocol, port, token, postFn, getFn, isBasic}) => {
const urls = getV1Urls(hostname, instance, protocol, port);
const headers = createHeaderObj(token, isBasic);
return {
create(assetType, assetData) {
invariant(assetType, `Error: there was no \`assetType\` provided to create`);
invariant(assetData, `Error: there was no \`assetAttributeData\` provided to create`);
invariant(Object.keys(assetData).length, `Error: there was no \`assetAttributeData\` provided to create`);
const postData = transformDataToAsset(assetData);
const url = `${urls.rest}/${assetType}`;
return postFn(url, postData, headers);
},
update(oidToken, assetData, changeComment) {
invariant(oidToken, `Error: there was no \`oidToken\` provided to update`);
invariant(assetData, `Error: there was no \`assetAttributeData\` provided to update`);
invariant(Object.keys(assetData).length, `Error: there was no \`assetAttributeData\` provided to update`);
const oid = new Oid(oidToken);
const postData = transformDataToAsset(assetData);
const comment = changeComment ? `?comment=${encodeURIComponent(changeComment)}` : '';
const url = `${urls.rest}/${oid.assetType}/${oid.number}${comment}`;
return postFn(url, postData, headers);
},
query(queryObj) {
invariant(queryObj.from, `Error: there was no \`from\` property on provided query: ${queryObj}`);
invariant(queryObj.select, `Error: there was no \`select\` property on provided query: ${queryObj}`);
invariant(Array.isArray(queryObj.select), `Error: \`select\` property must be an Array on provided query: ${queryObj}`);
invariant(queryObj.select.length, `Error: \`select\` property must contain values on provided query: ${queryObj}`);
const url = urls.query;
return postFn(url, queryObj, headers);
},
executeOperation(oidToken, operationName) {
invariant(oidToken, `Error: there was no \`oidToken\` provided to execute operation`);
invariant(operationName, `Error: there was no \`operationName\` provided to execute operation`);
const oid = new Oid(oidToken);
const url = `${urls.rest}/${oid.assetType}/${oid.number}?op=${operationName}`;
return postFn(url, null, headers);
},
queryDefinition(assetType) {
const queryAssetType = assetType ? assetType : '';
const url = `${urls.meta}/${queryAssetType}`;
return getFn(url, null, headers);
},
getActivityStream(oidToken) {
invariant(oidToken, `Error: there was no \`oidToken\` provided to execute operation`);
const url = `${urls.activityStream}/${oidToken}`;
return getFn(url, null, headers);
}
};
};
const createHeaderObj = (token, isBasic) => {
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
};
const hasToken = Boolean(token);
const hasType = typeof isBasic !== 'undefined';
if (hasToken || hasType) {
invariant(hasToken, `Error: there was no \`token\` provided to the SDK`);
invariant(hasType, `Error: there was no \`isBasic\` provided to the SDK`);
headers['Authorization'] = `${isBasic ? 'Basic' : 'Bearer'} ${token}`; // eslint-disable-line dot-notation
}
return headers;
};