@sdflc/backend-helpers 1.0.139 → 1.0.141
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 +471 -29
- package/build/EmergencyAlerter.d.ts +140 -0
- package/build/EmergencyAlerter.js +465 -0
- package/build/esm/EmergencyAlerter.d.ts +140 -0
- package/build/esm/EmergencyAlerter.js +454 -0
- package/build/esm/gateways/BaseExternalApiGateway.d.ts +94 -0
- package/build/esm/gateways/BaseExternalApiGateway.js +319 -0
- package/build/esm/gateways/DiscordWebhookGw.d.ts +50 -0
- package/build/esm/gateways/DiscordWebhookGw.js +70 -0
- package/build/esm/gateways/HttpWebhookGw.d.ts +43 -0
- package/build/esm/gateways/HttpWebhookGw.js +131 -0
- package/build/esm/gateways/SendGridGw.d.ts +41 -0
- package/build/esm/gateways/SendGridGw.js +116 -0
- package/build/esm/gateways/SlackWebhookGw.d.ts +23 -0
- package/build/esm/gateways/SlackWebhookGw.js +59 -0
- package/build/esm/gateways/TelegramGw.d.ts +45 -0
- package/build/esm/gateways/TelegramGw.js +110 -0
- package/build/esm/gateways/TwilioGw.d.ts +33 -0
- package/build/esm/gateways/TwilioGw.js +107 -0
- package/build/esm/gateways/externalUrl.d.ts +16 -0
- package/build/esm/gateways/externalUrl.js +116 -0
- package/build/esm/gateways/guardedFetch.d.ts +15 -0
- package/build/esm/gateways/guardedFetch.js +133 -0
- package/build/esm/gateways/index.d.ts +9 -0
- package/build/esm/gateways/index.js +9 -0
- package/build/esm/index.d.ts +2 -1
- package/build/esm/index.js +2 -1
- package/build/esm/interfaces/BaseExternalApiGatewayPropsInterface.d.ts +16 -2
- package/build/gateways/BaseExternalApiGateway.d.ts +94 -0
- package/build/gateways/BaseExternalApiGateway.js +326 -0
- package/build/gateways/DiscordWebhookGw.d.ts +50 -0
- package/build/gateways/DiscordWebhookGw.js +73 -0
- package/build/gateways/HttpWebhookGw.d.ts +43 -0
- package/build/gateways/HttpWebhookGw.js +135 -0
- package/build/gateways/SendGridGw.d.ts +41 -0
- package/build/gateways/SendGridGw.js +119 -0
- package/build/gateways/SlackWebhookGw.d.ts +23 -0
- package/build/gateways/SlackWebhookGw.js +62 -0
- package/build/gateways/TelegramGw.d.ts +45 -0
- package/build/gateways/TelegramGw.js +115 -0
- package/build/gateways/TwilioGw.d.ts +33 -0
- package/build/gateways/TwilioGw.js +110 -0
- package/build/gateways/externalUrl.d.ts +16 -0
- package/build/gateways/externalUrl.js +121 -0
- package/build/gateways/guardedFetch.d.ts +15 -0
- package/build/gateways/guardedFetch.js +141 -0
- package/build/gateways/index.d.ts +9 -0
- package/build/gateways/index.js +25 -0
- package/build/index.d.ts +2 -1
- package/build/index.js +2 -1
- package/build/interfaces/BaseExternalApiGatewayPropsInterface.d.ts +16 -2
- package/package.json +2 -1
- package/build/BaseExternalApiGateway.d.ts +0 -16
- package/build/BaseExternalApiGateway.js +0 -54
- package/build/esm/BaseExternalApiGateway.d.ts +0 -16
- package/build/esm/BaseExternalApiGateway.js +0 -48
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createGuardedFetch = exports.guardedLookup = void 0;
|
|
7
|
+
// ./src/gateways/guardedFetch.ts
|
|
8
|
+
//
|
|
9
|
+
// A `fetch` for URLs a USER gave us (webhooks) that refuses a forbidden address AT THE MOMENT OF CONNECTING.
|
|
10
|
+
//
|
|
11
|
+
// ⚠ WHY NOT JUST CHECK THE URL FIRST: `checkExternalUrl` resolves the host name once; the platform fetch then resolves it
|
|
12
|
+
// AGAIN, on every attempt. A DNS server that answers a public address to the first lookup and 169.254.169.254 to the
|
|
13
|
+
// second (DNS rebinding) walks straight past a check made beforehand. Here the address the socket actually connects
|
|
14
|
+
// to is the one checked: Node's `http(s).request` takes the lookup, and this lookup refuses a blocked address.
|
|
15
|
+
// ⚠ An IP written in the URL is never looked up — it is checked here directly.
|
|
16
|
+
// ⚠ Redirects are never followed (http.request does not), the reply is capped at MAX_REPLY_BYTES, and the request ends at
|
|
17
|
+
// its signal (the gateway's timeout).
|
|
18
|
+
const http_1 = __importDefault(require("http"));
|
|
19
|
+
const https_1 = __importDefault(require("https"));
|
|
20
|
+
const buffer_1 = require("buffer");
|
|
21
|
+
const net_1 = require("net");
|
|
22
|
+
const dns_1 = require("dns");
|
|
23
|
+
const BaseExternalApiGateway_1 = require("./BaseExternalApiGateway");
|
|
24
|
+
const externalUrl_1 = require("./externalUrl");
|
|
25
|
+
/** The error a refused address raises — the gateway reports it as VALIDATION_FAILED, never retried. */
|
|
26
|
+
const blockedError = (address) => Object.assign(new Error(`The address ${address} is not allowed`), { code: 'EBLOCKED' });
|
|
27
|
+
/**
|
|
28
|
+
* A `dns.lookup` that refuses when ANY address the name resolves to is blocked (a name with one private address among
|
|
29
|
+
* public ones is refused, whichever the socket would have picked).
|
|
30
|
+
*/
|
|
31
|
+
const guardedLookup = (opts = {}) => (hostname, options, callback) => {
|
|
32
|
+
const resolve = opts.dnsLookup ?? dns_1.lookup;
|
|
33
|
+
const isBlocked = opts.isBlocked ?? externalUrl_1.isBlockedAddress;
|
|
34
|
+
const wantsAll = typeof options === 'object' && options?.all === true;
|
|
35
|
+
resolve(hostname, { ...(typeof options === 'object' ? options : {}), all: true }, (err, addresses) => {
|
|
36
|
+
if (err) {
|
|
37
|
+
callback(err);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const list = Array.isArray(addresses) ? addresses : [];
|
|
41
|
+
const refused = list.find((one) => isBlocked(String(one?.address)));
|
|
42
|
+
if (refused || list.length === 0) {
|
|
43
|
+
callback(refused
|
|
44
|
+
? blockedError(String(refused.address))
|
|
45
|
+
: Object.assign(new Error(`${hostname} did not resolve`), { code: 'ENOTFOUND' }));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (wantsAll) {
|
|
49
|
+
callback(null, list);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
callback(null, list[0].address, list[0].family);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
exports.guardedLookup = guardedLookup;
|
|
57
|
+
/** A fetch whose every connection goes through `guardedLookup`. The answer has `status`, `headers` and `text()`. */
|
|
58
|
+
const createGuardedFetch = (opts = {}) => {
|
|
59
|
+
const isBlocked = opts.isBlocked ?? externalUrl_1.isBlockedAddress;
|
|
60
|
+
return (url, init = {}) => new Promise((resolve, reject) => {
|
|
61
|
+
let target;
|
|
62
|
+
try {
|
|
63
|
+
target = new URL(url);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
reject(new TypeError('Invalid URL'));
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const literal = target.hostname.replace(/^\[|\]$/g, '');
|
|
70
|
+
if ((0, net_1.isIP)(literal) && isBlocked(literal)) {
|
|
71
|
+
reject(blockedError(literal));
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const signal = init.signal;
|
|
75
|
+
if (signal?.aborted) {
|
|
76
|
+
reject(signal.reason);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const transport = target.protocol === 'https:' ? https_1.default : target.protocol === 'http:' ? http_1.default : null;
|
|
80
|
+
if (!transport) {
|
|
81
|
+
reject(new TypeError(`Unsupported protocol ${target.protocol}`));
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
let req;
|
|
85
|
+
try {
|
|
86
|
+
req = transport.request(target, {
|
|
87
|
+
method: init.method ?? 'GET',
|
|
88
|
+
headers: init.headers ?? {},
|
|
89
|
+
lookup: (0, exports.guardedLookup)(opts),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
// A malformed header is refused synchronously.
|
|
94
|
+
reject(e instanceof TypeError ? e : new TypeError(String(e?.message ?? e)));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const onAbort = () => req.destroy(signal?.reason);
|
|
98
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
99
|
+
req.on('error', (e) => {
|
|
100
|
+
signal?.removeEventListener('abort', onAbort);
|
|
101
|
+
reject(e);
|
|
102
|
+
});
|
|
103
|
+
req.on('response', (res) => {
|
|
104
|
+
const chunks = [];
|
|
105
|
+
let size = 0;
|
|
106
|
+
let settled = false;
|
|
107
|
+
const finish = () => {
|
|
108
|
+
if (settled) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
settled = true;
|
|
112
|
+
signal?.removeEventListener('abort', onAbort);
|
|
113
|
+
const text = buffer_1.Buffer.concat(chunks).toString('utf8');
|
|
114
|
+
resolve({ status: res.statusCode ?? 0, headers: { ...res.headers }, text: async () => text });
|
|
115
|
+
};
|
|
116
|
+
res.on('data', (chunk) => {
|
|
117
|
+
if (size + chunk.length > BaseExternalApiGateway_1.MAX_REPLY_BYTES) {
|
|
118
|
+
chunks.push(chunk.subarray(0, BaseExternalApiGateway_1.MAX_REPLY_BYTES - size));
|
|
119
|
+
size = BaseExternalApiGateway_1.MAX_REPLY_BYTES;
|
|
120
|
+
finish();
|
|
121
|
+
res.destroy();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
chunks.push(chunk);
|
|
125
|
+
size += chunk.length;
|
|
126
|
+
});
|
|
127
|
+
res.on('end', finish);
|
|
128
|
+
res.on('error', (e) => {
|
|
129
|
+
if (!settled) {
|
|
130
|
+
settled = true;
|
|
131
|
+
reject(e);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
if (init.body !== undefined && init.body !== null) {
|
|
136
|
+
req.write(typeof init.body === 'string' ? init.body : String(init.body));
|
|
137
|
+
}
|
|
138
|
+
req.end();
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
exports.createGuardedFetch = createGuardedFetch;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './BaseExternalApiGateway';
|
|
2
|
+
export * from './externalUrl';
|
|
3
|
+
export * from './SendGridGw';
|
|
4
|
+
export * from './TelegramGw';
|
|
5
|
+
export * from './TwilioGw';
|
|
6
|
+
export * from './SlackWebhookGw';
|
|
7
|
+
export * from './DiscordWebhookGw';
|
|
8
|
+
export * from './HttpWebhookGw';
|
|
9
|
+
export * from './guardedFetch';
|
|
@@ -0,0 +1,25 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./BaseExternalApiGateway"), exports);
|
|
18
|
+
__exportStar(require("./externalUrl"), exports);
|
|
19
|
+
__exportStar(require("./SendGridGw"), exports);
|
|
20
|
+
__exportStar(require("./TelegramGw"), exports);
|
|
21
|
+
__exportStar(require("./TwilioGw"), exports);
|
|
22
|
+
__exportStar(require("./SlackWebhookGw"), exports);
|
|
23
|
+
__exportStar(require("./DiscordWebhookGw"), exports);
|
|
24
|
+
__exportStar(require("./HttpWebhookGw"), exports);
|
|
25
|
+
__exportStar(require("./guardedFetch"), exports);
|
package/build/index.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export * from './commonValidators';
|
|
|
21
21
|
export * from './downloadHelpers';
|
|
22
22
|
export * from './resolveClientIp';
|
|
23
23
|
export * from './BaseRestApiCrudController';
|
|
24
|
-
export * from './
|
|
24
|
+
export * from './gateways';
|
|
25
25
|
export * from './IpRateLimiter';
|
|
26
26
|
export * from './uuidHelpers';
|
|
27
|
+
export * from './EmergencyAlerter';
|
package/build/index.js
CHANGED
|
@@ -37,6 +37,7 @@ __exportStar(require("./commonValidators"), exports);
|
|
|
37
37
|
__exportStar(require("./downloadHelpers"), exports);
|
|
38
38
|
__exportStar(require("./resolveClientIp"), exports);
|
|
39
39
|
__exportStar(require("./BaseRestApiCrudController"), exports);
|
|
40
|
-
__exportStar(require("./
|
|
40
|
+
__exportStar(require("./gateways"), exports);
|
|
41
41
|
__exportStar(require("./IpRateLimiter"), exports);
|
|
42
42
|
__exportStar(require("./uuidHelpers"), exports);
|
|
43
|
+
__exportStar(require("./EmergencyAlerter"), exports);
|
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
/** The HTTP client a gateway calls — the platform `fetch` unless a test passes its own. */
|
|
2
|
+
export type ExternalApiFetch = (url: string, init: any) => Promise<any>;
|
|
3
|
+
export interface BaseExternalApiGatewayPropsInterface<TSettings extends Record<string, any> = Record<string, any>> {
|
|
4
|
+
/**
|
|
5
|
+
* Optional settings to start with — the same as calling `configure(settings)` right after construction.
|
|
6
|
+
* A gateway is usually created ONCE and configured (and re-configured) at run time.
|
|
7
|
+
*/
|
|
8
|
+
settings?: TSettings;
|
|
2
9
|
/**
|
|
3
10
|
* Optional credentials — a plain API key string or a structured object
|
|
4
11
|
* with whatever keys the external service requires.
|
|
5
12
|
* Can also be set later via setCredentials() once resolved at runtime.
|
|
13
|
+
* ⓘ Kept for gateways written before `configure()`; a new gateway takes its credentials in its settings.
|
|
6
14
|
*/
|
|
7
15
|
credentials?: string | Record<string, any>;
|
|
8
16
|
/**
|
|
@@ -11,9 +19,15 @@ export interface BaseExternalApiGatewayPropsInterface {
|
|
|
11
19
|
*/
|
|
12
20
|
baseUrl?: string;
|
|
13
21
|
/**
|
|
14
|
-
* Timeout in milliseconds for outbound
|
|
22
|
+
* Timeout in milliseconds for one outbound request.
|
|
15
23
|
* Defaults to 10000.
|
|
16
24
|
*/
|
|
17
25
|
timeoutMs?: number;
|
|
26
|
+
/** Attempts AFTER the first one, for failures worth retrying (network, timeout, 429, 5xx). Defaults to 2. */
|
|
27
|
+
retries?: number;
|
|
28
|
+
/** The waits between attempts, in milliseconds (the last one repeats). Defaults to [1000, 2000]. */
|
|
29
|
+
retryDelaysMs?: number[];
|
|
30
|
+
/** The HTTP client. Defaults to the platform `fetch`. */
|
|
31
|
+
fetch?: ExternalApiFetch;
|
|
18
32
|
logger?: any;
|
|
19
33
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdflc/backend-helpers",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.141",
|
|
4
4
|
"description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"exports": {
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"lint": "ESLINT_USE_FLAT_CONFIG=true eslint .",
|
|
21
21
|
"test": "echo No Tests Available Yet",
|
|
22
22
|
"testToDo": "jest --config jest.config.ts",
|
|
23
|
+
"prove": "node scripts/proveCreateId.cjs && node scripts/proveGateways.cjs && node scripts/proveEmergencyAlerter.cjs",
|
|
23
24
|
"format": "prettier --write \"src/**/*.ts\"",
|
|
24
25
|
"prepare": "husky",
|
|
25
26
|
"prepare1": "husky install",
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Logger } from '@sdflc/utils';
|
|
2
|
-
import { BaseExternalApiGatewayPropsInterface } from './interfaces';
|
|
3
|
-
declare abstract class BaseExternalApiGateway {
|
|
4
|
-
protected logger: Logger;
|
|
5
|
-
protected baseUrl: string;
|
|
6
|
-
protected timeoutMs: number;
|
|
7
|
-
private _credentials;
|
|
8
|
-
private _credentialsSet;
|
|
9
|
-
constructor(props?: BaseExternalApiGatewayPropsInterface);
|
|
10
|
-
setCredentials(credentials: string | Record<string, any>): this;
|
|
11
|
-
hasCredentials(): boolean;
|
|
12
|
-
protected get credentials(): string | Record<string, any>;
|
|
13
|
-
protected assertCredentials(): void;
|
|
14
|
-
protected onCredentialsSet(): void;
|
|
15
|
-
}
|
|
16
|
-
export { BaseExternalApiGateway };
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// ./src/baseExternalApiGateway.ts
|
|
3
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
-
};
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.BaseExternalApiGateway = void 0;
|
|
8
|
-
const check_types_1 = __importDefault(require("check-types"));
|
|
9
|
-
const utils_1 = require("@sdflc/utils");
|
|
10
|
-
class BaseExternalApiGateway {
|
|
11
|
-
logger;
|
|
12
|
-
baseUrl;
|
|
13
|
-
timeoutMs;
|
|
14
|
-
_credentials = null;
|
|
15
|
-
_credentialsSet = false;
|
|
16
|
-
constructor(props) {
|
|
17
|
-
const { credentials, baseUrl, timeoutMs, logger } = props || {};
|
|
18
|
-
this.baseUrl = baseUrl ?? '';
|
|
19
|
-
this.timeoutMs = timeoutMs ?? 10_000;
|
|
20
|
-
this.logger = (0, utils_1.isLogger)(logger) ? logger : new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
|
|
21
|
-
if (credentials !== undefined) {
|
|
22
|
-
this.setCredentials(credentials);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
// ─── Credentials ───────────────────────────────────────────────────────────
|
|
26
|
-
setCredentials(credentials) {
|
|
27
|
-
if (!check_types_1.default.string(credentials) && !check_types_1.default.object(credentials)) {
|
|
28
|
-
throw new Error(`${this.constructor.name}.setCredentials: credentials must be a string or an object`);
|
|
29
|
-
}
|
|
30
|
-
if (check_types_1.default.string(credentials) && !credentials.trim()) {
|
|
31
|
-
throw new Error(`${this.constructor.name}.setCredentials: credentials string must not be empty`);
|
|
32
|
-
}
|
|
33
|
-
this._credentials = credentials;
|
|
34
|
-
this._credentialsSet = true;
|
|
35
|
-
this.onCredentialsSet();
|
|
36
|
-
return this;
|
|
37
|
-
}
|
|
38
|
-
hasCredentials() {
|
|
39
|
-
return this._credentialsSet;
|
|
40
|
-
}
|
|
41
|
-
get credentials() {
|
|
42
|
-
return this._credentials;
|
|
43
|
-
}
|
|
44
|
-
assertCredentials() {
|
|
45
|
-
if (!this._credentialsSet) {
|
|
46
|
-
throw new Error(`${this.constructor.name}: credentials have not been set. ` + `Call setCredentials() before making API calls.`);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
// ─── Lifecycle hook ────────────────────────────────────────────────────────
|
|
50
|
-
onCredentialsSet() {
|
|
51
|
-
// no-op — override in subclass to initialise SDK clients etc.
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
exports.BaseExternalApiGateway = BaseExternalApiGateway;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Logger } from '@sdflc/utils';
|
|
2
|
-
import { BaseExternalApiGatewayPropsInterface } from './interfaces';
|
|
3
|
-
declare abstract class BaseExternalApiGateway {
|
|
4
|
-
protected logger: Logger;
|
|
5
|
-
protected baseUrl: string;
|
|
6
|
-
protected timeoutMs: number;
|
|
7
|
-
private _credentials;
|
|
8
|
-
private _credentialsSet;
|
|
9
|
-
constructor(props?: BaseExternalApiGatewayPropsInterface);
|
|
10
|
-
setCredentials(credentials: string | Record<string, any>): this;
|
|
11
|
-
hasCredentials(): boolean;
|
|
12
|
-
protected get credentials(): string | Record<string, any>;
|
|
13
|
-
protected assertCredentials(): void;
|
|
14
|
-
protected onCredentialsSet(): void;
|
|
15
|
-
}
|
|
16
|
-
export { BaseExternalApiGateway };
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
// ./src/baseExternalApiGateway.ts
|
|
2
|
-
import check from 'check-types';
|
|
3
|
-
import { Logger, LoggerLevels, isLogger } from '@sdflc/utils';
|
|
4
|
-
class BaseExternalApiGateway {
|
|
5
|
-
logger;
|
|
6
|
-
baseUrl;
|
|
7
|
-
timeoutMs;
|
|
8
|
-
_credentials = null;
|
|
9
|
-
_credentialsSet = false;
|
|
10
|
-
constructor(props) {
|
|
11
|
-
const { credentials, baseUrl, timeoutMs, logger } = props || {};
|
|
12
|
-
this.baseUrl = baseUrl ?? '';
|
|
13
|
-
this.timeoutMs = timeoutMs ?? 10_000;
|
|
14
|
-
this.logger = isLogger(logger) ? logger : new Logger({ level: LoggerLevels.ERROR });
|
|
15
|
-
if (credentials !== undefined) {
|
|
16
|
-
this.setCredentials(credentials);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
// ─── Credentials ───────────────────────────────────────────────────────────
|
|
20
|
-
setCredentials(credentials) {
|
|
21
|
-
if (!check.string(credentials) && !check.object(credentials)) {
|
|
22
|
-
throw new Error(`${this.constructor.name}.setCredentials: credentials must be a string or an object`);
|
|
23
|
-
}
|
|
24
|
-
if (check.string(credentials) && !credentials.trim()) {
|
|
25
|
-
throw new Error(`${this.constructor.name}.setCredentials: credentials string must not be empty`);
|
|
26
|
-
}
|
|
27
|
-
this._credentials = credentials;
|
|
28
|
-
this._credentialsSet = true;
|
|
29
|
-
this.onCredentialsSet();
|
|
30
|
-
return this;
|
|
31
|
-
}
|
|
32
|
-
hasCredentials() {
|
|
33
|
-
return this._credentialsSet;
|
|
34
|
-
}
|
|
35
|
-
get credentials() {
|
|
36
|
-
return this._credentials;
|
|
37
|
-
}
|
|
38
|
-
assertCredentials() {
|
|
39
|
-
if (!this._credentialsSet) {
|
|
40
|
-
throw new Error(`${this.constructor.name}: credentials have not been set. ` + `Call setCredentials() before making API calls.`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
// ─── Lifecycle hook ────────────────────────────────────────────────────────
|
|
44
|
-
onCredentialsSet() {
|
|
45
|
-
// no-op — override in subclass to initialise SDK clients etc.
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
export { BaseExternalApiGateway };
|