@camunda8/sdk
Version:
[](https://www.npmjs.com/package/@camunda8/sdk)
59 lines • 2.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GotRetryConfig = exports.makeBeforeRetryHandlerFor401TokenRetry = exports.gotBeforeErrorHook = exports.gotErrorHandler = void 0;
const got_1 = require("got");
const GotErrors_1 = require("./GotErrors");
/**
*
* This function stores the call point from the application of got requests.
* This enables users to see where the error originated from.
*/
const gotErrorHandler = (options, next) => {
if (Object.isFrozen(options.context)) {
options.context = { ...options.context, hasRetried: false };
}
Error.captureStackTrace(options.context);
return next(options);
};
exports.gotErrorHandler = gotErrorHandler;
/**
* This function adds the call point to the error stack trace of got errors.
* This enables users to see where the error originated from.
*/
const gotBeforeErrorHook = (error) => {
const { request } = error;
let detail = '';
if (error instanceof got_1.HTTPError) {
error = new GotErrors_1.HTTPError(error.response);
try {
const details = JSON.parse(error.response?.body || '{detail:""}');
error.statusCode = details.status;
detail = details ?? '';
}
catch (e) {
error.statusCode = 0;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;
error.source = error.options.context.stack.split('\n');
error.message += ` (request to ${request?.options.url
.href}). ${JSON.stringify(detail)}`;
return error;
};
exports.gotBeforeErrorHook = gotBeforeErrorHook;
/**
*
* This function is used on a 401 response to retry the request with a new token, one single time.
* https://github.com/camunda/camunda-8-js-sdk/issues/125
*/
const makeBeforeRetryHandlerFor401TokenRetry = (getHeadersFn) => async (context) => {
context.headers.authorization = (await getHeadersFn()).authorization;
};
exports.makeBeforeRetryHandlerFor401TokenRetry = makeBeforeRetryHandlerFor401TokenRetry;
exports.GotRetryConfig = {
limit: 1,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
statusCodes: [401],
};
//# sourceMappingURL=GotHooks.js.map