mpp-sdk
Version:
SDK to talk to the Memento Payments Platform
43 lines (42 loc) • 1.35 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseLinkHeader = void 0;
const parseLinkHeaderAttribute = (property) => {
const r = /([a-z]+)\="?([a-zA-Z]+)"?/;
const match = property.match(r);
if (!match || match.length < 3) {
return undefined;
}
return [match[1], match[2]];
};
const parseLinkHeader = (linkHeader) => {
const tokens = linkHeader.split(",");
const links = [];
for (const token of tokens) {
const items = token.split("; ");
if (items.length <= 0) {
continue;
}
// The uri is returned surrounded by angle brackets so we remove them
const uri = items[0].slice(1, -1);
const link = {
uri,
attrs: {},
};
// Loop through the links attribute list and parse it
for (let i = 1; i < items.length; i++) {
// Parse the attribute, if nothing is returned it is considered invalid
// and skipped
const attr = parseLinkHeaderAttribute(items[i]);
if (!attr) {
continue;
}
// Collect the attributes
const [key, value] = attr;
link.attrs[key] = value;
}
links.push(link);
}
return links;
};
exports.parseLinkHeader = parseLinkHeader;
;