Continue to implement #2698: - Add `core/base-service/index.js` (but hold off on moving the things it imports) - Add shortcuts in `services/index.js` for Base*Service, errors, and deprecatedService. This file will be streamlined later to avoid cluttering it with rarely used bits. - Apply consistent ordering of imports and use of `module.exports` in testers. - Remove some renaming of imports. - Remove obsolete tests here and there.
50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
'use strict'
|
|
|
|
const Joi = require('joi')
|
|
const { BaseJsonService, InvalidResponse } = require('..')
|
|
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
|