How to use the ky-universal.get function in ky-universal

To help you get started, we’ve selected a few ky-universal 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 tunnckoCore / opensource / web / pkg.tunnckocore.com / api / index.js View on Github external
);

  const rawName = isScoped ? pkgName.replace(/^tunnckocore\//, '') : pkgName;
  let pkgLoc = isScoped ? `@${pkgName}` : `packages/${pkgName}`;

  const loc = /preset|config/.test(pkgName) ? `configs/${rawName}` : pkgLoc;

  pkgLoc = `${loc}${restPath.length > 0 ? `/${restPath.join('/')}` : ''}`;

  const pathname =
    version === 'master' ? `@master/${pkgLoc}` : `@${encodedName}/${pkgLoc}`;

  // try request `src/index.ts`, `src/index.js`, `index.ts`, `index.js`
  if (!/\.(jsx?|tsx?)$/.test(pathname)) {
    if (field) {
      const resp = await ky
        .get(`${JSDELIVR}${pathname}/package.json`)
        .then((x) => x.text())
        .catch(() => {});

      // if `package.json` is found, try getting the source of file given `field`
      // which field by default is the "module" one.
      // Pass `field=main` query param to get the main field.
      if (resp) {
        let srcPath = null;
        try {
          srcPath = JSON.parse(resp)[field];
        } catch (err) {
          res.send('Found `package.json` but failed to parse.', 500);
          return;
        }
github tunnckoCore / opensource / web / ghub.now.sh / api / _utils.js View on Github external
export async function packageJson(packageName, endpoint) {
  const { name, version } = parsePkgName(packageName);
  const tag = version === '' ? 'latest' : version;
  const uri =
    typeof endpoint === 'function'
      ? endpoint(name, tag)
      : `https://cdn.jsdelivr.net/npm/${name}@${tag}/package.json`;

  let pkg = null;
  try {
    pkg = await ky
      .get(uri)
      .then((resp) => resp.text())
      .then(JSON.parse);
  } catch (err) {
    // ! jsDelivr can response with 403 Forbidden, if over 50MB
    if (err.response && err.response.status === 403) {
      try {
        // ! so, try through UNPKG.com
        pkg = await packageJson(
          packageName,
          (x, t) => `https://unpkg.com/${x}@${t}/package.json`,
        );
      } catch (error) {
        throw new ky.HTTPError(
          `Package "${name}" not found, even through UNPKG.com!`,
        );
github tunnckoCore / opensource / web / pkg.tunnckocore.com / api / _utils.js View on Github external
async function packageJson(packageName, endpoint) {
  const { name, version } = parsePkgName(packageName);
  const tag = version === '' ? 'latest' : version;
  const uri =
    typeof endpoint === 'function'
      ? endpoint(name, tag)
      : `https://cdn.jsdelivr.net/npm/${name}@${tag}/package.json`;

  let pkg = null;
  try {
    pkg = await ky
      .get(uri)
      .then((resp) => resp.text())
      .then(JSON.parse);
  } catch (err) {
    // ! jsDelivr can response with 403 Forbidden, if over 50MB
    if (err.response && err.response.status === 403) {
      try {
        // ! so, try through UNPKG.com
        pkg = await packageJson(
          packageName,
          (x, t) => `https://unpkg.com/${x}@${t}/package.json`,
        );
      } catch (error) {
        throw new ky.HTTPError(
          `Package "${name}" not found, even through UNPKG.com!`,
        );
github tunnckoCore / opensource / web / pkg.tunnckocore.com / api / index.js View on Github external
.then((x) => x.text())
        .catch(() => {});

      // if `package.json` is found, try getting the source of file given `field`
      // which field by default is the "module" one.
      // Pass `field=main` query param to get the main field.
      if (resp) {
        let srcPath = null;
        try {
          srcPath = JSON.parse(resp)[field];
        } catch (err) {
          res.send('Found `package.json` but failed to parse.', 500);
          return;
        }

        await ky
          .get(`${JSDELIVR}${pathname}/${srcPath}`)
          .then(() => {
            res.send(`${JSDELIVR}${pathname}/${srcPath}`, 301);
          })
          .catch(() => {
            res.send(
              `Not found source path given in the "${field}" of package.json`,
              404,
            );
          });
        return;
      }
    }

    const result = await pLocate(
      ['src/index.ts', 'src/index.js', 'index.ts', 'index.js'].map((fp) =>
github tunnckoCore / opensource / web / pkg.tunnckocore.com / api / index.js View on Github external
['src/index.ts', 'src/index.js', 'index.ts', 'index.js'].map((fp) =>
        ky
          .get(`${JSDELIVR}${pathname}/${fp}`)
          .then(async (x) => ({ file: fp, value: await x.text() }))
          .catch(() => {}),
      ),
github saltyshiomix / ark / lib / http / index.ts View on Github external
public async get(url: string): Promise {
    return ky.get(url, { prefixUrl }).json();
  }