Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async loadPassage(context, urn) {
const pagination = {};
const url = `/library/passage/${urn}/json/`;
const resp = await fetch(url);
let passage;
if (resp.status >= 200 && resp.status < 300) {
if (resp.headers.has('link')) {
const links = parseLinkHeader(resp.headers.get('link'));
if (links.prev) {
pagination.prev = {
url: links.prev.url,
urn: links.prev.urn,
ref: rsplit(links.prev.urn, ':', 2).slice(-1)[0],
};
}
if (links.next) {
pagination.next = {
url: links.next.url,
urn: links.next.urn,
ref: rsplit(links.next.urn, ':', 2).slice(-1)[0],
};
}
}
passage = await resp.json();
function handler(err, statusCode, data, headers) {
if (err) {
return whenDone(err);
}
results = results.concat(data);
if (!headers.link) {
return whenDone(null, pickedResults());
}
if (typeof parse(headers.link).last === 'undefined') {
whenDone(null, pickedResults());
}
else {
get(handler);
}
}
(result) => {
const links = parse(result.headers.get('Link'));
this.setState({
isLoaded: true,
items: result.items,
currentPage: page,
nextLabel: (links && links.next),
prevLabel: (links && links.prev)
});
},
(error) => {
function parseResponse(resp) {
function linkContinuation(url) {
return continueReq({ url }).then(res => parseResponse(res));
}
const result = { items: resp.body.items, link: {} };
if (resp.headers.link) {
const links = headerParser(resp.headers.link);
result.link = Object.keys(links).reduce(
(link, key) => ({
...link,
[key]: () => linkContinuation(links[key].url)
}),
{}
);
}
return result;
}
handleNotificationResponse(error, response) {
if (response && response.status === 200) {
if (response.headers.link) {
let total = parseLinkHeader(response.headers.link).last.page;
if (parseInt(total, 10) > 99) {
total = '+99';
}
this.setState({
notificationCounter: total
});
} else if (Object.keys(response.body).length === 1) {
this.setState({
notificationCounter: 1
});
} else {
this.setState({
notificationCounter: undefined
});
DevicesApi.get(`${inventoryApiUrl}/devices?per_page=${perPage}&page=${page}`).then(res => {
const links = parse(res.headers['link']);
const deviceAccu = reduceReceivedDevices(res.body, devices, getState());
dispatch({
type: DeviceConstants.RECEIVE_DEVICES,
devicesById: deviceAccu.devicesById
});
if (links.next) {
return getAllDevices(perPage, page + 1, deviceAccu.ids);
}
let tasks = [
dispatch({
type: DeviceConstants.RECEIVE_ALL_DEVICE_IDS,
deviceIds: deviceAccu.ids
})
];
const state = getState();
if (state.devices.byStatus.accepted.deviceIds.length === state.devices.byStatus.accepted.total) {
const nextPage = function (responseObject) {
const linkHeader = responseObject.headers.get('Link')
if (!linkHeader) {
return
}
const link = ParseLink(linkHeader)
return link.next ? link.next.url : null
}
function parsePaginationLinks (headers: Headers): PaginationLinks | undefined {
const linkHeaderText = headers.get('link')
if (linkHeaderText === null) {
return;
}
const linkHeader = parseLinkHeader(linkHeaderText);
if (linkHeader === null) {
return;
}
const links: PaginationLinks = {}
type PaginationLinkType = keyof PaginationLinks
const linkTypes: PaginationLinkType[] = ['first', 'prev', 'next', 'last']
for (const linkType of linkTypes) {
const link = linkHeader[linkType];
if (link !== undefined) {
const { _page: page, _limit: limit } = link;
if (page !== undefined && limit !== undefined) {
links[linkType] = {
page: Number(page),
limit: Number(limit)
}
}
.then(res => {
const links = parseLinkHeader(res.headers.get('Link'));
return res.json().then(posts => {
dispatch(updatePaginationLinks(links));
dispatch(updateAvailablePosts(posts));
});
})
.catch(err => dispatch(createError(err)));