@trayio/cdk-dsl 1.3.0 → 1.5.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.
Files changed (2) hide show
  1. package/README.md +60 -9
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -5,6 +5,10 @@ The CDK Domain Specific Language (DSL) is the main component of Tray's CDK, it i
5
5
  A CDK connector consists of code written only using the DSL, which is declarative, so it only describes the connector, that description is then interpreted by the runtime
6
6
  to execute a connector's operations.
7
7
 
8
+ ## Documentation
9
+
10
+ The [Tray.io Developer Portal](https://developer.tray.io) has the most up to date documentation including a [quick start guide](https://developer.tray.io/developer-portal/cdk/quickstart/) for building connectors with the CDK
11
+
8
12
 
9
13
  ## Project Structure
10
14
 
@@ -216,7 +220,7 @@ export const myOperationHandler =
216
220
  );
217
221
  ```
218
222
 
219
- As an example of more complex behaviour, this handler reads a list of products using another operation and converts the result into a simple list of `{text: string, value: string}` pairs, this is known as a Dynamic Data List (DDL) operation used to help users select values as part of configuring workflows within the tray builder.
223
+ As an example of more complex behaviour, this handler reads a list of products using another operation and converts the result into a simple list of text and value pairs, this is known as a Dynamic Data List (DDL) operation used to help users select values as part of configuring workflows within the tray builder.
220
224
 
221
225
 
222
226
  To do this, the handler needs to invoke the regular `getProducts` operation, this is accomplished by using the `invoke` functions that composite handlers have access to, and passing it a **handler reference** and an input (no need to pass the auth value as it will be passed automatically), the handler reference passed to the `invoke` function is the result of the `OperationHandlerSetup.configureHandler()` function, which is why they are saved into a constant and exported like this:
@@ -230,6 +234,22 @@ export const getProductsHandler =
230
234
 
231
235
  The `getProductsHandler` constant contains the handler reference, which also has the input and output type information as part of its type, to make sure that when invoked or tested, only valid input and output values can be used.
232
236
 
237
+ To invoke the handler it is a simple call to the `invoke` function that takes the handler reference as an argument, and returns another function that takes the input type of that handler and returns a `Promise<OperationHandlerResult<T>>` where `T` is the output type of the handler and `OperationHandlerResult` contains a failure response if the call failed or a success response with a value of type `T` if the call was successful:
238
+
239
+ ```typescript
240
+ const productsResult: OperationHandlerResult<GetProductsOutput> = await invoke(getProductsHandler)({ storeId: input.storeId })
241
+ ```
242
+
243
+ The output type of a DDL operation is defined by the `DDLOperationOutput<T>` type, where `T` is the type of the values and can be a string or a number, that type has one field called `results` which is an array of objects of type `{text: string, value: T}`.
244
+
245
+ To use it as the output, it is recommended to define a custom type for the operation as usual in `output.ts` that derives from `DDLOperationOutput<T>` specifying whether `T` is of type string or number, like this example `output.ts` file:
246
+
247
+ ```typescript
248
+ import { DDLOperationOutput } from '@trayio/cdk-dsl/connector/operation/OperationHandler';
249
+
250
+ export type GetProductListDdlOutput = DDLOperationOutput<number> //the values of the elements of the DDL are of type number
251
+ ```
252
+
233
253
 
234
254
  With that in mind, this is what the DDL handler would look like:
235
255
 
@@ -238,12 +258,30 @@ With that in mind, this is what the DDL handler would look like:
238
258
  export const getProductsDDLHandler =
239
259
  OperationHandlerSetup.configureHandler<AuthType, GetProductsDDLInput, GetProductsDDLOutput>((handler) =>
240
260
  handler.usingComposite(async (auth, input, invoke) => {
241
- const productListResult: OperationHandlerResult<GetProductsOutput> =
261
+ //invoke the products operation
262
+ const productsResult: OperationHandlerResult<GetProductsOutput> =
242
263
  await invoke(getProductsHandler)({ storeId: input.storeId })
243
- return OperationHandlerResult.map(
244
- productListResult,
245
- productList => productList.map(product => { text: product.title, value: product.id })
246
- )
264
+
265
+ //if the invocation failed, propagate the failure
266
+ if (productsResult.isFailure) {
267
+ return productsResult
268
+ }
269
+
270
+ //productsResult is of type `OperationHandlerSuccess` now, because we handled the failure case above, so we can get the value
271
+ const products = productsResult.value
272
+
273
+ //converts a product list into a list of text (label) and values (identifiers) for the DDL
274
+ const productsDDL = products.map((product) => {
275
+ return {
276
+ text: product.title,
277
+ value: product.id
278
+ }
279
+ })
280
+
281
+ //returns the DDL list in an object with a "result" value to match the GetProductsDDLOutput type
282
+ return OperationHandlerResult.success({
283
+ result: productsDDL
284
+ })
247
285
  })
248
286
  );
249
287
  ```
@@ -257,11 +295,24 @@ The result of that invocation is of type `Promise<OperationHandlerResult<GetProd
257
295
  The `await` keyword unwraps the promise, and we are left with a `OperationHandlerResult<GetProductsOutput>`, which forces the handler to deal with both the failure case as well as the success case.
258
296
 
259
297
  There are multiple ways to do this
260
- - Using `if` or `switch` statements to narrow down the type
298
+ - Using `if` or `switch` statements to narrow down the type as shown in the example
261
299
  - Using the `OperationHandlerResult.getSuccessfulValueOrFail()` function which unwraps the value if successful or terminates the function propagating the error if it is not
262
- - As shown in the example, using the `OperationHandlerResult.map()` function.
300
+ - Using the `OperationHandlerResult.map()` function, which takes an `OperationHandlerResult<T>` as an argument and a function to convert `T` to another type in case it is successful, propagating the failure if it is not (works in the same way it works for the map function in `Array<T>`)
301
+
302
+ Once the handler has access to the product list value, it just needs to convert each element to a `{text: string, value: number}` pair and return that list in an object with a `result` property as shown above.
303
+
304
+ Finally, for DDL operations, we need to add an extra `type` property in the `operation.json` file with `ddl` as the value:
305
+
306
+ ```json
307
+ {
308
+ "name": "get_products_ddl",
309
+ "title": "Get Products DDL",
310
+ "description": "Returns a DDL with product ids as values and product titles as labels",
311
+ "type": "ddl"
312
+ }
313
+ ```
263
314
 
264
- Once the handler has access to the product list value, it just needs to convert each element to a `{text: string, value: string}` pair.
315
+ This will categorise this operation as a DDL and will exclude it from the list of visible operations of the connector.
265
316
 
266
317
  ## Testing
267
318
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/cdk-dsl",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "A DSL for connector development",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -14,7 +14,7 @@
14
14
  "access": "public"
15
15
  },
16
16
  "dependencies": {
17
- "@trayio/commons": "1.3.0"
17
+ "@trayio/commons": "1.5.0"
18
18
  },
19
19
  "typesVersions": {
20
20
  "*": {