@allthings/sdk 10.1.3 → 11.0.0
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/{cli.js → cli.cjs} +1 -1
- package/dist/{lib.cjs.js → lib.cjs} +1 -1
- package/dist/lib.esm.mjs +1352 -0
- package/dist/package.json +13 -13
- package/package.json +13 -13
- package/dist/src/cli.js +0 -15
- package/dist/src/constants.js +0 -25
- package/dist/src/index.js +0 -4
- package/dist/src/oauth/authorizationCodeGrant.js +0 -56
- package/dist/src/oauth/clientCredentialsGrant.js +0 -25
- package/dist/src/oauth/createTokenStore.js +0 -8
- package/dist/src/oauth/implicitGrant.js +0 -24
- package/dist/src/oauth/makeFetchTokenRequester.js +0 -38
- package/dist/src/oauth/maybeUpdateToken.js +0 -44
- package/dist/src/oauth/passwordGrant.js +0 -30
- package/dist/src/oauth/refreshTokenGrant.js +0 -26
- package/dist/src/oauth/requestAndSaveToStore.js +0 -5
- package/dist/src/oauth/types.js +0 -1
- package/dist/src/rest/delete.js +0 -3
- package/dist/src/rest/get.js +0 -3
- package/dist/src/rest/index.js +0 -160
- package/dist/src/rest/methods/agent.js +0 -26
- package/dist/src/rest/methods/app.js +0 -10
- package/dist/src/rest/methods/booking.js +0 -6
- package/dist/src/rest/methods/bucket.js +0 -22
- package/dist/src/rest/methods/conversation.js +0 -28
- package/dist/src/rest/methods/file.js +0 -11
- package/dist/src/rest/methods/group.js +0 -22
- package/dist/src/rest/methods/idLookup.js +0 -12
- package/dist/src/rest/methods/notification.js +0 -57
- package/dist/src/rest/methods/notificationSettings.js +0 -19
- package/dist/src/rest/methods/property.js +0 -17
- package/dist/src/rest/methods/registrationCode.js +0 -24
- package/dist/src/rest/methods/serviceProvider.js +0 -9
- package/dist/src/rest/methods/ticket.js +0 -34
- package/dist/src/rest/methods/unit.js +0 -95
- package/dist/src/rest/methods/user.js +0 -134
- package/dist/src/rest/methods/userRelation.js +0 -22
- package/dist/src/rest/methods/utilisationPeriod.js +0 -49
- package/dist/src/rest/patch.js +0 -3
- package/dist/src/rest/post.js +0 -3
- package/dist/src/rest/put.js +0 -3
- package/dist/src/rest/request.js +0 -159
- package/dist/src/rest/types.js +0 -62
- package/dist/src/utils/environment.js +0 -1
- package/dist/src/utils/functional.js +0 -14
- package/dist/src/utils/logger.js +0 -15
- package/dist/src/utils/object.js +0 -6
- package/dist/src/utils/queryString.js +0 -13
- package/dist/src/utils/random.js +0 -7
- package/dist/src/utils/sleep.js +0 -3
- package/dist/src/utils/string.js +0 -6
- package/dist/src/utils/stringToDate.js +0 -6
- package/dist/src/utils/upload.js +0 -24
package/dist/src/rest/request.js
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import Bottleneck from 'bottleneck';
|
|
2
|
-
import { QUEUE_CONCURRENCY, QUEUE_DELAY, QUEUE_RESERVOIR, QUEUE_RESERVOIR_REFILL_INTERVAL, USER_AGENT, } from '../constants';
|
|
3
|
-
import maybeUpdateToken from '../oauth/maybeUpdateToken';
|
|
4
|
-
import { environment } from '../utils/environment';
|
|
5
|
-
import { clearIntervalFunction, until } from '../utils/functional';
|
|
6
|
-
import makeLogger from '../utils/logger';
|
|
7
|
-
import { buildQueryString } from '../utils/queryString';
|
|
8
|
-
import sleep from '../utils/sleep';
|
|
9
|
-
const requestLogger = makeLogger('REST API Request');
|
|
10
|
-
const responseLogger = makeLogger('REST API Response');
|
|
11
|
-
const RETRYABLE_STATUS_CODES = [401, 408, 429, 502, 503, 504];
|
|
12
|
-
const TOKEN_REFRESH_STATUS_CODES = [401];
|
|
13
|
-
const queue = new Bottleneck({
|
|
14
|
-
maxConcurrent: QUEUE_CONCURRENCY,
|
|
15
|
-
minTime: QUEUE_DELAY,
|
|
16
|
-
reservoir: QUEUE_RESERVOIR,
|
|
17
|
-
});
|
|
18
|
-
const refillIntervalSet = new Set();
|
|
19
|
-
function isFormData(body) {
|
|
20
|
-
return typeof body !== 'undefined' && body.formData !== undefined;
|
|
21
|
-
}
|
|
22
|
-
function refillReservoir() {
|
|
23
|
-
if (refillIntervalSet.size === 0) {
|
|
24
|
-
const interval = setInterval(async () => {
|
|
25
|
-
const reservoir = (await queue.currentReservoir());
|
|
26
|
-
if (queue.empty() && (await queue.running()) === 0 && reservoir > 10) {
|
|
27
|
-
return ((await queue.incrementReservoir(1)) &&
|
|
28
|
-
clearIntervalFunction(interval) &&
|
|
29
|
-
refillIntervalSet.delete(interval));
|
|
30
|
-
}
|
|
31
|
-
return reservoir < QUEUE_RESERVOIR
|
|
32
|
-
? queue.incrementReservoir(1)
|
|
33
|
-
: clearIntervalFunction(interval) && refillIntervalSet.delete(interval);
|
|
34
|
-
}, QUEUE_RESERVOIR_REFILL_INTERVAL);
|
|
35
|
-
return refillIntervalSet.add(interval);
|
|
36
|
-
}
|
|
37
|
-
return refillIntervalSet;
|
|
38
|
-
}
|
|
39
|
-
async function makeResultFromResponse(response) {
|
|
40
|
-
if (RETRYABLE_STATUS_CODES.includes(response.status)) {
|
|
41
|
-
return response.clone();
|
|
42
|
-
}
|
|
43
|
-
if (!response.ok) {
|
|
44
|
-
return new Error(`${response.status} ${response.statusText}\n\n${await response.text()}`);
|
|
45
|
-
}
|
|
46
|
-
if (response.headers.get('content-type') !== 'application/json' &&
|
|
47
|
-
response.status !== 204) {
|
|
48
|
-
return new Error(`Response content type was "${response.headers.get('content-type')}" but expected JSON`);
|
|
49
|
-
}
|
|
50
|
-
return {
|
|
51
|
-
body: response.status === 204 ? '' : await response.json(),
|
|
52
|
-
status: response.status,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
export function responseWasSuccessful(response) {
|
|
56
|
-
return !RETRYABLE_STATUS_CODES.includes(response.status);
|
|
57
|
-
}
|
|
58
|
-
export function makeApiRequest(oauthTokenStore, oauthTokenRequester, options, httpMethod, apiMethod, payload, returnRawResultObject) {
|
|
59
|
-
return async (previousResult, retryCount) => {
|
|
60
|
-
if (retryCount > 0) {
|
|
61
|
-
if (retryCount > options.requestMaxRetries) {
|
|
62
|
-
const error = `Maximum number of retries reached while retrying ${previousResult.method} request ${previousResult.path}.`;
|
|
63
|
-
requestLogger.error(error);
|
|
64
|
-
throw new Error(error);
|
|
65
|
-
}
|
|
66
|
-
requestLogger.warn(`Warning: encountered ${previousResult.status}. Retrying ${previousResult.method} request ${previousResult.path} (retry #${retryCount}).`);
|
|
67
|
-
await sleep(Math.ceil(Math.random() *
|
|
68
|
-
options.requestBackOffInterval *
|
|
69
|
-
2 ** retryCount));
|
|
70
|
-
}
|
|
71
|
-
await maybeUpdateToken(oauthTokenStore, oauthTokenRequester, options, retryCount > 0 &&
|
|
72
|
-
TOKEN_REFRESH_STATUS_CODES.includes(previousResult.status));
|
|
73
|
-
const payloadQuery = payload?.query
|
|
74
|
-
? (apiMethod.includes('?') ? '&' : '?') + buildQueryString(payload.query)
|
|
75
|
-
: '';
|
|
76
|
-
const url = `${options.apiUrl}/api${apiMethod}${payloadQuery}`;
|
|
77
|
-
if (!oauthTokenStore.get('accessToken')) {
|
|
78
|
-
throw new Error(`Unable to get OAuth2 access token. Error trying to call endpoint: ${url}`);
|
|
79
|
-
}
|
|
80
|
-
try {
|
|
81
|
-
return (refillReservoir() &&
|
|
82
|
-
(await queue.schedule(async () => {
|
|
83
|
-
const method = httpMethod.toUpperCase();
|
|
84
|
-
const body = payload?.body;
|
|
85
|
-
const hasForm = isFormData(body);
|
|
86
|
-
const form = isFormData(body) ? body.formData : {};
|
|
87
|
-
const formData = Object.entries(form).reduce((previous, [name, value]) => {
|
|
88
|
-
if (Array.isArray(value)) {
|
|
89
|
-
const [content, filename] = value;
|
|
90
|
-
const blobContent = content instanceof Blob ? content : new Blob([content]);
|
|
91
|
-
previous.append(name, blobContent, filename);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
previous.append(name, value);
|
|
95
|
-
}
|
|
96
|
-
return previous;
|
|
97
|
-
}, new FormData());
|
|
98
|
-
const headers = {
|
|
99
|
-
accept: 'application/json',
|
|
100
|
-
authorization: `Bearer ${oauthTokenStore.get('accessToken')}`,
|
|
101
|
-
'X-Allthings-Caller': `${options.serviceName
|
|
102
|
-
? options.serviceName
|
|
103
|
-
:
|
|
104
|
-
environment.SERVICE_NAME
|
|
105
|
-
? environment.SERVICE_NAME
|
|
106
|
-
: 'unknown service name'} --- clientID ${options.clientId?.split('_')[0] ??
|
|
107
|
-
options.clientId ??
|
|
108
|
-
'no client id present'}`,
|
|
109
|
-
...(hasForm ? {} : { 'content-type': 'application/json' }),
|
|
110
|
-
...(globalThis.window === undefined &&
|
|
111
|
-
typeof document === 'undefined' && { 'user-agent': USER_AGENT }),
|
|
112
|
-
...payload?.headers,
|
|
113
|
-
};
|
|
114
|
-
if (environment.LOG_REQUEST) {
|
|
115
|
-
console.log({ sdkLogs: { method, url } });
|
|
116
|
-
}
|
|
117
|
-
requestLogger.log(method, url, {
|
|
118
|
-
body,
|
|
119
|
-
headers,
|
|
120
|
-
});
|
|
121
|
-
const requestBody = {
|
|
122
|
-
body: hasForm ? formData : JSON.stringify(body),
|
|
123
|
-
};
|
|
124
|
-
const response = await fetch(url, {
|
|
125
|
-
cache: 'no-cache',
|
|
126
|
-
credentials: 'omit',
|
|
127
|
-
headers,
|
|
128
|
-
method,
|
|
129
|
-
mode: 'cors',
|
|
130
|
-
...(hasForm || body ? requestBody : {}),
|
|
131
|
-
});
|
|
132
|
-
const result = await makeResultFromResponse(response);
|
|
133
|
-
responseLogger.log(method, url, result instanceof Error
|
|
134
|
-
? { error: result }
|
|
135
|
-
: {
|
|
136
|
-
body: result.body,
|
|
137
|
-
status: response.status,
|
|
138
|
-
});
|
|
139
|
-
return result instanceof Error && returnRawResultObject
|
|
140
|
-
? {
|
|
141
|
-
body: result.message,
|
|
142
|
-
status: response.status,
|
|
143
|
-
}
|
|
144
|
-
: result;
|
|
145
|
-
})));
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
return error;
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
export default async function request(oauthTokenStore, oauthTokenRequester, options, httpMethod, apiMethod, payload, returnRawResultObject) {
|
|
153
|
-
const result = await until(responseWasSuccessful, makeApiRequest(oauthTokenStore, oauthTokenRequester, options, httpMethod, apiMethod, payload, returnRawResultObject));
|
|
154
|
-
if (result instanceof Error) {
|
|
155
|
-
requestLogger.log('Request Error', result, payload);
|
|
156
|
-
throw result;
|
|
157
|
-
}
|
|
158
|
-
return returnRawResultObject ? result : result.body;
|
|
159
|
-
}
|
package/dist/src/rest/types.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
export var EnumResource;
|
|
2
|
-
(function (EnumResource) {
|
|
3
|
-
EnumResource["group"] = "group";
|
|
4
|
-
EnumResource["property"] = "property";
|
|
5
|
-
EnumResource["serviceProvider"] = "propertyManager";
|
|
6
|
-
EnumResource["registrationCode"] = "registrationCode";
|
|
7
|
-
EnumResource["unit"] = "unit";
|
|
8
|
-
EnumResource["user"] = "user";
|
|
9
|
-
EnumResource["utilisationPeriod"] = "utilisationPeriod";
|
|
10
|
-
})(EnumResource || (EnumResource = {}));
|
|
11
|
-
export var EnumCountryCode;
|
|
12
|
-
(function (EnumCountryCode) {
|
|
13
|
-
EnumCountryCode["CH"] = "CH";
|
|
14
|
-
EnumCountryCode["DE"] = "DE";
|
|
15
|
-
EnumCountryCode["FR"] = "FR";
|
|
16
|
-
EnumCountryCode["IT"] = "IT";
|
|
17
|
-
EnumCountryCode["NL"] = "NL";
|
|
18
|
-
EnumCountryCode["PT"] = "PT";
|
|
19
|
-
EnumCountryCode["US"] = "US";
|
|
20
|
-
})(EnumCountryCode || (EnumCountryCode = {}));
|
|
21
|
-
export var EnumLocale;
|
|
22
|
-
(function (EnumLocale) {
|
|
23
|
-
EnumLocale["ch_de"] = "ch_DE";
|
|
24
|
-
EnumLocale["ch_fr"] = "ch_FR";
|
|
25
|
-
EnumLocale["ch_it"] = "ch_it";
|
|
26
|
-
EnumLocale["de_DE"] = "de_DE";
|
|
27
|
-
EnumLocale["it_IT"] = "it_IT";
|
|
28
|
-
EnumLocale["fr_FR"] = "fr_FR";
|
|
29
|
-
EnumLocale["pt_PT"] = "pt_PT";
|
|
30
|
-
EnumLocale["en_US"] = "en_US";
|
|
31
|
-
})(EnumLocale || (EnumLocale = {}));
|
|
32
|
-
export var EnumTimezone;
|
|
33
|
-
(function (EnumTimezone) {
|
|
34
|
-
EnumTimezone["EuropeBerlin"] = "Europe/Berlin";
|
|
35
|
-
EnumTimezone["EuropeLondon"] = "Europe/London";
|
|
36
|
-
EnumTimezone["EuropeSofia"] = "Europe/Sofia";
|
|
37
|
-
EnumTimezone["EuropeZurich"] = "Europe/Zurich";
|
|
38
|
-
EnumTimezone["UTC"] = "UTC";
|
|
39
|
-
})(EnumTimezone || (EnumTimezone = {}));
|
|
40
|
-
export var EnumServiceProviderType;
|
|
41
|
-
(function (EnumServiceProviderType) {
|
|
42
|
-
EnumServiceProviderType["propertyManager"] = "property-manager";
|
|
43
|
-
EnumServiceProviderType["craftspeople"] = "craftspeople";
|
|
44
|
-
})(EnumServiceProviderType || (EnumServiceProviderType = {}));
|
|
45
|
-
export var EnumCommunicationMethodType;
|
|
46
|
-
(function (EnumCommunicationMethodType) {
|
|
47
|
-
EnumCommunicationMethodType["email"] = "email";
|
|
48
|
-
})(EnumCommunicationMethodType || (EnumCommunicationMethodType = {}));
|
|
49
|
-
export var EnumInputChannel;
|
|
50
|
-
(function (EnumInputChannel) {
|
|
51
|
-
EnumInputChannel["APP"] = "app";
|
|
52
|
-
EnumInputChannel["COCKPIT"] = "cockpit";
|
|
53
|
-
EnumInputChannel["CRAFTSMEN"] = "craftsmen";
|
|
54
|
-
EnumInputChannel["EMAIL"] = "email";
|
|
55
|
-
EnumInputChannel["PHONE"] = "phone";
|
|
56
|
-
EnumInputChannel["WHATS_APP"] = "whats_app";
|
|
57
|
-
})(EnumInputChannel || (EnumInputChannel = {}));
|
|
58
|
-
export var EnumLookupUserType;
|
|
59
|
-
(function (EnumLookupUserType) {
|
|
60
|
-
EnumLookupUserType["agent"] = "agent";
|
|
61
|
-
EnumLookupUserType["tenant"] = "tenant";
|
|
62
|
-
})(EnumLookupUserType || (EnumLookupUserType = {}));
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const environment = typeof process !== 'undefined' && process.env ? process.env : {};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export const partial = (function_, ...partialArguments) => (...arguments_) => function_(...partialArguments, ...arguments_);
|
|
2
|
-
export function times(function_, n) {
|
|
3
|
-
return [...new Array(n)].map(function_);
|
|
4
|
-
}
|
|
5
|
-
export async function until(predicate, transformer, initialValue, iterationCount = 0) {
|
|
6
|
-
const transformed = await transformer(initialValue, iterationCount);
|
|
7
|
-
return (await predicate(transformed, iterationCount))
|
|
8
|
-
? transformed
|
|
9
|
-
: until(predicate, transformer, transformed, iterationCount + 1);
|
|
10
|
-
}
|
|
11
|
-
export function clearIntervalFunction(intervalId) {
|
|
12
|
-
clearInterval(intervalId);
|
|
13
|
-
return true;
|
|
14
|
-
}
|
package/dist/src/utils/logger.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { environment } from './environment';
|
|
2
|
-
const SUBSCRIPTIONS = environment.DEBUG?.split(',').map((item) => item.trim()) || [];
|
|
3
|
-
export default function makeLogger(name) {
|
|
4
|
-
return ['log', 'info', 'warn', 'error'].reduce((logger, type) => ({
|
|
5
|
-
...logger,
|
|
6
|
-
[type]: function log(...logs) {
|
|
7
|
-
if (SUBSCRIPTIONS.includes('*') ||
|
|
8
|
-
SUBSCRIPTIONS.includes(name) ||
|
|
9
|
-
SUBSCRIPTIONS.includes(name.toLocaleLowerCase())) {
|
|
10
|
-
console[type](`${name}:`, ...logs);
|
|
11
|
-
}
|
|
12
|
-
return true;
|
|
13
|
-
},
|
|
14
|
-
}), {});
|
|
15
|
-
}
|
package/dist/src/utils/object.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export function buildQueryString(params) {
|
|
2
|
-
const usp = new URLSearchParams();
|
|
3
|
-
for (const [key, value] of Object.entries(params).sort(([a], [b]) => a.localeCompare(b))) {
|
|
4
|
-
if (value !== undefined && value !== null && value !== '') {
|
|
5
|
-
usp.append(key, String(value));
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
return usp.toString();
|
|
9
|
-
}
|
|
10
|
-
export function parseQueryString(query) {
|
|
11
|
-
const cleaned = query.startsWith('#') ? query.slice(1) : query;
|
|
12
|
-
return Object.fromEntries(new URLSearchParams(cleaned));
|
|
13
|
-
}
|
package/dist/src/utils/random.js
DELETED
package/dist/src/utils/sleep.js
DELETED
package/dist/src/utils/string.js
DELETED
package/dist/src/utils/upload.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export const createManyFiles = async (attachments, apiClient) => {
|
|
2
|
-
const responses = await Promise.all(attachments.map(async (attachment) => {
|
|
3
|
-
try {
|
|
4
|
-
const result = await apiClient.fileCreate({
|
|
5
|
-
file: attachment.content,
|
|
6
|
-
name: attachment.filename,
|
|
7
|
-
});
|
|
8
|
-
return result;
|
|
9
|
-
}
|
|
10
|
-
catch (error) {
|
|
11
|
-
return error instanceof Error ? error : new Error(String(error));
|
|
12
|
-
}
|
|
13
|
-
}));
|
|
14
|
-
return responses;
|
|
15
|
-
};
|
|
16
|
-
export async function createManyFilesSorted(files, client) {
|
|
17
|
-
const result = await createManyFiles(files, client);
|
|
18
|
-
return {
|
|
19
|
-
error: result.filter((item) => item instanceof Error),
|
|
20
|
-
success: result
|
|
21
|
-
.filter((item) => !(item instanceof Error))
|
|
22
|
-
.map((item) => item.id),
|
|
23
|
-
};
|
|
24
|
-
}
|