Skip to content
Snippets Groups Projects
Select Git revision
  • b23d25a7fd71252805d9ef2c84ad224741b29b83
  • main default protected
  • renovate/main-renovatebot-github-action-43.x
  • next
  • renovate/main-containerbase-internal-tools-3.x
  • renovate/main-lock-file-maintenance
  • renovate/main-docs-renovate-renovate-41.x
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • renovate/main-redis-5.x
  • chore/punycode
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • 41.43.5
  • 41.43.4
  • 41.43.3
  • 41.43.2
  • 41.43.1
  • 41.43.0
  • 41.42.12
  • 41.42.11
  • 41.42.10
  • 41.42.9
  • 41.42.8
  • 41.42.7
  • 41.42.6
  • 41.42.5
  • 41.42.4
  • 41.42.3
  • 41.42.2
  • 41.42.1
  • 41.42.0
  • 41.41.0
41 results

error.ts

Blame
  • crates-downloads.service.js 3.15 KiB
    'use strict'
    
    const { downloadCount: downloadCountColor } = require('../color-formatters')
    const { metric } = require('../text-formatters')
    const { InvalidParameter, NotFound } = require('..')
    const { BaseCratesService, keywords } = require('./crates-base')
    
    module.exports = class CratesDownloads extends BaseCratesService {
      static get category() {
        return 'downloads'
      }
    
      static get route() {
        return {
          base: 'crates',
          pattern: ':variant(d|dv|dr)/:crate/:version?',
        }
      }
    
      static get examples() {
        return [
          {
            title: 'Crates.io',
            pattern: 'd/:crate',
            namedParams: {
              crate: 'rustc-serialize',
            },
            staticPreview: this.render({ variant: 'd', downloads: 5000000 }),
            keywords,
          },
          {
            title: 'Crates.io (latest)',
            pattern: 'dv/:crate',
            namedParams: {
              crate: 'rustc-serialize',
            },
            staticPreview: this.render({ variant: 'dv', downloads: 2000000 }),
            keywords,
          },
          {
            title: 'Crates.io (version)',
            pattern: 'dv/:crate/:version',
            namedParams: {
              crate: 'rustc-serialize',
              version: '0.3.24',
            },
            staticPreview: this.render({
              variant: 'dv',
              downloads: 2000000,
              version: '0.3.24',
            }),
            keywords,
          },
          {
            title: 'Crates.io (recent)',
            pattern: 'dr/:crate',
            namedParams: {
              crate: 'rustc-serialize',
            },
            staticPreview: this.render({ variant: 'dr', downloads: 2000000 }),
            keywords,
          },
        ]
      }
    
      static _getLabel(version, variant) {
        switch (variant) {
          case 'dv':
            return version ? `downloads@${version}` : 'downloads@latest'
          case 'dr':
            return 'recent downloads'
          default:
            return version ? `downloads@${version}` : 'downloads'
        }
      }
    
      static render({ variant, downloads, version }) {
        return {
          label: this._getLabel(version, variant),
          message: metric(downloads),
          color: downloadCountColor(downloads),
        }
      }
    
      transform({ variant, json }) {
        switch (variant) {
          case 'dv':
            return json.crate ? json.versions[0].downloads : json.version.downloads
          case 'dr':
            return json.crate.recent_downloads || 0
          default:
            return json.crate ? json.crate.downloads : json.version.downloads
        }
      }
    
      async handle({ variant, crate, version }) {
        if (variant === 'dr' && version) {
          /* crates.io doesn't currently expose
             recent download counts for individual
             versions */
          throw new InvalidParameter({
            prettyMessage: 'recent downloads not supported for specific versions',
          })
        }
    
        const json = await this.fetch({ crate, version })
    
        if (json.errors) {
          /* a call like
             https://crates.io/api/v1/crates/libc/0.1
             or
             https://crates.io/api/v1/crates/libc/0.1.76
             returns a 200 OK with an errors object */
          throw new NotFound({ prettyMessage: json.errors[0].detail })
        }
    
        const downloads = this.transform({ variant, json })
    
        return this.constructor.render({ variant, downloads, version })
      }
    }