@sola-air-ui/relay 1.0.4 → 2.0.1
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 +5 -0
- package/package.json +1 -1
- package/src/index.js +60 -25
package/README.md
CHANGED
|
@@ -36,6 +36,11 @@ npx sola-relay --port 4040
|
|
|
36
36
|
- **PostgreSQL** (via `pg`)
|
|
37
37
|
- **MySQL** (via `mysql2`)
|
|
38
38
|
- **Google Sheets** (`type: "sheet"` with a `sheetId`): reads a sheet that is shared publicly, as CSV.
|
|
39
|
+
- **REST table API** (`type: "rest-table"`): reads, and can update, records through a table-style JSON API. Settings: `host` (a hostname such as `records.example.com`, or a full URL), `table` (default table name), and either `token` or `username` and `password`. Each can come from the environment instead: `REST_TABLE_HOST`, `REST_TABLE_BEARER_TOKEN`, `REST_TABLE_USERNAME`, `REST_TABLE_PASSWORD`. Queries pass `table`, `query`, `fields` and `limit` options.
|
|
40
|
+
|
|
41
|
+
### Upgrading from 1.x
|
|
42
|
+
|
|
43
|
+
2.0.0 renames the table connector. Change `type` from the old vendor-named type (or its short alias) to `rest-table`, put a full hostname or URL in `host`, and rename the `SN_*` environment variables to the `REST_TABLE_*` names above. A bare instance name no longer gets a vendor domain added. SQL and Google Sheets connections are unchanged.
|
|
39
44
|
|
|
40
45
|
## Usage from a component
|
|
41
46
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -9,18 +9,55 @@ const { version: RELAY_VERSION } = JSON.parse(
|
|
|
9
9
|
readFileSync(new URL('../package.json', import.meta.url), 'utf8')
|
|
10
10
|
);
|
|
11
11
|
|
|
12
|
+
// Trims whitespace and any run of trailing semicolons (with whitespace between).
|
|
13
|
+
function trimTrailingSemicolons(sql) {
|
|
14
|
+
let end = sql.length;
|
|
15
|
+
while (end > 0 && (sql[end - 1] === ';' || /\s/.test(sql[end - 1]))) end--;
|
|
16
|
+
return sql.slice(0, end).trim();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Replaces string literals with empty ones ('' or "") and comments with a space,
|
|
20
|
+
// in one pass so a quote inside a comment or `--` inside a string is read right.
|
|
21
|
+
// Returns '' for an unterminated string or block comment, which callers reject.
|
|
22
|
+
function stripSqlLiterals(sql) {
|
|
23
|
+
let out = '';
|
|
24
|
+
let i = 0;
|
|
25
|
+
while (i < sql.length) {
|
|
26
|
+
const c = sql[i];
|
|
27
|
+
if (c === "'" || c === '"') {
|
|
28
|
+
let j = i + 1;
|
|
29
|
+
for (;;) {
|
|
30
|
+
if (j >= sql.length) return '';
|
|
31
|
+
if (sql[j] === '\\') { j += 2; continue; }
|
|
32
|
+
if (sql[j] === c) {
|
|
33
|
+
if (sql[j + 1] === c) { j += 2; continue; }
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
j++;
|
|
37
|
+
}
|
|
38
|
+
out += c + c;
|
|
39
|
+
i = j + 1;
|
|
40
|
+
} else if (c === '-' && sql[i + 1] === '-') {
|
|
41
|
+
out += ' ';
|
|
42
|
+
while (i < sql.length && sql[i] !== '\n' && sql[i] !== '\r') i++;
|
|
43
|
+
} else if (c === '/' && sql[i + 1] === '*') {
|
|
44
|
+
const close = sql.indexOf('*/', i + 2);
|
|
45
|
+
if (close < 0) return '';
|
|
46
|
+
out += ' ';
|
|
47
|
+
i = close + 2;
|
|
48
|
+
} else {
|
|
49
|
+
out += c;
|
|
50
|
+
i++;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return out.trim();
|
|
54
|
+
}
|
|
55
|
+
|
|
12
56
|
// Only single-statement read-only SELECT queries are permitted on SQL connectors.
|
|
13
57
|
export function isReadOnlySql(sql) {
|
|
14
58
|
if (typeof sql !== 'string') return false;
|
|
15
|
-
const
|
|
16
|
-
if (!
|
|
17
|
-
const withoutStrings = trimmed
|
|
18
|
-
.replace(/'(?:''|\\.|[^'])*'/g, "''")
|
|
19
|
-
.replace(/"(?:""|\\.|[^"])*"/g, '""');
|
|
20
|
-
const withoutComments = withoutStrings
|
|
21
|
-
.replace(/--[^\r\n]*/g, ' ')
|
|
22
|
-
.replace(/\/\*[\s\S]*?\*\//g, ' ')
|
|
23
|
-
.trim();
|
|
59
|
+
const withoutComments = stripSqlLiterals(trimTrailingSemicolons(sql));
|
|
60
|
+
if (!withoutComments) return false;
|
|
24
61
|
if (!/^SELECT\b/i.test(withoutComments)) return false;
|
|
25
62
|
if (withoutComments.includes(';')) return false;
|
|
26
63
|
// INTO covers SELECT ... INTO, which creates a table (PostgreSQL) or writes a file
|
|
@@ -149,18 +186,18 @@ class GoogleSheetsConnector {
|
|
|
149
186
|
async disconnect() {}
|
|
150
187
|
}
|
|
151
188
|
|
|
152
|
-
class
|
|
189
|
+
class RestTableConnector {
|
|
153
190
|
constructor(config) {
|
|
154
|
-
this.instance = config.instance || config.host || process.env.
|
|
155
|
-
this.username = config.username || config.user || process.env.
|
|
156
|
-
this.password = config.password || process.env.
|
|
157
|
-
this.token = config.token || config.bearerToken || process.env.
|
|
191
|
+
this.instance = config.instance || config.host || process.env.REST_TABLE_HOST || '';
|
|
192
|
+
this.username = config.username || config.user || process.env.REST_TABLE_USERNAME || '';
|
|
193
|
+
this.password = config.password || process.env.REST_TABLE_PASSWORD || '';
|
|
194
|
+
this.token = config.token || config.bearerToken || process.env.REST_TABLE_BEARER_TOKEN || '';
|
|
158
195
|
this.defaultTable = config.table || 'incident';
|
|
159
196
|
}
|
|
160
197
|
|
|
161
198
|
async connect() {
|
|
162
199
|
if (!this.instance) {
|
|
163
|
-
throw new Error("
|
|
200
|
+
throw new Error("REST table host (a hostname or full URL) is required (e.g. 'records.example.com')");
|
|
164
201
|
}
|
|
165
202
|
}
|
|
166
203
|
|
|
@@ -170,7 +207,7 @@ class ServiceNowConnector {
|
|
|
170
207
|
const limit = options.limit || options.sysparm_limit || 20;
|
|
171
208
|
const fields = options.fields || 'number,short_description,priority,state,assigned_to,assignment_group,sys_created_on';
|
|
172
209
|
|
|
173
|
-
const baseUrl = this.instance.startsWith('http') ? this.instance : `https://${this.instance}
|
|
210
|
+
const baseUrl = this.instance.startsWith('http') ? this.instance : `https://${this.instance}`;
|
|
174
211
|
const endpoint = `${baseUrl}/api/now/table/${table}?sysparm_query=${encodeURIComponent(query)}&sysparm_limit=${limit}&sysparm_display_value=true&sysparm_fields=${encodeURIComponent(fields)}`;
|
|
175
212
|
|
|
176
213
|
const headers = {
|
|
@@ -188,7 +225,7 @@ class ServiceNowConnector {
|
|
|
188
225
|
const res = await fetch(endpoint, { headers });
|
|
189
226
|
if (!res.ok) {
|
|
190
227
|
const errText = await res.text();
|
|
191
|
-
throw new Error(`
|
|
228
|
+
throw new Error(`Table API error (${res.status} ${res.statusText}): ${errText}`);
|
|
192
229
|
}
|
|
193
230
|
|
|
194
231
|
const data = await res.json();
|
|
@@ -199,9 +236,9 @@ class ServiceNowConnector {
|
|
|
199
236
|
async mutate(action, payload = {}) {
|
|
200
237
|
const table = payload.table || this.defaultTable;
|
|
201
238
|
const sysId = payload.sys_id || payload.id;
|
|
202
|
-
if (!sysId) throw new Error("sys_id is required to mutate
|
|
239
|
+
if (!sysId) throw new Error("sys_id is required to mutate record");
|
|
203
240
|
|
|
204
|
-
const baseUrl = this.instance.startsWith('http') ? this.instance : `https://${this.instance}
|
|
241
|
+
const baseUrl = this.instance.startsWith('http') ? this.instance : `https://${this.instance}`;
|
|
205
242
|
const endpoint = `${baseUrl}/api/now/table/${table}/${sysId}`;
|
|
206
243
|
|
|
207
244
|
const headers = {
|
|
@@ -225,7 +262,7 @@ class ServiceNowConnector {
|
|
|
225
262
|
|
|
226
263
|
if (!res.ok) {
|
|
227
264
|
const errText = await res.text();
|
|
228
|
-
throw new Error(`
|
|
265
|
+
throw new Error(`Table mutation error (${res.status} ${res.statusText}): ${errText}`);
|
|
229
266
|
}
|
|
230
267
|
|
|
231
268
|
const data = await res.json();
|
|
@@ -326,9 +363,8 @@ export function addConnection(name, type, config) {
|
|
|
326
363
|
case 'googlesheets':
|
|
327
364
|
connector = new GoogleSheetsConnector(config);
|
|
328
365
|
break;
|
|
329
|
-
case '
|
|
330
|
-
|
|
331
|
-
connector = new ServiceNowConnector(config);
|
|
366
|
+
case 'rest-table':
|
|
367
|
+
connector = new RestTableConnector(config);
|
|
332
368
|
break;
|
|
333
369
|
default:
|
|
334
370
|
throw new Error(`Unsupported connector type: ${type}`);
|
|
@@ -488,8 +524,7 @@ export class RelayServer {
|
|
|
488
524
|
const isNonSql =
|
|
489
525
|
conn.type === 'sheet' ||
|
|
490
526
|
conn.type === 'googlesheets' ||
|
|
491
|
-
conn.type === '
|
|
492
|
-
conn.type === 'now';
|
|
527
|
+
conn.type === 'rest-table';
|
|
493
528
|
|
|
494
529
|
let result;
|
|
495
530
|
if (isNonSql) {
|