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

deprecate [pkgreview] service #8499

Merged
merged 2 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 9 additions & 81 deletions services/pkgreview/package-rating.service.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,11 @@
import Joi from 'joi'
import { starRating, metric } from '../text-formatters.js'
import { colorScale } from '../color-formatters.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'
import { deprecatedService } from '../index.js'

const pkgReviewColor = colorScale([2, 3, 4])

const schema = Joi.object({
rating: Joi.number().min(0).max(1).precision(1).required().allow(null),
reviewsCount: nonNegativeInteger,
}).required()

// Repository for this service is: https:/iqubex-technologies/pkgreview.dev
// Internally the service leverages the npms.io API (https://api.npms.io/v2)
export default class PkgreviewRating extends BaseJsonService {
static category = 'rating'

static route = {
export default deprecatedService({
category: 'rating',
route: {
base: 'pkgreview',
pattern: ':format(rating|stars)/:pkgManager(npm)/:pkgSlug+',
}

static examples = [
{
title: 'pkgreview.dev Package Ratings',
pattern: 'rating/:pkgManager/:pkgSlug+',
namedParams: { pkgManager: 'npm', pkgSlug: 'react' },
staticPreview: this.render({
format: 'rating',
rating: 3.5,
reviewsCount: 237,
}),
},
{
title: 'pkgreview.dev Star Ratings',
pattern: 'stars/:pkgManager/:pkgSlug+',
namedParams: { pkgManager: 'npm', pkgSlug: 'react' },
staticPreview: this.render({
format: 'stars',
rating: 1.5,
reviewsCount: 200,
}),
},
]

static render({ rating, reviewsCount, format }) {
const message =
format === 'rating'
? `${+parseFloat(rating).toFixed(1)}/5 (${metric(reviewsCount)})`
: starRating(rating)

return {
message,
label: format,
color: pkgReviewColor(rating),
}
}

async fetch({ pkgManager, pkgSlug }) {
return this._requestJson({
schema,
url: `https://pkgreview.now.sh/api/v1/${pkgManager}/${encodeURIComponent(
pkgSlug
)}`,
errorMessages: {
404: 'package not found',
},
})
}

async handle({ format, pkgManager, pkgSlug }) {
const { reviewsCount, rating } = await this.fetch({
pkgManager,
pkgSlug,
})
return this.constructor.render({
reviewsCount,
format,
rating: rating * 5,
})
}
}
pattern: ':various*',
},
label: 'pkgreview',
dateAdded: new Date('2022-10-07'),
})
28 changes: 10 additions & 18 deletions services/pkgreview/package-rating.tester.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import { withRegex, isStarRating } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()
import { ServiceTester } from '../tester.js'

const isRatingWithReviews = withRegex(
/^(([0-4](.?([0-9]))?)|5)\/5?\s*\([0-9]*\)$/
)
export const t = new ServiceTester({
id: 'pkgreview',
title: 'PkgReview',
pathPrefix: '/pkgreview',
})

t.create('Stars Badge renders')
t.create('Stars Badge')
.get('/stars/npm/react.json')
.expectBadge({ label: 'stars', message: isStarRating })
.expectBadge({ label: 'pkgreview', message: 'no longer available' })

t.create('Rating Badge renders')
t.create('Rating Badge')
.get('/rating/npm/react.json')
.expectBadge({ label: 'rating', message: isRatingWithReviews })

t.create('nonexistent package')
.get('/rating/npm/ohlolweallknowthispackagewontexist.json')
.expectBadge({
label: 'rating',
message: 'package not found',
color: 'red',
})
.expectBadge({ label: 'pkgreview', message: 'no longer available' })