@omnia/velcron 8.0.539-dev → 8.0.542-dev

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.
@@ -0,0 +1,4 @@
1
+ import { IActionHandler } from "./IActionHandler";
2
+ export declare const bridgeGet: IActionHandler;
3
+ export declare const bridgePost: IActionHandler;
4
+ export declare const bridgePut: IActionHandler;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bridgePut = exports.bridgePost = exports.bridgeGet = void 0;
4
+ const Http_1 = require("./Http");
5
+ const BRIDGE_GUID = "a4e4586b-8612-4dc9-a9bb-15af1c34a8cf";
6
+ function buildBridgeUrl(path) {
7
+ const cleanPath = path.startsWith("/") ? path.substring(1) : path;
8
+ return `${BRIDGE_GUID}/api/bridgescenarios/webhook/${cleanPath}`;
9
+ }
10
+ exports.bridgeGet = {
11
+ name: "@bridge.get",
12
+ action: (action, incomingPath) => {
13
+ const url = buildBridgeUrl(incomingPath);
14
+ return (0, Http_1.handleHttpAction)("get", action, url);
15
+ }
16
+ };
17
+ exports.bridgePost = {
18
+ name: "@bridge.post",
19
+ action: (action, incomingPath, body) => {
20
+ const url = buildBridgeUrl(incomingPath);
21
+ return (0, Http_1.handleHttpAction)("post", action, url, body);
22
+ }
23
+ };
24
+ exports.bridgePut = {
25
+ name: "@bridge.put",
26
+ action: (action, incomingPath, body) => {
27
+ const url = buildBridgeUrl(incomingPath);
28
+ return (0, Http_1.handleHttpAction)("put", action, url, body);
29
+ }
30
+ };
package/actions/Http.d.ts CHANGED
@@ -1,2 +1,6 @@
1
1
  import { IActionHandler } from "./IActionHandler";
2
2
  export declare const httpGet: IActionHandler;
3
+ export declare const httpPost: IActionHandler;
4
+ export declare const httpPut: IActionHandler;
5
+ export declare const httpDelete: IActionHandler;
6
+ export declare function handleHttpAction(verb: "get" | "post" | "put" | "delete", action: any, incomingUrl: string, body?: string): Promise<any>;
package/actions/Http.js CHANGED
@@ -1,30 +1,72 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.httpGet = void 0;
3
+ exports.httpDelete = exports.httpPut = exports.httpPost = exports.httpGet = void 0;
4
+ exports.handleHttpAction = handleHttpAction;
4
5
  const parser_1 = require("../parser");
6
+ const shared_1 = require("internal/fx/shared");
5
7
  exports.httpGet = {
6
8
  name: "@http.get",
7
- action: (action, incomingUrl) => {
8
- incomingUrl = parser_1.VelcronDataBinder.bind(action, incomingUrl);
9
- let requestInitLocal = { "method": "GET" };
10
- const hookResult = parser_1.VelcronActions.tryTriggerBeforeActionHook("http.get", { url: incomingUrl, requestInit: requestInitLocal });
11
- incomingUrl = hookResult?.url ?? incomingUrl;
12
- requestInitLocal = hookResult?.requestInit ?? requestInitLocal;
13
- fetch(incomingUrl, requestInitLocal)
14
- .then(async (response) => {
15
- const res = parser_1.VelcronActions.tryTriggerAfterActionHook("http.get", {
16
- json: await response.json(),
17
- response: response
18
- });
19
- return res.json;
20
- })
21
- .then((json) => {
22
- action.promise.resolve(json);
23
- })
24
- .catch((error) => {
25
- //How do we handle errors?
26
- console.error("Velcron @http.get error occurred:", error);
27
- throw error;
28
- });
29
- }
9
+ action: (action, incomingUrl) => handleHttpAction("get", action, incomingUrl)
10
+ };
11
+ exports.httpPost = {
12
+ name: "@http.post",
13
+ action: (action, incomingUrl, body) => handleHttpAction("post", action, incomingUrl, body)
14
+ };
15
+ exports.httpPut = {
16
+ name: "@http.put",
17
+ action: (action, incomingUrl, body) => handleHttpAction("put", action, incomingUrl, body)
30
18
  };
