@oondemand/oon-core-back 0.3.63 → 0.3.65
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/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const SENSITIVE_KEY = /(authorization|cookie|password|passwd|secret|token|api[-_]?key|app[-_]?key|app[-_]?secret|private[-_]?key|credential|senha)/i;
|
|
4
4
|
const DEFAULTS = {
|
|
5
|
-
maxDepth:
|
|
5
|
+
maxDepth: 12,
|
|
6
6
|
maxArrayItems: 20,
|
|
7
7
|
maxObjectKeys: 100,
|
|
8
8
|
maxStringLength: 4000,
|
|
@@ -95,6 +95,22 @@ function responseError(body, statusCode) {
|
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
function normalizedStringList(value) {
|
|
99
|
+
const values = Array.isArray(value) ? value : value === undefined || value === null ? [] : [value];
|
|
100
|
+
return values.map((item) => String(item).trim()).filter(Boolean);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function isConfiguredEmptyResult(callDefinition = {}, body = {}) {
|
|
104
|
+
const code = String(body?.faultcode ?? body?.code ?? body?.codigo_status ?? "").trim();
|
|
105
|
+
const description = String(
|
|
106
|
+
body?.faultstring ?? body?.description ?? body?.descricao_status ?? "",
|
|
107
|
+
).trim();
|
|
108
|
+
const codes = normalizedStringList(callDefinition.emptyResultFaultCodes);
|
|
109
|
+
const messages = normalizedStringList(callDefinition.emptyResultMessages);
|
|
110
|
+
return (code && codes.includes(code))
|
|
111
|
+
|| (description && messages.some((message) => description.includes(message)));
|
|
112
|
+
}
|
|
113
|
+
|
|
98
114
|
function retryableTransportError(error) {
|
|
99
115
|
const statusCode = Number(error?.response?.status || error?.statusCode || 0);
|
|
100
116
|
return RETRYABLE_HTTP.has(statusCode)
|
|
@@ -106,6 +122,17 @@ function wait(ms) {
|
|
|
106
122
|
return ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve();
|
|
107
123
|
}
|
|
108
124
|
|
|
125
|
+
async function resolveCallParam(callDefinition, input = {}, context = {}) {
|
|
126
|
+
const declared = typeof callDefinition.param === "function"
|
|
127
|
+
? await callDefinition.param({ input, context, cursor: context.cursor })
|
|
128
|
+
: callDefinition.param;
|
|
129
|
+
return resolveTemplate(declared ?? [{}], {
|
|
130
|
+
input,
|
|
131
|
+
context,
|
|
132
|
+
cursor: context.cursor,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
109
136
|
function createOmieClient(options = {}) {
|
|
110
137
|
const credentials = options.credentials || {};
|
|
111
138
|
const appKey = String(credentials.appKey || "").trim();
|
|
@@ -148,20 +175,17 @@ function createOmieClient(options = {}) {
|
|
|
148
175
|
code: "OMIE_CALL_MISSING",
|
|
149
176
|
});
|
|
150
177
|
}
|
|
151
|
-
const resolvedParam =
|
|
152
|
-
input,
|
|
153
|
-
context,
|
|
154
|
-
cursor: context.cursor,
|
|
155
|
-
});
|
|
178
|
+
const resolvedParam = await resolveCallParam(callDefinition, input, context);
|
|
156
179
|
const body = {
|
|
157
180
|
call,
|
|
158
181
|
app_key: appKey,
|
|
159
182
|
app_secret: appSecret,
|
|
160
183
|
param: Array.isArray(resolvedParam) ? resolvedParam : [resolvedParam],
|
|
161
184
|
};
|
|
185
|
+
const callMaxAttempts = Math.max(1, Number(callDefinition.maxAttempts ?? maxAttempts));
|
|
162
186
|
let lastError;
|
|
163
187
|
|
|
164
|
-
for (let attempt = 1; attempt <=
|
|
188
|
+
for (let attempt = 1; attempt <= callMaxAttempts; attempt += 1) {
|
|
165
189
|
const startedAt = Date.now();
|
|
166
190
|
const trace = {
|
|
167
191
|
endpoint: callDefinition.endpoint,
|
|
@@ -178,14 +202,18 @@ function createOmieClient(options = {}) {
|
|
|
178
202
|
});
|
|
179
203
|
const status = Number(response?.status || 200);
|
|
180
204
|
const data = response?.data ?? {};
|
|
205
|
+
const emptyResult = isConfiguredEmptyResult(callDefinition, data);
|
|
181
206
|
trace.httpStatus = status;
|
|
182
207
|
trace.response = sanitizeIntegrationValue(data);
|
|
183
208
|
trace.durationMs = Date.now() - startedAt;
|
|
184
|
-
|
|
185
|
-
if (
|
|
209
|
+
trace.emptyResult = emptyResult;
|
|
210
|
+
if (!emptyResult) {
|
|
211
|
+
const normalized = responseError(data, status);
|
|
212
|
+
if (normalized) throw normalized;
|
|
213
|
+
}
|
|
186
214
|
trace.status = "success";
|
|
187
215
|
context.recordRequest?.(trace);
|
|
188
|
-
return { data, trace };
|
|
216
|
+
return { data, trace, emptyResult };
|
|
189
217
|
} catch (error) {
|
|
190
218
|
const normalized = error instanceof OmieApiError
|
|
191
219
|
? error
|
|
@@ -202,7 +230,7 @@ function createOmieClient(options = {}) {
|
|
|
202
230
|
context.recordRequest?.(trace);
|
|
203
231
|
normalized.trace = trace;
|
|
204
232
|
lastError = normalized;
|
|
205
|
-
if (!normalized.retryable || attempt >=
|
|
233
|
+
if (!normalized.retryable || attempt >= callMaxAttempts) throw normalized;
|
|
206
234
|
const delay = Math.min(5_000, retryInitialMs * (2 ** (attempt - 1)));
|
|
207
235
|
await wait(delay);
|
|
208
236
|
}
|
|
@@ -246,7 +274,9 @@ module.exports = {
|
|
|
246
274
|
OmieApiError,
|
|
247
275
|
createOmieClient,
|
|
248
276
|
getPath,
|
|
277
|
+
isConfiguredEmptyResult,
|
|
249
278
|
normalizeEndpoint,
|
|
279
|
+
resolveCallParam,
|
|
250
280
|
resolveTemplate,
|
|
251
281
|
responseError,
|
|
252
|
-
};
|
|
282
|
+
};
|