@trayio/cdk-dsl
Advanced tools
Comparing version 1.3.0 to 1.4.0
{ | ||
"name": "@trayio/cdk-dsl", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "A DSL for connector development", | ||
@@ -17,3 +17,3 @@ "exports": { | ||
"dependencies": { | ||
"@trayio/commons": "1.3.0" | ||
"@trayio/commons": "1.4.0" | ||
}, | ||
@@ -20,0 +20,0 @@ "typesVersions": { |
@@ -219,3 +219,3 @@ # Connector Development Kit (CDK) DSL | ||
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. | ||
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. | ||
@@ -234,3 +234,19 @@ | ||
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: | ||
```typescript | ||
const productsResult: OperationHandlerResult<GetProductsOutput> = await invoke(getProductsHandler)({ storeId: input.storeId }) | ||
``` | ||
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}`. | ||
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: | ||
```typescript | ||
import { DDLOperationOutput } from '@trayio/cdk-dsl/connector/operation/OperationHandler'; | ||
export type GetProductListDdlOutput = DDLOperationOutput<number> //the values of the elements of the DDL are of type number | ||
``` | ||
With that in mind, this is what the DDL handler would look like: | ||
@@ -243,8 +259,26 @@ | ||
handler.usingComposite(async (auth, input, invoke) => { | ||
const productListResult: OperationHandlerResult<GetProductsOutput> = | ||
//invoke the products operation | ||
const productsResult: OperationHandlerResult<GetProductsOutput> = | ||
await invoke(getProductsHandler)({ storeId: input.storeId }) | ||
return OperationHandlerResult.map( | ||
productListResult, | ||
productList => productList.map(product => { text: product.title, value: product.id }) | ||
) | ||
//if the invocation failed, propagate the failure | ||
if (productsResult.isFailure) { | ||
return productsResult | ||
} | ||
//productsResult is of type `OperationHandlerSuccess` now, because we handled the failure case above, so we can get the value | ||
const products = productsResult.value | ||
//converts a product list into a list of text (label) and values (identifiers) for the DDL | ||
const productsDDL = products.map((product) => { | ||
return { | ||
text: product.title, | ||
value: product.id | ||
} | ||
}) | ||
//returns the DDL list in an object with a "result" value to match the GetProductsDDLOutput type | ||
return OperationHandlerResult.success({ | ||
result: productsDDL | ||
}) | ||
}) | ||
@@ -263,8 +297,21 @@ ); | ||
There are multiple ways to do this | ||
- Using `if` or `switch` statements to narrow down the type | ||
- Using `if` or `switch` statements to narrow down the type as shown in the example | ||
- Using the `OperationHandlerResult.getSuccessfulValueOrFail()` function which unwraps the value if successful or terminates the function propagating the error if it is not | ||
- As shown in the example, using the `OperationHandlerResult.map()` function. | ||
- 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>`) | ||
Once the handler has access to the product list value, it just needs to convert each element to a `{text: string, value: string}` pair. | ||
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. | ||
Finally, for DDL operations, we need to add an extra `type` property in the `operation.json` file with `ddl` as the value: | ||
```json | ||
{ | ||
"name": "get_products_ddl", | ||
"title": "Get Products DDL", | ||
"description": "Returns a DDL with product ids as values and product titles as labels", | ||
"type": "ddl" | ||
} | ||
``` | ||
This will categorise this operation as a DDL and will exclude it from the list of visible operations of the connector. | ||
## Testing | ||
@@ -271,0 +318,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114929
411
+ Added@trayio/commons@1.4.0(transitive)
- Removed@trayio/commons@1.3.0(transitive)
Updated@trayio/commons@1.4.0