@trayio/cdk-dsl 7.0.0 → 7.2.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/README.md CHANGED
@@ -1482,6 +1482,74 @@ export const myOperationHandler =
1482
1482
  );
1483
1483
  ```
1484
1484
 
1485
+ ### Handling files in composite operations
1486
+
1487
+ Composite handlers receive a fourth argument, `cdk`, with the dependencies the CDK provides to the handler. Today that is Promise-based file handling under `cdk.files`; more may be added later. Use it when an operation produces or transforms bytes in memory (for example resizing an image, or zipping the output of several operations) and needs to return them as a `FileReference`, or when it needs to read the content of a `FileReference` it received as input.
1488
+
1489
+ The types are exported from `@trayio/cdk-dsl/connector/operation/OperationHandlerDependencies` (`FileUpload`, `FileDownload`, `FileHandling`, `OperationHandlerDependencies`); handlers rarely need to import them because `cdk` is already typed. Their shape:
1490
+
1491
+ ```typescript
1492
+ type FileUpload = {
1493
+ name: string;
1494
+ contentType: string;
1495
+ content: Readable | Buffer;
1496
+ size?: number; // optional for streams, lets the runtime tune the upload; derived for Buffers
1497
+ };
1498
+
1499
+ type FileDownload = {
1500
+ stream: Readable;
1501
+ size?: number;
1502
+ contentType?: string;
1503
+ };
1504
+
1505
+ type OperationHandlerDependencies = {
1506
+ files: {
1507
+ upload(file: FileUpload): Promise<FileReference>;
1508
+ download(file: FileReference): Promise<FileDownload>;
1509
+ };
1510
+ };
1511
+ ```
1512
+
1513
+ - `cdk.files.download(fileReference)` resolves the reference to a readable stream (plus `size` and `contentType` when known).
1514
+ - `cdk.files.upload({ name, contentType, content })` writes a `Readable` or `Buffer` to Tray file storage and returns a `FileReference` that can be returned as operation output or passed to other operations.
1515
+
1516
+ ```typescript
1517
+ // input.ts
1518
+ import { FileReference } from '@trayio/cdk-dsl/connector/operation/OperationHandler';
1519
+
1520
+ export type ResizeImageInput = {
1521
+ file: FileReference;
1522
+ width: number;
1523
+ };
1524
+ ```
1525
+
1526
+ ```typescript
1527
+ // output.ts
1528
+ import { FileReference } from '@trayio/cdk-dsl/connector/operation/OperationHandler';
1529
+
1530
+ export type ResizeImageOutput = {
1531
+ file: FileReference;
1532
+ };
1533
+ ```
1534
+
1535
+ ```typescript
1536
+ // handler.ts
1537
+ handler.usingComposite(async (ctx, input, invoke, cdk) => {
1538
+ const { stream } = await cdk.files.download(input.file);
1539
+ const resized = stream.pipe(sharp().resize(input.width));
1540
+ const file = await cdk.files.upload({
1541
+ name: input.file.name,
1542
+ contentType: input.file.mime_type,
1543
+ content: resized,
1544
+ });
1545
+ return OperationHandlerResult.success({ file });
1546
+ });
1547
+ ```
1548
+
1549
+ If an upload or download fails, the returned promise rejects with an `Error`. A rejection that the handler does not catch is reported as a connector error, exactly like any other exception thrown from a composite handler. The `cdk` argument is optional to declare: existing three-argument composite handlers keep working unchanged.
1550
+
1551
+ When handler tests run through the CDK test runner, composite operations get a local filesystem-backed file storage automatically, so uploads and downloads round-trip without any extra setup.
1552
+
1485
1553
  ### How to enable / disable raw http operation.
1486
1554
 
1487
1555
  By default, all the connectors come with a Raw Http Operation. You can disable it by updating the config in the connector.json
@@ -1,6 +1,7 @@
1
1
  import { OperationHandlerAuth, OperationHandlerContext, OperationHandlerResult } from './OperationHandler';
2
2
  import { OperationHandlerInvocation } from './OperationHandlerInvocation';
3
- export type CompositeOperationHandlerFunction<AUTH extends OperationHandlerAuth<unknown, unknown>, IN, OUT> = (ctx: OperationHandlerContext<AUTH>, input: IN, invoke: OperationHandlerInvocation<AUTH>) => Promise<OperationHandlerResult<OUT>>;
3
+ import { OperationHandlerDependencies } from './OperationHandlerDependencies';
4
+ export type CompositeOperationHandlerFunction<AUTH extends OperationHandlerAuth<unknown, unknown>, IN, OUT> = (ctx: OperationHandlerContext<AUTH>, input: IN, invoke: OperationHandlerInvocation<AUTH>, cdk: OperationHandlerDependencies) => Promise<OperationHandlerResult<OUT>>;
4
5
  export declare class CompositeOperationHandler<AUTH extends OperationHandlerAuth<unknown, unknown>, IN, OUT> {
5
6
  readonly _tag: 'CompositeOperationHandler';
6
7
  readonly handlerFunction: CompositeOperationHandlerFunction<AUTH, IN, OUT>;
@@ -1 +1 @@
1
- {"version":3,"file":"CompositeOperationHandler.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/CompositeOperationHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,oBAAoB,EACpB,uBAAuB,EACvB,sBAAsB,EACtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAE1E,MAAM,MAAM,iCAAiC,CAC5C,IAAI,SAAS,oBAAoB,CAAC,OAAO,EAAE,OAAO,CAAC,EACnD,EAAE,EACF,GAAG,IACA,CACH,GAAG,EAAE,uBAAuB,CAAC,IAAI,CAAC,EAClC,KAAK,EAAE,EAAE,EACT,MAAM,EAAE,0BAA0B,CAAC,IAAI,CAAC,KACpC,OAAO,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC,CAAC;AAE1C,qBAAa,yBAAyB,CACrC,IAAI,SAAS,oBAAoB,CAAC,OAAO,EAAE,OAAO,CAAC,EACnD,EAAE,EACF,GAAG;IAEH,QAAQ,CAAC,IAAI,EAAE,2BAA2B,CAA+B;IACzE,QAAQ,CAAC,eAAe,EAAE,iCAAiC,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC,CAAC;gBAG1E,eAAe,EAAE,iCAAiC,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC;CAIlE"}
1
+ {"version":3,"file":"CompositeOperationHandler.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/CompositeOperationHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,oBAAoB,EACpB,uBAAuB,EACvB,sBAAsB,EACtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,4BAA4B,EAAE,MAAM,gCAAgC,CAAC;AAE9E,MAAM,MAAM,iCAAiC,CAC5C,IAAI,SAAS,oBAAoB,CAAC,OAAO,EAAE,OAAO,CAAC,EACnD,EAAE,EACF,GAAG,IACA,CACH,GAAG,EAAE,uBAAuB,CAAC,IAAI,CAAC,EAClC,KAAK,EAAE,EAAE,EACT,MAAM,EAAE,0BAA0B,CAAC,IAAI,CAAC,EACxC,GAAG,EAAE,4BAA4B,KAC7B,OAAO,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC,CAAC;AAE1C,qBAAa,yBAAyB,CACrC,IAAI,SAAS,oBAAoB,CAAC,OAAO,EAAE,OAAO,CAAC,EACnD,EAAE,EACF,GAAG;IAEH,QAAQ,CAAC,IAAI,EAAE,2BAA2B,CAA+B;IACzE,QAAQ,CAAC,eAAe,EAAE,iCAAiC,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC,CAAC;gBAG1E,eAAe,EAAE,iCAAiC,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC;CAIlE"}
@@ -0,0 +1,40 @@
1
+ import { Readable } from 'stream';
2
+ import { FileReference } from './OperationHandler';
3
+ /**
4
+ * A file produced by a composite operation that should be written to Tray
5
+ * file storage. `content` may be a stream or an in-memory buffer; for a
6
+ * buffer `size` is derived automatically, for a stream it is optional but
7
+ * lets the runtime tune the multipart upload.
8
+ */
9
+ export type FileUpload = {
10
+ name: string;
11
+ contentType: string;
12
+ content: Readable | Buffer;
13
+ size?: number;
14
+ };
15
+ /**
16
+ * The content of a `FileReference` resolved by the runtime.
17
+ */
18
+ export type FileDownload = {
19
+ stream: Readable;
20
+ size?: number;
21
+ contentType?: string;
22
+ };
23
+ /**
24
+ * Promise-based file handling exposed to composite operation handlers as
25
+ * `cdk.files`. A rejected promise is mapped to a connector error by the
26
+ * runtime if the handler does not catch it.
27
+ */
28
+ export type FileHandling = {
29
+ upload(file: FileUpload): Promise<FileReference>;
30
+ download(file: FileReference): Promise<FileDownload>;
31
+ };
32
+ /**
33
+ * Dependencies the CDK provides to a composite operation handler, injected as
34
+ * its fourth argument (`cdk`). Unlike `ctx`, this is not a serialisable value:
35
+ * it is implemented and provided by `@trayio/cdk-runtime`.
36
+ */
37
+ export type OperationHandlerDependencies = {
38
+ files: FileHandling;
39
+ };
40
+ //# sourceMappingURL=OperationHandlerDependencies.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OperationHandlerDependencies.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/OperationHandlerDependencies.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG;IAC1B,MAAM,EAAE,QAAQ,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG;IAC1B,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACjD,QAAQ,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CACrD,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,4BAA4B,GAAG;IAC1C,KAAK,EAAE,YAAY,CAAC;CACpB,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/cdk-dsl",
3
- "version": "7.0.0",
3
+ "version": "7.2.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": "7.0.0",
17
+ "@trayio/commons": "7.2.0",
18
18
  "fp-ts": "2.12.3"
19
19
  },
20
20
  "typesVersions": {