@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/integration.js
CHANGED
|
@@ -3,6 +3,7 @@ import { InvalidHandler } from './errors.js';
|
|
|
3
3
|
import correlationIdMiddleware from './middlewares/correlationId.js';
|
|
4
4
|
import loggerMiddleware from './middlewares/logger.js';
|
|
5
5
|
import credentialsMiddleware from './middlewares/credentials.js';
|
|
6
|
+
import signalMiddleware from './middlewares/signal.js';
|
|
6
7
|
import secretsMiddleware from './middlewares/secrets.js';
|
|
7
8
|
import selectsMiddleware from './middlewares/selects.js';
|
|
8
9
|
import errorsMiddleware from './middlewares/errors.js';
|
|
@@ -14,14 +15,56 @@ function printErrorMessage(message) {
|
|
|
14
15
|
console.error(`\x1b[31m Oops! Something went wrong! \x1b[0m`);
|
|
15
16
|
console.error(message);
|
|
16
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Main class for the Integration SDK providing an abstraction layer between the Integration's Graph definition
|
|
20
|
+
* and the underlying HTTP server.
|
|
21
|
+
*
|
|
22
|
+
* An `Integration` instance can have multiple handlers configured to handle different routes. Upon receiving a request,
|
|
23
|
+
* the Integration will parse the request to extract meaninful information, match the request to the appropriate handler
|
|
24
|
+
* method and forward that information in the form a {@link Context} object.
|
|
25
|
+
* The Integration also offer standardized error handling and logging to help you build a robust
|
|
26
|
+
* and reliable Integration.
|
|
27
|
+
*
|
|
28
|
+
* See our {@link https://dev.unito.io/docs/ | documentation} for more examples on how to build an integration.
|
|
29
|
+
*/
|
|
17
30
|
export default class Integration {
|
|
18
31
|
handlers;
|
|
19
32
|
instance = undefined;
|
|
20
33
|
port;
|
|
34
|
+
/**
|
|
35
|
+
* Creates a new Integration instance with default port set to 9200.
|
|
36
|
+
*
|
|
37
|
+
* @param options The {@link Options} to configure the Integration instance. Can be used to override the default port.
|
|
38
|
+
*/
|
|
21
39
|
constructor(options = {}) {
|
|
22
40
|
this.port = options.port || 9200;
|
|
23
41
|
this.handlers = [];
|
|
24
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Adds a group of common handlers to the integration.
|
|
45
|
+
*
|
|
46
|
+
* Handlers added to the integration can be one of the following:
|
|
47
|
+
* - `ItemHandlers`: A group of handlers defining the implementation of the Operations available for a given item.
|
|
48
|
+
* - `CredentialAccountHandlers`: A handler returning the CredentialAccount linked to the caller's credentials.
|
|
49
|
+
* - `ParseWebhookHandlers`: A handler parsing the content of an incoming webhook.
|
|
50
|
+
* - `WebhookSubscriptionHandlers`: A handler subscribing or unsubscribing to a particular webhook.
|
|
51
|
+
* - `AcknowledgeWebhookHandlers`: A handler acknowledging the reception of a webhook.
|
|
52
|
+
*
|
|
53
|
+
* To accomodate the fact that ItemHandlers may specify multiple operations, some at the collection level, some at the
|
|
54
|
+
* item level, we need a way to define the route for each of these operations.
|
|
55
|
+
* To achieve this, we assume that if the last part of the path is a variable, then it is the item identifier.
|
|
56
|
+
*
|
|
57
|
+
* @example The following path: `/trainer/:trainerId/pokemons/:pokemonId` will lead to the following
|
|
58
|
+
* routes:
|
|
59
|
+
* - getCollection will be called for `GET /trainer/:trainerId/pokemons/` requests
|
|
60
|
+
* - getItem will be called for `GET /trainer/:trainerId/pokemons/:pokemonId` requests
|
|
61
|
+
* - createItem will be called for `POST /trainer/:trainerId/pokemons/` requests
|
|
62
|
+
* - updateItem will be called for `PATCH /trainer/:trainerId/pokemons/:pokemonId` requests
|
|
63
|
+
* - deleteItem will be called for `DELETE /trainer/:trainerId/pokemons/:pokemonId` requests
|
|
64
|
+
*
|
|
65
|
+
* @param path The path to be used as Route for the handlers.
|
|
66
|
+
* @param handlers The Handlers definition.
|
|
67
|
+
*/
|
|
25
68
|
addHandler(path, handlers) {
|
|
26
69
|
if (this.instance) {
|
|
27
70
|
printErrorMessage(`
|
|
@@ -54,6 +97,13 @@ export default class Integration {
|
|
|
54
97
|
process.exit(1);
|
|
55
98
|
}
|
|
56
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Starts the server and listens on the specified port (default to 9200).
|
|
102
|
+
*
|
|
103
|
+
* @remarks
|
|
104
|
+
* This function should be called after all the handlers have been added to the integration
|
|
105
|
+
* and any other configuration is completed.
|
|
106
|
+
*/
|
|
57
107
|
start() {
|
|
58
108
|
// Express Server initialization
|
|
59
109
|
const app = express();
|
|
@@ -67,6 +117,7 @@ export default class Integration {
|
|
|
67
117
|
app.use(credentialsMiddleware);
|
|
68
118
|
app.use(secretsMiddleware);
|
|
69
119
|
app.use(selectsMiddleware);
|
|
120
|
+
app.use(signalMiddleware);
|
|
70
121
|
// Load handlers as needed.
|
|
71
122
|
if (this.handlers.length) {
|
|
72
123
|
for (const handler of this.handlers) {
|
|
@@ -7,10 +7,19 @@ declare global {
|
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Represents one filter parsed from the filters query param.
|
|
12
|
+
*
|
|
13
|
+
* Given a filters query param containing:
|
|
14
|
+
* `filters=[...],name=John,[...]`
|
|
15
|
+
*
|
|
16
|
+
* Filter for `name=John` will be:
|
|
17
|
+
* `{ field: 'name', operator: 'EQUAL', values: ['John'] }`
|
|
18
|
+
*/
|
|
19
|
+
export type Filter = {
|
|
11
20
|
field: string;
|
|
12
21
|
operator: OperatorType;
|
|
13
22
|
values: string[] | undefined;
|
|
14
|
-
}
|
|
23
|
+
};
|
|
15
24
|
declare const middleware: (req: Request, res: Response, next: NextFunction) => void;
|
|
16
25
|
export default middleware;
|
|
@@ -6,6 +6,11 @@ declare global {
|
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Represents the secrets parsed from the X-Unito-Secrets header.
|
|
11
|
+
*
|
|
12
|
+
* This is the decrypted payload of the secrets defined in the integration's configuration.
|
|
13
|
+
*/
|
|
9
14
|
export type Secrets = {
|
|
10
15
|
[keys: string]: unknown;
|
|
11
16
|
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
declare global {
|
|
3
|
+
namespace Express {
|
|
4
|
+
interface Locals {
|
|
5
|
+
/**
|
|
6
|
+
* An AbortSignal instantiated with the X-Unito-Operation-Deadline header. This header contains the timestamp
|
|
7
|
+
* after which Unito will consider the operation to be timed out. You can use this signal to abort any
|
|
8
|
+
* operation that would exceed this time frame.
|
|
9
|
+
*/
|
|
10
|
+
signal: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
declare const middleware: (req: Request, res: Response, next: NextFunction) => void;
|
|
15
|
+
export default middleware;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { TimeoutError } from '../httpErrors.js';
|
|
2
|
+
const OPERATION_DEADLINE_HEADER = 'X-Unito-Operation-Deadline';
|
|
3
|
+
const middleware = (req, res, next) => {
|
|
4
|
+
const operationDeadlineHeader = Number(req.header(OPERATION_DEADLINE_HEADER));
|
|
5
|
+
if (operationDeadlineHeader) {
|
|
6
|
+
// `operationDeadlineHeader` represents a timestamp in the future, in seconds.
|
|
7
|
+
// We need to convert it to a number of milliseconds.
|
|
8
|
+
const deadline = operationDeadlineHeader * 1000 - Date.now();
|
|
9
|
+
if (deadline > 0) {
|
|
10
|
+
res.locals.signal = AbortSignal.timeout(deadline);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
throw new TimeoutError('Request already timed out upon reception');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
// Default to 20s, which is the maximum time frame allowed for an operation by Unito.
|
|
18
|
+
res.locals.signal = AbortSignal.timeout(20000);
|
|
19
|
+
}
|
|
20
|
+
next();
|
|
21
|
+
};
|
|
22
|
+
export default middleware;
|
|
@@ -1,14 +1,64 @@
|
|
|
1
1
|
import { FetchingFunction, CachableValue } from 'cachette';
|
|
2
|
+
/**
|
|
3
|
+
* The Cache class provides caching capabilities that can be used across your integration.
|
|
4
|
+
* It can be backed by a Redis instance (by passing it a URL to the instance) or a local cache.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link Cache.create}
|
|
7
|
+
*/
|
|
2
8
|
export declare class Cache {
|
|
3
9
|
private cacheInstance;
|
|
4
10
|
private constructor();
|
|
11
|
+
/**
|
|
12
|
+
* Get or fetch a value
|
|
13
|
+
*
|
|
14
|
+
* @param key The key of the value to get
|
|
15
|
+
* @param ttl The time to live of the value in seconds.
|
|
16
|
+
* @param fetchFn The function that can retrieve the original value
|
|
17
|
+
* @param lockTtl Global distributed lock TTL (in seconds) protecting fetching.
|
|
18
|
+
* If undefined, 0 or falsy, locking is not preformed
|
|
19
|
+
* @param shouldCacheError A callback being passed errors, controlling whether
|
|
20
|
+
* to cache or not errors. Defaults to never cache.
|
|
21
|
+
*
|
|
22
|
+
* @returns The cached or fetched value
|
|
23
|
+
*/
|
|
5
24
|
getOrFetchValue<F extends FetchingFunction = FetchingFunction>(key: string, ttl: number, fetcher: F, lockTtl?: number, shouldCacheError?: (err: Error) => boolean): Promise<ReturnType<F>>;
|
|
25
|
+
/**
|
|
26
|
+
* Get a value from the cache.
|
|
27
|
+
*
|
|
28
|
+
* @param key The key of the value to get.
|
|
29
|
+
*
|
|
30
|
+
* @return The value associated with the key, or undefined if
|
|
31
|
+
* no such value exists.
|
|
32
|
+
*/
|
|
6
33
|
getValue(key: string): Promise<CachableValue>;
|
|
34
|
+
/**
|
|
35
|
+
* Set a value in the cache.
|
|
36
|
+
*
|
|
37
|
+
* @param key The key of the value to set.
|
|
38
|
+
* @param value The value to set.
|
|
39
|
+
* @param ttl The time to live of the value in seconds.
|
|
40
|
+
* By default, the value will not expire
|
|
41
|
+
*
|
|
42
|
+
* @return true if the value was stored, false otherwise.
|
|
43
|
+
*/
|
|
7
44
|
setValue(key: string, value: CachableValue, ttl?: number): Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* Delete a value from the cache.
|
|
47
|
+
* @param key — The key of the value to set.
|
|
48
|
+
*/
|
|
8
49
|
delValue(key: string): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Get the TTL of an entry, in ms
|
|
52
|
+
*
|
|
53
|
+
* @param key The key of the entry whose ttl to retrieve
|
|
54
|
+
*
|
|
55
|
+
* @return The remaining TTL on the entry, in ms.
|
|
56
|
+
* undefined if the entry does not exist.
|
|
57
|
+
* 0 if the entry does not expire.
|
|
58
|
+
*/
|
|
9
59
|
getTtl(key: string): Promise<number | undefined>;
|
|
10
60
|
/**
|
|
11
|
-
* Initializes a
|
|
61
|
+
* Initializes a Cache backed by the Redis instance at the provided url if present, or a LocalCache otherwise.
|
|
12
62
|
*
|
|
13
63
|
* @param redisUrl - The redis url to connect to (optional).
|
|
14
64
|
* @returns A cache instance.
|
|
@@ -1,28 +1,78 @@
|
|
|
1
1
|
import { WriteThroughCache, LocalCache } from 'cachette';
|
|
2
2
|
import * as uuid from 'uuid';
|
|
3
3
|
import Logger from './logger.js';
|
|
4
|
+
/**
|
|
5
|
+
* The Cache class provides caching capabilities that can be used across your integration.
|
|
6
|
+
* It can be backed by a Redis instance (by passing it a URL to the instance) or a local cache.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link Cache.create}
|
|
9
|
+
*/
|
|
4
10
|
export class Cache {
|
|
5
11
|
cacheInstance;
|
|
6
12
|
constructor(cacheInstance) {
|
|
7
13
|
this.cacheInstance = cacheInstance;
|
|
8
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Get or fetch a value
|
|
17
|
+
*
|
|
18
|
+
* @param key The key of the value to get
|
|
19
|
+
* @param ttl The time to live of the value in seconds.
|
|
20
|
+
* @param fetchFn The function that can retrieve the original value
|
|
21
|
+
* @param lockTtl Global distributed lock TTL (in seconds) protecting fetching.
|
|
22
|
+
* If undefined, 0 or falsy, locking is not preformed
|
|
23
|
+
* @param shouldCacheError A callback being passed errors, controlling whether
|
|
24
|
+
* to cache or not errors. Defaults to never cache.
|
|
25
|
+
*
|
|
26
|
+
* @returns The cached or fetched value
|
|
27
|
+
*/
|
|
9
28
|
getOrFetchValue(key, ttl, fetcher, lockTtl, shouldCacheError) {
|
|
10
29
|
return this.cacheInstance.getOrFetchValue(key, ttl, fetcher, lockTtl, shouldCacheError);
|
|
11
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Get a value from the cache.
|
|
33
|
+
*
|
|
34
|
+
* @param key The key of the value to get.
|
|
35
|
+
*
|
|
36
|
+
* @return The value associated with the key, or undefined if
|
|
37
|
+
* no such value exists.
|
|
38
|
+
*/
|
|
12
39
|
getValue(key) {
|
|
13
40
|
return this.cacheInstance.getValue(key);
|
|
14
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Set a value in the cache.
|
|
44
|
+
*
|
|
45
|
+
* @param key The key of the value to set.
|
|
46
|
+
* @param value The value to set.
|
|
47
|
+
* @param ttl The time to live of the value in seconds.
|
|
48
|
+
* By default, the value will not expire
|
|
49
|
+
*
|
|
50
|
+
* @return true if the value was stored, false otherwise.
|
|
51
|
+
*/
|
|
15
52
|
setValue(key, value, ttl) {
|
|
16
53
|
return this.cacheInstance.setValue(key, value, ttl);
|
|
17
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Delete a value from the cache.
|
|
57
|
+
* @param key — The key of the value to set.
|
|
58
|
+
*/
|
|
18
59
|
delValue(key) {
|
|
19
60
|
return this.cacheInstance.delValue(key);
|
|
20
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Get the TTL of an entry, in ms
|
|
64
|
+
*
|
|
65
|
+
* @param key The key of the entry whose ttl to retrieve
|
|
66
|
+
*
|
|
67
|
+
* @return The remaining TTL on the entry, in ms.
|
|
68
|
+
* undefined if the entry does not exist.
|
|
69
|
+
* 0 if the entry does not expire.
|
|
70
|
+
*/
|
|
21
71
|
getTtl(key) {
|
|
22
72
|
return this.cacheInstance.getTtl(key);
|
|
23
73
|
}
|
|
24
74
|
/**
|
|
25
|
-
* Initializes a
|
|
75
|
+
* Initializes a Cache backed by the Redis instance at the provided url if present, or a LocalCache otherwise.
|
|
26
76
|
*
|
|
27
77
|
* @param redisUrl - The redis url to connect to (optional).
|
|
28
78
|
* @returns A cache instance.
|
|
@@ -3,7 +3,20 @@ import Logger from './logger.js';
|
|
|
3
3
|
import { Credentials } from '../middlewares/credentials.js';
|
|
4
4
|
import { Secrets } from 'src/middlewares/secrets.js';
|
|
5
5
|
import { Filter } from '../middlewares/filters.js';
|
|
6
|
-
|
|
6
|
+
type Maybe<T> = T | null;
|
|
7
|
+
type Empty = Record<string, never>;
|
|
8
|
+
type Params = Record<string, string>;
|
|
9
|
+
type Query = {
|
|
10
|
+
[key: string]: undefined | string | string[] | Query | Query[];
|
|
11
|
+
};
|
|
12
|
+
type CreateItemBody = API.CreateItemRequestPayload;
|
|
13
|
+
type UpdateItemBody = API.UpdateItemRequestPayload;
|
|
14
|
+
/**
|
|
15
|
+
* The base context object is passed to every handler function.
|
|
16
|
+
*
|
|
17
|
+
* It contains the parsed request params, query string, credentials, secrets, etc.
|
|
18
|
+
*/
|
|
19
|
+
export type Context<P extends Maybe<Params> = Params, Q extends Maybe<Query> = Query> = {
|
|
7
20
|
/**
|
|
8
21
|
* The parsed credentials associated with the request through the X-Unito-Credentials header.
|
|
9
22
|
*
|
|
@@ -20,6 +33,12 @@ export type Context<P extends Record<string, string>, Q extends ParsedQueryStrin
|
|
|
20
33
|
* The logger pre decorated with the correlation ID and the additionnal metadata provided through the request headers.
|
|
21
34
|
*/
|
|
22
35
|
logger: Logger;
|
|
36
|
+
/**
|
|
37
|
+
* Each request is expected to complete within a certain time frame. This signal object has been instantiated with the
|
|
38
|
+
* X-Unito-Operation-Deadline header. This header contains the timestamp after which Unito will consider the operation
|
|
39
|
+
* to be timed out. You can use this signal to abort any operation that would exceed this time frame.
|
|
40
|
+
*/
|
|
41
|
+
signal: AbortSignal;
|
|
23
42
|
/**
|
|
24
43
|
* The request params.
|
|
25
44
|
*
|
|
@@ -36,8 +55,19 @@ export type Context<P extends Record<string, string>, Q extends ParsedQueryStrin
|
|
|
36
55
|
*/
|
|
37
56
|
query: Q;
|
|
38
57
|
};
|
|
39
|
-
|
|
40
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Context received by the `GetItemHandler`.
|
|
60
|
+
*
|
|
61
|
+
* @see {@link Context}
|
|
62
|
+
*/
|
|
63
|
+
export type GetItemContext<P extends Maybe<Params> = Empty, Q extends Query = Empty> = Context<P, Q>;
|
|
64
|
+
/**
|
|
65
|
+
* Context received by the `GetCollectionHandler`.
|
|
66
|
+
*
|
|
67
|
+
* @filters Array of {@link Filter} representing the filterss parsed from the query params
|
|
68
|
+
* @see {@link Context} for base params
|
|
69
|
+
*/
|
|
70
|
+
export type GetCollectionContext<P extends Maybe<Params> = Empty, Q extends Query = Empty> = Context<P, Q> & {
|
|
41
71
|
/**
|
|
42
72
|
* Parsed filter query param yielding a list of filters.
|
|
43
73
|
*
|
|
@@ -49,6 +79,8 @@ export type GetCollectionContext<P extends Record<string, string> = Record<strin
|
|
|
49
79
|
* { field: 'name', operator: 'EQUAL', values: ['John'] },
|
|
50
80
|
* { field: 'department.name', operator: 'EQUAL', values: ['Engineering'] }
|
|
51
81
|
* ]
|
|
82
|
+
*
|
|
83
|
+
* @see {@link Filter}
|
|
52
84
|
*/
|
|
53
85
|
filters: Filter[];
|
|
54
86
|
/**
|
|
@@ -62,24 +94,21 @@ export type GetCollectionContext<P extends Record<string, string> = Record<strin
|
|
|
62
94
|
*/
|
|
63
95
|
selects: string[];
|
|
64
96
|
};
|
|
65
|
-
export type CreateItemContext<P extends
|
|
97
|
+
export type CreateItemContext<P extends Maybe<Params> = Empty, Q extends Maybe<Query> = Empty, B extends CreateItemBody = API.CreateItemRequestPayload> = Context<P, Q> & {
|
|
66
98
|
body: B;
|
|
67
99
|
};
|
|
68
|
-
export type UpdateItemContext<P extends
|
|
100
|
+
export type UpdateItemContext<P extends Maybe<Params> = Empty, Q extends Maybe<Query> = Empty, B extends UpdateItemBody = API.UpdateItemRequestPayload> = Context<P, Q> & {
|
|
69
101
|
body: B;
|
|
70
102
|
};
|
|
71
|
-
export type DeleteItemContext<P extends
|
|
72
|
-
export type GetCredentialAccountContext<P extends
|
|
73
|
-
export type ParseWebhooksContext<P extends
|
|
103
|
+
export type DeleteItemContext<P extends Maybe<Params> = Empty, Q extends Query = Empty> = Context<P, Q>;
|
|
104
|
+
export type GetCredentialAccountContext<P extends Maybe<Params> = Empty, Q extends Query = Empty> = Context<P, Q>;
|
|
105
|
+
export type ParseWebhooksContext<P extends Maybe<Params> = Empty, Q extends Maybe<Query> = Empty, B extends API.WebhookParseRequestPayload = API.WebhookParseRequestPayload> = Omit<Context<P, Q>, 'credentials'> & {
|
|
74
106
|
body: B;
|
|
75
107
|
};
|
|
76
|
-
export type UpdateWebhookSubscriptionsContext<P extends
|
|
108
|
+
export type UpdateWebhookSubscriptionsContext<P extends Maybe<Params> = Empty, Q extends Maybe<Query> = Empty, B extends API.WebhookSubscriptionRequestPayload = API.WebhookSubscriptionRequestPayload> = Context<P, Q> & {
|
|
77
109
|
body: B;
|
|
78
110
|
};
|
|
79
|
-
export type AcknowledgeWebhooksContext<P extends
|
|
111
|
+
export type AcknowledgeWebhooksContext<P extends Maybe<Params> = Empty, Q extends Maybe<Query> = Empty, B extends API.WebhookParseRequestPayload = API.WebhookParseRequestPayload> = Omit<Context<P, Q>, 'credentials'> & {
|
|
80
112
|
body: B;
|
|
81
113
|
};
|
|
82
|
-
interface ParsedQueryString {
|
|
83
|
-
[key: string]: undefined | string | string[] | ParsedQueryString | ParsedQueryString[];
|
|
84
|
-
}
|
|
85
114
|
export {};
|
|
@@ -6,6 +6,9 @@ export type Metadata = Value & {
|
|
|
6
6
|
message?: never;
|
|
7
7
|
};
|
|
8
8
|
type ForbidenMetadataKey = 'message';
|
|
9
|
+
/**
|
|
10
|
+
* Logger class that can be configured with metadata add creation and when logging to add additional context to your logs.
|
|
11
|
+
*/
|
|
9
12
|
export default class Logger {
|
|
10
13
|
private metadata;
|
|
11
14
|
constructor(metadata?: Metadata);
|
|
@@ -44,8 +47,22 @@ export default class Logger {
|
|
|
44
47
|
* @param metadata Additional metadata to be added to the logger.
|
|
45
48
|
*/
|
|
46
49
|
decorate(metadata: Metadata): void;
|
|
50
|
+
/**
|
|
51
|
+
* Return a copy of the Logger's metadata.
|
|
52
|
+
* @returns The {@link Metadata} associated with the logger.
|
|
53
|
+
*/
|
|
47
54
|
getMetadata(): Metadata;
|
|
55
|
+
/**
|
|
56
|
+
* Sets a key-value pair in the metadata. If the key already exists, it will be overwritten.
|
|
57
|
+
*
|
|
58
|
+
* @param key Key of the metadata to be set.
|
|
59
|
+
* May be any string other than 'message', which is reserved for the actual message logged.
|
|
60
|
+
* @param value Value of the metadata to be set.
|
|
61
|
+
*/
|
|
48
62
|
setMetadata<Key extends string>(key: Key extends ForbidenMetadataKey ? never : Key, value: PrimitiveValue | Value): void;
|
|
63
|
+
/**
|
|
64
|
+
* Clears the Logger's metadata.
|
|
65
|
+
*/
|
|
49
66
|
clearMetadata(): void;
|
|
50
67
|
private send;
|
|
51
68
|
private snakifyKeys;
|
|
@@ -6,6 +6,9 @@ var LogLevel;
|
|
|
6
6
|
LogLevel["LOG"] = "log";
|
|
7
7
|
LogLevel["DEBUG"] = "debug";
|
|
8
8
|
})(LogLevel || (LogLevel = {}));
|
|
9
|
+
/**
|
|
10
|
+
* Logger class that can be configured with metadata add creation and when logging to add additional context to your logs.
|
|
11
|
+
*/
|
|
9
12
|
export default class Logger {
|
|
10
13
|
metadata;
|
|
11
14
|
constructor(metadata = {}) {
|
|
@@ -58,12 +61,26 @@ export default class Logger {
|
|
|
58
61
|
decorate(metadata) {
|
|
59
62
|
this.metadata = { ...this.metadata, ...metadata };
|
|
60
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Return a copy of the Logger's metadata.
|
|
66
|
+
* @returns The {@link Metadata} associated with the logger.
|
|
67
|
+
*/
|
|
61
68
|
getMetadata() {
|
|
62
69
|
return structuredClone(this.metadata);
|
|
63
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Sets a key-value pair in the metadata. If the key already exists, it will be overwritten.
|
|
73
|
+
*
|
|
74
|
+
* @param key Key of the metadata to be set.
|
|
75
|
+
* May be any string other than 'message', which is reserved for the actual message logged.
|
|
76
|
+
* @param value Value of the metadata to be set.
|
|
77
|
+
*/
|
|
64
78
|
setMetadata(key, value) {
|
|
65
79
|
this.metadata[key] = value;
|
|
66
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Clears the Logger's metadata.
|
|
83
|
+
*/
|
|
67
84
|
clearMetadata() {
|
|
68
85
|
this.metadata = {};
|
|
69
86
|
}
|
|
@@ -20,21 +20,48 @@ export type RateLimiter = <T>(options: {
|
|
|
20
20
|
credentials: Credentials;
|
|
21
21
|
logger: Logger;
|
|
22
22
|
}, targetFunction: () => Promise<Response<T>>) => Promise<Response<T>>;
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* RequestOptions are the options passed to the Provider's call.
|
|
25
|
+
*
|
|
26
|
+
* @property credentials - The credentials to use for the call.
|
|
27
|
+
* @property logger - The logger to use during the call.
|
|
28
|
+
* @property queryParams - The query parameters to add when calling the provider.
|
|
29
|
+
* @property additionnalheaders - The headers to add when calling the provider.
|
|
30
|
+
*/
|
|
31
|
+
export type RequestOptions = {
|
|
24
32
|
credentials: Credentials;
|
|
25
33
|
logger: Logger;
|
|
34
|
+
signal: AbortSignal;
|
|
26
35
|
queryParams?: {
|
|
27
36
|
[key: string]: string;
|
|
28
37
|
};
|
|
29
38
|
additionnalheaders?: {
|
|
30
39
|
[key: string]: string;
|
|
31
40
|
};
|
|
32
|
-
}
|
|
33
|
-
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Response object returned by the Provider's method.
|
|
44
|
+
*
|
|
45
|
+
* Contains;
|
|
46
|
+
* - the body typed as specified when calling the method
|
|
47
|
+
* - the status code of the response
|
|
48
|
+
* - the headers of the response.
|
|
49
|
+
*/
|
|
50
|
+
export type Response<T> = {
|
|
34
51
|
body: T;
|
|
35
52
|
status: number;
|
|
36
53
|
headers: Headers;
|
|
37
|
-
}
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* The Provider class is a wrapper around the fetch function to call a provider's HTTP API.
|
|
57
|
+
*
|
|
58
|
+
* Defines methods for the following HTTP methods: GET, POST, PUT, PATCH, DELETE.
|
|
59
|
+
*
|
|
60
|
+
* Needs to be initialized with a prepareRequest function to define the Provider's base URL and any specific headers to
|
|
61
|
+
* add to the requests, and can also be configured to use a provided rate limiting function.
|
|
62
|
+
* @see {@link RateLimiter}
|
|
63
|
+
* @see {@link prepareRequest}
|
|
64
|
+
*/
|
|
38
65
|
export declare class Provider {
|
|
39
66
|
protected rateLimiter: RateLimiter | undefined;
|
|
40
67
|
protected prepareRequest: (options: {
|
|
@@ -45,7 +72,7 @@ export declare class Provider {
|
|
|
45
72
|
headers: Record<string, string>;
|
|
46
73
|
};
|
|
47
74
|
/**
|
|
48
|
-
*
|
|
75
|
+
* Initializes a Provider with the given options.
|
|
49
76
|
*
|
|
50
77
|
* @property prepareRequest - function to define the Provider's base URL and specific headers to add to the request.
|
|
51
78
|
* @property rateLimiter - function to limit the rate of calls to the provider based on the caller's credentials.
|
|
@@ -54,10 +81,68 @@ export declare class Provider {
|
|
|
54
81
|
prepareRequest: typeof Provider.prototype.prepareRequest;
|
|
55
82
|
rateLimiter?: RateLimiter;
|
|
56
83
|
});
|
|
84
|
+
/**
|
|
85
|
+
* Performs a GET request to the provider.
|
|
86
|
+
*
|
|
87
|
+
* Uses the prepareRequest function to get the base URL and any specific headers to add to the request and by default
|
|
88
|
+
* adds the following headers:
|
|
89
|
+
* - Accept: application/json
|
|
90
|
+
*
|
|
91
|
+
* @param endpoint Path to the provider's resource. Will be added to the URL returned by the prepareRequest function.
|
|
92
|
+
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
93
|
+
* @returns The {@link Response} extracted from the provider.
|
|
94
|
+
*/
|
|
57
95
|
get<T>(endpoint: string, options: RequestOptions): Promise<Response<T>>;
|
|
96
|
+
/**
|
|
97
|
+
* Performs a POST request to the provider.
|
|
98
|
+
*
|
|
99
|
+
* Uses the prepareRequest function to get the base URL and any specific headers to add to the request and by default
|
|
100
|
+
* adds the following headers:
|
|
101
|
+
* - Content-Type: application/json',
|
|
102
|
+
* - Accept: application/json
|
|
103
|
+
*
|
|
104
|
+
* @param endpoint Path to the provider's resource. Will be added to the URL returned by the prepareRequest function.
|
|
105
|
+
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
106
|
+
* @returns The {@link Response} extracted from the provider.
|
|
107
|
+
*/
|
|
58
108
|
post<T>(endpoint: string, body: Record<string, unknown>, options: RequestOptions): Promise<Response<T>>;
|
|
109
|
+
/**
|
|
110
|
+
* Performs a PUT request to the provider.
|
|
111
|
+
*
|
|
112
|
+
* Uses the prepareRequest function to get the base URL and any specific headers to add to the request and by default
|
|
113
|
+
* adds the following headers:
|
|
114
|
+
* - Content-Type: application/json',
|
|
115
|
+
* - Accept: application/json
|
|
116
|
+
*
|
|
117
|
+
* @param endpoint Path to the provider's resource. Will be added to the URL returned by the prepareRequest function.
|
|
118
|
+
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
119
|
+
* @returns The {@link Response} extracted from the provider.
|
|
120
|
+
*/
|
|
59
121
|
put<T>(endpoint: string, body: Record<string, unknown>, options: RequestOptions): Promise<Response<T>>;
|
|
122
|
+
/**
|
|
123
|
+
* Performs a PATCH request to the provider.
|
|
124
|
+
*
|
|
125
|
+
* Uses the prepareRequest function to get the base URL and any specific headers to add to the request and by default
|
|
126
|
+
* adds the following headers:
|
|
127
|
+
* - Content-Type: application/json',
|
|
128
|
+
* - Accept: application/json
|
|
129
|
+
*
|
|
130
|
+
* @param endpoint Path to the provider's resource. Will be added to the URL returned by the prepareRequest function.
|
|
131
|
+
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
132
|
+
* @returns The {@link Response} extracted from the provider.
|
|
133
|
+
*/
|
|
60
134
|
patch<T>(endpoint: string, body: Record<string, unknown>, options: RequestOptions): Promise<Response<T>>;
|
|
135
|
+
/**
|
|
136
|
+
* Performs a DELETE request to the provider.
|
|
137
|
+
*
|
|
138
|
+
* Uses the prepareRequest function to get the base URL and any specific headers to add to the request and by default
|
|
139
|
+
* adds the following headers:
|
|
140
|
+
* - Accept: application/json
|
|
141
|
+
*
|
|
142
|
+
* @param endpoint Path to the provider's resource. Will be added to the URL returned by the prepareRequest function.
|
|
143
|
+
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
144
|
+
* @returns The {@link Response} extracted from the provider.
|
|
145
|
+
*/
|
|
61
146
|
delete<T = undefined>(endpoint: string, options: RequestOptions): Promise<Response<T>>;
|
|
62
147
|
private fetchWrapper;
|
|
63
148
|
}
|