@etainabl/nodejs-sdk 1.0.5 → 1.0.7

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 CHANGED
@@ -32,6 +32,10 @@ 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>;
38
+ getCompany: (id: string, options?: AxiosRequestConfig<any>) => Promise<any>;
35
39
  getConsumption: (id: string, options?: AxiosRequestConfig<any>) => Promise<any>;
36
40
  listConsumptions: (options?: AxiosRequestConfig<any>) => Promise<ETNPagedResponse>;
37
41
  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
- log.info(`API Request: GET ${process.env.ETAINABL_API_URL}/${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`);
7
- let response;
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
- response = await etainablApi.get(`${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`, options);
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
- if (!response) {
16
- throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
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
- log.info(`API Request: GET ${process.env.ETAINABL_API_URL}/${endpoint}`);
28
- let response;
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
- response = await etainablApi.get(`${endpoint}`, options);
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
- if (!response) {
37
- throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
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
- log.info(`API Request: PATCH ${process.env.ETAINABL_API_URL}/${endpoint}/${id}`);
49
- let response;
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
- response = await etainablApi.patch(`${endpoint}/${id}`, data, options);
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
- if (!response) {
58
- throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
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
- log.info(`API Request: POST ${process.env.ETAINABL_API_URL}/${endpoint}`);
70
- let response;
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
- response = await etainablApi.post(`${endpoint}`, data, options);
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
- if (!response) {
79
- throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
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
- log.info(`API Request: DELETE ${process.env.ETAINABL_API_URL}/${endpoint}/${id}`);
91
- let response;
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
- response = await etainablApi.delete(`${endpoint}/${id}`, options);
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
- if (!response) {
100
- throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
101
- }
102
- if (response.status !== 200) {
103
- throw new Error(`${response.status} ${response.statusText} response from API (DELETE ${endpoint})`);
104
- }
105
- if (!response.data) {
106
- throw new Error(`No data from API (DELETE ${endpoint})`);
107
- }
108
- return response.data;
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,11 @@ 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'),
176
+ // company
177
+ getCompany: factory.get(etainablApi, 'companies'),
155
178
  // consumption
156
179
  getConsumption: factory.get(etainablApi, 'consumptions'),
157
180
  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;AASlC,MAAM,OAAO,GAAG;IACd,GAAG,EAAE,CAAC,WAA0B,EAAE,QAAa,EAAE,YAAkB,EAAE,EAAE,CAAC,KAAK,EAAE,EAAU,EAAE,UAA8B,EAAE,EAAE,EAAE;QAC7H,GAAG,CAAC,IAAI,CAAC,oBAAoB,OAAO,CAAC,GAAG,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CAAC,IAAI,CAAC,WAAW,EAAE,cAAc,CAAC;QAC3D,iBAAiB,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC;QAC9D,iBAAiB,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC;QAC9D,iBAAiB,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC;QAE9D,WAAW;QACX,UAAU,EAAE,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,UAAU,CAAC;QAChD,YAAY,EAAE,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC;QACnD,aAAa,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,UAAU,CAAC;QACtD,aAAa,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,UAAU,CAAC;QACtD,aAAa,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,UAAU,CAAC;KACvD,CAAA;AACH,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etainabl/nodejs-sdk",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "main": "dist/index.js",
5
5
  "author": "Jonathan Lambert <jonathan@etainabl.com>",
6
6
  "type": "module",
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: any, postEndpoint?: any) => async (id: string, options: AxiosRequestConfig = {}) => {
17
- log.info(`API Request: GET ${process.env.ETAINABL_API_URL}/${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`);
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
+ };
46
+
47
+ log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
18
48
 
19
- let response;
49
+ let res: AxiosResponse;
20
50
 
21
51
  try {
22
- response = await etainablApi.get(`${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`, options);
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
- if (!response) {
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
- }
58
+ _handleResponse(req, res)
34
59
 
35
- if (!response.data) {
36
- throw new Error(`No data from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
37
- }
38
-
39
- return response.data;
60
+ return res.data;
40
61
  },
41
- list: (etainablApi: AxiosInstance, endpoint: any, postEndpoint?: any) => async (options: AxiosRequestConfig = {}): Promise<ETNPagedResponse> => {
42
- log.info(`API Request: GET ${process.env.ETAINABL_API_URL}/${endpoint}`);
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 response;
70
+ let res: AxiosResponse;
45
71
 
46
72
  try {
47
- response = await etainablApi.get(`${endpoint}`, options);
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
- if (!response) {
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
- }
79
+ _handleResponse(req, res, true)
59
80
 
60
- if (!response.data || !response.data.data) {
61
- throw new Error(`No data from API (GET ${endpoint})`);
62
- }
63
-
64
- return response.data;
81
+ return res.data;
65
82
  },
66
- update: (etainablApi: AxiosInstance, endpoint: any, postEndpoint?: any) => async (id: string, data: any, options: AxiosRequestConfig = {}) => {
67
- log.info(`API Request: PATCH ${process.env.ETAINABL_API_URL}/${endpoint}/${id}`);
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 response;
91
+ let res: AxiosResponse;
70
92
 
71
93
  try {
72
- response = await etainablApi.patch(`${endpoint}/${id}`, data, options);
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
- if (!response) {
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
- }
100
+ _handleResponse(req, res)
84
101
 
85
- if (!response.data) {
86
- throw new Error(`No data from API (PATCH ${endpoint})`);
87
- }
88
-
89
- return response.data;
102
+ return res.data;
90
103
  },
91
- create: (etainablApi: AxiosInstance, endpoint: any, postEndpoint?: any) => async (data: any, options: AxiosRequestConfig = {}) => {
92
- log.info(`API Request: POST ${process.env.ETAINABL_API_URL}/${endpoint}`);
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
+ };
109
+
110
+ log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
93
111
 
94
- let response;
112
+ let res: AxiosResponse;
95
113
 
96
114
  try {
97
- response = await etainablApi.post(`${endpoint}`, data, options);
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
- if (!response) {
103
- throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
104
- }
121
+ _handleResponse(req, res)
105
122
 
106
- if (response.status !== 200) {
107
- throw new Error(`${response.status} ${response.statusText} response from API (POST ${endpoint})`);
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
- if (!response.data) {
111
- throw new Error(`No data from API (POST ${endpoint})`);
112
- }
131
+ let res: AxiosResponse;
113
132
 
114
- return response.data;
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
- response = await etainablApi.delete(`${endpoint}/${id}`, options);
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
- if (!response) {
128
- throw new Error(`No response from API (GET ${endpoint}/:id${postEndpoint ? `/${postEndpoint}` : ''})`);
129
- }
142
+ _handleResponse(req, res);
130
143
 
131
- if (response.status !== 200) {
132
- throw new Error(`${response.status} ${response.statusText} response from API (DELETE ${endpoint})`);
133
- }
144
+ return res.data;
145
+ },
146
+ };
134
147
 
135
- if (!response.data) {
136
- throw new Error(`No data from API (DELETE ${endpoint})`);
137
- }
148
+ // ETN Sub Endpoints
149
+ // e.g. /assets/:id/documents/:documentId
138
150
 
139
- return response.data;
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,12 @@ 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'),
227
+
228
+ // company
229
+ getCompany: factory.get(etainablApi, 'companies'),
197
230
 
198
231
  // consumption
199
232
  getConsumption: factory.get(etainablApi, 'consumptions'),