-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_request.js
More file actions
127 lines (118 loc) · 3.53 KB
/
http_request.js
File metadata and controls
127 lines (118 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { NetsuiteApiClient } from "netsuite-api-client";
export default {
name: "NetSuite Request",
description: "Send a request to the NetSuite REST API.",
key: "netsuite_request",
version: "0.0.12",
type: "action",
props: {
config: {
type: "string",
label: "NetSuite Config",
description: `NetSuite Configuration JSON object. In format, {"consumer_key":"...","consumer_secret_key":"...","token":"...","token_secret":"...","realm":"..."}`,
secret: true,
},
method: {
type: "string",
label: "HTTP Method",
description: "GET, POST, PUT, DELETE, etc.",
options: ["GET", "POST", "PUT", "DELETE", "PATCH"],
default: "GET",
},
path: {
type: "string",
label: "Endpoint Path",
description: "For example: `/record/v1/customer`",
},
body: {
type: "object",
label: "Request Body (optional)",
description: "JSON body for POST/PUT requests.",
optional: true,
},
headers: {
type: "object",
label: "Headers (optional)",
description: "Additional headers to include in the request.",
optional: true,
},
get_more: {
type: "boolean",
label: "Get all records if has more",
description: "Get all records if has more",
optional: true,
},
skip: {
type: "boolean",
label: "Skip",
description: "Set Skip to TRUE or an expression that evaluates to true to skip this step.",
default: false,
optional: true,
},
throw_errors: {
type: "boolean",
label: "Throw errors (vs. returning `error` property)",
description: "If true, Errors thrown during NetSuite call will be thrown by this. If false, they will be returned in an `error` property in the return value object.",
default: true,
optional: true,
}
},
methods: {
getNextLink(links) {
const next = links.find(link => link.rel == "next");
const url = new URL(next.href)
return this.trimPrefix(url.pathname, "/services/rest") + url.search
},
trimPrefix(str, prefix) {
if (str.startsWith(prefix)) {
return str.substring(prefix.length)
}
return str
}
},
async run({ $ }) {
if (this.skip) {
$.export("$summary", `Skipped`)
return {}
}
const client = new NetsuiteApiClient(JSON.parse(this.config));
const options = {
method: this.method,
path: this.path,
body: this.body ? JSON.stringify(this.body) : undefined,
headers: this.headers || undefined,
};
if (this.method == "GET") {
delete options.body;
}
try {
let response = await client.request(options);
if (this.get_more && response.data.hasMore) {
let items = response.data.items
do {
options.path = await this.getNextLink(response.data.links)
response = await client.request(options)
items = items.concat(response.data.items)
} while (response.data.hasMore)
// try to keep response similar
response.data.items = items
}
$.export("$summary", `${this.method} ${this.path} succeeded.`);
return response.data;
} catch (error) {
console.error(
"NetSuite API Error:",
error.response?.data || error.message
);
const errorMessage = `Failed to execute NetSuite request: ${
error.response?.data?.detail || error.message
}`
if (this.throw_errors) {
throw new Error(errorMessage);
}
return {
error: errorMessage,
}
}
},
};