@unito/integration-sdk 0.1.11 → 1.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/src/handler.d.ts +39 -0
- package/dist/src/handler.js +9 -0
- package/dist/src/httpErrors.d.ts +29 -0
- package/dist/src/httpErrors.js +30 -0
- package/dist/src/index.cjs +274 -16
- package/dist/src/index.d.ts +1 -0
- package/dist/src/integration.d.ts +49 -0
- package/dist/src/integration.js +51 -0
- package/dist/src/middlewares/filters.d.ts +11 -2
- package/dist/src/middlewares/secrets.d.ts +5 -0
- package/dist/src/middlewares/signal.d.ts +15 -0
- package/dist/src/middlewares/signal.js +22 -0
- package/dist/src/resources/cache.d.ts +51 -1
- package/dist/src/resources/cache.js +51 -1
- package/dist/src/resources/context.d.ts +42 -13
- package/dist/src/resources/logger.d.ts +17 -0
- package/dist/src/resources/logger.js +17 -0
- package/dist/src/resources/provider.d.ts +90 -5
- package/dist/src/resources/provider.js +92 -11
- package/dist/test/middlewares/signal.test.d.ts +1 -0
- package/dist/test/middlewares/signal.test.js +20 -0
- package/dist/test/resources/provider.test.js +116 -21
- package/package.json +4 -4
- package/src/handler.ts +48 -0
- package/src/httpErrors.ts +30 -0
- package/src/index.ts +1 -0
- package/src/integration.ts +51 -0
- package/src/middlewares/filters.ts +11 -2
- package/src/middlewares/secrets.ts +5 -0
- package/src/middlewares/signal.ts +41 -0
- package/src/resources/cache.ts +51 -1
- package/src/resources/context.ts +50 -33
- package/src/resources/logger.ts +17 -0
- package/src/resources/provider.ts +115 -16
- package/test/middlewares/signal.test.ts +28 -0
- package/test/resources/provider.test.ts +122 -21
package/dist/src/handler.d.ts
CHANGED
|
@@ -3,40 +3,79 @@ import * as API from '@unito/integration-api';
|
|
|
3
3
|
import { GetItemContext, GetCollectionContext, CreateItemContext, UpdateItemContext, DeleteItemContext, GetCredentialAccountContext, ParseWebhooksContext, UpdateWebhookSubscriptionsContext, AcknowledgeWebhooksContext } from './resources/context.js';
|
|
4
4
|
/**
|
|
5
5
|
* Handler called to get an individual item.
|
|
6
|
+
*
|
|
7
|
+
* @param context {@link GetItemContext}
|
|
8
|
+
* @returns The requested {@link API.Item}.
|
|
6
9
|
*/
|
|
7
10
|
export type GetItemHandler = (context: GetItemContext<any, any>) => Promise<API.Item>;
|
|
8
11
|
/**
|
|
9
12
|
* Handler called to retrieve a collection of items.
|
|
13
|
+
*
|
|
14
|
+
* @param context {@link GetCollectionContext}
|
|
15
|
+
* @return An {@link API.Collection} containing requested items and a link to the next page, if applicable.
|
|
10
16
|
*/
|
|
11
17
|
export type GetCollectionHandler = (context: GetCollectionContext<any, any>) => Promise<API.Collection>;
|
|
12
18
|
/**
|
|
13
19
|
* Handler called to create an item.
|
|
20
|
+
*
|
|
21
|
+
* @param context {@link CreateItemContext}
|
|
22
|
+
* @returns An {@link API.ItemSummary} containing a path to the created item.
|
|
14
23
|
*/
|
|
15
24
|
export type CreateItemHandler = (context: CreateItemContext<any, any, any>) => Promise<API.ItemSummary>;
|
|
16
25
|
/**
|
|
17
26
|
* Handler called to update an item.
|
|
27
|
+
*
|
|
28
|
+
* @param context {@link UpdateItemContext}
|
|
29
|
+
* @returns The updated {@link API.Item}.
|
|
18
30
|
*/
|
|
19
31
|
export type UpdateItemHandler = (context: UpdateItemContext<any, any, any>) => Promise<API.Item>;
|
|
20
32
|
/**
|
|
21
33
|
* Handler called to delete an item.
|
|
34
|
+
*
|
|
35
|
+
* @param context {@link DeleteItemContext}
|
|
22
36
|
*/
|
|
23
37
|
export type DeleteItemHandler = (context: DeleteItemContext<any, any>) => Promise<void>;
|
|
24
38
|
/**
|
|
25
39
|
* Handler called to retrieve the account details associated with the credentials.
|
|
40
|
+
*
|
|
41
|
+
* @param context {@link GetCredentialAccountContext}
|
|
42
|
+
* @returns The {@link API.CredentialAccount} associated with the credentials.
|
|
26
43
|
*/
|
|
27
44
|
export type GetCredentialAccountHandler = (context: GetCredentialAccountContext<any, any>) => Promise<API.CredentialAccount>;
|
|
28
45
|
/**
|
|
29
46
|
* Handler called to parse the content of an incoming webhook.
|
|
47
|
+
*
|
|
48
|
+
* @param context {@link ParseWebhooksContext}
|
|
49
|
+
* @returns The parsed content of the webhook as a {@link API.WebhookParseResponsePayload}.
|
|
30
50
|
*/
|
|
31
51
|
export type ParseWebhooksHandler = (context: ParseWebhooksContext<any, any, any>) => Promise<API.WebhookParseResponsePayload>;
|
|
32
52
|
/**
|
|
33
53
|
* Handler called to subscribe or unsubscribe to a particular webhook.
|
|
54
|
+
*
|
|
55
|
+
* @param context {@link UpdateWebhookSubscriptionsContext}
|
|
34
56
|
*/
|
|
35
57
|
export type UpdateWebhookSubscriptionsHandler = (context: UpdateWebhookSubscriptionsContext<any, any, any>) => Promise<void>;
|
|
36
58
|
/**
|
|
37
59
|
* Handler called to acknowledge the reception of a webhook.
|
|
60
|
+
*
|
|
61
|
+
* @param context {@link AcknowledgeWebhooksContext}
|
|
62
|
+
* @returns The {@link API.WebhookAcknowledgeResponsePayload} to be sent back to the webhook provider.
|
|
38
63
|
*/
|
|
39
64
|
export type AcknowledgeWebhooksHandler = (context: AcknowledgeWebhooksContext<any, any, any>) => Promise<API.WebhookAcknowledgeResponsePayload>;
|
|
65
|
+
/**
|
|
66
|
+
* Defines the implementation of the operations available for a given `Item`.
|
|
67
|
+
* - In some cases (e.g. defining the `root` or {@link https://dev.unito.io/docs/connectors/apiSpecification/credentialAccount | me}
|
|
68
|
+
* handlers), only a {@link GetItemHandler | getItem} handler is necessary.
|
|
69
|
+
* - In most cases, you will want to define {@link GetCollectionHandler | getCollection},
|
|
70
|
+
* most likely {@link GetItemHandler | getItem}, and any other handlers relevant to the item.
|
|
71
|
+
*
|
|
72
|
+
* The `ItemHandlers` object can contain any of the following:
|
|
73
|
+
* - {@link GetItemHandler | getItem}: A handler called to get an individual item.
|
|
74
|
+
* - {@link GetCollectionHandler | getCollection}: A handler called to retrieve a collection of items.
|
|
75
|
+
* - {@link CreateItemHandler | createItem}: A handler called to create an item.
|
|
76
|
+
* - {@link UpdateItemHandler | updateItem}: A handler called to update an item.
|
|
77
|
+
* - {@link DeleteItemHandler | deleteItem}: A handler called to delete an item.
|
|
78
|
+
*/
|
|
40
79
|
export type ItemHandlers = {
|
|
41
80
|
getItem?: GetItemHandler;
|
|
42
81
|
getCollection?: GetCollectionHandler;
|
package/dist/src/handler.js
CHANGED
|
@@ -94,6 +94,7 @@ export class Handler {
|
|
|
94
94
|
selects: res.locals.selects,
|
|
95
95
|
filters: res.locals.filters,
|
|
96
96
|
logger: res.locals.logger,
|
|
97
|
+
signal: res.locals.signal,
|
|
97
98
|
params: req.params,
|
|
98
99
|
query: req.query,
|
|
99
100
|
});
|
|
@@ -113,6 +114,7 @@ export class Handler {
|
|
|
113
114
|
secrets: res.locals.secrets,
|
|
114
115
|
body: req.body,
|
|
115
116
|
logger: res.locals.logger,
|
|
117
|
+
signal: res.locals.signal,
|
|
116
118
|
params: req.params,
|
|
117
119
|
query: req.query,
|
|
118
120
|
});
|
|
@@ -130,6 +132,7 @@ export class Handler {
|
|
|
130
132
|
credentials: res.locals.credentials,
|
|
131
133
|
secrets: res.locals.secrets,
|
|
132
134
|
logger: res.locals.logger,
|
|
135
|
+
signal: res.locals.signal,
|
|
133
136
|
params: req.params,
|
|
134
137
|
query: req.query,
|
|
135
138
|
});
|
|
@@ -149,6 +152,7 @@ export class Handler {
|
|
|
149
152
|
secrets: res.locals.secrets,
|
|
150
153
|
body: req.body,
|
|
151
154
|
logger: res.locals.logger,
|
|
155
|
+
signal: res.locals.signal,
|
|
152
156
|
params: req.params,
|
|
153
157
|
query: req.query,
|
|
154
158
|
});
|
|
@@ -166,6 +170,7 @@ export class Handler {
|
|
|
166
170
|
credentials: res.locals.credentials,
|
|
167
171
|
secrets: res.locals.secrets,
|
|
168
172
|
logger: res.locals.logger,
|
|
173
|
+
signal: res.locals.signal,
|
|
169
174
|
params: req.params,
|
|
170
175
|
query: req.query,
|
|
171
176
|
});
|
|
@@ -183,6 +188,7 @@ export class Handler {
|
|
|
183
188
|
credentials: res.locals.credentials,
|
|
184
189
|
secrets: res.locals.secrets,
|
|
185
190
|
logger: res.locals.logger,
|
|
191
|
+
signal: res.locals.signal,
|
|
186
192
|
params: req.params,
|
|
187
193
|
query: req.query,
|
|
188
194
|
});
|
|
@@ -197,6 +203,7 @@ export class Handler {
|
|
|
197
203
|
const response = await handler({
|
|
198
204
|
secrets: res.locals.secrets,
|
|
199
205
|
logger: res.locals.logger,
|
|
206
|
+
signal: res.locals.signal,
|
|
200
207
|
params: req.params,
|
|
201
208
|
query: req.query,
|
|
202
209
|
body: req.body,
|
|
@@ -212,6 +219,7 @@ export class Handler {
|
|
|
212
219
|
const response = await handler({
|
|
213
220
|
secrets: res.locals.secrets,
|
|
214
221
|
logger: res.locals.logger,
|
|
222
|
+
signal: res.locals.signal,
|
|
215
223
|
params: req.params,
|
|
216
224
|
query: req.query,
|
|
217
225
|
body: req.body,
|
|
@@ -232,6 +240,7 @@ export class Handler {
|
|
|
232
240
|
credentials: res.locals.credentials,
|
|
233
241
|
body: req.body,
|
|
234
242
|
logger: res.locals.logger,
|
|
243
|
+
signal: res.locals.signal,
|
|
235
244
|
params: req.params,
|
|
236
245
|
query: req.query,
|
|
237
246
|
});
|
package/dist/src/httpErrors.d.ts
CHANGED
|
@@ -1,25 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error class meant to be returned by integrations in case of exceptions. These errors will be caught and handled
|
|
3
|
+
* appropriately. Any other error would result in an unhandled server error accompanied by a 500 status code.
|
|
4
|
+
*
|
|
5
|
+
* @field message - The error message
|
|
6
|
+
* @field status - The HTTP status code to return
|
|
7
|
+
*/
|
|
1
8
|
export declare class HttpError extends Error {
|
|
2
9
|
readonly status: number;
|
|
3
10
|
constructor(message: string, status: number);
|
|
4
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Used to generate a 400 Bad Request. Usually used when something is missing to properly handle the request.
|
|
14
|
+
*/
|
|
5
15
|
export declare class BadRequestError extends HttpError {
|
|
6
16
|
constructor(message?: string);
|
|
7
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Used to generate a 401 Unauthorized. Usually used when the credentials are missing or invalid.
|
|
20
|
+
*/
|
|
8
21
|
export declare class UnauthorizedError extends HttpError {
|
|
9
22
|
constructor(message?: string);
|
|
10
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Used to generate a 404 Not Found. Usually used when the requested `Item` is not found.
|
|
26
|
+
*/
|
|
11
27
|
export declare class NotFoundError extends HttpError {
|
|
12
28
|
constructor(message?: string);
|
|
13
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Used to generate a 408 Timeout Error. Usually used when the call length exceeds the received Operation Deadline.
|
|
32
|
+
*/
|
|
14
33
|
export declare class TimeoutError extends HttpError {
|
|
15
34
|
constructor(message?: string);
|
|
16
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Used to generate a 410 Resource Gone.
|
|
38
|
+
*/
|
|
17
39
|
export declare class ResourceGoneError extends HttpError {
|
|
18
40
|
constructor(message?: string);
|
|
19
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Used to generate a 422 Unprocessable Entity. Usually used when an operation is invalid.
|
|
44
|
+
*/
|
|
20
45
|
export declare class UnprocessableEntityError extends HttpError {
|
|
21
46
|
constructor(message?: string);
|
|
22
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Used to generate a 429 Unprocessable Entity. Usually used when an operation triggers or would trigger a rate limit
|
|
50
|
+
* error on the provider's side.
|
|
51
|
+
*/
|
|
23
52
|
export declare class RateLimitExceededError extends HttpError {
|
|
24
53
|
constructor(message?: string);
|
|
25
54
|
}
|
package/dist/src/httpErrors.js
CHANGED
|
@@ -1,40 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error class meant to be returned by integrations in case of exceptions. These errors will be caught and handled
|
|
3
|
+
* appropriately. Any other error would result in an unhandled server error accompanied by a 500 status code.
|
|
4
|
+
*
|
|
5
|
+
* @field message - The error message
|
|
6
|
+
* @field status - The HTTP status code to return
|
|
7
|
+
*/
|
|
1
8
|
export class HttpError extends Error {
|
|
2
9
|
status;
|
|
3
10
|
constructor(message, status) {
|
|
4
11
|
super(message);
|
|
5
12
|
this.status = status;
|
|
13
|
+
this.name = this.constructor.name;
|
|
6
14
|
}
|
|
7
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Used to generate a 400 Bad Request. Usually used when something is missing to properly handle the request.
|
|
18
|
+
*/
|
|
8
19
|
export class BadRequestError extends HttpError {
|
|
9
20
|
constructor(message) {
|
|
10
21
|
super(message || 'Bad request', 400);
|
|
11
22
|
}
|
|
12
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Used to generate a 401 Unauthorized. Usually used when the credentials are missing or invalid.
|
|
26
|
+
*/
|
|
13
27
|
export class UnauthorizedError extends HttpError {
|
|
14
28
|
constructor(message) {
|
|
15
29
|
super(message || 'Unauthorized', 401);
|
|
16
30
|
}
|
|
17
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Used to generate a 404 Not Found. Usually used when the requested `Item` is not found.
|
|
34
|
+
*/
|
|
18
35
|
export class NotFoundError extends HttpError {
|
|
19
36
|
constructor(message) {
|
|
20
37
|
super(message || 'Not found', 404);
|
|
21
38
|
}
|
|
22
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Used to generate a 408 Timeout Error. Usually used when the call length exceeds the received Operation Deadline.
|
|
42
|
+
*/
|
|
23
43
|
export class TimeoutError extends HttpError {
|
|
24
44
|
constructor(message) {
|
|
25
45
|
super(message || 'Not found', 408);
|
|
26
46
|
}
|
|
27
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Used to generate a 410 Resource Gone.
|
|
50
|
+
*/
|
|
28
51
|
export class ResourceGoneError extends HttpError {
|
|
29
52
|
constructor(message) {
|
|
30
53
|
super(message || 'Resource gone or unavailable', 410);
|
|
31
54
|
}
|
|
32
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Used to generate a 422 Unprocessable Entity. Usually used when an operation is invalid.
|
|
58
|
+
*/
|
|
33
59
|
export class UnprocessableEntityError extends HttpError {
|
|
34
60
|
constructor(message) {
|
|
35
61
|
super(message || 'Unprocessable Entity', 422);
|
|
36
62
|
}
|
|
37
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Used to generate a 429 Unprocessable Entity. Usually used when an operation triggers or would trigger a rate limit
|
|
66
|
+
* error on the provider's side.
|
|
67
|
+
*/
|
|
38
68
|
export class RateLimitExceededError extends HttpError {
|
|
39
69
|
constructor(message) {
|
|
40
70
|
super(message || 'Rate Limit Exceeded', 429);
|