Files
shields/services/librariesio/librariesio-base.js
T
Paul MelnikowandGitHub 02ec19fd22 BaseService terminology: Rename url to route (#2278)
The term “url” is overloaded in services, to refer to the Shields route and also the API URL. Calling the Shields URL a “route” is on the whole more descriptive, and makes it clearer and more obvious which one of these we’re talking about. It’s a small thing, though seems like an improvement.

We have a few functions called `buildUrl`. I’ve renamed them to `buildRoute` when they refer to routes, and left them as `buildUrl` when they refer to API URLs.

I included a minor style tweak and some formatting cleanup in `TUTORIAL.md`.
2018-11-09 15:11:03 -05:00

51 lines
1.5 KiB
JavaScript

'use strict'
const Joi = require('joi')
const BaseJsonService = require('../base-json')
const { InvalidResponse } = require('../errors')
const { nonNegativeInteger, anyInteger } = require('../validators')
// API doc: https://libraries.io/api#project
// We distinguishb between packages and repos based on the presence of the
// `platform` key.
const packageSchema = Joi.object({
platform: Joi.string().required(),
dependents_count: nonNegativeInteger,
dependent_repos_count: nonNegativeInteger,
rank: anyInteger,
}).required()
const repoSchema = Joi.object({
platform: Joi.any().forbidden(),
}).required()
const packageOrRepoSchema = Joi.alternatives(repoSchema, packageSchema)
class LibrariesIoBase extends BaseJsonService {
static buildRoute(base) {
return {
base,
format: '(\\w+)/(.+)',
capture: ['platform', 'packageName'],
}
}
async fetch({ platform, packageName }, { allowPackages, allowRepos } = {}) {
const json = await this._requestJson({
schema: packageOrRepoSchema,
url: `https://libraries.io/api/${platform}/${packageName}`,
errorMessages: { 404: 'package not found' },
})
const isPackage = Boolean(json.platform)
if (isPackage && !allowPackages) {
throw new InvalidResponse({ prettyMessage: 'not supported for packages' })
}
if (!isPackage && !allowRepos) {
throw new InvalidResponse({ prettyMessage: 'not supported for repos' })
}
return json
}
}
module.exports = LibrariesIoBase