Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what you think about typescript support? #30

Open
gustawdaniel opened this issue Mar 29, 2023 · 1 comment
Open

what you think about typescript support? #30

gustawdaniel opened this issue Mar 29, 2023 · 1 comment

Comments

@gustawdaniel
Copy link

gustawdaniel commented Mar 29, 2023

i have problem with https://www.npmjs.com/package/@types/parse-link-header

TS7016: Could not find a declaration file for module 'parse-link-header'. '/home/daniel/pro/aic/front/node_modules/parse-link-header/index.js' implicitly has an 'any' type.   Try `npm i --save-dev @types/parse-link-header` if it exists or add a new declaration (.d.ts) file containing `declare module 'parse-link-header';`
@gustawdaniel
Copy link
Author

gustawdaniel commented Mar 29, 2023

there is typescript implementation without any dependencies that works in browsers

const PARSE_LINK_HEADER_MAXLEN = 2000;
const PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED = false;

interface Link {
  rel: string;
  url: string;
  [key: string]: string
}

function hasRel(x: Link | undefined): x is Link {
  return x !== undefined && x.rel !== undefined;
}

function intoRels(acc: Record<string, Link>, x: Link): Record<string, Link> {
  function splitRel(rel: string): void {
    acc[rel] = {...x, rel};
  }

  x.rel.split(/\s+/).forEach(splitRel);

  return acc;
}

export function parseSingleLink(linkHeader: string): Link | undefined {
  const linkRegex = /<([^>]+)>(?:\s*;\s*(.+))?/i;
  const matches = linkHeader.match(linkRegex);
  if (!matches) return undefined;

  const url = new URL(matches[1]);
  const params = matches[2]?.split(';')
    .map(param => param.trim().split('='))
    .reduce((acc: Record<string, string>, [key, value]) => {
      if (value && value.startsWith('"') && value.endsWith('"')) {
        value = value.slice(1, -1);
      }
      acc[key] = value;
      return acc;
    }, {}) ?? {};

  return {
    rel: "",
    ...Object.fromEntries(url.searchParams),
    ...params,
    url: url.href,
  };
}

function checkHeader(linkHeader: string | null): boolean {
  if (!linkHeader) return false;

  if (linkHeader.length > PARSE_LINK_HEADER_MAXLEN) {
    if (PARSE_LINK_HEADER_THROW_ON_MAXLEN_EXCEEDED) {
      throw new Error('Input string too long, it should be under ' + PARSE_LINK_HEADER_MAXLEN + ' characters.');
    } else {
      return false;
    }
  }
  return true;
}


export function parseLinkHeader(linkHeader: string | null): Record<string, Link> | null {
  if (!linkHeader || !checkHeader(linkHeader)) return null;

  return linkHeader.split(/,\s*/)
    .map(e => {
      console.log(e);
      return e
    })

    .map(parseSingleLink)
    .filter(Boolean)
    .filter(hasRel)
    .reduce(intoRels, {});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant