@trayio/cdk-dsl 6.0.0 → 6.2.0-unstable.38a8cdc

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,79 @@ export const myOperationHandler =
1482
1482
  );
1483
1483
  ```
1484
1484
 
1485
+ ### Handling files in composite operations
1486
+
1487
+ Composite handlers receive a fourth argument, `runtime`, with Promise-based file handling under `runtime.files`. 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
+ ```typescript
1490
+ import { FileReference } from '@trayio/cdk-dsl/dist/connector/operation/OperationHandler';
1491
+ import {
1492
+ FileUpload,
1493
+ FileDownload,
1494
+ OperationHandlerRuntime,
1495
+ } from '@trayio/cdk-dsl/dist/connector/operation/OperationHandlerRuntime';
1496
+
1497
+ type FileUpload = {
1498
+ name: string;
1499
+ contentType: string;
1500
+ content: Readable | Buffer;
1501
+ size?: number; // optional for streams, lets the runtime tune the upload; derived for Buffers
1502
+ };
1503
+
1504
+ type FileDownload = {
1505
+ stream: Readable;
1506
+ size?: number;
1507
+ contentType?: string;
1508
+ };
1509
+
1510
+ type OperationHandlerRuntime = {
1511
+ files: {
1512
+ upload(file: FileUpload): Promise<FileReference>;
1513
+ download(file: FileReference): Promise<FileDownload>;
1514
+ };
1515
+ };
1516
+ ```
1517
+
1518
+ - `runtime.files.download(fileReference)` resolves the reference to a readable stream (plus `size` and `contentType` when known).
1519
+ - `runtime.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.
1520
+
1521
+ ```typescript
1522
+ // input.ts
1523
+ import { FileReference } from '@trayio/cdk-dsl/dist/connector/operation/OperationHandler';
1524
+
1525
+ export type ResizeImageInput = {
1526
+ file: FileReference;
1527
+ width: number;
1528
+ };
1529
+ ```
1530
+
1531
+ ```typescript
1532
+ // output.ts
1533
+ import { FileReference } from '@trayio/cdk-dsl/dist/connector/operation/OperationHandler';
1534
+
1535
+ export type ResizeImageOutput = {
1536
+ file: FileReference;
1537
+ };
1538
+ ```
1539
+
1540
+ ```typescript
1541
+ // handler.ts
1542
+ handler.usingComposite(async (ctx, input, invoke, runtime) => {
1543
+ const { stream } = await runtime.files.download(input.file);
1544
+ const resized = stream.pipe(sharp().resize(input.width));
1545
+ const file = await runtime.files.upload({
1546
+ name: input.file.name,
1547
+ contentType: input.file.mime_type,
1548
+ content: resized,
1549
+ });
1550
+ return OperationHandlerResult.success({ file });
1551
+ });
1552
+ ```
1553
+
1554
+ 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 `runtime` argument is optional to declare: existing three-argument composite handlers keep working unchanged.
1555
+
1556
+ 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.
1557
+
1485
1558
  ### How to enable / disable raw http operation.
1486
1559
 
1487
1560
  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 { OperationHandlerRuntime } from './OperationHandlerRuntime';
4
+ export type CompositeOperationHandlerFunction<AUTH extends OperationHandlerAuth<unknown, unknown>, IN, OUT> = (ctx: OperationHandlerContext<AUTH>, input: IN, invoke: OperationHandlerInvocation<AUTH>, runtime: OperationHandlerRuntime) => 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,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAEpE,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,OAAO,EAAE,uBAAuB,KAC5B,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,42 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { Readable } from 'stream';
4
+ import { FileReference } from './OperationHandler';
5
+ /**
6
+ * A file produced by a composite operation that should be written to Tray
7
+ * file storage. `content` may be a stream or an in-memory buffer; for a
8
+ * buffer `size` is derived automatically, for a stream it is optional but
9
+ * lets the runtime tune the multipart upload.
10
+ */
11
+ export type FileUpload = {
12
+ name: string;
13
+ contentType: string;
14
+ content: Readable | Buffer;
15
+ size?: number;
16
+ };
17
+ /**
18
+ * The content of a `FileReference` resolved by the runtime.
19
+ */
20
+ export type FileDownload = {
21
+ stream: Readable;
22
+ size?: number;
23
+ contentType?: string;
24
+ };
25
+ /**
26
+ * Promise-based file handling exposed to composite operation handlers.
27
+ * A rejected promise is mapped to a connector error by the runtime if the
28
+ * handler does not catch it.
29
+ */
30
+ export type FileHandling = {
31
+ upload(file: FileUpload): Promise<FileReference>;
32
+ download(file: FileReference): Promise<FileDownload>;
33
+ };
34
+ /**
35
+ * Runtime services injected as the fourth argument of a composite operation
36
+ * handler. Unlike `ctx`, this is not a serialisable value: it is implemented
37
+ * and provided by `@trayio/cdk-runtime`.
38
+ */
39
+ export type OperationHandlerRuntime = {
40
+ files: FileHandling;
41
+ };
42
+ //# sourceMappingURL=OperationHandlerRuntime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OperationHandlerRuntime.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/OperationHandlerRuntime.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,uBAAuB,GAAG;IACrC,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": "6.0.0",
3
+ "version": "6.2.0-unstable.38a8cdc",
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": "6.0.0",
17
+ "@trayio/commons": "6.2.0-unstable.38a8cdc",
18
18
  "fp-ts": "2.12.3"
19
19
  },
20
20
  "typesVersions": {