@atom8n/rest-api-client 2.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 +22 -0
- package/dist/api/api-keys.cjs +8 -0
- package/dist/api/api-keys.d.cts +3 -0
- package/dist/api/api-keys.d.mts +3 -0
- package/dist/api/api-keys.mjs +4 -0
- package/dist/api/breaking-changes.cjs +18 -0
- package/dist/api/breaking-changes.cjs.map +1 -0
- package/dist/api/breaking-changes.d.cts +13 -0
- package/dist/api/breaking-changes.d.mts +13 -0
- package/dist/api/breaking-changes.mjs +16 -0
- package/dist/api/breaking-changes.mjs.map +1 -0
- package/dist/api/cloudPlans.cjs +9 -0
- package/dist/api/cloudPlans.d.cts +3 -0
- package/dist/api/cloudPlans.d.mts +3 -0
- package/dist/api/cloudPlans.mjs +4 -0
- package/dist/api/communityNodes.cjs +8 -0
- package/dist/api/communityNodes.d.cts +3 -0
- package/dist/api/communityNodes.d.mts +3 -0
- package/dist/api/communityNodes.mjs +4 -0
- package/dist/api/consent.cjs +14 -0
- package/dist/api/consent.cjs.map +1 -0
- package/dist/api/consent.d.cts +16 -0
- package/dist/api/consent.d.mts +16 -0
- package/dist/api/consent.mjs +13 -0
- package/dist/api/consent.mjs.map +1 -0
- package/dist/api/credentialResolvers.cjs +9 -0
- package/dist/api/credentialResolvers.d.cts +3 -0
- package/dist/api/credentialResolvers.d.mts +3 -0
- package/dist/api/credentialResolvers.mjs +4 -0
- package/dist/api/ctas.cjs +4 -0
- package/dist/api/ctas.d.cts +3 -0
- package/dist/api/ctas.d.mts +3 -0
- package/dist/api/ctas.mjs +4 -0
- package/dist/api/dynamic-banners.cjs +10 -0
- package/dist/api/dynamic-banners.cjs.map +1 -0
- package/dist/api/dynamic-banners.d.cts +25 -0
- package/dist/api/dynamic-banners.d.mts +25 -0
- package/dist/api/dynamic-banners.mjs +10 -0
- package/dist/api/dynamic-banners.mjs.map +1 -0
- package/dist/api/eventbus.ee.cjs +9 -0
- package/dist/api/eventbus.ee.d.cts +3 -0
- package/dist/api/eventbus.ee.d.mts +3 -0
- package/dist/api/eventbus.ee.mjs +4 -0
- package/dist/api/events.cjs +4 -0
- package/dist/api/events.d.cts +3 -0
- package/dist/api/events.d.mts +3 -0
- package/dist/api/events.mjs +4 -0
- package/dist/api/execution.cjs +0 -0
- package/dist/api/execution.d.cts +1 -0
- package/dist/api/execution.d.mts +1 -0
- package/dist/api/execution.mjs +1 -0
- package/dist/api/externalSecrets.ee.cjs +10 -0
- package/dist/api/externalSecrets.ee.d.cts +3 -0
- package/dist/api/externalSecrets.ee.d.mts +3 -0
- package/dist/api/externalSecrets.ee.mjs +4 -0
- package/dist/api/index.cjs +149 -0
- package/dist/api/index.d.cts +32 -0
- package/dist/api/index.d.mts +32 -0
- package/dist/api/index.mjs +31 -0
- package/dist/api/ldap.cjs +8 -0
- package/dist/api/ldap.d.cts +3 -0
- package/dist/api/ldap.d.mts +3 -0
- package/dist/api/ldap.mjs +4 -0
- package/dist/api/mfa.cjs +9 -0
- package/dist/api/mfa.d.cts +3 -0
- package/dist/api/mfa.d.mts +3 -0
- package/dist/api/mfa.mjs +4 -0
- package/dist/api/module-settings.cjs +4 -0
- package/dist/api/module-settings.d.cts +3 -0
- package/dist/api/module-settings.d.mts +3 -0
- package/dist/api/module-settings.mjs +4 -0
- package/dist/api/nodeTypes.cjs +13 -0
- package/dist/api/nodeTypes.d.cts +3 -0
- package/dist/api/nodeTypes.d.mts +3 -0
- package/dist/api/nodeTypes.mjs +4 -0
- package/dist/api/npsSurvey.cjs +4 -0
- package/dist/api/npsSurvey.d.cts +3 -0
- package/dist/api/npsSurvey.d.mts +3 -0
- package/dist/api/npsSurvey.mjs +4 -0
- package/dist/api/orchestration.cjs +4 -0
- package/dist/api/orchestration.d.cts +3 -0
- package/dist/api/orchestration.d.mts +3 -0
- package/dist/api/orchestration.mjs +4 -0
- package/dist/api/prompts.cjs +5 -0
- package/dist/api/prompts.d.cts +2 -0
- package/dist/api/prompts.d.mts +2 -0
- package/dist/api/prompts.mjs +4 -0
- package/dist/api/provisioning.cjs +5 -0
- package/dist/api/provisioning.d.cts +3 -0
- package/dist/api/provisioning.d.mts +3 -0
- package/dist/api/provisioning.mjs +4 -0
- package/dist/api/roles.cjs +8 -0
- package/dist/api/roles.d.cts +3 -0
- package/dist/api/roles.d.mts +3 -0
- package/dist/api/roles.mjs +4 -0
- package/dist/api/settings.cjs +4 -0
- package/dist/api/settings.d.cts +3 -0
- package/dist/api/settings.d.mts +3 -0
- package/dist/api/settings.mjs +4 -0
- package/dist/api/sso.cjs +12 -0
- package/dist/api/sso.d.cts +3 -0
- package/dist/api/sso.d.mts +3 -0
- package/dist/api/sso.mjs +4 -0
- package/dist/api/tags.cjs +0 -0
- package/dist/api/tags.d.cts +2 -0
- package/dist/api/tags.d.mts +2 -0
- package/dist/api/tags.mjs +1 -0
- package/dist/api/templates.cjs +10 -0
- package/dist/api/templates.d.cts +5 -0
- package/dist/api/templates.d.mts +5 -0
- package/dist/api/templates.mjs +4 -0
- package/dist/api/third-party-licenses.cjs +4 -0
- package/dist/api/third-party-licenses.d.cts +3 -0
- package/dist/api/third-party-licenses.d.mts +3 -0
- package/dist/api/third-party-licenses.mjs +4 -0
- package/dist/api/ui.cjs +4 -0
- package/dist/api/ui.d.cts +3 -0
- package/dist/api/ui.d.mts +3 -0
- package/dist/api/ui.mjs +4 -0
- package/dist/api/usage.cjs +8 -0
- package/dist/api/usage.d.cts +3 -0
- package/dist/api/usage.d.mts +3 -0
- package/dist/api/usage.mjs +4 -0
- package/dist/api/users.cjs +22 -0
- package/dist/api/users.d.cts +3 -0
- package/dist/api/users.d.mts +3 -0
- package/dist/api/users.mjs +4 -0
- package/dist/api/versions.cjs +5 -0
- package/dist/api/versions.d.cts +2 -0
- package/dist/api/versions.d.mts +2 -0
- package/dist/api/versions.mjs +4 -0
- package/dist/api/webhooks.cjs +4 -0
- package/dist/api/webhooks.d.cts +3 -0
- package/dist/api/webhooks.d.mts +3 -0
- package/dist/api/webhooks.mjs +4 -0
- package/dist/api/workflowHistory.cjs +6 -0
- package/dist/api/workflowHistory.d.cts +3 -0
- package/dist/api/workflowHistory.d.mts +3 -0
- package/dist/api/workflowHistory.mjs +4 -0
- package/dist/api/workflows.cjs +0 -0
- package/dist/api/workflows.d.cts +3 -0
- package/dist/api/workflows.d.mts +3 -0
- package/dist/api/workflows.mjs +1 -0
- package/dist/api-keys.cjs +51 -0
- package/dist/api-keys.cjs.map +1 -0
- package/dist/api-keys.d.cts +17 -0
- package/dist/api-keys.d.mts +17 -0
- package/dist/api-keys.mjs +22 -0
- package/dist/api-keys.mjs.map +1 -0
- package/dist/api.cjs +0 -0
- package/dist/api.mjs +1 -0
- package/dist/cloudPlans.cjs +60 -0
- package/dist/cloudPlans.cjs.map +1 -0
- package/dist/cloudPlans.d.cts +62 -0
- package/dist/cloudPlans.d.mts +62 -0
- package/dist/cloudPlans.mjs +25 -0
- package/dist/cloudPlans.mjs.map +1 -0
- package/dist/communityNodes.cjs +60 -0
- package/dist/communityNodes.cjs.map +1 -0
- package/dist/communityNodes.d.cts +12 -0
- package/dist/communityNodes.d.mts +12 -0
- package/dist/communityNodes.mjs +31 -0
- package/dist/communityNodes.mjs.map +1 -0
- package/dist/credentialResolvers.cjs +60 -0
- package/dist/credentialResolvers.cjs.map +1 -0
- package/dist/credentialResolvers.d.cts +21 -0
- package/dist/credentialResolvers.d.mts +21 -0
- package/dist/credentialResolvers.mjs +25 -0
- package/dist/credentialResolvers.mjs.map +1 -0
- package/dist/ctas.cjs +15 -0
- package/dist/ctas.cjs.map +1 -0
- package/dist/ctas.d.cts +7 -0
- package/dist/ctas.d.mts +7 -0
- package/dist/ctas.mjs +10 -0
- package/dist/ctas.mjs.map +1 -0
- package/dist/eventbus.ee.cjs +63 -0
- package/dist/eventbus.ee.cjs.map +1 -0
- package/dist/eventbus.ee.d.cts +16 -0
- package/dist/eventbus.ee.d.mts +16 -0
- package/dist/eventbus.ee.mjs +28 -0
- package/dist/eventbus.ee.mjs.map +1 -0
- package/dist/events.cjs +15 -0
- package/dist/events.cjs.map +1 -0
- package/dist/events.d.cts +7 -0
- package/dist/events.d.mts +7 -0
- package/dist/events.mjs +10 -0
- package/dist/events.mjs.map +1 -0
- package/dist/externalSecrets.ee.cjs +69 -0
- package/dist/externalSecrets.ee.cjs.map +1 -0
- package/dist/externalSecrets.ee.d.cts +18 -0
- package/dist/externalSecrets.ee.d.mts +18 -0
- package/dist/externalSecrets.ee.mjs +28 -0
- package/dist/externalSecrets.ee.mjs.map +1 -0
- package/dist/index.cjs +160 -0
- package/dist/index.d.cts +33 -0
- package/dist/index.d.mts +33 -0
- package/dist/index.mjs +31 -0
- package/dist/index2.d.cts +1 -0
- package/dist/index2.d.mts +1 -0
- package/dist/ldap.cjs +51 -0
- package/dist/ldap.cjs.map +1 -0
- package/dist/ldap.d.cts +55 -0
- package/dist/ldap.d.mts +55 -0
- package/dist/ldap.mjs +22 -0
- package/dist/ldap.mjs.map +1 -0
- package/dist/mfa.cjs +60 -0
- package/dist/mfa.cjs.map +1 -0
- package/dist/mfa.d.cts +24 -0
- package/dist/mfa.d.mts +24 -0
- package/dist/mfa.mjs +25 -0
- package/dist/mfa.mjs.map +1 -0
- package/dist/module-settings.cjs +15 -0
- package/dist/module-settings.cjs.map +1 -0
- package/dist/module-settings.d.cts +8 -0
- package/dist/module-settings.d.mts +8 -0
- package/dist/module-settings.mjs +10 -0
- package/dist/module-settings.mjs.map +1 -0
- package/dist/nodeTypes.cjs +109 -0
- package/dist/nodeTypes.cjs.map +1 -0
- package/dist/nodeTypes.d.cts +19 -0
- package/dist/nodeTypes.d.mts +19 -0
- package/dist/nodeTypes.mjs +49 -0
- package/dist/nodeTypes.mjs.map +1 -0
- package/dist/npsSurvey.cjs +15 -0
- package/dist/npsSurvey.cjs.map +1 -0
- package/dist/npsSurvey.d.cts +8 -0
- package/dist/npsSurvey.d.mts +8 -0
- package/dist/npsSurvey.mjs +10 -0
- package/dist/npsSurvey.mjs.map +1 -0
- package/dist/orchestration.cjs +16 -0
- package/dist/orchestration.cjs.map +1 -0
- package/dist/orchestration.d.cts +7 -0
- package/dist/orchestration.d.mts +7 -0
- package/dist/orchestration.mjs +11 -0
- package/dist/orchestration.mjs.map +1 -0
- package/dist/prompts.cjs +31 -0
- package/dist/prompts.cjs.map +1 -0
- package/dist/prompts.d.cts +14 -0
- package/dist/prompts.d.mts +14 -0
- package/dist/prompts.mjs +20 -0
- package/dist/prompts.mjs.map +1 -0
- package/dist/provisioning.cjs +24 -0
- package/dist/provisioning.cjs.map +1 -0
- package/dist/provisioning.d.cts +15 -0
- package/dist/provisioning.d.mts +15 -0
- package/dist/provisioning.mjs +13 -0
- package/dist/provisioning.mjs.map +1 -0
- package/dist/roles.cjs +51 -0
- package/dist/roles.cjs.map +1 -0
- package/dist/roles.d.cts +15 -0
- package/dist/roles.d.mts +15 -0
- package/dist/roles.mjs +22 -0
- package/dist/roles.mjs.map +1 -0
- package/dist/settings.cjs +15 -0
- package/dist/settings.cjs.map +1 -0
- package/dist/settings.d.cts +8 -0
- package/dist/settings.d.mts +8 -0
- package/dist/settings.mjs +10 -0
- package/dist/settings.mjs.map +1 -0
- package/dist/sso.cjs +87 -0
- package/dist/sso.cjs.map +1 -0
- package/dist/sso.d.cts +20 -0
- package/dist/sso.d.mts +20 -0
- package/dist/sso.mjs +34 -0
- package/dist/sso.mjs.map +1 -0
- package/dist/tags.d.cts +11 -0
- package/dist/tags.d.mts +11 -0
- package/dist/templates.cjs +83 -0
- package/dist/templates.cjs.map +1 -0
- package/dist/templates.d.cts +129 -0
- package/dist/templates.d.mts +129 -0
- package/dist/templates.mjs +42 -0
- package/dist/templates.mjs.map +1 -0
- package/dist/third-party-licenses.cjs +19 -0
- package/dist/third-party-licenses.cjs.map +1 -0
- package/dist/third-party-licenses.d.cts +7 -0
- package/dist/third-party-licenses.d.mts +7 -0
- package/dist/third-party-licenses.mjs +14 -0
- package/dist/third-party-licenses.mjs.map +1 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +2 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +1 -0
- package/dist/types2.d.cts +8 -0
- package/dist/types2.d.mts +8 -0
- package/dist/ui.cjs +15 -0
- package/dist/ui.cjs.map +1 -0
- package/dist/ui.d.cts +11 -0
- package/dist/ui.d.mts +11 -0
- package/dist/ui.mjs +10 -0
- package/dist/ui.mjs.map +1 -0
- package/dist/usage.cjs +51 -0
- package/dist/usage.cjs.map +1 -0
- package/dist/usage.d.cts +18 -0
- package/dist/usage.d.mts +18 -0
- package/dist/usage.mjs +22 -0
- package/dist/usage.mjs.map +1 -0
- package/dist/users.cjs +178 -0
- package/dist/users.cjs.map +1 -0
- package/dist/users.d.cts +133 -0
- package/dist/users.d.mts +133 -0
- package/dist/users.mjs +65 -0
- package/dist/users.mjs.map +1 -0
- package/dist/utils.cjs +13 -0
- package/dist/utils.d.cts +3 -0
- package/dist/utils.d.mts +3 -0
- package/dist/utils.mjs +3 -0
- package/dist/utils2.cjs +305 -0
- package/dist/utils2.cjs.map +1 -0
- package/dist/utils2.d.cts +42 -0
- package/dist/utils2.d.mts +42 -0
- package/dist/utils2.mjs +210 -0
- package/dist/utils2.mjs.map +1 -0
- package/dist/versions.cjs +28 -0
- package/dist/versions.cjs.map +1 -0
- package/dist/versions.d.cts +48 -0
- package/dist/versions.d.mts +48 -0
- package/dist/versions.mjs +17 -0
- package/dist/versions.mjs.map +1 -0
- package/dist/webhooks.cjs +15 -0
- package/dist/webhooks.cjs.map +1 -0
- package/dist/webhooks.d.cts +17 -0
- package/dist/webhooks.d.mts +17 -0
- package/dist/webhooks.mjs +10 -0
- package/dist/webhooks.mjs.map +1 -0
- package/dist/workflowHistory.cjs +36 -0
- package/dist/workflowHistory.cjs.map +1 -0
- package/dist/workflowHistory.d.cts +43 -0
- package/dist/workflowHistory.d.mts +43 -0
- package/dist/workflowHistory.mjs +19 -0
- package/dist/workflowHistory.mjs.map +1 -0
- package/dist/workflows.d.cts +47 -0
- package/dist/workflows.d.mts +47 -0
- package/package.json +58 -0
package/dist/utils2.cjs
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
let __n8n_constants = require("@n8n/constants");
|
|
25
|
+
let __n8n_utils_assert = require("@n8n/utils/assert");
|
|
26
|
+
let axios = require("axios");
|
|
27
|
+
axios = __toESM(axios);
|
|
28
|
+
let n8n_workflow = require("n8n-workflow");
|
|
29
|
+
|
|
30
|
+
//#region src/utils.ts
|
|
31
|
+
const getBrowserId = () => {
|
|
32
|
+
let browserId = localStorage.getItem(__n8n_constants.BROWSER_ID_STORAGE_KEY);
|
|
33
|
+
if (!browserId) {
|
|
34
|
+
browserId = crypto.randomUUID();
|
|
35
|
+
localStorage.setItem(__n8n_constants.BROWSER_ID_STORAGE_KEY, browserId);
|
|
36
|
+
}
|
|
37
|
+
return browserId;
|
|
38
|
+
};
|
|
39
|
+
const NO_NETWORK_ERROR_CODE = 999;
|
|
40
|
+
const STREAM_SEPARATOR = "⧉⇋⇋➽⌑⧉§§\n";
|
|
41
|
+
var MfaRequiredError = class extends n8n_workflow.ApplicationError {
|
|
42
|
+
constructor() {
|
|
43
|
+
super("MFA is required to access this resource. Please set up MFA in your user settings.");
|
|
44
|
+
this.name = "MfaRequiredError";
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var ResponseError = class extends n8n_workflow.ApplicationError {
|
|
48
|
+
httpStatusCode;
|
|
49
|
+
errorCode;
|
|
50
|
+
serverStackTrace;
|
|
51
|
+
meta;
|
|
52
|
+
/**
|
|
53
|
+
* Creates an instance of ResponseError.
|
|
54
|
+
* @param {string} message The error message
|
|
55
|
+
* @param {number} [errorCode] The error code which can be used by frontend to identify the actual error
|
|
56
|
+
* @param {number} [httpStatusCode] The HTTP status code the response should have
|
|
57
|
+
* @param {string} [stack] The stack trace
|
|
58
|
+
* @param {Record<string, unknown>} [meta] Additional metadata from the server
|
|
59
|
+
*/
|
|
60
|
+
constructor(message, options = {}) {
|
|
61
|
+
super(message);
|
|
62
|
+
this.name = "ResponseError";
|
|
63
|
+
const { errorCode, httpStatusCode, stack, meta } = options;
|
|
64
|
+
if (errorCode) this.errorCode = errorCode;
|
|
65
|
+
if (httpStatusCode) this.httpStatusCode = httpStatusCode;
|
|
66
|
+
if (stack) this.serverStackTrace = stack;
|
|
67
|
+
if (meta) this.meta = meta;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const legacyParamSerializer = (params) => Object.keys(params).filter((key) => params[key] !== void 0).map((key) => {
|
|
71
|
+
if (Array.isArray(params[key])) return params[key].map((v) => `${key}[]=${encodeURIComponent(v)}`).join("&");
|
|
72
|
+
if (typeof params[key] === "object") params[key] = JSON.stringify(params[key]);
|
|
73
|
+
return `${key}=${encodeURIComponent(params[key])}`;
|
|
74
|
+
}).join("&");
|
|
75
|
+
async function request(config) {
|
|
76
|
+
const { method, baseURL, endpoint, headers, data } = config;
|
|
77
|
+
let absoluteBaseURL = baseURL;
|
|
78
|
+
if (baseURL.startsWith("/")) absoluteBaseURL = (typeof window !== "undefined" && window.location?.origin?.startsWith("vscode-webview:") ? "http://localhost:5678" : typeof window !== "undefined" ? window.location?.origin : "http://localhost:5678") + baseURL;
|
|
79
|
+
const options = {
|
|
80
|
+
method,
|
|
81
|
+
url: endpoint,
|
|
82
|
+
baseURL: absoluteBaseURL,
|
|
83
|
+
headers: headers ?? {}
|
|
84
|
+
};
|
|
85
|
+
if (baseURL.startsWith("/")) options.headers["browser-id"] = getBrowserId();
|
|
86
|
+
if ({}.env.NODE_ENV !== "production" && !baseURL.includes("api.n8n.io") && !baseURL.includes("n8n.cloud")) options.withCredentials = options.withCredentials ?? true;
|
|
87
|
+
if ([
|
|
88
|
+
"POST",
|
|
89
|
+
"PATCH",
|
|
90
|
+
"PUT"
|
|
91
|
+
].includes(method)) options.data = data;
|
|
92
|
+
else if (data) {
|
|
93
|
+
options.params = data;
|
|
94
|
+
options.paramsSerializer = legacyParamSerializer;
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
return (await axios.default.request(options)).data;
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (error.message === "Network Error") throw new ResponseError("Can't connect to n8n.", { errorCode: NO_NETWORK_ERROR_CODE });
|
|
100
|
+
const errorResponseData = error.response?.data;
|
|
101
|
+
if (errorResponseData?.mfaRequired === true) throw new MfaRequiredError();
|
|
102
|
+
if (errorResponseData?.message !== void 0) {
|
|
103
|
+
if (errorResponseData.name === "NodeApiError") {
|
|
104
|
+
errorResponseData.httpStatusCode = error.response.status;
|
|
105
|
+
throw errorResponseData;
|
|
106
|
+
}
|
|
107
|
+
throw new ResponseError(errorResponseData.message, {
|
|
108
|
+
errorCode: errorResponseData.code,
|
|
109
|
+
httpStatusCode: error.response.status,
|
|
110
|
+
stack: errorResponseData.stack,
|
|
111
|
+
meta: errorResponseData.meta
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Sends a request to the API and returns the response without extracting the data key.
|
|
119
|
+
* @param context Rest API context
|
|
120
|
+
* @param method HTTP method
|
|
121
|
+
* @param endpoint relative path to the API endpoint
|
|
122
|
+
* @param data request data
|
|
123
|
+
* @returns data and total count
|
|
124
|
+
*/
|
|
125
|
+
async function getFullApiResponse(context, method, endpoint, data) {
|
|
126
|
+
return await request({
|
|
127
|
+
method,
|
|
128
|
+
baseURL: context.baseUrl,
|
|
129
|
+
endpoint,
|
|
130
|
+
headers: { "push-ref": context.pushRef },
|
|
131
|
+
data
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
async function makeRestApiRequest(context, method, endpoint, data) {
|
|
135
|
+
return (await request({
|
|
136
|
+
method,
|
|
137
|
+
baseURL: context.baseUrl,
|
|
138
|
+
endpoint,
|
|
139
|
+
headers: { "push-ref": context.pushRef },
|
|
140
|
+
data
|
|
141
|
+
})).data;
|
|
142
|
+
}
|
|
143
|
+
async function get(baseURL, endpoint, params, headers) {
|
|
144
|
+
return await request({
|
|
145
|
+
method: "GET",
|
|
146
|
+
baseURL,
|
|
147
|
+
endpoint,
|
|
148
|
+
headers,
|
|
149
|
+
data: params
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
async function post(baseURL, endpoint, params, headers) {
|
|
153
|
+
return await request({
|
|
154
|
+
method: "POST",
|
|
155
|
+
baseURL,
|
|
156
|
+
endpoint,
|
|
157
|
+
headers,
|
|
158
|
+
data: params
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
async function patch(baseURL, endpoint, params, headers) {
|
|
162
|
+
return await request({
|
|
163
|
+
method: "PATCH",
|
|
164
|
+
baseURL,
|
|
165
|
+
endpoint,
|
|
166
|
+
headers,
|
|
167
|
+
data: params
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
async function streamRequest(context, apiEndpoint, payload, onChunk, onDone, onError, separator = STREAM_SEPARATOR, abortSignal) {
|
|
171
|
+
let onErrorOnce = (e) => {
|
|
172
|
+
onErrorOnce = void 0;
|
|
173
|
+
onError?.(e);
|
|
174
|
+
};
|
|
175
|
+
let absoluteBaseUrl = context.baseUrl;
|
|
176
|
+
if (context.baseUrl.startsWith("/")) absoluteBaseUrl = (typeof window !== "undefined" && window.location?.origin?.startsWith("vscode-webview:") ? "http://localhost:5678" : typeof window !== "undefined" ? window.location?.origin : "http://localhost:5678") + context.baseUrl;
|
|
177
|
+
const assistantRequest = {
|
|
178
|
+
headers: {
|
|
179
|
+
"browser-id": getBrowserId(),
|
|
180
|
+
"Content-Type": "application/json"
|
|
181
|
+
},
|
|
182
|
+
method: "POST",
|
|
183
|
+
credentials: "include",
|
|
184
|
+
body: JSON.stringify(payload),
|
|
185
|
+
signal: abortSignal
|
|
186
|
+
};
|
|
187
|
+
try {
|
|
188
|
+
const response = await fetch(`${absoluteBaseUrl}${apiEndpoint}`, assistantRequest);
|
|
189
|
+
if (response.body) {
|
|
190
|
+
const reader = response.body.getReader();
|
|
191
|
+
const decoder = new TextDecoder("utf-8");
|
|
192
|
+
let buffer = "";
|
|
193
|
+
async function readStream() {
|
|
194
|
+
const { done, value } = await reader.read();
|
|
195
|
+
if (done) {
|
|
196
|
+
if (response.ok) onDone?.();
|
|
197
|
+
else onErrorOnce?.(new ResponseError(response.statusText, { httpStatusCode: response.status }));
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const chunk = decoder.decode(value);
|
|
201
|
+
buffer += chunk;
|
|
202
|
+
const splitChunks = buffer.split(separator);
|
|
203
|
+
buffer = "";
|
|
204
|
+
for (const splitChunk of splitChunks) if (splitChunk) {
|
|
205
|
+
let data;
|
|
206
|
+
try {
|
|
207
|
+
data = (0, n8n_workflow.jsonParse)(splitChunk, { errorMessage: "Invalid json" });
|
|
208
|
+
} catch (e) {
|
|
209
|
+
buffer += splitChunk;
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
if (response.ok) onChunk?.(data);
|
|
214
|
+
else {
|
|
215
|
+
const message = "message" in data ? data.message : response.statusText;
|
|
216
|
+
onErrorOnce?.(new ResponseError(String(message), { httpStatusCode: response.status }));
|
|
217
|
+
}
|
|
218
|
+
} catch (e) {
|
|
219
|
+
if (e instanceof Error) onErrorOnce?.(e);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
await readStream();
|
|
223
|
+
}
|
|
224
|
+
await readStream();
|
|
225
|
+
} else if (onErrorOnce) onErrorOnce(new Error(response.statusText));
|
|
226
|
+
} catch (e) {
|
|
227
|
+
(0, __n8n_utils_assert.assert)(e instanceof Error);
|
|
228
|
+
onErrorOnce?.(e);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
//#endregion
|
|
233
|
+
Object.defineProperty(exports, 'MfaRequiredError', {
|
|
234
|
+
enumerable: true,
|
|
235
|
+
get: function () {
|
|
236
|
+
return MfaRequiredError;
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
Object.defineProperty(exports, 'NO_NETWORK_ERROR_CODE', {
|
|
240
|
+
enumerable: true,
|
|
241
|
+
get: function () {
|
|
242
|
+
return NO_NETWORK_ERROR_CODE;
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
Object.defineProperty(exports, 'ResponseError', {
|
|
246
|
+
enumerable: true,
|
|
247
|
+
get: function () {
|
|
248
|
+
return ResponseError;
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
Object.defineProperty(exports, 'STREAM_SEPARATOR', {
|
|
252
|
+
enumerable: true,
|
|
253
|
+
get: function () {
|
|
254
|
+
return STREAM_SEPARATOR;
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
Object.defineProperty(exports, '__toESM', {
|
|
258
|
+
enumerable: true,
|
|
259
|
+
get: function () {
|
|
260
|
+
return __toESM;
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
Object.defineProperty(exports, 'get', {
|
|
264
|
+
enumerable: true,
|
|
265
|
+
get: function () {
|
|
266
|
+
return get;
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
Object.defineProperty(exports, 'getFullApiResponse', {
|
|
270
|
+
enumerable: true,
|
|
271
|
+
get: function () {
|
|
272
|
+
return getFullApiResponse;
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
Object.defineProperty(exports, 'makeRestApiRequest', {
|
|
276
|
+
enumerable: true,
|
|
277
|
+
get: function () {
|
|
278
|
+
return makeRestApiRequest;
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
Object.defineProperty(exports, 'patch', {
|
|
282
|
+
enumerable: true,
|
|
283
|
+
get: function () {
|
|
284
|
+
return patch;
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
Object.defineProperty(exports, 'post', {
|
|
288
|
+
enumerable: true,
|
|
289
|
+
get: function () {
|
|
290
|
+
return post;
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
Object.defineProperty(exports, 'request', {
|
|
294
|
+
enumerable: true,
|
|
295
|
+
get: function () {
|
|
296
|
+
return request;
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
Object.defineProperty(exports, 'streamRequest', {
|
|
300
|
+
enumerable: true,
|
|
301
|
+
get: function () {
|
|
302
|
+
return streamRequest;
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
//# sourceMappingURL=utils2.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils2.cjs","names":["BROWSER_ID_STORAGE_KEY","ApplicationError","options: AxiosRequestConfig","onErrorOnce: ((e: Error) => void) | undefined","assistantRequest: RequestInit","data: T","e: unknown"],"sources":["../src/utils.ts"],"sourcesContent":["import { BROWSER_ID_STORAGE_KEY } from '@n8n/constants';\nimport { assert } from '@n8n/utils/assert';\nimport type { AxiosRequestConfig, Method, RawAxiosRequestHeaders } from 'axios';\nimport axios from 'axios';\nimport { ApplicationError, jsonParse } from 'n8n-workflow';\nimport type { GenericValue, IDataObject } from 'n8n-workflow';\n\nimport type { IRestApiContext } from './types';\n\nconst getBrowserId = () => {\n\tlet browserId = localStorage.getItem(BROWSER_ID_STORAGE_KEY);\n\tif (!browserId) {\n\t\tbrowserId = crypto.randomUUID();\n\t\tlocalStorage.setItem(BROWSER_ID_STORAGE_KEY, browserId);\n\t}\n\treturn browserId;\n};\n\nexport const NO_NETWORK_ERROR_CODE = 999;\nexport const STREAM_SEPARATOR = '⧉⇋⇋➽⌑⧉§§\\n';\n\nexport class MfaRequiredError extends ApplicationError {\n\tconstructor() {\n\t\tsuper('MFA is required to access this resource. Please set up MFA in your user settings.');\n\t\tthis.name = 'MfaRequiredError';\n\t}\n}\n\nexport class ResponseError extends ApplicationError {\n\t// The HTTP status code of response\n\thttpStatusCode?: number;\n\n\t// The error code in the response\n\terrorCode?: number;\n\n\t// The stack trace of the server\n\tserverStackTrace?: string;\n\n\t// Additional metadata from the server (e.g., EULA URL)\n\tmeta?: Record<string, unknown>;\n\n\t/**\n\t * Creates an instance of ResponseError.\n\t * @param {string} message The error message\n\t * @param {number} [errorCode] The error code which can be used by frontend to identify the actual error\n\t * @param {number} [httpStatusCode] The HTTP status code the response should have\n\t * @param {string} [stack] The stack trace\n\t * @param {Record<string, unknown>} [meta] Additional metadata from the server\n\t */\n\tconstructor(\n\t\tmessage: string,\n\t\toptions: {\n\t\t\terrorCode?: number;\n\t\t\thttpStatusCode?: number;\n\t\t\tstack?: string;\n\t\t\tmeta?: Record<string, unknown>;\n\t\t} = {},\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'ResponseError';\n\n\t\tconst { errorCode, httpStatusCode, stack, meta } = options;\n\t\tif (errorCode) {\n\t\t\tthis.errorCode = errorCode;\n\t\t}\n\t\tif (httpStatusCode) {\n\t\t\tthis.httpStatusCode = httpStatusCode;\n\t\t}\n\t\tif (stack) {\n\t\t\tthis.serverStackTrace = stack;\n\t\t}\n\t\tif (meta) {\n\t\t\tthis.meta = meta;\n\t\t}\n\t}\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst legacyParamSerializer = (params: Record<string, any>) =>\n\tObject.keys(params)\n\t\t.filter((key) => params[key] !== undefined)\n\t\t.map((key) => {\n\t\t\tif (Array.isArray(params[key])) {\n\t\t\t\treturn params[key].map((v: string) => `${key}[]=${encodeURIComponent(v)}`).join('&');\n\t\t\t}\n\t\t\tif (typeof params[key] === 'object') {\n\t\t\t\tparams[key] = JSON.stringify(params[key]);\n\t\t\t}\n\t\t\treturn `${key}=${encodeURIComponent(params[key])}`;\n\t\t})\n\t\t.join('&');\n\nexport async function request(config: {\n\tmethod: Method;\n\tbaseURL: string;\n\tendpoint: string;\n\theaders?: RawAxiosRequestHeaders;\n\tdata?: GenericValue | GenericValue[];\n\twithCredentials?: boolean;\n}) {\n\tconst { method, baseURL, endpoint, headers, data } = config;\n\n\t// Ensure baseURL is absolute for VS Code webview context\n\tlet absoluteBaseURL = baseURL;\n\tif (baseURL.startsWith('/')) {\n\t\t// In VS Code webview, window.location.origin is not a valid HTTP URL\n\t\tconst origin =\n\t\t\ttypeof window !== 'undefined' && window.location?.origin?.startsWith('vscode-webview:')\n\t\t\t\t? 'http://localhost:5678'\n\t\t\t\t: typeof window !== 'undefined'\n\t\t\t\t\t? window.location?.origin\n\t\t\t\t\t: 'http://localhost:5678';\n\t\tabsoluteBaseURL = origin + baseURL;\n\t}\n\n\tconst options: AxiosRequestConfig = {\n\t\tmethod,\n\t\turl: endpoint,\n\t\tbaseURL: absoluteBaseURL,\n\t\theaders: headers ?? {},\n\t};\n\tif (baseURL.startsWith('/')) {\n\t\toptions.headers!['browser-id'] = getBrowserId();\n\t}\n\tif (\n\t\timport.meta.env.NODE_ENV !== 'production' &&\n\t\t!baseURL.includes('api.n8n.io') &&\n\t\t!baseURL.includes('n8n.cloud')\n\t) {\n\t\toptions.withCredentials = options.withCredentials ?? true;\n\t}\n\tif (['POST', 'PATCH', 'PUT'].includes(method)) {\n\t\toptions.data = data;\n\t} else if (data) {\n\t\toptions.params = data;\n\t\toptions.paramsSerializer = legacyParamSerializer;\n\t}\n\n\ttry {\n\t\tconst response = await axios.request(options);\n\t\treturn response.data;\n\t} catch (error) {\n\t\tif (error.message === 'Network Error') {\n\t\t\tthrow new ResponseError(\"Can't connect to n8n.\", {\n\t\t\t\terrorCode: NO_NETWORK_ERROR_CODE,\n\t\t\t});\n\t\t}\n\n\t\tconst errorResponseData = error.response?.data;\n\t\tif (errorResponseData?.mfaRequired === true) {\n\t\t\tthrow new MfaRequiredError();\n\t\t}\n\t\tif (errorResponseData?.message !== undefined) {\n\t\t\tif (errorResponseData.name === 'NodeApiError') {\n\t\t\t\terrorResponseData.httpStatusCode = error.response.status;\n\t\t\t\tthrow errorResponseData;\n\t\t\t}\n\n\t\t\tthrow new ResponseError(errorResponseData.message, {\n\t\t\t\terrorCode: errorResponseData.code,\n\t\t\t\thttpStatusCode: error.response.status,\n\t\t\t\tstack: errorResponseData.stack,\n\t\t\t\tmeta: errorResponseData.meta,\n\t\t\t});\n\t\t}\n\n\t\tthrow error;\n\t}\n}\n\n/**\n * Sends a request to the API and returns the response without extracting the data key.\n * @param context Rest API context\n * @param method HTTP method\n * @param endpoint relative path to the API endpoint\n * @param data request data\n * @returns data and total count\n */\nexport async function getFullApiResponse<T>(\n\tcontext: IRestApiContext,\n\tmethod: Method,\n\tendpoint: string,\n\tdata?: GenericValue | GenericValue[],\n) {\n\tconst response = await request({\n\t\tmethod,\n\t\tbaseURL: context.baseUrl,\n\t\tendpoint,\n\t\theaders: { 'push-ref': context.pushRef },\n\t\tdata,\n\t});\n\n\treturn response as { count: number; data: T };\n}\n\nexport async function makeRestApiRequest<T>(\n\tcontext: IRestApiContext,\n\tmethod: Method,\n\tendpoint: string,\n\tdata?: GenericValue | GenericValue[],\n) {\n\tconst response = await request({\n\t\tmethod,\n\t\tbaseURL: context.baseUrl,\n\t\tendpoint,\n\t\theaders: { 'push-ref': context.pushRef },\n\t\tdata,\n\t});\n\n\t// All cli rest api endpoints return data wrapped in `data` key\n\treturn response.data as T;\n}\n\nexport async function get(\n\tbaseURL: string,\n\tendpoint: string,\n\tparams?: IDataObject,\n\theaders?: RawAxiosRequestHeaders,\n) {\n\treturn await request({ method: 'GET', baseURL, endpoint, headers, data: params });\n}\n\nexport async function post(\n\tbaseURL: string,\n\tendpoint: string,\n\tparams?: IDataObject,\n\theaders?: RawAxiosRequestHeaders,\n) {\n\treturn await request({ method: 'POST', baseURL, endpoint, headers, data: params });\n}\n\nexport async function patch(\n\tbaseURL: string,\n\tendpoint: string,\n\tparams?: IDataObject,\n\theaders?: RawAxiosRequestHeaders,\n) {\n\treturn await request({ method: 'PATCH', baseURL, endpoint, headers, data: params });\n}\n\nexport async function streamRequest<T extends object>(\n\tcontext: IRestApiContext,\n\tapiEndpoint: string,\n\tpayload: object,\n\tonChunk?: (chunk: T) => void,\n\tonDone?: () => void,\n\tonError?: (e: Error) => void,\n\tseparator = STREAM_SEPARATOR,\n\tabortSignal?: AbortSignal,\n): Promise<void> {\n\tlet onErrorOnce: ((e: Error) => void) | undefined = (e: Error) => {\n\t\tonErrorOnce = undefined;\n\t\tonError?.(e);\n\t};\n\n\t// Ensure baseUrl is absolute for VS Code webview context\n\tlet absoluteBaseUrl = context.baseUrl;\n\tif (context.baseUrl.startsWith('/')) {\n\t\t// In VS Code webview, window.location.origin is not a valid HTTP URL\n\t\tconst origin =\n\t\t\ttypeof window !== 'undefined' && window.location?.origin?.startsWith('vscode-webview:')\n\t\t\t\t? 'http://localhost:5678'\n\t\t\t\t: typeof window !== 'undefined'\n\t\t\t\t\t? window.location?.origin\n\t\t\t\t\t: 'http://localhost:5678';\n\t\tabsoluteBaseUrl = origin + context.baseUrl;\n\t}\n\n\tconst headers: Record<string, string> = {\n\t\t'browser-id': getBrowserId(),\n\t\t'Content-Type': 'application/json',\n\t};\n\tconst assistantRequest: RequestInit = {\n\t\theaders,\n\t\tmethod: 'POST',\n\t\tcredentials: 'include',\n\t\tbody: JSON.stringify(payload),\n\t\tsignal: abortSignal,\n\t};\n\ttry {\n\t\tconst response = await fetch(`${absoluteBaseUrl}${apiEndpoint}`, assistantRequest);\n\n\t\tif (response.body) {\n\t\t\t// Handle the streaming response\n\t\t\tconst reader = response.body.getReader();\n\t\t\tconst decoder = new TextDecoder('utf-8');\n\n\t\t\tlet buffer = '';\n\n\t\t\tasync function readStream() {\n\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\tif (done) {\n\t\t\t\t\tif (response.ok) {\n\t\t\t\t\t\tonDone?.();\n\t\t\t\t\t} else {\n\t\t\t\t\t\tonErrorOnce?.(\n\t\t\t\t\t\t\tnew ResponseError(response.statusText, {\n\t\t\t\t\t\t\t\thttpStatusCode: response.status,\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst chunk = decoder.decode(value);\n\t\t\t\tbuffer += chunk;\n\n\t\t\t\tconst splitChunks = buffer.split(separator);\n\n\t\t\t\tbuffer = '';\n\t\t\t\tfor (const splitChunk of splitChunks) {\n\t\t\t\t\tif (splitChunk) {\n\t\t\t\t\t\tlet data: T;\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tdata = jsonParse<T>(splitChunk, { errorMessage: 'Invalid json' });\n\t\t\t\t\t\t} catch (e) {\n\t\t\t\t\t\t\t// incomplete json. append to buffer to complete\n\t\t\t\t\t\t\tbuffer += splitChunk;\n\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tif (response.ok) {\n\t\t\t\t\t\t\t\t// Call chunk callback if request was successful\n\t\t\t\t\t\t\t\tonChunk?.(data);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// Otherwise, call error callback\n\t\t\t\t\t\t\t\tconst message = 'message' in data ? data.message : response.statusText;\n\t\t\t\t\t\t\t\tonErrorOnce?.(\n\t\t\t\t\t\t\t\t\tnew ResponseError(String(message), {\n\t\t\t\t\t\t\t\t\t\thttpStatusCode: response.status,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\t\tonErrorOnce?.(e);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tawait readStream();\n\t\t\t}\n\n\t\t\t// Start reading the stream\n\t\t\tawait readStream();\n\t\t} else if (onErrorOnce) {\n\t\t\tonErrorOnce(new Error(response.statusText));\n\t\t}\n\t} catch (e: unknown) {\n\t\tassert(e instanceof Error);\n\t\tonErrorOnce?.(e);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AASA,MAAM,qBAAqB;CAC1B,IAAI,YAAY,aAAa,QAAQA,uCAAuB;AAC5D,KAAI,CAAC,WAAW;AACf,cAAY,OAAO,YAAY;AAC/B,eAAa,QAAQA,wCAAwB,UAAU;;AAExD,QAAO;;AAGR,MAAa,wBAAwB;AACrC,MAAa,mBAAmB;AAEhC,IAAa,mBAAb,cAAsCC,8BAAiB;CACtD,cAAc;AACb,QAAM,oFAAoF;AAC1F,OAAK,OAAO;;;AAId,IAAa,gBAAb,cAAmCA,8BAAiB;CAEnD;CAGA;CAGA;CAGA;;;;;;;;;CAUA,YACC,SACA,UAKI,EAAE,EACL;AACD,QAAM,QAAQ;AACd,OAAK,OAAO;EAEZ,MAAM,EAAE,WAAW,gBAAgB,OAAO,SAAS;AACnD,MAAI,UACH,MAAK,YAA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aACR,YACV,aAAY,IAAI,MAAM,SAAS,WAAW,CAAC;UAEpCA,GAAY;AACpB,iCAAO,aAAa,MAAM;AAC1B,gBAAc,EAAE"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { t as IRestApiContext } from "./types2.cjs";
|
|
2
|
+
import { ApplicationError, GenericValue, IDataObject } from "n8n-workflow";
|
|
3
|
+
import { Method, RawAxiosRequestHeaders } from "axios";
|
|
4
|
+
|
|
5
|
+
//#region src/utils.d.ts
|
|
6
|
+
declare const NO_NETWORK_ERROR_CODE = 999;
|
|
7
|
+
declare const STREAM_SEPARATOR = "\u29C9\u21CB\u21CB\u27BD\u2311\u29C9\u00A7\u00A7\n";
|
|
8
|
+
declare class MfaRequiredError extends ApplicationError {
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
declare class ResponseError extends ApplicationError {
|
|
12
|
+
httpStatusCode?: number;
|
|
13
|
+
errorCode?: number;
|
|
14
|
+
serverStackTrace?: string;
|
|
15
|
+
meta?: Record<string, unknown>;
|
|
16
|
+
constructor(message: string, options?: {
|
|
17
|
+
errorCode?: number;
|
|
18
|
+
httpStatusCode?: number;
|
|
19
|
+
stack?: string;
|
|
20
|
+
meta?: Record<string, unknown>;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
declare function request(config: {
|
|
24
|
+
method: Method;
|
|
25
|
+
baseURL: string;
|
|
26
|
+
endpoint: string;
|
|
27
|
+
headers?: RawAxiosRequestHeaders;
|
|
28
|
+
data?: GenericValue | GenericValue[];
|
|
29
|
+
withCredentials?: boolean;
|
|
30
|
+
}): Promise<any>;
|
|
31
|
+
declare function getFullApiResponse<T>(context: IRestApiContext, method: Method, endpoint: string, data?: GenericValue | GenericValue[]): Promise<{
|
|
32
|
+
count: number;
|
|
33
|
+
data: T;
|
|
34
|
+
}>;
|
|
35
|
+
declare function makeRestApiRequest<T>(context: IRestApiContext, method: Method, endpoint: string, data?: GenericValue | GenericValue[]): Promise<T>;
|
|
36
|
+
declare function get(baseURL: string, endpoint: string, params?: IDataObject, headers?: RawAxiosRequestHeaders): Promise<any>;
|
|
37
|
+
declare function post(baseURL: string, endpoint: string, params?: IDataObject, headers?: RawAxiosRequestHeaders): Promise<any>;
|
|
38
|
+
declare function patch(baseURL: string, endpoint: string, params?: IDataObject, headers?: RawAxiosRequestHeaders): Promise<any>;
|
|
39
|
+
declare function streamRequest<T extends object>(context: IRestApiContext, apiEndpoint: string, payload: object, onChunk?: (chunk: T) => void, onDone?: () => void, onError?: (e: Error) => void, separator?: string, abortSignal?: AbortSignal): Promise<void>;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { get as a, patch as c, streamRequest as d, STREAM_SEPARATOR as i, post as l, NO_NETWORK_ERROR_CODE as n, getFullApiResponse as o, ResponseError as r, makeRestApiRequest as s, MfaRequiredError as t, request as u };
|
|
42
|
+
//# sourceMappingURL=utils2.d.cts.map
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { t as IRestApiContext } from "./types2.mjs";
|
|
2
|
+
import { Method, RawAxiosRequestHeaders } from "axios";
|
|
3
|
+
import { ApplicationError, GenericValue, IDataObject } from "n8n-workflow";
|
|
4
|
+
|
|
5
|
+
//#region src/utils.d.ts
|
|
6
|
+
declare const NO_NETWORK_ERROR_CODE = 999;
|
|
7
|
+
declare const STREAM_SEPARATOR = "\u29C9\u21CB\u21CB\u27BD\u2311\u29C9\u00A7\u00A7\n";
|
|
8
|
+
declare class MfaRequiredError extends ApplicationError {
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
declare class ResponseError extends ApplicationError {
|
|
12
|
+
httpStatusCode?: number;
|
|
13
|
+
errorCode?: number;
|
|
14
|
+
serverStackTrace?: string;
|
|
15
|
+
meta?: Record<string, unknown>;
|
|
16
|
+
constructor(message: string, options?: {
|
|
17
|
+
errorCode?: number;
|
|
18
|
+
httpStatusCode?: number;
|
|
19
|
+
stack?: string;
|
|
20
|
+
meta?: Record<string, unknown>;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
declare function request(config: {
|
|
24
|
+
method: Method;
|
|
25
|
+
baseURL: string;
|
|
26
|
+
endpoint: string;
|
|
27
|
+
headers?: RawAxiosRequestHeaders;
|
|
28
|
+
data?: GenericValue | GenericValue[];
|
|
29
|
+
withCredentials?: boolean;
|
|
30
|
+
}): Promise<any>;
|
|
31
|
+
declare function getFullApiResponse<T>(context: IRestApiContext, method: Method, endpoint: string, data?: GenericValue | GenericValue[]): Promise<{
|
|
32
|
+
count: number;
|
|
33
|
+
data: T;
|
|
34
|
+
}>;
|
|
35
|
+
declare function makeRestApiRequest<T>(context: IRestApiContext, method: Method, endpoint: string, data?: GenericValue | GenericValue[]): Promise<T>;
|
|
36
|
+
declare function get(baseURL: string, endpoint: string, params?: IDataObject, headers?: RawAxiosRequestHeaders): Promise<any>;
|
|
37
|
+
declare function post(baseURL: string, endpoint: string, params?: IDataObject, headers?: RawAxiosRequestHeaders): Promise<any>;
|
|
38
|
+
declare function patch(baseURL: string, endpoint: string, params?: IDataObject, headers?: RawAxiosRequestHeaders): Promise<any>;
|
|
39
|
+
declare function streamRequest<T extends object>(context: IRestApiContext, apiEndpoint: string, payload: object, onChunk?: (chunk: T) => void, onDone?: () => void, onError?: (e: Error) => void, separator?: string, abortSignal?: AbortSignal): Promise<void>;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { get as a, patch as c, streamRequest as d, STREAM_SEPARATOR as i, post as l, NO_NETWORK_ERROR_CODE as n, getFullApiResponse as o, ResponseError as r, makeRestApiRequest as s, MfaRequiredError as t, request as u };
|
|
42
|
+
//# sourceMappingURL=utils2.d.mts.map
|
package/dist/utils2.mjs
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { BROWSER_ID_STORAGE_KEY } from "@n8n/constants";
|
|
2
|
+
import { assert } from "@n8n/utils/assert";
|
|
3
|
+
import axios from "axios";
|
|
4
|
+
import { ApplicationError, jsonParse } from "n8n-workflow";
|
|
5
|
+
|
|
6
|
+
//#region src/utils.ts
|
|
7
|
+
const getBrowserId = () => {
|
|
8
|
+
let browserId = localStorage.getItem(BROWSER_ID_STORAGE_KEY);
|
|
9
|
+
if (!browserId) {
|
|
10
|
+
browserId = crypto.randomUUID();
|
|
11
|
+
localStorage.setItem(BROWSER_ID_STORAGE_KEY, browserId);
|
|
12
|
+
}
|
|
13
|
+
return browserId;
|
|
14
|
+
};
|
|
15
|
+
const NO_NETWORK_ERROR_CODE = 999;
|
|
16
|
+
const STREAM_SEPARATOR = "⧉⇋⇋➽⌑⧉§§\n";
|
|
17
|
+
var MfaRequiredError = class extends ApplicationError {
|
|
18
|
+
constructor() {
|
|
19
|
+
super("MFA is required to access this resource. Please set up MFA in your user settings.");
|
|
20
|
+
this.name = "MfaRequiredError";
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var ResponseError = class extends ApplicationError {
|
|
24
|
+
httpStatusCode;
|
|
25
|
+
errorCode;
|
|
26
|
+
serverStackTrace;
|
|
27
|
+
meta;
|
|
28
|
+
/**
|
|
29
|
+
* Creates an instance of ResponseError.
|
|
30
|
+
* @param {string} message The error message
|
|
31
|
+
* @param {number} [errorCode] The error code which can be used by frontend to identify the actual error
|
|
32
|
+
* @param {number} [httpStatusCode] The HTTP status code the response should have
|
|
33
|
+
* @param {string} [stack] The stack trace
|
|
34
|
+
* @param {Record<string, unknown>} [meta] Additional metadata from the server
|
|
35
|
+
*/
|
|
36
|
+
constructor(message, options = {}) {
|
|
37
|
+
super(message);
|
|
38
|
+
this.name = "ResponseError";
|
|
39
|
+
const { errorCode, httpStatusCode, stack, meta } = options;
|
|
40
|
+
if (errorCode) this.errorCode = errorCode;
|
|
41
|
+
if (httpStatusCode) this.httpStatusCode = httpStatusCode;
|
|
42
|
+
if (stack) this.serverStackTrace = stack;
|
|
43
|
+
if (meta) this.meta = meta;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const legacyParamSerializer = (params) => Object.keys(params).filter((key) => params[key] !== void 0).map((key) => {
|
|
47
|
+
if (Array.isArray(params[key])) return params[key].map((v) => `${key}[]=${encodeURIComponent(v)}`).join("&");
|
|
48
|
+
if (typeof params[key] === "object") params[key] = JSON.stringify(params[key]);
|
|
49
|
+
return `${key}=${encodeURIComponent(params[key])}`;
|
|
50
|
+
}).join("&");
|
|
51
|
+
async function request(config) {
|
|
52
|
+
const { method, baseURL, endpoint, headers, data } = config;
|
|
53
|
+
let absoluteBaseURL = baseURL;
|
|
54
|
+
if (baseURL.startsWith("/")) absoluteBaseURL = (typeof window !== "undefined" && window.location?.origin?.startsWith("vscode-webview:") ? "http://localhost:5678" : typeof window !== "undefined" ? window.location?.origin : "http://localhost:5678") + baseURL;
|
|
55
|
+
const options = {
|
|
56
|
+
method,
|
|
57
|
+
url: endpoint,
|
|
58
|
+
baseURL: absoluteBaseURL,
|
|
59
|
+
headers: headers ?? {}
|
|
60
|
+
};
|
|
61
|
+
if (baseURL.startsWith("/")) options.headers["browser-id"] = getBrowserId();
|
|
62
|
+
if (import.meta.env.NODE_ENV !== "production" && !baseURL.includes("api.n8n.io") && !baseURL.includes("n8n.cloud")) options.withCredentials = options.withCredentials ?? true;
|
|
63
|
+
if ([
|
|
64
|
+
"POST",
|
|
65
|
+
"PATCH",
|
|
66
|
+
"PUT"
|
|
67
|
+
].includes(method)) options.data = data;
|
|
68
|
+
else if (data) {
|
|
69
|
+
options.params = data;
|
|
70
|
+
options.paramsSerializer = legacyParamSerializer;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
return (await axios.request(options)).data;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (error.message === "Network Error") throw new ResponseError("Can't connect to n8n.", { errorCode: NO_NETWORK_ERROR_CODE });
|
|
76
|
+
const errorResponseData = error.response?.data;
|
|
77
|
+
if (errorResponseData?.mfaRequired === true) throw new MfaRequiredError();
|
|
78
|
+
if (errorResponseData?.message !== void 0) {
|
|
79
|
+
if (errorResponseData.name === "NodeApiError") {
|
|
80
|
+
errorResponseData.httpStatusCode = error.response.status;
|
|
81
|
+
throw errorResponseData;
|
|
82
|
+
}
|
|
83
|
+
throw new ResponseError(errorResponseData.message, {
|
|
84
|
+
errorCode: errorResponseData.code,
|
|
85
|
+
httpStatusCode: error.response.status,
|
|
86
|
+
stack: errorResponseData.stack,
|
|
87
|
+
meta: errorResponseData.meta
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Sends a request to the API and returns the response without extracting the data key.
|
|
95
|
+
* @param context Rest API context
|
|
96
|
+
* @param method HTTP method
|
|
97
|
+
* @param endpoint relative path to the API endpoint
|
|
98
|
+
* @param data request data
|
|
99
|
+
* @returns data and total count
|
|
100
|
+
*/
|
|
101
|
+
async function getFullApiResponse(context, method, endpoint, data) {
|
|
102
|
+
return await request({
|
|
103
|
+
method,
|
|
104
|
+
baseURL: context.baseUrl,
|
|
105
|
+
endpoint,
|
|
106
|
+
headers: { "push-ref": context.pushRef },
|
|
107
|
+
data
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
async function makeRestApiRequest(context, method, endpoint, data) {
|
|
111
|
+
return (await request({
|
|
112
|
+
method,
|
|
113
|
+
baseURL: context.baseUrl,
|
|
114
|
+
endpoint,
|
|
115
|
+
headers: { "push-ref": context.pushRef },
|
|
116
|
+
data
|
|
117
|
+
})).data;
|
|
118
|
+
}
|
|
119
|
+
async function get(baseURL, endpoint, params, headers) {
|
|
120
|
+
return await request({
|
|
121
|
+
method: "GET",
|
|
122
|
+
baseURL,
|
|
123
|
+
endpoint,
|
|
124
|
+
headers,
|
|
125
|
+
data: params
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
async function post(baseURL, endpoint, params, headers) {
|
|
129
|
+
return await request({
|
|
130
|
+
method: "POST",
|
|
131
|
+
baseURL,
|
|
132
|
+
endpoint,
|
|
133
|
+
headers,
|
|
134
|
+
data: params
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async function patch(baseURL, endpoint, params, headers) {
|
|
138
|
+
return await request({
|
|
139
|
+
method: "PATCH",
|
|
140
|
+
baseURL,
|
|
141
|
+
endpoint,
|
|
142
|
+
headers,
|
|
143
|
+
data: params
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
async function streamRequest(context, apiEndpoint, payload, onChunk, onDone, onError, separator = STREAM_SEPARATOR, abortSignal) {
|
|
147
|
+
let onErrorOnce = (e) => {
|
|
148
|
+
onErrorOnce = void 0;
|
|
149
|
+
onError?.(e);
|
|
150
|
+
};
|
|
151
|
+
let absoluteBaseUrl = context.baseUrl;
|
|
152
|
+
if (context.baseUrl.startsWith("/")) absoluteBaseUrl = (typeof window !== "undefined" && window.location?.origin?.startsWith("vscode-webview:") ? "http://localhost:5678" : typeof window !== "undefined" ? window.location?.origin : "http://localhost:5678") + context.baseUrl;
|
|
153
|
+
const assistantRequest = {
|
|
154
|
+
headers: {
|
|
155
|
+
"browser-id": getBrowserId(),
|
|
156
|
+
"Content-Type": "application/json"
|
|
157
|
+
},
|
|
158
|
+
method: "POST",
|
|
159
|
+
credentials: "include",
|
|
160
|
+
body: JSON.stringify(payload),
|
|
161
|
+
signal: abortSignal
|
|
162
|
+
};
|
|
163
|
+
try {
|
|
164
|
+
const response = await fetch(`${absoluteBaseUrl}${apiEndpoint}`, assistantRequest);
|
|
165
|
+
if (response.body) {
|
|
166
|
+
const reader = response.body.getReader();
|
|
167
|
+
const decoder = new TextDecoder("utf-8");
|
|
168
|
+
let buffer = "";
|
|
169
|
+
async function readStream() {
|
|
170
|
+
const { done, value } = await reader.read();
|
|
171
|
+
if (done) {
|
|
172
|
+
if (response.ok) onDone?.();
|
|
173
|
+
else onErrorOnce?.(new ResponseError(response.statusText, { httpStatusCode: response.status }));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const chunk = decoder.decode(value);
|
|
177
|
+
buffer += chunk;
|
|
178
|
+
const splitChunks = buffer.split(separator);
|
|
179
|
+
buffer = "";
|
|
180
|
+
for (const splitChunk of splitChunks) if (splitChunk) {
|
|
181
|
+
let data;
|
|
182
|
+
try {
|
|
183
|
+
data = jsonParse(splitChunk, { errorMessage: "Invalid json" });
|
|
184
|
+
} catch (e) {
|
|
185
|
+
buffer += splitChunk;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
if (response.ok) onChunk?.(data);
|
|
190
|
+
else {
|
|
191
|
+
const message = "message" in data ? data.message : response.statusText;
|
|
192
|
+
onErrorOnce?.(new ResponseError(String(message), { httpStatusCode: response.status }));
|
|
193
|
+
}
|
|
194
|
+
} catch (e) {
|
|
195
|
+
if (e instanceof Error) onErrorOnce?.(e);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
await readStream();
|
|
199
|
+
}
|
|
200
|
+
await readStream();
|
|
201
|
+
} else if (onErrorOnce) onErrorOnce(new Error(response.statusText));
|
|
202
|
+
} catch (e) {
|
|
203
|
+
assert(e instanceof Error);
|
|
204
|
+
onErrorOnce?.(e);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
//#endregion
|
|
209
|
+
export { get as a, patch as c, streamRequest as d, STREAM_SEPARATOR as i, post as l, NO_NETWORK_ERROR_CODE as n, getFullApiResponse as o, ResponseError as r, makeRestApiRequest as s, MfaRequiredError as t, request as u };
|
|
210
|
+
//# sourceMappingURL=utils2.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils2.mjs","names":["options: AxiosRequestConfig","onErrorOnce: ((e: Error) => void) | undefined","assistantRequest: RequestInit","data: T","e: unknown"],"sources":["../src/utils.ts"],"sourcesContent":["import { BROWSER_ID_STORAGE_KEY } from '@n8n/constants';\nimport { assert } from '@n8n/utils/assert';\nimport type { AxiosRequestConfig, Method, RawAxiosRequestHeaders } from 'axios';\nimport axios from 'axios';\nimport { ApplicationError, jsonParse } from 'n8n-workflow';\nimport type { GenericValue, IDataObject } from 'n8n-workflow';\n\nimport type { IRestApiContext } from './types';\n\nconst getBrowserId = () => {\n\tlet browserId = localStorage.getItem(BROWSER_ID_STORAGE_KEY);\n\tif (!browserId) {\n\t\tbrowserId = crypto.randomUUID();\n\t\tlocalStorage.setItem(BROWSER_ID_STORAGE_KEY, browserId);\n\t}\n\treturn browserId;\n};\n\nexport const NO_NETWORK_ERROR_CODE = 999;\nexport const STREAM_SEPARATOR = '⧉⇋⇋➽⌑⧉§§\\n';\n\nexport class MfaRequiredError extends ApplicationError {\n\tconstructor() {\n\t\tsuper('MFA is required to access this resource. Please set up MFA in your user settings.');\n\t\tthis.name = 'MfaRequiredError';\n\t}\n}\n\nexport class ResponseError extends ApplicationError {\n\t// The HTTP status code of response\n\thttpStatusCode?: number;\n\n\t// The error code in the response\n\terrorCode?: number;\n\n\t// The stack trace of the server\n\tserverStackTrace?: string;\n\n\t// Additional metadata from the server (e.g., EULA URL)\n\tmeta?: Record<string, unknown>;\n\n\t/**\n\t * Creates an instance of ResponseError.\n\t * @param {string} message The error message\n\t * @param {number} [errorCode] The error code which can be used by frontend to identify the actual error\n\t * @param {number} [httpStatusCode] The HTTP status code the response should have\n\t * @param {string} [stack] The stack trace\n\t * @param {Record<string, unknown>} [meta] Additional metadata from the server\n\t */\n\tconstructor(\n\t\tmessage: string,\n\t\toptions: {\n\t\t\terrorCode?: number;\n\t\t\thttpStatusCode?: number;\n\t\t\tstack?: string;\n\t\t\tmeta?: Record<string, unknown>;\n\t\t} = {},\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'ResponseError';\n\n\t\tconst { errorCode, httpStatusCode, stack, meta } = options;\n\t\tif (errorCode) {\n\t\t\tthis.errorCode = errorCode;\n\t\t}\n\t\tif (httpStatusCode) {\n\t\t\tthis.httpStatusCode = httpStatusCode;\n\t\t}\n\t\tif (stack) {\n\t\t\tthis.serverStackTrace = stack;\n\t\t}\n\t\tif (meta) {\n\t\t\tthis.meta = meta;\n\t\t}\n\t}\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst legacyParamSerializer = (params: Record<string, any>) =>\n\tObject.keys(params)\n\t\t.filter((key) => params[key] !== undefined)\n\t\t.map((key) => {\n\t\t\tif (Array.isArray(params[key])) {\n\t\t\t\treturn params[key].map((v: string) => `${key}[]=${encodeURIComponent(v)}`).join('&');\n\t\t\t}\n\t\t\tif (typeof params[key] === 'object') {\n\t\t\t\tparams[key] = JSON.stringify(params[key]);\n\t\t\t}\n\t\t\treturn `${key}=${encodeURIComponent(params[key])}`;\n\t\t})\n\t\t.join('&');\n\nexport async function request(config: {\n\tmethod: Method;\n\tbaseURL: string;\n\tendpoint: string;\n\theaders?: RawAxiosRequestHeaders;\n\tdata?: GenericValue | GenericValue[];\n\twithCredentials?: boolean;\n}) {\n\tconst { method, baseURL, endpoint, headers, data } = config;\n\n\t// Ensure baseURL is absolute for VS Code webview context\n\tlet absoluteBaseURL = baseURL;\n\tif (baseURL.startsWith('/')) {\n\t\t// In VS Code webview, window.location.origin is not a valid HTTP URL\n\t\tconst origin =\n\t\t\ttypeof window !== 'undefined' && window.location?.origin?.startsWith('vscode-webview:')\n\t\t\t\t? 'http://localhost:5678'\n\t\t\t\t: typeof window !== 'undefined'\n\t\t\t\t\t? window.location?.origin\n\t\t\t\t\t: 'http://localhost:5678';\n\t\tabsoluteBaseURL = origin + baseURL;\n\t}\n\n\tconst options: AxiosRequestConfig = {\n\t\tmethod,\n\t\turl: endpoint,\n\t\tbaseURL: absoluteBaseURL,\n\t\theaders: headers ?? {},\n\t};\n\tif (baseURL.startsWith('/')) {\n\t\toptions.headers!['browser-id'] = getBrowserId();\n\t}\n\tif (\n\t\timport.meta.env.NODE_ENV !== 'production' &&\n\t\t!baseURL.includes('api.n8n.io') &&\n\t\t!baseURL.includes('n8n.cloud')\n\t) {\n\t\toptions.withCredentials = options.withCredentials ?? true;\n\t}\n\tif (['POST', 'PATCH', 'PUT'].includes(method)) {\n\t\toptions.data = data;\n\t} else if (data) {\n\t\toptions.params = data;\n\t\toptions.paramsSerializer = legacyParamSerializer;\n\t}\n\n\ttry {\n\t\tconst response = await axios.request(options);\n\t\treturn response.data;\n\t} catch (error) {\n\t\tif (error.message === 'Network Error') {\n\t\t\tthrow new ResponseError(\"Can't connect to n8n.\", {\n\t\t\t\terrorCode: NO_NETWORK_ERROR_CODE,\n\t\t\t});\n\t\t}\n\n\t\tconst errorResponseData = error.response?.data;\n\t\tif (errorResponseData?.mfaRequired === true) {\n\t\t\tthrow new MfaRequiredError();\n\t\t}\n\t\tif (errorResponseData?.message !== undefined) {\n\t\t\tif (errorResponseData.name === 'NodeApiError') {\n\t\t\t\terrorResponseData.httpStatusCode = error.response.status;\n\t\t\t\tthrow errorResponseData;\n\t\t\t}\n\n\t\t\tthrow new ResponseError(errorResponseData.message, {\n\t\t\t\terrorCode: errorResponseData.code,\n\t\t\t\thttpStatusCode: error.response.status,\n\t\t\t\tstack: errorResponseData.stack,\n\t\t\t\tmeta: errorResponseData.meta,\n\t\t\t});\n\t\t}\n\n\t\tthrow error;\n\t}\n}\n\n/**\n * Sends a request to the API and returns the response without extracting the data key.\n * @param context Rest API context\n * @param method HTTP method\n * @param endpoint relative path to the API endpoint\n * @param data request data\n * @returns data and total count\n */\nexport async function getFullApiResponse<T>(\n\tcontext: IRestApiContext,\n\tmethod: Method,\n\tendpoint: string,\n\tdata?: GenericValue | GenericValue[],\n) {\n\tconst response = await request({\n\t\tmethod,\n\t\tbaseURL: context.baseUrl,\n\t\tendpoint,\n\t\theaders: { 'push-ref': context.pushRef },\n\t\tdata,\n\t});\n\n\treturn response as { count: number; data: T };\n}\n\nexport async function makeRestApiRequest<T>(\n\tcontext: IRestApiContext,\n\tmethod: Method,\n\tendpoint: string,\n\tdata?: GenericValue | GenericValue[],\n) {\n\tconst response = await request({\n\t\tmethod,\n\t\tbaseURL: context.baseUrl,\n\t\tendpoint,\n\t\theaders: { 'push-ref': context.pushRef },\n\t\tdata,\n\t});\n\n\t// All cli rest api endpoints return data wrapped in `data` key\n\treturn response.data as T;\n}\n\nexport async function get(\n\tbaseURL: string,\n\tendpoint: string,\n\tparams?: IDataObject,\n\theaders?: RawAxiosRequestHeaders,\n) {\n\treturn await request({ method: 'GET', baseURL, endpoint, headers, data: params });\n}\n\nexport async function post(\n\tbaseURL: string,\n\tendpoint: string,\n\tparams?: IDataObject,\n\theaders?: RawAxiosRequestHeaders,\n) {\n\treturn await request({ method: 'POST', baseURL, endpoint, headers, data: params });\n}\n\nexport async function patch(\n\tbaseURL: string,\n\tendpoint: string,\n\tparams?: IDataObject,\n\theaders?: RawAxiosRequestHeaders,\n) {\n\treturn await request({ method: 'PATCH', baseURL, endpoint, headers, data: params });\n}\n\nexport async function streamRequest<T extends object>(\n\tcontext: IRestApiContext,\n\tapiEndpoint: string,\n\tpayload: object,\n\tonChunk?: (chunk: T) => void,\n\tonDone?: () => void,\n\tonError?: (e: Error) => void,\n\tseparator = STREAM_SEPARATOR,\n\tabortSignal?: AbortSignal,\n): Promise<void> {\n\tlet onErrorOnce: ((e: Error) => void) | undefined = (e: Error) => {\n\t\tonErrorOnce = undefined;\n\t\tonError?.(e);\n\t};\n\n\t// Ensure baseUrl is absolute for VS Code webview context\n\tlet absoluteBaseUrl = context.baseUrl;\n\tif (context.baseUrl.startsWith('/')) {\n\t\t// In VS Code webview, window.location.origin is not a valid HTTP URL\n\t\tconst origin =\n\t\t\ttypeof window !== 'undefined' && window.location?.origin?.startsWith('vscode-webview:')\n\t\t\t\t? 'http://localhost:5678'\n\t\t\t\t: typeof window !== 'undefined'\n\t\t\t\t\t? window.location?.origin\n\t\t\t\t\t: 'http://localhost:5678';\n\t\tabsoluteBaseUrl = origin + context.baseUrl;\n\t}\n\n\tconst headers: Record<string, string> = {\n\t\t'browser-id': getBrowserId(),\n\t\t'Content-Type': 'application/json',\n\t};\n\tconst assistantRequest: RequestInit = {\n\t\theaders,\n\t\tmethod: 'POST',\n\t\tcredentials: 'include',\n\t\tbody: JSON.stringify(payload),\n\t\tsignal: abortSignal,\n\t};\n\ttry {\n\t\tconst response = await fetch(`${absoluteBaseUrl}${apiEndpoint}`, assistantRequest);\n\n\t\tif (response.body) {\n\t\t\t// Handle the streaming response\n\t\t\tconst reader = response.body.getReader();\n\t\t\tconst decoder = new TextDecoder('utf-8');\n\n\t\t\tlet buffer = '';\n\n\t\t\tasync function readStream() {\n\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\tif (done) {\n\t\t\t\t\tif (response.ok) {\n\t\t\t\t\t\tonDone?.();\n\t\t\t\t\t} else {\n\t\t\t\t\t\tonErrorOnce?.(\n\t\t\t\t\t\t\tnew ResponseError(response.statusText, {\n\t\t\t\t\t\t\t\thttpStatusCode: response.status,\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst chunk = decoder.decode(value);\n\t\t\t\tbuffer += chunk;\n\n\t\t\t\tconst splitChunks = buffer.split(separator);\n\n\t\t\t\tbuffer = '';\n\t\t\t\tfor (const splitChunk of splitChunks) {\n\t\t\t\t\tif (splitChunk) {\n\t\t\t\t\t\tlet data: T;\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tdata = jsonParse<T>(splitChunk, { errorMessage: 'Invalid json' });\n\t\t\t\t\t\t} catch (e) {\n\t\t\t\t\t\t\t// incomplete json. append to buffer to complete\n\t\t\t\t\t\t\tbuffer += splitChunk;\n\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tif (response.ok) {\n\t\t\t\t\t\t\t\t// Call chunk callback if request was successful\n\t\t\t\t\t\t\t\tonChunk?.(data);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// Otherwise, call error callback\n\t\t\t\t\t\t\t\tconst message = 'message' in data ? data.message : response.statusText;\n\t\t\t\t\t\t\t\tonErrorOnce?.(\n\t\t\t\t\t\t\t\t\tnew ResponseError(String(message), {\n\t\t\t\t\t\t\t\t\t\thttpStatusCode: response.status,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\t\tonErrorOnce?.(e);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tawait readStream();\n\t\t\t}\n\n\t\t\t// Start reading the stream\n\t\t\tawait readStream();\n\t\t} else if (onErrorOnce) {\n\t\t\tonErrorOnce(new Error(response.statusText));\n\t\t}\n\t} catch (e: unknown) {\n\t\tassert(e instanceof Error);\n\t\tonErrorOnce?.(e);\n\t}\n}\n"],"mappings":";;;;;;AASA,MAAM,qBAAqB;CAC1B,IAAI,YAAY,aAAa,QAAQ,uBAAuB;AAC5D,KAAI,CAAC,WAAW;AACf,cAAY,OAAO,YAAY;AAC/B,eAAa,QAAQ,wBAAwB,UAAU;;AAExD,QAAO;;AAGR,MAAa,wBAAwB;AACrC,MAAa,mBAAmB;AAEhC,IAAa,mBAAb,cAAsC,iBAAiB;CACtD,cAAc;AACb,QAAM,oFAAoF;AAC1F,OAAK,OAAO;;;AAId,IAAa,gBAAb,cAAmC,iBAAiB;CAEnD;CAGA;CAGA;CAGA;;;;;;;;;CAUA,YACC,SACA,UAKI,EAAE,EACL;AACD,QAAM,QAAQ;AACd,OAAK,OAAO;EAEZ,MAAM,EAAE,WAAW,gBAAgB,OAAO,SAAS;AACnD,MAAI,UACH,MAAK,YAAY;AAElB,MAAI,eACH,MAAK,iBAAiB;AAEvB,MAAI,MACH,MAAK,mBAAmB;AAEzB,MAAI,KACH,MAAK,OAAO;;;AAMf,MAAM,yBAAyB,WAC9B,OAAO,KAAK,OAAO,CACjB,QAAQ,QAAQ,OAAO,SAAS,OAAU,CAC1C,KAAK,QAAQ;AACb,KAAI,MAAM,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