UNPKG

axis-core

Version:

A Node.js library written in TypeScript containing shared behavior for the other packages, e.g. code handling communication and authentication with Axis Communication cameras.

42 lines 2.01 kB
"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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchBuilder = void 0; const basic = require("./auth/basic"); const challenge_1 = require("./auth/challenge"); const digest = require("./auth/digest"); const fetchBuilder = (fetch) => { return (input, init) => __awaiter(void 0, void 0, void 0, function* () { let res = yield fetch(input, init); if (res.status !== 401) { return res; } const wwwAuthenticate = res.headers.get('www-authenticate'); if (!wwwAuthenticate) { return res; } const challenge = (0, challenge_1.parse)(wwwAuthenticate); switch (challenge.type) { case basic.BASIC: input.headers.set('authorization', basic.createHeader(input.username, input.password, challenge)); break; case digest.DIGEST: input.headers.set('authorization', digest.createHeader(input.method, input.url, input.username, input.password, challenge, challenge.qop === 'auth' ? digest.createCnonce() : undefined)); break; default: return res; } res = yield fetch(input, init); return res; }); }; exports.fetchBuilder = fetchBuilder; //# sourceMappingURL=fetch-builder.js.map