@trayio/cdk-runtime 2.4.0 → 2.6.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.
@@ -1 +1 @@
1
- {"version":3,"file":"HttpOperationExecution.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/HttpOperationExecution.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,oBAAoB,EACpB,uBAAuB,EACvB,sBAAsB,EAEtB,MAAM,sDAAsD,CAAC;AAC9D,OAAO,EAEN,oBAAoB,EAKpB,MAAM,0DAA0D,CAAC;AAClE,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAQ7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,qBAAa,sBAAsB,CAClC,IAAI,SAAS,oBAAoB,CAAC,OAAO,EAAE,OAAO,CAAC,EACnD,EAAE,EACF,GAAG,CACF,YAAW,kBAAkB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC;IAE7C,OAAO,CAAC,iBAAiB,CAAoB;IAC7C,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,OAAO,CAAsC;gBAGpD,UAAU,EAAE,UAAU,EACtB,OAAO,EAAE,oBAAoB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC;IAOvC,OAAO,CACZ,GAAG,EAAE,uBAAuB,CAAC,IAAI,CAAC,EAClC,KAAK,EAAE,EAAE,GACP,OAAO,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC;IAyCvC,OAAO,CAAC,6BAA6B;IA4BrC,OAAO,CAAC,eAAe;IAKvB,OAAO,CAAC,eAAe;IAKvB,OAAO,CAAC,oBAAoB;IAI5B,OAAO,CAAC,eAAe;IAIvB,OAAO,CAAC,kBAAkB;IAI1B,OAAO,CAAC,oBAAoB;IAc5B,OAAO,CAAC,aAAa;IAiCrB,OAAO,CAAC,eAAe;IAmBvB,OAAO,CAAC,cAAc;IAMtB,OAAO,CAAC,eAAe;IAUvB,OAAO,CAAC,gBAAgB;CAexB"}
1
+ {"version":3,"file":"HttpOperationExecution.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/HttpOperationExecution.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,oBAAoB,EACpB,uBAAuB,EACvB,sBAAsB,EAGtB,MAAM,sDAAsD,CAAC;AAC9D,OAAO,EAEN,oBAAoB,EAKpB,MAAM,0DAA0D,CAAC;AAClE,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAc7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,qBAAa,sBAAsB,CAClC,IAAI,SAAS,oBAAoB,CAAC,OAAO,EAAE,OAAO,CAAC,EACnD,EAAE,EACF,GAAG,CACF,YAAW,kBAAkB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC;IAE7C,OAAO,CAAC,iBAAiB,CAAoB;IAC7C,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,OAAO,CAAsC;gBAGpD,UAAU,EAAE,UAAU,EACtB,OAAO,EAAE,oBAAoB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC;IAOvC,OAAO,CACZ,GAAG,EAAE,uBAAuB,CAAC,IAAI,CAAC,EAClC,KAAK,EAAE,EAAE,GACP,OAAO,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC;IAiDvC,OAAO,CAAC,6BAA6B;IA2BrC,OAAO,CAAC,eAAe;IAOvB,OAAO,CAAC,eAAe;IAOvB,OAAO,CAAC,oBAAoB;IAM5B,OAAO,CAAC,eAAe;IA0BvB,OAAO,CAAC,kBAAkB;IAI1B,OAAO,CAAC,oBAAoB;IAc5B,OAAO,CAAC,aAAa;IAiCrB,OAAO,CAAC,eAAe;IAmBvB,OAAO,CAAC,cAAc;IAMtB,OAAO,CAAC,eAAe;IAUvB,OAAO,CAAC,gBAAgB;CAexB"}
@@ -38,6 +38,7 @@ const HttpOperationHandler_1 = require("@trayio/cdk-dsl/connector/operation/Http
38
38
  const Http_1 = require("@trayio/commons/http/Http");
39
39
  const JsonSerialization_1 = require("@trayio/commons/serialization/JsonSerialization");
40
40
  const TE = __importStar(require("fp-ts/TaskEither"));
41
+ const E = __importStar(require("fp-ts/Either"));
41
42
  const O = __importStar(require("fp-ts/Option"));
42
43
  const function_1 = require("fp-ts/function");
43
44
  const BufferExtensions_1 = require("@trayio/commons/buffer/BufferExtensions");
@@ -50,8 +51,11 @@ class HttpOperationExecution {
50
51
  execute(ctx, input) {
51
52
  return __awaiter(this, void 0, void 0, function* () {
52
53
  const operationRequest = this.handler.requestHandler(ctx, input, this.handler.request);
53
- const httpRequest = this.operationRequestToHttpRequest(operationRequest);
54
- const resultTE = this.httpClient.execute(operationRequest.method, operationRequest.path, httpRequest);
54
+ const httpRequestE = yield this.operationRequestToHttpRequest(operationRequest)();
55
+ if (E.isLeft(httpRequestE)) {
56
+ return OperationHandler_1.OperationHandlerResult.failure(OperationHandler_1.OperationHandlerError.connectorError(httpRequestE.left.message));
57
+ }
58
+ const resultTE = this.httpClient.execute(operationRequest.method, operationRequest.path, httpRequestE.right);
55
59
  const operationResponseTE = (0, function_1.pipe)(resultTE,
56
60
  // get the dsl http response object
57
61
  TE.map((response) => this.handler.responseHandler(ctx, input, new HttpOperationHandler_1.HttpOperationResponseBuilder(response, this.defaultErrorHandling(response.statusCode)))),
@@ -62,14 +66,13 @@ class HttpOperationExecution {
62
66
  return TE.toUnion(operationResponseTE)();
63
67
  });
64
68
  }
65
- // TODO: Add file support, the output may need to be changed to TastEither<Error, HttpRequest>
66
69
  operationRequestToHttpRequest(operationRequest) {
67
- const serializedBody = (0, function_1.pipe)(operationRequest.contentType, O.map((contentType) => {
70
+ return (0, function_1.pipe)(operationRequest.contentType, O.map((contentType) => {
68
71
  switch (contentType) {
69
72
  case Http_1.HttpContentType.Json:
70
73
  return this.serializeAsJson(operationRequest.body);
71
74
  case Http_1.HttpContentType.OctetStream:
72
- return this.serializeAsFile(operationRequest.body);
75
+ return this.serializeAsFile(operationRequest);
73
76
  case Http_1.HttpContentType.MultipartRequestBody:
74
77
  return this.serializeAsMultipart(operationRequest.body);
75
78
  case Http_1.HttpContentType.Text:
@@ -77,25 +80,35 @@ class HttpOperationExecution {
77
80
  default:
78
81
  return this.serializeAsJson(operationRequest.body);
79
82
  }
80
- }), O.getOrElse(() => this.serializeEmptyBody()));
81
- return Object.assign(Object.assign({}, operationRequest.request), { body: serializedBody });
83
+ }), O.getOrElse(() => this.serializeEmptyBody()), TE.map((serializedBody) => (Object.assign(Object.assign({}, operationRequest.request), { body: serializedBody }))));
82
84
  }
83
85
  serializeAsJson(body) {
84
86
  const serializedBody = this.jsonSerialization.serialize(body);
85
- return BufferExtensions_1.BufferExtensions.arrayBufferToReadable(serializedBody);
87
+ return TE.right(BufferExtensions_1.BufferExtensions.arrayBufferToReadable(serializedBody));
86
88
  }
87
89
  serializeAsText(body) {
88
90
  const serializedBody = new TextEncoder().encode(body);
89
- return BufferExtensions_1.BufferExtensions.arrayBufferToReadable(serializedBody);
91
+ return TE.right(BufferExtensions_1.BufferExtensions.arrayBufferToReadable(serializedBody));
90
92
  }
91
93
  serializeAsMultipart(body) {
92
94
  throw new Error('unimplemented');
93
95
  }
94
- serializeAsFile(body) {
95
- throw new Error('unimplemented');
96
+ serializeAsFile(operationRequest) {
97
+ const downloadFilefromReference = this.httpClient.execute(Http_1.HttpMethod.Get, operationRequest.body.url, {
98
+ headers: { [Http_1.HttpHeader.ContentType]: Http_1.HttpContentType.OctetStream },
99
+ pathParams: {},
100
+ queryString: {},
101
+ body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0)),
102
+ });
103
+ return (0, function_1.pipe)(downloadFilefromReference, TE.chain((response) => {
104
+ if (response.statusCode >= 300) {
105
+ return TE.left(Error('error downloading file from source'));
106
+ }
107
+ return TE.right(response.body);
108
+ }));
96
109
  }
97
110
  serializeEmptyBody() {
98
- return BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0));
111
+ return TE.right(BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0)));
99
112
  }
