@etainabl/nodejs-sdk 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +3 -0
- package/dist/api.js +86 -65
- package/dist/api.js.map +1 -1
- package/package.json +1 -2
- package/src/api.ts +110 -80
package/dist/api.d.ts
CHANGED
|
@@ -32,6 +32,9 @@ declare const _default: (auth: AuthOptions, instanceOptions?: CreateAxiosDefault
|
|
|
32
32
|
updateAutomation: (id: string, data: any, options?: AxiosRequestConfig<any>) => Promise<any>;
|
|
33
33
|
createAutomation: (data: any, options?: AxiosRequestConfig<any>) => Promise<any>;
|
|
34
34
|
removeAutomation: (id: string, options?: AxiosRequestConfig<any>) => Promise<any>;
|
|
35
|
+
createAutomationLog: (id: string, options?: AxiosRequestConfig<any>) => Promise<any>;
|
|
36
|
+
updateAutomationLog: (id: string, subId: string, options?: AxiosRequestConfig<any>) => Promise<any>;
|
|
37
|
+
removeAutomationLog: (id: string, subId: string, options?: AxiosRequestConfig<any>) => Promise<any>;
|
|
35
38
|
getConsumption: (id: string, options?: AxiosRequestConfig<any>) => Promise<any>;
|
|
36
39
|
listConsumptions: (options?: AxiosRequestConfig<any>) => Promise<ETNPagedResponse>;
|
|
37
40
|
updateConsumption: (id: string, data: any, options?: AxiosRequestConfig<any>) => Promise<any>;
|
package/dist/api.js
CHANGED
|
@@ -1,111 +1,129 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
2
|
import logger from './logger.js';
|
|
3
3
|
const log = logger('etainablApi');
|
|
4
|
+
function _handleResponse(req, res, isPaged = false) {
|
|
5
|
+
if (!res) {
|
|
6
|
+
throw new Error(`No response from API (${req.method} ${req.url})`);
|
|
7
|
+
}
|
|
8
|
+
if (res.status !== 200) {
|
|
9
|
+
throw new Error(`${res.status} ${res.statusText} response from API (${req.method} ${req.url})`);
|
|
10
|
+
}
|
|
11
|
+
if (!res.data) {
|
|
12
|
+
throw new Error(`No data from API (${req.method} ${req.url})`);
|
|
13
|
+
}
|
|
14
|
+
if (isPaged && !res.data.data) {
|
|
15
|
+
throw new Error(`No data from API (${req.method} ${req.url})`);
|
|
16
|
+
}
|
|
17
|
+
return res;
|
|
18
|
+
}
|
|
4
19
|
const factory = {
|
|
5
20
|
get: (etainablApi, endpoint, postEndpoint) => async (id, options = {}) => {
|
|
6
|
-
|
|
7
|
-
|
|
21
|
+
const req = {
|
|
22
|
+
method: 'GET',
|
|
23
|
+
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
24
|
+
};
|
|
25
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
26
|
+
let res;
|
|
8
27
|
try {
|
|
9
|
-
|
|
28
|
+
res = await etainablApi.get(req.url, options);
|
|
10
29
|
}
|
|
11
30
|
catch (e) {
|
|
12
31
|
if (e.response?.data)
|
|
13
32
|
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
33
|
+
throw e;
|
|
14
34
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
if (response.status !== 200) {
|
|
19
|
-
throw new Error(`${response.status} ${response.statusText} response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
|
|
20
|
-
}
|
|
21
|
-
if (!response.data) {
|
|
22
|
-
throw new Error(`No data from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
|
|
23
|
-
}
|
|
24
|
-
return response.data;
|
|
35
|
+
_handleResponse(req, res);
|
|
36
|
+
return res.data;
|
|
25
37
|
},
|
|
26
38
|
list: (etainablApi, endpoint, postEndpoint) => async (options = {}) => {
|
|
27
|
-
|
|
28
|
-
|
|
39
|
+
const req = {
|
|
40
|
+
method: 'GET',
|
|
41
|
+
url: `${endpoint}/${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
42
|
+
};
|
|
43
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
44
|
+
let res;
|
|
29
45
|
try {
|
|
30
|
-
|
|
46
|
+
res = await etainablApi.get(req.url, options);
|
|
31
47
|
}
|
|
32
48
|
catch (e) {
|
|
33
49
|
if (e.response?.data)
|
|
34
50
|
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
51
|
+
throw e;
|
|
35
52
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
if (response.status !== 200) {
|
|
40
|
-
throw new Error(`${response.status} ${response.statusText} response from API (GET ${endpoint})`);
|
|
41
|
-
}
|
|
42
|
-
if (!response.data || !response.data.data) {
|
|
43
|
-
throw new Error(`No data from API (GET ${endpoint})`);
|
|
44
|
-
}
|
|
45
|
-
return response.data;
|
|
53
|
+
_handleResponse(req, res, true);
|
|
54
|
+
return res.data;
|
|
46
55
|
},
|
|
47
56
|
update: (etainablApi, endpoint, postEndpoint) => async (id, data, options = {}) => {
|
|
48
|
-
|
|
49
|
-
|
|
57
|
+
const req = {
|
|
58
|
+
method: 'PATCH',
|
|
59
|
+
url: `${endpoint}/${id}/${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
60
|
+
};
|
|
61
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
62
|
+
let res;
|
|
50
63
|
try {
|
|
51
|
-
|
|
64
|
+
res = await etainablApi.patch(req.url, data, options);
|
|
52
65
|
}
|
|
53
66
|
catch (e) {
|
|
54
67
|
if (e.response?.data)
|
|
55
68
|
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
69
|
+
throw e;
|
|
56
70
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
if (response.status !== 200) {
|
|
61
|
-
throw new Error(`${response.status} ${response.statusText} response from API (PATCH ${endpoint})`);
|
|
62
|
-
}
|
|
63
|
-
if (!response.data) {
|
|
64
|
-
throw new Error(`No data from API (PATCH ${endpoint})`);
|
|
65
|
-
}
|
|
66
|
-
return response.data;
|
|
71
|
+
_handleResponse(req, res);
|
|
72
|
+
return res.data;
|
|
67
73
|
},
|
|
68
74
|
create: (etainablApi, endpoint, postEndpoint) => async (data, options = {}) => {
|
|
69
|
-
|
|
70
|
-
|
|
75
|
+
const req = {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
url: `${endpoint}/${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
78
|
+
};
|
|
79
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
80
|
+
let res;
|
|
71
81
|
try {
|
|
72
|
-
|
|
82
|
+
res = await etainablApi.post(req.url, data, options);
|
|
73
83
|
}
|
|
74
84
|
catch (e) {
|
|
75
85
|
if (e.response?.data)
|
|
76
86
|
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
87
|
+
throw e;
|
|
77
88
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
if (response.status !== 200) {
|
|
82
|
-
throw new Error(`${response.status} ${response.statusText} response from API (POST ${endpoint})`);
|
|
83
|
-
}
|
|
84
|
-
if (!response.data) {
|
|
85
|
-
throw new Error(`No data from API (POST ${endpoint})`);
|
|
86
|
-
}
|
|
87
|
-
return response.data;
|
|
89
|
+
_handleResponse(req, res);
|
|
90
|
+
return res.data;
|
|
88
91
|
},
|
|
89
92
|
remove: (etainablApi, endpoint, postEndpoint) => async (id, options = {}) => {
|
|
90
|
-
|
|
91
|
-
|
|
93
|
+
const req = {
|
|
94
|
+
method: 'DELETE',
|
|
95
|
+
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
96
|
+
};
|
|
97
|
+
let res;
|
|
98
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
92
99
|
try {
|
|
93
|
-
|
|
100
|
+
res = await etainablApi.delete(req.url, options);
|
|
94
101
|
}
|
|
95
102
|
catch (e) {
|
|
96
103
|
if (e.response?.data)
|
|
97
104
|
throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
105
|
+
throw e;
|
|
98
106
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
return
|
|
107
|
+
_handleResponse(req, res);
|
|
108
|
+
return res.data;
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
// ETN Sub Endpoints
|
|
112
|
+
// e.g. /assets/:id/documents/:documentId
|
|
113
|
+
const subFactory = {
|
|
114
|
+
// e.g. POST /assets/:id/documents
|
|
115
|
+
create: (etainablApi, endpoint, subEndpoint) => async (id, options = {}) => {
|
|
116
|
+
return factory.create(etainablApi, endpoint, subEndpoint)(id, options);
|
|
117
|
+
},
|
|
118
|
+
// e.g. PATCH /assets/:id/documents/:documentId
|
|
119
|
+
update: (etainablApi, endpoint, subEndpoint) => async (id, subId, options = {}) => {
|
|
120
|
+
const subUrl = `${subEndpoint}/${subId}`;
|
|
121
|
+
return factory.update(etainablApi, endpoint, subUrl)(id, options);
|
|
122
|
+
},
|
|
123
|
+
// e.g. DELETE /assets/:id/documents/:documentId
|
|
124
|
+
remove: (etainablApi, endpoint, subEndpoint) => async (id, subId, options = {}) => {
|
|
125
|
+
const subUrl = `${subEndpoint}/${subId}`;
|
|
126
|
+
return factory.remove(etainablApi, endpoint, subUrl)(id, options);
|
|
109
127
|
}
|
|
110
128
|
};
|
|
111
129
|
export default (auth, instanceOptions = {}) => {
|
|
@@ -152,6 +170,9 @@ export default (auth, instanceOptions = {}) => {
|
|
|
152
170
|
updateAutomation: factory.update(etainablApi, 'automation'),
|
|
153
171
|
createAutomation: factory.create(etainablApi, 'automation'),
|
|
154
172
|
removeAutomation: factory.remove(etainablApi, 'automation'),
|
|
173
|
+
createAutomationLog: subFactory.create(etainablApi, 'automation', 'logs'),
|
|
174
|
+
updateAutomationLog: subFactory.update(etainablApi, 'automation', 'logs'),
|
|
175
|
+
removeAutomationLog: subFactory.remove(etainablApi, 'automation', 'logs'),
|
|
155
176
|
// consumption
|
|
156
177
|
getConsumption: factory.get(etainablApi, 'consumptions'),
|
|
157
178
|
listConsumptions: factory.list(etainablApi, 'consumptions'),
|
package/dist/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,MAAM,GAAG,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,MAAM,GAAG,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AAclC,SAAS,eAAe,CAAC,GAAW,EAAE,GAAkB,EAAE,OAAO,GAAG,KAAK;IACvE,IAAI,CAAC,GAAG,EAAE;QACR,MAAM,IAAI,KAAK,CAAC,yBAAyB,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC;KACpE;IAED,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE;QACtB,MAAM,IAAI,KAAK,CAAC,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,UAAU,uBAAuB,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC;KACjG;IAED,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE;QACb,MAAM,IAAI,KAAK,CAAC,qBAAqB,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC;KAChE;IAED,IAAI,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE;QAC7B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KACvD,CAAA;AACH,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etainabl/nodejs-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"author": "Jonathan Lambert <jonathan@etainabl.com>",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"types": "dist/etainabl.d.ts",
|
|
8
7
|
"dependencies": {
|
|
9
8
|
"axios": "^1.4.0",
|
|
10
9
|
"moment": "^2.29.4",
|
package/src/api.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
-
import type { AxiosRequestConfig, AxiosInstance, CreateAxiosDefaults } from 'axios';
|
|
2
|
+
import type { AxiosRequestConfig, AxiosInstance, CreateAxiosDefaults, AxiosResponse } from 'axios';
|
|
3
3
|
|
|
4
4
|
import logger from './logger.js';
|
|
5
5
|
|
|
@@ -12,133 +12,160 @@ interface ETNPagedResponse {
|
|
|
12
12
|
skip: number;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
interface ETNReq {
|
|
16
|
+
method: string;
|
|
17
|
+
url: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function _handleResponse(req: ETNReq, res: AxiosResponse, isPaged = false) {
|
|
21
|
+
if (!res) {
|
|
22
|
+
throw new Error(`No response from API (${req.method} ${req.url})`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (res.status !== 200) {
|
|
26
|
+
throw new Error(`${res.status} ${res.statusText} response from API (${req.method} ${req.url})`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!res.data) {
|
|
30
|
+
throw new Error(`No data from API (${req.method} ${req.url})`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (isPaged && !res.data.data) {
|
|
34
|
+
throw new Error(`No data from API (${req.method} ${req.url})`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return res;
|
|
38
|
+
}
|
|
39
|
+
|
|
15
40
|
const factory = {
|
|
16
|
-
get: (etainablApi: AxiosInstance, endpoint:
|
|
17
|
-
|
|
41
|
+
get: (etainablApi: AxiosInstance, endpoint: string, postEndpoint?: string) => async (id: string, options: AxiosRequestConfig = {}) => {
|
|
42
|
+
const req = {
|
|
43
|
+
method: 'GET',
|
|
44
|
+
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
45
|
+
};
|
|
18
46
|
|
|
19
|
-
|
|
47
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
48
|
+
|
|
49
|
+
let res: AxiosResponse;
|
|
20
50
|
|
|
21
51
|
try {
|
|
22
|
-
|
|
52
|
+
res = await etainablApi.get(req.url, options);
|
|
23
53
|
} catch (e: any) {
|
|
24
54
|
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
55
|
+
throw e;
|
|
25
56
|
}
|
|
26
57
|
|
|
27
|
-
|
|
28
|
-
throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (response.status !== 200) {
|
|
32
|
-
throw new Error(`${response.status} ${response.statusText} response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (!response.data) {
|
|
36
|
-
throw new Error(`No data from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
|
|
37
|
-
}
|
|
58
|
+
_handleResponse(req, res)
|
|
38
59
|
|
|
39
|
-
return
|
|
60
|
+
return res.data;
|
|
40
61
|
},
|
|
41
|
-
list: (etainablApi: AxiosInstance, endpoint:
|
|
42
|
-
|
|
62
|
+
list: (etainablApi: AxiosInstance, endpoint: string, postEndpoint?: string) => async (options: AxiosRequestConfig = {}): Promise<ETNPagedResponse> => {
|
|
63
|
+
const req = {
|
|
64
|
+
method: 'GET',
|
|
65
|
+
url: `${endpoint}/${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
43
69
|
|
|
44
|
-
let
|
|
70
|
+
let res: AxiosResponse;
|
|
45
71
|
|
|
46
72
|
try {
|
|
47
|
-
|
|
73
|
+
res = await etainablApi.get(req.url, options);
|
|
48
74
|
} catch (e: any) {
|
|
49
75
|
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
76
|
+
throw e;
|
|
50
77
|
}
|
|
51
78
|
|
|
52
|
-
|
|
53
|
-
throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (response.status !== 200) {
|
|
57
|
-
throw new Error(`${response.status} ${response.statusText} response from API (GET ${endpoint})`);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (!response.data || !response.data.data) {
|
|
61
|
-
throw new Error(`No data from API (GET ${endpoint})`);
|
|
62
|
-
}
|
|
79
|
+
_handleResponse(req, res, true)
|
|
63
80
|
|
|
64
|
-
return
|
|
81
|
+
return res.data;
|
|
65
82
|
},
|
|
66
|
-
update: (etainablApi: AxiosInstance, endpoint:
|
|
67
|
-
|
|
83
|
+
update: (etainablApi: AxiosInstance, endpoint: string, postEndpoint?: string) => async (id: string, data: any, options: AxiosRequestConfig = {}) => {
|
|
84
|
+
const req = {
|
|
85
|
+
method: 'PATCH',
|
|
86
|
+
url: `${endpoint}/${id}/${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
68
90
|
|
|
69
|
-
let
|
|
91
|
+
let res: AxiosResponse;
|
|
70
92
|
|
|
71
93
|
try {
|
|
72
|
-
|
|
94
|
+
res = await etainablApi.patch(req.url, data, options);
|
|
73
95
|
} catch (e: any) {
|
|
74
96
|
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
97
|
+
throw e;
|
|
75
98
|
}
|
|
76
99
|
|
|
77
|
-
|
|
78
|
-
throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (response.status !== 200) {
|
|
82
|
-
throw new Error(`${response.status} ${response.statusText} response from API (PATCH ${endpoint})`);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (!response.data) {
|
|
86
|
-
throw new Error(`No data from API (PATCH ${endpoint})`);
|
|
87
|
-
}
|
|
100
|
+
_handleResponse(req, res)
|
|
88
101
|
|
|
89
|
-
return
|
|
102
|
+
return res.data;
|
|
90
103
|
},
|
|
91
|
-
create: (etainablApi: AxiosInstance, endpoint:
|
|
92
|
-
|
|
104
|
+
create: (etainablApi: AxiosInstance, endpoint: string, postEndpoint?: string) => async (data: any, options: AxiosRequestConfig = {}) => {
|
|
105
|
+
const req = {
|
|
106
|
+
method: 'POST',
|
|
107
|
+
url: `${endpoint}/${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
108
|
+
};
|
|
93
109
|
|
|
94
|
-
|
|
110
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
111
|
+
|
|
112
|
+
let res: AxiosResponse;
|
|
95
113
|
|
|
96
114
|
try {
|
|
97
|
-
|
|
115
|
+
res = await etainablApi.post(req.url, data, options);
|
|
98
116
|
} catch (e: any) {
|
|
99
117
|
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
118
|
+
throw e;
|
|
100
119
|
}
|
|
101
120
|
|
|
102
|
-
|
|
103
|
-
throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
|
|
104
|
-
}
|
|
121
|
+
_handleResponse(req, res)
|
|
105
122
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
123
|
+
return res.data;
|
|
124
|
+
},
|
|
125
|
+
remove: (etainablApi: AxiosInstance, endpoint: string, postEndpoint?: string) => async (id: string, options: AxiosRequestConfig = {}) => {
|
|
126
|
+
const req = {
|
|
127
|
+
method: 'DELETE',
|
|
128
|
+
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`
|
|
129
|
+
};
|
|
109
130
|
|
|
110
|
-
|
|
111
|
-
throw new Error(`No data from API (POST ${endpoint})`);
|
|
112
|
-
}
|
|
131
|
+
let res: AxiosResponse;
|
|
113
132
|
|
|
114
|
-
|
|
115
|
-
},
|
|
116
|
-
remove: (etainablApi: AxiosInstance, endpoint: any, postEndpoint?: any) => async (id: string, options: AxiosRequestConfig = {}) => {
|
|
117
|
-
log.info(`API Request: DELETE ${process.env.ETAINABL_API_URL}/${endpoint}/${id}`);
|
|
133
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
118
134
|
|
|
119
|
-
let response;
|
|
120
|
-
|
|
121
135
|
try {
|
|
122
|
-
|
|
136
|
+
res = await etainablApi.delete(req.url, options);
|
|
123
137
|
} catch (e: any) {
|
|
124
138
|
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
139
|
+
throw e;
|
|
125
140
|
}
|
|
126
141
|
|
|
127
|
-
|
|
128
|
-
throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
|
|
129
|
-
}
|
|
142
|
+
_handleResponse(req, res);
|
|
130
143
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
144
|
+
return res.data;
|
|
145
|
+
},
|
|
146
|
+
};
|
|
134
147
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
148
|
+
// ETN Sub Endpoints
|
|
149
|
+
// e.g. /assets/:id/documents/:documentId
|
|
138
150
|
|
|
139
|
-
|
|
151
|
+
const subFactory = {
|
|
152
|
+
// e.g. POST /assets/:id/documents
|
|
153
|
+
create: (etainablApi: AxiosInstance, endpoint: string, subEndpoint: string) => async (id: string, options: AxiosRequestConfig = {}) => {
|
|
154
|
+
return factory.create(etainablApi, endpoint, subEndpoint)(id, options);
|
|
155
|
+
},
|
|
156
|
+
// e.g. PATCH /assets/:id/documents/:documentId
|
|
157
|
+
update: (etainablApi: AxiosInstance, endpoint: string, subEndpoint: string) => async (id: string, subId: string, options: AxiosRequestConfig = {}) => {
|
|
158
|
+
const subUrl = `${subEndpoint}/${subId}`;
|
|
159
|
+
|
|
160
|
+
return factory.update(etainablApi, endpoint, subUrl)(id, options);
|
|
161
|
+
},
|
|
162
|
+
// e.g. DELETE /assets/:id/documents/:documentId
|
|
163
|
+
remove: (etainablApi: AxiosInstance, endpoint: string, subEndpoint: string) => async (id: string, subId: string, options: AxiosRequestConfig = {}) => {
|
|
164
|
+
const subUrl = `${subEndpoint}/${subId}`;
|
|
165
|
+
|
|
166
|
+
return factory.remove(etainablApi, endpoint, subUrl)(id, options);
|
|
140
167
|
}
|
|
141
|
-
}
|
|
168
|
+
}
|
|
142
169
|
|
|
143
170
|
interface AuthOptions {
|
|
144
171
|
key?: string;
|
|
@@ -194,6 +221,9 @@ export default (auth: AuthOptions, instanceOptions: CreateAxiosDefaults = {}) =>
|
|
|
194
221
|
updateAutomation: factory.update(etainablApi, 'automation'),
|
|
195
222
|
createAutomation: factory.create(etainablApi, 'automation'),
|
|
196
223
|
removeAutomation: factory.remove(etainablApi, 'automation'),
|
|
224
|
+
createAutomationLog: subFactory.create(etainablApi, 'automation', 'logs'),
|
|
225
|
+
updateAutomationLog: subFactory.update(etainablApi, 'automation', 'logs'),
|
|
226
|
+
removeAutomationLog: subFactory.remove(etainablApi, 'automation', 'logs'),
|
|
197
227
|
|
|
198
228
|
// consumption
|
|
199
229
|
getConsumption: factory.get(etainablApi, 'consumptions'),
|