@devrev/ts-adaas 1.2.5 → 1.2.6
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/README.md +246 -84
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,135 +1,297 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Airdrop SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://coveralls.io/github/devrev/adaas-sdk?branch=main)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Overview
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
|
|
9
|
-
- Remove unneccessary postState from default workers.
|
|
10
|
-
- Fix bugs related to attachment streaming.
|
|
7
|
+
The Airdrop SDK for TypeScript helps developers build snap-ins that integrate with DevRev’s Airdrop platform.
|
|
8
|
+
This SDK simplifies the workflow for handling data extraction and loading, event-driven actions, state management, and artifact handling.
|
|
11
9
|
|
|
12
|
-
|
|
10
|
+
It provides features such as:
|
|
13
11
|
|
|
14
|
-
-
|
|
12
|
+
- Type Definitions: Structured types for Airdrop control protocol
|
|
13
|
+
- Event Management: Easily emit events for different extraction or loading phases
|
|
14
|
+
- State Handling: Update and access state in real-time within tasks
|
|
15
|
+
- Artifact Management: Supports batched storage of artifacts
|
|
16
|
+
- Error & Timeout Support: Error handling and timeout management for long-running tasks
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
## Installation
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
- Fix bugs related to event handling, error logging.
|
|
20
|
+
```bash
|
|
21
|
+
npm install @devrev/ts-adaas
|
|
22
|
+
```
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
## Reference
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
- Improve axios client and adapter logging.
|
|
27
|
-
- Fix bugs related to state handling.
|
|
26
|
+
### `spawn` function
|
|
28
27
|
|
|
29
|
-
|
|
28
|
+
This function initializes a new worker thread and oversees its lifecycle.
|
|
29
|
+
It should be invoked when the snap-in receives a message from the Airdrop platform.
|
|
30
|
+
The worker script provided then handles the event accordingly.
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
- Correct the setting of the `lastSyncStarted` timestamp.
|
|
33
|
-
- Improve logging for attachment extraction and loading.
|
|
34
|
-
- Fix several bugs related to the control protocol.
|
|
32
|
+
#### Usage
|
|
35
33
|
|
|
36
|
-
|
|
34
|
+
```typescript
|
|
35
|
+
spawn({ event, initialState, workerPath, options })
|
|
36
|
+
```
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
#### Parameters
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
* _event_
|
|
41
|
+
|
|
42
|
+
Required. An object of type __AirdropEvent__ that is received from the Airdrop platform.
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
- Gracefully handle failure to upload extracted attachments.
|
|
44
|
+
* _initialState_
|
|
44
45
|
|
|
45
|
-
|
|
46
|
+
Required. Object of __any__ type that represents the initial state of the snap-in.
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
- Provide an inject function for streaming attachments.
|
|
49
|
-
- Fix the attachments streaming bug.
|
|
48
|
+
* _workerPath_
|
|
50
49
|
|
|
51
|
-
|
|
50
|
+
Required. A __string__ that represents the path to the worker file.
|
|
52
51
|
|
|
53
|
-
|
|
52
|
+
* _options_
|
|
54
53
|
|
|
55
|
-
|
|
54
|
+
Optional. An object of type **WorkerAdapterOptions**, which will be passed to the newly created worker. This worker will then initialize a `WorkerAdapter` by invoking the `processTask` function. The options include:
|
|
55
|
+
|
|
56
|
+
* `isLocalDevelopment`
|
|
57
|
+
|
|
58
|
+
A __boolean__ flag. If set to `true`, intermediary files containing extracted data will be stored on the local machine, which is useful during development. The default value is `false`.
|
|
56
59
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
* `timeout`
|
|
61
|
+
|
|
62
|
+
A __number__ that specifies the timeout duration for the lambda function, in milliseconds. The default is 10 minutes (10 * 60 * 1000 milliseconds), with a maximum allowable duration of 13 minutes (13 * 60 * 1000 milliseconds).
|
|
63
|
+
|
|
64
|
+
* `batchSize`
|
|
61
65
|
|
|
62
|
-
|
|
66
|
+
A __number__ that determines the maximum number of items to be processed and saved to an intermediary file before being sent to the Airdrop platform. The default batch size is 2,000.
|
|
63
67
|
|
|
64
|
-
|
|
65
|
-
- Add `dev_oid` to logger tags.
|
|
68
|
+
#### Return value
|
|
66
69
|
|
|
67
|
-
|
|
70
|
+
A __promise__ that resolves once the worker has completed processing.
|
|
68
71
|
|
|
69
|
-
|
|
72
|
+
#### Example
|
|
70
73
|
|
|
71
|
-
|
|
74
|
+
```typescript
|
|
75
|
+
const run = async (events: AirdropEvent[]) => {
|
|
76
|
+
for (const event of events) {
|
|
77
|
+
const file = getWorkerPerExtractionPhase(event);
|
|
78
|
+
await spawn<ExtractorState>({
|
|
79
|
+
event,
|
|
80
|
+
initialState,
|
|
81
|
+
workerPath: file,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
```
|
|
72
86
|
|
|
73
|
-
|
|
87
|
+
### `processTask` function
|
|
74
88
|
|
|
75
|
-
|
|
89
|
+
The `processTask` function retrieves the current state from the Airdrop platform and initializes a new `WorkerAdapter`.
|
|
90
|
+
It executes the code specified in the `task` parameter, which contains the worker's functionality.
|
|
91
|
+
If a timeout occurs, the function handles it by executing the `onTimeout` callback, ensuring the worker exits gracefully.
|
|
92
|
+
Both functions receive an `adapter` parameter, representing the initialized `WorkerAdapter` object.
|
|
76
93
|
|
|
77
|
-
- Fix logging from worker threads.
|
|
78
94
|
|
|
79
|
-
|
|
95
|
+
#### Usage
|
|
96
|
+
```typescript
|
|
97
|
+
processTask({ task, onTimeout })
|
|
98
|
+
```
|
|
80
99
|
|
|
81
|
-
|
|
100
|
+
#### Parameters
|
|
82
101
|
|
|
83
|
-
|
|
102
|
+
* _task_
|
|
103
|
+
|
|
104
|
+
Required. A __function__ that defines the logic associated with the given event type.
|
|
84
105
|
|
|
85
|
-
|
|
86
|
-
|
|
106
|
+
* _onTimeout_
|
|
107
|
+
|
|
108
|
+
Required. A __function__ managing the timeout of the lambda invocation, including saving any necessary progress at the time of timeout.
|
|
87
109
|
|
|
88
|
-
|
|
110
|
+
#### Example
|
|
89
111
|
|
|
90
|
-
|
|
112
|
+
````typescript
|
|
113
|
+
// External sync units extraction
|
|
114
|
+
processTask({
|
|
115
|
+
task: async ({ adapter }) => {
|
|
116
|
+
const httpClient = new HttpClient(adapter.event);
|
|
91
117
|
|
|
92
|
-
|
|
118
|
+
const todoLists = await httpClient.getTodoLists();
|
|
93
119
|
|
|
94
|
-
|
|
95
|
-
- Simplify metadata and data normalization and uploading with the repo implementation.
|
|
96
|
-
- Provide default handling of the attachment extraction phase in the ADaaS SDK library.
|
|
97
|
-
- Reduce file size and streamline processes with gzip compression.
|
|
98
|
-
- Fix bugs and improve error handling.
|
|
120
|
+
const externalSyncUnits: ExternalSyncUnit[] = todoLists.map((todoList) => normalizeTodoList(todoList));
|
|
99
121
|
|
|
100
|
-
|
|
122
|
+
await adapter.emit(ExtractorEventType.ExtractionExternalSyncUnitsDone, {
|
|
123
|
+
external_sync_units: externalSyncUnits,
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
onTimeout: async ({ adapter }) => {
|
|
127
|
+
await adapter.emit(ExtractorEventType.ExtractionExternalSyncUnitsError, {
|
|
128
|
+
error: {
|
|
129
|
+
message: 'Failed to extract external sync units. Lambda timeout.',
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
````
|
|
101
135
|
|
|
102
|
-
|
|
136
|
+
### `WorkerAdapter` class
|
|
103
137
|
|
|
104
|
-
|
|
138
|
+
Used to interact with Airdrop platform.
|
|
139
|
+
Provides utilities to emit events to the Airdrop platform, update the state of the snap-in and upload artifacts (files with data) to the platform.
|
|
105
140
|
|
|
106
|
-
|
|
107
|
-
- Provide an HTTP client for API requests.
|
|
108
|
-
- Create local artifact files in the local development environment.
|
|
109
|
-
- Improve logging.
|
|
141
|
+
### Usage
|
|
110
142
|
|
|
111
|
-
|
|
143
|
+
```typescript
|
|
144
|
+
new WorkerAdapter({
|
|
145
|
+
event,
|
|
146
|
+
adapterState,
|
|
147
|
+
options,
|
|
148
|
+
});
|
|
149
|
+
```
|
|
112
150
|
|
|
113
|
-
|
|
114
|
-
- Add an adapter for the ADaaS control protocol with helper functions.
|
|
115
|
-
- Provide an uploader for uploading artifacts.
|
|
151
|
+
#### Parameters
|
|
116
152
|
|
|
117
|
-
|
|
153
|
+
* _event_
|
|
154
|
+
|
|
155
|
+
Required. An object of type __AirdropEvent__ that is received from the Airdrop platform.
|
|
118
156
|
|
|
119
|
-
|
|
157
|
+
* _adapterState_
|
|
158
|
+
|
|
159
|
+
Required. An object of type __State__, which represents the initial state of the adapter.
|
|
120
160
|
|
|
121
|
-
|
|
161
|
+
* _options_
|
|
162
|
+
|
|
163
|
+
Optional. An object of type __WorkerAdapterOptions__ that specifies additional configuration options for the `WorkerAdapter`. This object is passed via the `spawn` function.
|
|
122
164
|
|
|
123
|
-
|
|
165
|
+
#### Example
|
|
124
166
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
167
|
+
```typescript
|
|
168
|
+
const adapter = new WorkerAdapter<ConnectorState>({
|
|
169
|
+
event,
|
|
170
|
+
adapterState,
|
|
171
|
+
options,
|
|
172
|
+
});
|
|
173
|
+
```
|
|
130
174
|
|
|
131
|
-
|
|
175
|
+
### `WorkerAdapter.state` property
|
|
132
176
|
|
|
133
|
-
|
|
134
|
-
|
|
177
|
+
Getter and setter methods for working with the adapter state.
|
|
178
|
+
|
|
179
|
+
### Usage
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
// get state
|
|
183
|
+
const adapterState = adapter.state;
|
|
184
|
+
|
|
185
|
+
// set state
|
|
186
|
+
adapter.state = newAdapterState;
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### Example
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
export const initialState: ExtractorState = {
|
|
193
|
+
users: { completed: false },
|
|
194
|
+
tasks: { completed: false },
|
|
195
|
+
attachments: { completed: false },
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
adapter.state = initialState;
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### `WorkerAdapter.initializeRepos` method
|
|
202
|
+
|
|
203
|
+
Initializes a `Repo` object for each item provided.
|
|
204
|
+
|
|
205
|
+
### Usage
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
adapter.initializeRepos(repos);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### Parameters
|
|
212
|
+
|
|
213
|
+
* _repos_
|
|
214
|
+
|
|
215
|
+
Required. An array of objects of type `RepoInterface`.
|
|
216
|
+
|
|
217
|
+
#### Example
|
|
218
|
+
|
|
219
|
+
This should typically be called within the function passed as a parameter to the `processTask` function in the data extraction phase.
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
const repos = [
|
|
223
|
+
{
|
|
224
|
+
itemType: 'tasks',
|
|
225
|
+
normalize: normalizeTask,
|
|
226
|
+
}
|
|
227
|
+
];
|
|
228
|
+
|
|
229
|
+
adapter.initializeRepos(repos);
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### `WorkerAdapter.getRepo` method
|
|
233
|
+
|
|
234
|
+
Finds a Repo from the initialized repos.
|
|
235
|
+
|
|
236
|
+
### Usage
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
adapter.getRepo(itemType);
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
#### Parameters
|
|
243
|
+
|
|
244
|
+
* _itemType_
|
|
245
|
+
|
|
246
|
+
Required. A __string__ that represents the itemType property for the searched repo.
|
|
247
|
+
|
|
248
|
+
#### Return value
|
|
249
|
+
|
|
250
|
+
An object of type __Repo__ if the repo is found, otherwise __undefined__.
|
|
251
|
+
|
|
252
|
+
#### Example
|
|
253
|
+
|
|
254
|
+
This should typically be called within the function passed as a parameter to the `processTask` function.
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
// Push users to the repository designated for 'users' data.
|
|
258
|
+
await adapter.getRepo('users')?.push(users);
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### `WorkerAdapter.emit` method
|
|
262
|
+
|
|
263
|
+
Emits an event to the Airdrop platform.
|
|
264
|
+
|
|
265
|
+
### Usage
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
adapter.emit( newEventType, data ):
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
#### Parameters
|
|
272
|
+
|
|
273
|
+
* _newEventType_
|
|
274
|
+
|
|
275
|
+
Required. The event type to be emitted, of type __ExtractorEventType__ or __LoaderEventType__.
|
|
276
|
+
|
|
277
|
+
* _data_
|
|
278
|
+
|
|
279
|
+
Optional. An object of type __EventData__ which represents the data to be sent with the event.
|
|
280
|
+
|
|
281
|
+
#### Return value
|
|
282
|
+
|
|
283
|
+
A __promise__, which resolves to undefined after the emit function completes its execution or rejects with an error.
|
|
284
|
+
|
|
285
|
+
#### Example
|
|
286
|
+
|
|
287
|
+
This should typically be called within the function passed as a parameter to the `processTask` function.
|
|
288
|
+
|
|
289
|
+
```typescript
|
|
290
|
+
// Emitting successfully finished data extraction.
|
|
291
|
+
await adapter.emit(ExtractorEventType.ExtractionDataDone);
|
|
292
|
+
|
|
293
|
+
// Emitting a delay in attachments extraction phase.
|
|
294
|
+
await adapter.emit(ExtractorEventType.ExtractionAttachmentsDelay, {
|
|
295
|
+
delay: 10,
|
|
296
|
+
});
|
|
135
297
|
```
|