100
113
  defaultErrorHandling(statusCode) {
101
114
  return () => OperationHandler_1.OperationHandlerResult.failure(OperationHandler_1.OperationHandlerError.apiError(`API returned a status code of ${statusCode}`, {
@@ -1 +1 @@
1
- {"version":3,"file":"HttpOperationExecutionFactory.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/HttpOperationExecutionFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,sDAAsD,CAAC;AAC5F,OAAO,EAAE,oBAAoB,EAAE,MAAM,0DAA0D,CAAC;AAChG,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAG1D,qBAAa,6BAA6B;IACzC,OAAO,CAAC,UAAU,CAAa;gBAEnB,UAAU,EAAE,UAAU;IAIlC,aAAa,CAAC,IAAI,SAAS,oBAAoB,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,EAAE,GAAG,EACzE,OAAO,EAAE,oBAAoB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC,GAC1C,kBAAkB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC;CAGpC"}
1
+ {"version":3,"file":"HttpOperationExecutionFactory.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/HttpOperationExecutionFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,sDAAsD,CAAC;AAC5F,OAAO,EAAE,oBAAoB,EAAE,MAAM,0DAA0D,CAAC;AAChG,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAG1D,qBAAa,6BAA6B;IAC7B,OAAO,CAAC,UAAU;gBAAV,UAAU,EAAE,UAAU;IAE1C,aAAa,CAAC,IAAI,SAAS,oBAAoB,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,EAAE,GAAG,EACzE,OAAO,EAAE,oBAAoB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC,GAC1C,kBAAkB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC;CAGpC"}
@@ -1,6 +1,6 @@
1
+ import * as O from 'fp-ts/Option';
1
2
  import { OperationHandlerAuth, OperationHandlerReference } from '@trayio/cdk-dsl/connector/operation/OperationHandler';
2
3
  import { HttpClient } from '@trayio/commons/http/HttpClient';
3
- import * as O from 'fp-ts/Option';
4
4
  import { OperationHandlerInvocationFactory } from './OperationHandlerInvocationFactory';
5
5
  import { OperationExecution } from './OperationExecution';
6
6
  export declare class OperationExecutionGateway {
@@ -1 +1 @@
1
- {"version":3,"file":"OperationExecutionGateway.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/OperationExecutionGateway.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,oBAAoB,EAEpB,yBAAyB,EAGzB,MAAM,sDAAsD,CAAC;AAE9D,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAG1D,qBAAa,yBAAyB;IACrC,OAAO,CAAC,gBAAgB,CAA4B;IACpD,iCAAiC,EAAE,iCAAiC,CAAC;gBAEzD,UAAU,EAAE,UAAU;IA6BlC,MAAM,CAAC,IAAI,SAAS,oBAAoB,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,EAAE,GAAG,EAClE,gBAAgB,EAAE,yBAAyB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC,EAC1D,cAAc,EAAE,OAAO,GACrB,CAAC,CAAC,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC,CAAC;CAO9C"}
1
+ {"version":3,"file":"OperationExecutionGateway.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/OperationExecutionGateway.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAGlC,OAAO,EACN,oBAAoB,EAEpB,yBAAyB,EAGzB,MAAM,sDAAsD,CAAC;AAE9D,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAE7D,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAG1D,qBAAa,yBAAyB;IACrC,OAAO,CAAC,gBAAgB,CAA4B;IACpD,iCAAiC,EAAE,iCAAiC,CAAC;gBAEzD,UAAU,EAAE,UAAU;IA6BlC,MAAM,CAAC,IAAI,SAAS,oBAAoB,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,EAAE,GAAG,EAClE,gBAAgB,EAAE,yBAAyB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC,EAC1D,cAAc,EAAE,OAAO,GACrB,CAAC,CAAC,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,CAAC,CAAC;CAO9C"}
@@ -24,10 +24,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.OperationExecutionGateway = void 0;
27
- const OperationHandler_1 = require("@trayio/cdk-dsl/connector/operation/OperationHandler");
28
- const OperationHandlerSetup_1 = require("@trayio/cdk-dsl/connector/operation/OperationHandlerSetup");
29
27
  const O = __importStar(require("fp-ts/Option"));
30
28
  const function_1 = require("fp-ts/function");
29
+ const OperationHandler_1 = require("@trayio/cdk-dsl/connector/operation/OperationHandler");
30
+ const OperationHandlerSetup_1 = require("@trayio/cdk-dsl/connector/operation/OperationHandlerSetup");
31
31
  const OperationExecutionFactory_1 = require("./OperationExecutionFactory");
32
32
  class OperationExecutionGateway {
33
33
  constructor(httpClient) {
@@ -1 +1 @@
1
- {"version":3,"file":"OperationExecutionGateway.unit.test.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/OperationExecutionGateway.unit.test.ts"],"names":[],"mappings":"AAGA,OAAO,8BAA8B,CAAC"}
1
+ {"version":3,"file":"OperationExecutionGateway.unit.test.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/OperationExecutionGateway.unit.test.ts"],"names":[],"mappings":"AAIA,OAAO,8BAA8B,CAAC"}
@@ -1,62 +1,179 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  const OperationHandlerSetup_1 = require("@trayio/cdk-dsl/connector/operation/OperationHandlerSetup");
4
27
  const OperationHandlerTest_1 = require("@trayio/cdk-dsl/connector/operation/OperationHandlerTest");
5
28
  const OperationHandler_1 = require("@trayio/cdk-dsl/connector/operation/OperationHandler");
6
29
  require("./OperationHandlerTestRunner");
7
- const getProductOperation = OperationHandlerSetup_1.OperationHandlerSetup.configureHandler((handler) => handler
8
- .addInputValidation((inputValidation) => inputValidation
9
- .condition((ctx, input) => input.id > 0)
10
- .errorMessage(() => 'id must be bigger than zero'))
11
- .usingHttp((http) => http
12
- .get('https://jsonplaceholder.typicode.com/posts/:id')
13
- .handleRequest((ctx, input, request) => request.addPathParameter('id', input.id.toString()).withoutBody())
14
- .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())));
15
- OperationHandlerTest_1.OperationHandlerTestSetup.configureHandlerTest(getProductOperation, (handlerTest) => handlerTest
16
- .usingHandlerContext('test')
17
- .nothingBeforeAll()
18
- .testCase('should get a product', (testCase) => testCase
19
- .usingHandlerContext('test')
20
- .givenNothing()
21
- .when(() => ({ id: 2 }))
22
- .then(({ output }) => {
23
- const outputValue = OperationHandler_1.OperationHandlerResult.getSuccessfulValueOrFail(output);
24
- expect(outputValue.id).toEqual(2);
25
- })
26
- .finallyDoNothing())
27
- .testCase('should validate id', (testCase) => testCase
28
- .givenNothing()
29
- .when(() => ({ id: -1 }))
30
- .then(({ output }) => {
31
- expect(output.isFailure).toBe(true);
32
- })
33
- .finallyDoNothing())
34
- .nothingAfterAll());
35
- const getProductOperationAsText = OperationHandlerSetup_1.OperationHandlerSetup.configureHandler((handler) => handler
36
- .addInputValidation((inputValidation) => inputValidation
37
- .condition((ctx, input) => input.id > 0)
38
- .errorMessage(() => 'id must be bigger than zero'))
39
- .usingHttp((http) => http
40
- .get('https://jsonplaceholder.typicode.com/posts/:id')
41
- .handleRequest((ctx, input, request) => request.addPathParameter('id', input.id.toString()).withoutBody())
42
- .handleResponse((ctx, input, response) => response.parseWithBodyAsText((text) => OperationHandler_1.OperationHandlerResult.success({ textField: text })))));
43
- OperationHandlerTest_1.OperationHandlerTestSetup.configureHandlerTest(getProductOperationAsText, (handlerTest) => handlerTest
44
- .usingHandlerContext('test')
45
- .nothingBeforeAll()
46
- .testCase('should get a product', (testCase) => testCase
47
- .usingHandlerContext('test')
48
- .givenNothing()
49
- .when(() => ({ id: 2 }))
50
- .then(({ output }) => {
51
- const outputValue = OperationHandler_1.OperationHandlerResult.getSuccessfulValueOrFail(output);
52
- expect(outputValue.textField).toContain(`"id": 2`);
53
- })
54
- .finallyDoNothing())
55
- .testCase('should validate id', (testCase) => testCase
56
- .givenNothing()
57
- .when(() => ({ id: -1 }))
58
- .then(({ output }) => {
59
- expect(output.isFailure).toBe(true);
60
- })
61
- .finallyDoNothing())
62
- .nothingAfterAll());
30
+ const T = __importStar(require("fp-ts/Task"));
31
+ const TE = __importStar(require("fp-ts/TaskEither"));
32
+ const function_1 = require("fp-ts/function");
33
+ const Http_1 = require("@trayio/commons/http/Http");
34
+ const stream_1 = require("stream");
35
+ const ExpressHttpServer_1 = require("@trayio/express/http/ExpressHttpServer");
36
+ const ExpressHttpController_1 = require("@trayio/express/http/ExpressHttpController");
37
+ const NodeFsFileStorage_1 = require("@trayio/commons/file/NodeFsFileStorage");
38
+ const fileStorage = new NodeFsFileStorage_1.NodeFsFileStorage('/tmp');
39
+ class TestControllerHttp {
40
+ constructor() {
41
+ this.getEndpoints = () => [
42
+ {
43
+ path: `/posts/:id`,
44
+ method: Http_1.HttpMethod.Get,
45
+ execute: (request) => {
46
+ const { id } = request.pathParams;
47
+ return T.of({
48
+ body: stream_1.Readable.from(JSON.stringify({
49
+ id: parseInt(id, 10),
50
+ title: 'test',
51
+ })),
52
+ headers: {},
53
+ statusCode: 200,
54
+ });
55
+ },
56
+ },
57
+ {
58
+ path: `/image-upload`,
59
+ method: Http_1.HttpMethod.Post,
60
+ execute: (request) => {
61
+ const { body } = request;
62
+ return (0, function_1.pipe)(fileStorage.write({
63
+ content: body,
64
+ key: 'test.ico',
65
+ metadata: {
66
+ name: 'test.ico',
67
+ },
68
+ }), TE.fold(() => T.of({
69
+ body: stream_1.Readable.from(JSON.stringify({
70
+ success: false,
71
+ })),
72
+ headers: {},
73
+ statusCode: 500,
74
+ }), () => T.of({
75
+ body: stream_1.Readable.from(JSON.stringify({
76
+ success: true,
77
+ })),
78
+ headers: {},
79
+ statusCode: 200,
80
+ })));
81
+ },
82
+ },
83
+ ];
84
+ }
85
+ }
86
+ const testHttpController = new ExpressHttpController_1.ExpressHttpController(new TestControllerHttp(), '/tmp');
87
+ describe('OperationExecutionGateway', () => {
88
+ let server;
89
+ beforeAll(() => {
90
+ server = (0, ExpressHttpServer_1.startServer)([testHttpController], { port: 3000 });
91
+ });
92
+ afterAll(() => {
93
+ server.close();
94
+ });
95
+ describe('get a product', () => {
96
+ const getProductOperation = OperationHandlerSetup_1.OperationHandlerSetup.configureHandler((handler) => handler
97
+ .addInputValidation((inputValidation) => inputValidation
98
+ .condition((ctx, input) => input.id > 0)
99
+ .errorMessage(() => 'id must be bigger than zero'))
100
+ .usingHttp((http) => http
101
+ .get('http://localhost:3000/posts/:id')
102
+ .handleRequest((ctx, input, request) => request.addPathParameter('id', input.id.toString()).withoutBody())
103
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())));
104
+ OperationHandlerTest_1.OperationHandlerTestSetup.configureHandlerTest(getProductOperation, (handlerTest) => handlerTest
105
+ .usingHandlerContext('test')
106
+ .nothingBeforeAll()
107
+ .testCase('should get a product', (testCase) => testCase
108
+ .usingHandlerContext('test')
109
+ .givenNothing()
110
+ .when(() => ({ id: 2 }))
111
+ .then(({ output }) => {
112
+ const outputValue = OperationHandler_1.OperationHandlerResult.getSuccessfulValueOrFail(output);
113
+ expect(outputValue.id).toEqual(2);
114
+ })
115
+ .finallyDoNothing())
116
+ .testCase('should validate id', (testCase) => testCase
117
+ .givenNothing()
118
+ .when(() => ({ id: -1 }))
119
+ .then(({ output }) => {
120
+ expect(output.isFailure).toBe(true);
121
+ })
122
+ .finallyDoNothing())
123
+ .nothingAfterAll());
124
+ });
125
+ describe('get a product as text', () => {
126
+ const getProductOperationAsText = OperationHandlerSetup_1.OperationHandlerSetup.configureHandler((handler) => handler
127
+ .addInputValidation((inputValidation) => inputValidation
128
+ .condition((ctx, input) => input.id > 0)
129
+ .errorMessage(() => 'id must be bigger than zero'))
130
+ .usingHttp((http) => http
131
+ .get('http://localhost:3000/posts/:id')
132
+ .handleRequest((ctx, input, request) => request.addPathParameter('id', input.id.toString()).withoutBody())
133
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsText((text) => OperationHandler_1.OperationHandlerResult.success({ textField: text })))));
134
+ OperationHandlerTest_1.OperationHandlerTestSetup.configureHandlerTest(getProductOperationAsText, (handlerTest) => handlerTest
135
+ .usingHandlerContext('test')
136
+ .nothingBeforeAll()
137
+ .testCase('should get a product as text', (testCase) => testCase
138
+ .usingHandlerContext('test')
139
+ .givenNothing()
140
+ .when(() => ({ id: 2 }))
141
+ .then(({ output }) => {
142
+ const outputValue = OperationHandler_1.OperationHandlerResult.getSuccessfulValueOrFail(output);
143
+ expect(outputValue.textField).toContain(`"id":2`);
144
+ })
145
+ .finallyDoNothing())
146
+ .testCase('should validate id', (testCase) => testCase
147
+ .givenNothing()
148
+ .when(() => ({ id: -1 }))
149
+ .then(({ output }) => {
150
+ expect(output.isFailure).toBe(true);
151
+ })
152
+ .finallyDoNothing())
153
+ .nothingAfterAll());
154
+ });
155
+ describe('upload an image', () => {
156
+ const uploadImageOperation = OperationHandlerSetup_1.OperationHandlerSetup.configureHandler((handler) => handler.usingHttp((http) => http
157
+ .post('http://localhost:3000/image-upload')
158
+ .handleRequest((ctx, input, request) => request.withBodyAsFile({
159
+ name: 'test-image',
160
+ url: 'https://tray.io/favicon.ico',
161
+ mime_type: 'image/png',
162
+ expires: 0,
163
+ }))
164
+ .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())));
165
+ OperationHandlerTest_1.OperationHandlerTestSetup.configureHandlerTest(uploadImageOperation, (handlerTest) => handlerTest
166
+ .usingHandlerContext('test')
167
+ .nothingBeforeAll()
168
+ .testCase('should upload an image', (testCase) => testCase
169
+ .usingHandlerContext('test')
170
+ .givenNothing()
171
+ .when(() => ({}))
172
+ .then(({ output }) => {
173
+ const outputValue = OperationHandler_1.OperationHandlerResult.getSuccessfulValueOrFail(output);
174
+ expect(outputValue.success).toEqual(true);
175
+ })
176
+ .finallyDoNothing())
177
+ .nothingAfterAll());
178
+ });
179
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/cdk-runtime",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "description": "A Runtime that executes connector operations defined using the CDK DSL",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -14,9 +14,10 @@
14
14
  "node": ">=18.x"
15
15
  },
16
16
  "dependencies": {
17
- "@trayio/axios": "2.4.0",
18
- "@trayio/cdk-dsl": "2.4.0",
19
- "@trayio/winston": "2.4.0"
17
+ "@trayio/axios": "2.6.0",
18
+ "@trayio/cdk-dsl": "2.6.0",
19
+ "@trayio/express": "2.6.0",
20
+ "@trayio/winston": "2.6.0"
20
21
  },
21
22
  "typesVersions": {
22
23
  "*": {