-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdk.js
244 lines (202 loc) · 5.26 KB
/
sdk.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
export default class SimproSDK {
#baseUrl;
#headers;
constructor({ fqdn = "", companyId = 0, accessToken = "" }) {
this.#baseUrl = `https://${fqdn}/api/v1.0/companies/${companyId}`;
this.#headers = {
"Authorization": `Bearer ${accessToken}`,
"Accept": "application/json",
"Content-Type": "application/json",
}
}
/**
* Ensures that the method string parameter is a valid HTTP verb.
*
* @param {string} method
*
* @returns {boolean}
*/
#isValidMethod(method) {
switch(method) {
case "GET":
return true;
case "POST":
return true;
case "PATCH":
return true;
case "DELETE":
return true;
default:
return false;
}
}
/**
* Ensures that the search type string parameter is valid.
*
* @param {string} searchType
*
* @returns {boolean}
*/
#isValidSearchType(searchType) {
switch(searchType) {
case undefined:
return true;
case "all":
return true;
case "any":
return true;
default:
return false
}
}
/**
* Makes an HTTP request to the Simpro instance's API.
*
* @param {string} method
* @param {string} resource
* @param {Object} body
* @param {Object} opts
*
* @returns {Response|Object}
*/
async send(method, resource = "", body = null, opts = {
// searchType: string<all|any>
// search: string[]
// columns: string[]
// orderBy: string[]
// page: uint
// pageSize: uint<1..250>
// limit: uint
}) {
if (!this.#isValidMethod(method))
return { error: "Invalid method string: Must be one of: GET|POST|PATCH|DELETE" };
if (!this.#isValidSearchType(opts.searchType))
return { error: "Invalid search type: Must be one of: any|all" };
if (opts.limit && opts.limit < 0)
return { error: "Invalid limit value: Must be zero or a positive integer" };
let
searchType = "",
search = "",
columns = "",
orderBy = "",
page = "",
pageSize = "",
limit = "";
// Set when not `undefined`
if (opts.searchType)
searchType = `search=${opts.searchType}`;
if (opts.search)
search = `&${opts.search.join("&")}`;
if (opts.columns)
columns = `&columns=${opts.columns.join(",")}`;
if (opts.orderBy)
orderBy = `&orderby=${opts.orderBy.join(",")}`;
if (opts.page)
page = `&page=${opts.page}`;
if (opts.pageSize)
pageSize = `&pageSize=${opts.pageSize}`;
if (opts.limit)
limit = `&limit=${opts.limit}`;
let querySeparator = "";
if (method === "GET")
querySeparator = "?";
if (body)
body = JSON.stringify(body);
// Query the API
const apiResponse = await fetch(
`${this.#baseUrl}/${resource}${querySeparator}${searchType}${search}${columns}${orderBy}${page}${pageSize}${limit}`,
{
method,
headers: this.#headers,
body,
},
);
return apiResponse;
}
/**
* Returns all pages of data from a resource.
*
* @param {string} resource
* @param {Object} opts
*
* @returns {Object}
*/
async getPages(resource, opts = {}) {
const dataPages = {};
let resultPages = null;
let pageNum = 1;
do {
opts.page = pageNum;
const apiResponse = await this.send("GET", resource, null, opts);
if (apiResponse.status !== 200)
return { error: `Returned non-200 response: ${apiResponse.status} ${apiResponse.statusText}` };
if (!resultPages)
resultPages = parseInt(apiResponse.headers.get("result-pages"));
const dataPage = await apiResponse.json();
dataPages[`pg${pageNum}`] = dataPage;
pageNum += 1;
} while (pageNum <= resultPages);
return dataPages;
}
/**
* Returns all invoices in a paginated format.
*
* @param {Object} opts
*
* @returns {Object}
*/
async getInvoices(opts = {}) {
const invoicePages = await this.getPages("invoices/", opts);
if (invoicePages.error) {
return console.log(invoicePages.error);
}
return invoicePages;
}
/**
* Returns all customers in a paginated format.
*
* @param {Object} opts
*
* @return {Object}
*/
async getCustomers(opts = {}) {
const customerPages = await this.getPages("customers/", opts);
if (customerPages.error) {
return console.log(customerPages.error);
}
return customerPages;
}
/**
* Returns all the employees in a paginated format.
*
* @param {Object} opts
*
* @return {Object}
*/
async getEmployees(opts = {}) {
const employeePages = await this.getPages("employees/", opts);
if (employeePages.error) {
return console.log(employeePages.error);
}
return employeePages;
}
/**
* Returns the data relating to the specified employee ID.
*
* @param {uint} id
* @param {string[]} columns
*
* @return {Object}
*/
async getEmployeeById(id = null, columns = []) {
if (!id || id <= 0) {
return { error: "Invalid `id` parameter: Must be a positive integer" };
}
const res = await this.send("GET", `employees/${id}`, null, { columns });
if (res.error) {
return res;
}
const employee = await res.json();
return employee;
}
}