@vendure/core
Version:
A modern, headless ecommerce framework
97 lines • 3.96 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomHttpHealthIndicator = exports.HttpHealthCheckStrategy = void 0;
const common_1 = require("@nestjs/common");
const terminus_compat_1 = require("./terminus-compat");
/**
* @description
* A {@link HealthCheckStrategy} used to check health by pinging a url.
*
* @example
* ```ts
* import { HttpHealthCheckStrategy, TypeORMHealthCheckStrategy } from '\@vendure/core';
*
* export const config = {
* // ...
* systemOptions: {
* healthChecks: [
* new TypeORMHealthCheckStrategy(),
* new HttpHealthCheckStrategy({ key: 'my-service', url: 'https://my-service.com' }),
* ]
* },
* };
* ```
*
* @docsCategory health-check
* @deprecated Use infrastructure-level health checks (e.g. Kubernetes probes, Docker healthchecks,
* load balancer checks) instead of application-level health checks. This class will be removed in v4.0.0.
*/
class HttpHealthCheckStrategy {
constructor(options) {
this.options = options;
}
init(injector) {
this.injector = injector;
}
getHealthIndicator() {
const { key, url, timeout } = this.options;
return async () => {
const indicator = await this.injector.resolve(CustomHttpHealthIndicator);
return indicator.pingCheck(key, url, timeout);
};
}
}
exports.HttpHealthCheckStrategy = HttpHealthCheckStrategy;
/**
* A simple HTTP health indicator that pings a URL via the native `fetch` API
* and converts any error into a `HealthCheckError`. Subclasses {@link HealthIndicator}
* to pick up the shared `getStatus()` helper.
*
* @deprecated This class is part of the deprecated health check feature and will be removed in v4.0.0.
*/
let CustomHttpHealthIndicator = class CustomHttpHealthIndicator extends terminus_compat_1.HealthIndicator {
/**
* Prepares and throw a HealthCheckError
*
* @throws {HealthCheckError}
*/
generateHttpError(key, error) {
const response = {
message: error.message,
};
if (error.response) {
response.statusCode = error.response.status;
response.statusText = error.response.statusText;
}
throw new terminus_compat_1.HealthCheckError(error.message, this.getStatus(key, false, response));
}
async pingCheck(key, url, timeout) {
let isHealthy = false;
try {
// Native `fetch` doesn't support a `timeout` option (node-fetch did);
// use AbortSignal.timeout when a positive timeout is supplied. Bare
// truthiness, not `!= null`, because `AbortSignal.timeout(0)` would
// fire immediately whereas the prior `node-fetch` semantics treated
// `timeout: 0` as "no timeout".
await fetch(url, timeout ? { signal: AbortSignal.timeout(timeout) } : undefined);
isHealthy = true;
}
catch (err) {
this.generateHttpError(key, err);
}
return this.getStatus(key, isHealthy);
}
};
exports.CustomHttpHealthIndicator = CustomHttpHealthIndicator;
exports.CustomHttpHealthIndicator = CustomHttpHealthIndicator = __decorate([
(0, common_1.Injectable)({
scope: common_1.Scope.TRANSIENT,
})
], CustomHttpHealthIndicator);
//# sourceMappingURL=http-health-check-strategy.js.map