How to use parse-link-header - 9 common examples

To help you get started, we’ve selected a few parse-link-header examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scaife-viewer / scaife-viewer / static / src / js / store / reader.js View on Github external
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();
github paypal / Gander / service / index.js View on Github external
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);
    }

  }
github RubyNepal / rubynepal.github.io / js / talkSuggestions / index.js View on Github external
(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) => {
github feinoujc / gh-search-cli / src / GitHubSearch.js View on Github external
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;
  }
github devspace / devspace / src / scripts / nav.js View on Github external
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
				});
github mendersoftware / gui / src / js / actions / deviceActions.js View on Github external
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) {
github atom / github / lib / cached-request.js View on Github external
const nextPage = function (responseObject) {
  const linkHeader = responseObject.headers.get('Link')
  if (!linkHeader) {
    return
  }

  const link = ParseLink(linkHeader)
  return link.next ? link.next.url : null
}
github ealmansi / graphqlzero / packages / graphqlzero / src / models / util / json-placeholder.ts View on Github external
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)
        }
      }
github react-in-action / letters-social / src / actions / posts.js View on Github external
.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)));

parse-link-header

Parses a link header and returns paging information for each contained link.

MIT
Latest version published 3 years ago

Package Health Score

67 / 100
Full package analysis

Popular parse-link-header functions