dcc-decoder
Version:
<img alttext="COVID Green Logo" src="https://raw.githubusercontent.com/lfph/artwork/master/projects/covidgreen/stacked/color/covidgreen-stacked-color.png" width="300" />
96 lines (95 loc) • 4.56 kB
JavaScript
;
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.populateCertValues = exports.getDCCData = exports.mapToJSON = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const hcert_1 = require("./types/hcert");
const _1 = require(".");
function mapToJSON(map) {
if (!(map instanceof Map))
return map;
const out = Object.create(null);
map.forEach((value, key) => {
if (value instanceof Map) {
out[key] = mapToJSON(value);
}
else {
out[key] = value;
}
});
return out;
}
exports.mapToJSON = mapToJSON;
function getDCCData(url) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield node_fetch_1.default(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (resp.status === 200) {
const body = yield resp.json();
return {
signingKeys: body.certs,
ruleSet: body.rules,
valueSets: {
countryCodes: body.valueSets.countryCodes.valueSetValues,
diseaseAgentTargeted: body.valueSets.diseaseAgentTargeted.valueSetValues,
testManf: body.valueSets.testManf.valueSetValues,
testResult: body.valueSets.testResult.valueSetValues,
testType: body.valueSets.testType.valueSetValues,
vaccineMahManf: body.valueSets.vaccineMahManf.valueSetValues,
vaccineMedicinalProduct: body.valueSets.vaccineMedicinalProduct.valueSetValues,
vaccineProphylaxis: body.valueSets.vaccineProphylaxis.valueSetValues,
},
valuesetsComputed: _1.buildValuesetsComputed(body.valueSets),
};
}
const data = resp.json ? yield resp.json() : 'Unable to request data';
throw new Error(data);
});
}
exports.getDCCData = getDCCData;
function getValue(valueset, key, defaultValue = key) {
var _a;
return ((_a = valueset[key]) === null || _a === void 0 ? void 0 : _a.display) || defaultValue;
}
const populateVaccineCert = (cert, valueSets) => {
return Object.assign(Object.assign({}, cert), { tg: getValue(valueSets.diseaseAgentTargeted, cert.tg), vp: getValue(valueSets.vaccineProphylaxis, cert.vp), mp: getValue(valueSets.vaccineMedicinalProduct, cert.mp), ma: getValue(valueSets.vaccineMahManf, cert.ma), co: getValue(valueSets.countryCodes, cert.co) });
};
const populateTestCert = (cert, valueSets) => {
return Object.assign(Object.assign({}, cert), { tg: getValue(valueSets.diseaseAgentTargeted, cert.tg), tt: getValue(valueSets.testType, cert.tt), ma: getValue(valueSets.vaccineMahManf, cert.ma), tr: getValue(valueSets.testResult, cert.tr), co: getValue(valueSets.countryCodes, cert.co) });
};
const populateRecoveryCert = (cert, valueSets) => {
return Object.assign(Object.assign({}, cert), { tg: getValue(valueSets.diseaseAgentTargeted, cert.tg), co: getValue(valueSets.countryCodes, cert.co) });
};
function populateCertValues(cert, type, valueSets) {
const populatedCert = {
nam: cert.nam,
dob: cert.dob,
ver: cert.ver,
};
if (type === hcert_1.CERT_TYPE.VACCINE) {
populatedCert.v = [populateVaccineCert(cert.v[0], valueSets)];
}
else if (type === hcert_1.CERT_TYPE.TEST) {
populatedCert.t = [populateTestCert(cert.t[0], valueSets)];
}
else if (type === hcert_1.CERT_TYPE.RECOVERY) {
populatedCert.r = [populateRecoveryCert(cert.r[0], valueSets)];
}
return populatedCert;
}
exports.populateCertValues = populateCertValues;