Repository URL to install this package:
|
Version:
1.0.0 ▾
|
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchWithTimeout = exports.respondWithFetchPromise = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
function respondWithFetchPromise(id, response, promise) {
return __awaiter(this, void 0, void 0, function* () {
const context = yield promise;
if (!context) {
response.statusCode = 503;
response.end('503 Service Unavailable');
return;
}
for (const [key, value] of Object.entries(context.headers)) {
const lowerCaseKey = key.toLowerCase();
if (lowerCaseKey === 'content-length' || lowerCaseKey === 'content-encoding') {
continue;
}
response.setHeader(key, value);
}
response.statusCode = context.status;
context.json.id = id;
response.end(JSON.stringify(context.json));
});
}
exports.respondWithFetchPromise = respondWithFetchPromise;
function fetchWithTimeout(url, options) {
return __awaiter(this, void 0, void 0, function* () {
const response = (0, node_fetch_1.default)(url, Object.assign(Object.assign({}, options), { timeout: 10000 }))
.then((x) => __awaiter(this, void 0, void 0, function* () {
return ({
status: x.status,
headers: x.headers.raw(),
json: yield x.json()
});
}))
.catch(error => {
console.error(error);
return null;
});
return response;
});
}
exports.fetchWithTimeout = fetchWithTimeout;