@shushed/helpers 0.0.198-v2-20251107122648 → 0.0.198-v2-20251107140212
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.
|
@@ -14,6 +14,7 @@ exports.splitTimeToArr = splitTimeToArr;
|
|
|
14
14
|
exports.minutesAgo = minutesAgo;
|
|
15
15
|
exports.setRetryAfterVariable = setRetryAfterVariable;
|
|
16
16
|
exports.createFunction = createFunction;
|
|
17
|
+
exports.createOnResponse = createOnResponse;
|
|
17
18
|
exports.onResponse = onResponse;
|
|
18
19
|
const crypto_1 = __importDefault(require("crypto"));
|
|
19
20
|
const firestore_1 = require("@google-cloud/firestore");
|
|
@@ -236,20 +237,20 @@ function createFunction(expression) {
|
|
|
236
237
|
}
|
|
237
238
|
return (payload) => global.functionsCache[expression](exports.FUNCTION_EXPR_ERROR, payload);
|
|
238
239
|
}
|
|
239
|
-
function
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
240
|
+
function createOnResponse(opts) {
|
|
241
|
+
return (config, options) => onResponse(config, options, opts.requiredFlag);
|
|
242
|
+
}
|
|
243
|
+
function onResponse(config, options, requiredFlag) {
|
|
244
|
+
options.logging.log('onResponse', config);
|
|
245
|
+
const triggerOutput = options.root[options.triggerId || options.trigger?.id];
|
|
246
|
+
const flags = triggerOutput?.flags;
|
|
247
|
+
const numberOfInputMessages = triggerOutput?.body?.length ?? 0;
|
|
248
|
+
const outputBody = config.outputs?.body;
|
|
249
|
+
const outputStatus = config.outputs?.status;
|
|
245
250
|
let headersToSet = {};
|
|
246
|
-
if (outputBody
|
|
247
|
-
responsePayload = outputBody.body;
|
|
251
|
+
if (outputBody?.headersToSet) {
|
|
248
252
|
headersToSet = outputBody.headersToSet;
|
|
249
253
|
}
|
|
250
|
-
else {
|
|
251
|
-
responsePayload = outputBody;
|
|
252
|
-
}
|
|
253
254
|
if (options.root?.state?.['_$hsRetryAfter_'] && !Object.keys(headersToSet).some(x => x.toLocaleLowerCase() === 'retry-after')) {
|
|
254
255
|
let retryAfter = options.root.state['_$hsRetryAfter_'];
|
|
255
256
|
if (retryAfter) {
|
|
@@ -259,6 +260,22 @@ function onResponse(config, options) {
|
|
|
259
260
|
}
|
|
260
261
|
headersToSet['retry-after'] = retryAfter;
|
|
261
262
|
}
|
|
263
|
+
if (headersToSet) {
|
|
264
|
+
for (const [key, value] of Object.entries(headersToSet)) {
|
|
265
|
+
options.request.headers[key] = value;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (!triggerOutput) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
let responsePayload;
|
|
272
|
+
if (outputBody.headersToSet && outputBody.body) {
|
|
273
|
+
responsePayload = outputBody.body;
|
|
274
|
+
headersToSet = outputBody.headersToSet;
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
responsePayload = outputBody;
|
|
278
|
+
}
|
|
262
279
|
if (Array.isArray(responsePayload) && responsePayload.length === numberOfInputMessages) {
|
|
263
280
|
return {
|
|
264
281
|
status: (responsePayload.every(x => x instanceof Error) ? 500 : (outputStatus ? outputStatus : 200)),
|
|
@@ -268,16 +285,11 @@ function onResponse(config, options) {
|
|
|
268
285
|
}
|
|
269
286
|
return {
|
|
270
287
|
status: x instanceof Error ? (headersToSet['retry-after'] ? 429 : 500) : 200,
|
|
271
|
-
body: x instanceof Error ? x.message : (flags.includes('product-data') ? x : '<Redacted>. No product-data permissions>')
|
|
288
|
+
body: x instanceof Error ? x.message : (flags.includes(requiredFlag || 'product-data') ? x : '<Redacted>. No product-data permissions>')
|
|
272
289
|
};
|
|
273
290
|
})
|
|
274
291
|
};
|
|
275
292
|
}
|
|
276
|
-
if (headersToSet) {
|
|
277
|
-
for (const [key, value] of Object.entries(headersToSet)) {
|
|
278
|
-
options.request.headers[key] = value;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
293
|
return {
|
|
282
294
|
status: headersToSet['retry-after'] ? 429 : (outputStatus ? outputStatus : (responsePayload instanceof Error ? 500 : 200)),
|
|
283
295
|
value: responsePayload,
|
|
@@ -35,12 +35,28 @@ declare global {
|
|
|
35
35
|
}
|
|
36
36
|
export declare const FUNCTION_EXPR_ERROR: unique symbol;
|
|
37
37
|
export declare function createFunction(expression: string): (payload: any) => any;
|
|
38
|
+
export declare function createOnResponse(opts: {
|
|
39
|
+
requiredFlag: string;
|
|
40
|
+
}): (config: any & {
|
|
41
|
+
outputs: {
|
|
42
|
+
status: number;
|
|
43
|
+
body: any;
|
|
44
|
+
};
|
|
45
|
+
}, options: TriggerOnResponseOptions) => {
|
|
46
|
+
status: any;
|
|
47
|
+
value: any[];
|
|
48
|
+
cacheMaxAge?: undefined;
|
|
49
|
+
} | {
|
|
50
|
+
status: any;
|
|
51
|
+
value: any;
|
|
52
|
+
cacheMaxAge: number;
|
|
53
|
+
} | undefined;
|
|
38
54
|
export declare function onResponse(config: any & {
|
|
39
55
|
outputs: {
|
|
40
56
|
status: number;
|
|
41
57
|
body: any;
|
|
42
58
|
};
|
|
43
|
-
}, options: TriggerOnResponseOptions): {
|
|
59
|
+
}, options: TriggerOnResponseOptions, requiredFlag?: string): {
|
|
44
60
|
status: any;
|
|
45
61
|
value: any[];
|
|
46
62
|
cacheMaxAge?: undefined;
|
|
@@ -48,4 +64,4 @@ export declare function onResponse(config: any & {
|
|
|
48
64
|
status: any;
|
|
49
65
|
value: any;
|
|
50
66
|
cacheMaxAge: number;
|
|
51
|
-
};
|
|
67
|
+
} | undefined;
|