19
+ exports.httpDelete = {
20
+ name: "@http.delete",
21
+ action: (action, incomingUrl, body) => handleHttpAction("delete", action, incomingUrl, body)
22
+ };
23
+ async function handleHttpAction(verb, action, incomingUrl, body) {
24
+ incomingUrl = parser_1.VelcronDataBinder.bind(action, incomingUrl);
25
+ if (body !== undefined) {
26
+ body = parser_1.VelcronData.getOrSet(action, body);
27
+ }
28
+ const urlPattern = /^https?:\/\//i;
29
+ let urlToUse = incomingUrl;
30
+ // If incomingUrl starts with a guid, replace it using ServiceLocator
31
+ if (!urlPattern.test(incomingUrl)) {
32
+ const firstSlash = incomingUrl.indexOf("/");
33
+ let guidPart = incomingUrl;
34
+ let rest = "";
35
+ if (firstSlash !== -1) {
36
+ guidPart = incomingUrl.substring(0, firstSlash);
37
+ rest = incomingUrl.substring(firstSlash); // includes the slash
38
+ }
39
+ if ((0, shared_1.isValidGuid)((0, shared_1.maybeGuid)(guidPart))) {
40
+ urlToUse = shared_1.ServiceLocator.getUrl((0, shared_1.maybeGuid)(guidPart)) + rest;
41
+ }
42
+ }
43
+ // Always use http client with full url
44
+ const client = new shared_1.HttpBuilder().withJsonApiSupport().build();
45
+ const path = urlToUse;
46
+ const hookResult = parser_1.VelcronActions.tryTriggerBeforeActionHook(`http.${verb}`, { url: urlToUse, config: client._baseConfig, body });
47
+ urlToUse = hookResult?.url ?? urlToUse;
48
+ const config = hookResult?.config ?? client._baseConfig;
49
+ const requestBody = hookResult?.body ?? body;
50
+ let result;
51
+ if (verb === "get") {
52
+ result = await client.get(path).tryCatch();
53
+ }
54
+ else if (verb === "post") {
55
+ result = await client.post(path, requestBody, config).tryCatch();
56
+ }
57
+ else if (verb === "put") {
58
+ result = await client.put(path, requestBody, config).tryCatch();
59
+ }
60
+ else if (verb === "delete") {
61
+ result = await client.delete(path, config).tryCatch();
62
+ }
63
+ if (result.error) {
64
+ parser_1.VelcronLogger.logError(`Velcron @http.${verb} error occurred:`);
65
+ return null;
66
+ }
67
+ const afterHookResult = parser_1.VelcronActions.tryTriggerAfterActionHook(`http.${verb}`, {
68
+ data: result.success.data,
69
+ response: result.success
70
+ });
71
+ return afterHookResult?.data ?? result.success.data;
72
+ }
@@ -12,3 +12,4 @@ export * from "./Emit";
12
12
  export * from "./Timer";
13
13
  export * from "./Date";
14
14
  export * from "./Clone";
15
+ export * from "./Bridge";
package/actions/index.js CHANGED
@@ -15,3 +15,4 @@ tslib_1.__exportStar(require("./Emit"), exports);
15
15
  tslib_1.__exportStar(require("./Timer"), exports);
16
16
  tslib_1.__exportStar(require("./Date"), exports);
17
17
  tslib_1.__exportStar(require("./Clone"), exports);
18
+ tslib_1.__exportStar(require("./Bridge"), exports);
@@ -1,10 +1,11 @@
1
+ import { HttpConfig } from "internal/fx/shared";
1
2
  export interface VelcronActionHooks<TBefore = any, TAfter = any> {
2
3
  beforeHandler?: (args: TBefore) => TBefore;
3
4
  afterHandler?: (args: TBefore) => TAfter;
4
5
  }
5
6
  export interface HttpGetBeforeActionHookArgs {
6
7
  url: string;
7
- requestInit: RequestInit;
8
+ config: HttpConfig;
8
9
  }
9
10
  export interface HttpGetAfterActionHookArgs {
10
11
  json: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@omnia/velcron",
3
3
  "license": "MIT",
4
- "version": "8.0.539-dev",
4
+ "version": "8.0.542-dev",
5
5
  "description": "Provide Omnia Velcron Stuffs.",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1"