-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor [vaadindirectory] badges (#3339)
- Loading branch information
Showing
13 changed files
with
385 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict' | ||
|
||
const Joi = require('joi') | ||
const { BaseJsonService } = require('..') | ||
const { nonNegativeInteger } = require('../validators') | ||
|
||
const schema = Joi.object({ | ||
ratingCount: nonNegativeInteger, | ||
averageRating: Joi.number() | ||
.min(0) | ||
.required(), | ||
latestAvailableRelease: Joi.object({ | ||
publicationDate: Joi.date().required(), | ||
name: Joi.string().required(), | ||
}).required(), | ||
status: Joi.string().required(), | ||
}).required() | ||
|
||
class BaseVaadinDirectoryService extends BaseJsonService { | ||
async fetch({ packageName }) { | ||
return this._requestJson({ | ||
schema, | ||
url: `https://vaadin.com/vaadincom/directory-service/components/search/findByUrlIdentifier`, | ||
options: { | ||
qs: { | ||
projection: 'summary', | ||
urlIdentifier: packageName, | ||
}, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
module.exports = { BaseVaadinDirectoryService } |
46 changes: 46 additions & 0 deletions
46
services/vaadin-directory/vaadin-directory-rating-count.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const { metric } = require('../text-formatters') | ||
const { floorCount: floorCountColor } = require('../color-formatters') | ||
const { BaseVaadinDirectoryService } = require('./vaadin-directory-base') | ||
|
||
module.exports = class VaadinDirectoryRatingCount extends BaseVaadinDirectoryService { | ||
static get category() { | ||
return 'rating' | ||
} | ||
|
||
static get route() { | ||
return { | ||
base: 'vaadin-directory', | ||
pattern: ':which(rc|rating-count)/:packageName', | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Vaadin Directory', | ||
pattern: 'rating-count/:packageName', | ||
namedParams: { packageName: 'vaadinvaadin-grid' }, | ||
staticPreview: this.render({ ratingCount: 6 }), | ||
keywords: ['vaadin-directory', 'rating-count', 'rating count'], | ||
}, | ||
] | ||
} | ||
|
||
static get defaultBadgeData() { | ||
return { label: 'rating count' } | ||
} | ||
|
||
static render({ ratingCount }) { | ||
return { | ||
message: `${metric(ratingCount)} total`, | ||
color: floorCountColor(ratingCount, 5, 50, 500), | ||
} | ||
} | ||
|
||
async handle({ which, packageName }) { | ||
const { ratingCount } = await this.fetch({ packageName }) | ||
return this.constructor.render({ ratingCount }) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
services/vaadin-directory/vaadin-directory-rating-count.tester.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
const Joi = require('joi') | ||
const t = (module.exports = require('../tester').createServiceTester()) | ||
|
||
t.create('rating count of component') | ||
.get('/rating-count/vaadinvaadin-grid.json') | ||
.expectBadge({ | ||
label: 'rating count', | ||
message: Joi.string().regex(/^\d+?\stotal$/), | ||
}) | ||
|
||
t.create('rating count of component') | ||
.get('/rc/vaadinvaadin-grid.json') | ||
.expectBadge({ | ||
label: 'rating count', | ||
message: Joi.string().regex(/^\d+?\stotal$/), | ||
}) | ||
|
||
t.create('not found') | ||
.get('/rating-count/does-not-exist.json') | ||
.expectBadge({ | ||
label: 'rating count', | ||
message: 'not found', | ||
}) |
55 changes: 55 additions & 0 deletions
55
services/vaadin-directory/vaadin-directory-rating.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict' | ||
|
||
const { starRating } = require('../text-formatters') | ||
const { floorCount: floorCountColor } = require('../color-formatters') | ||
const { BaseVaadinDirectoryService } = require('./vaadin-directory-base') | ||
|
||
module.exports = class VaadinDirectoryRating extends BaseVaadinDirectoryService { | ||
static get category() { | ||
return 'rating' | ||
} | ||
|
||
static get route() { | ||
return { | ||
base: 'vaadin-directory', | ||
pattern: ':which(star|stars|rating)/:packageName', | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Vaadin Directory', | ||
pattern: ':which(stars|rating)/:packageName', | ||
namedParams: { which: 'rating', packageName: 'vaadinvaadin-grid' }, | ||
staticPreview: this.render({ which: 'rating', score: 4.75 }), | ||
keywords: ['vaadin-directory', 'rating'], | ||
}, | ||
] | ||
} | ||
|
||
static get defaultBadgeData() { | ||
return { label: 'rating' } | ||
} | ||
|
||
static render({ which, score }) { | ||
const rating = (Math.round(score * 10) / 10).toFixed(1) | ||
if (which === 'rating') { | ||
return { | ||
label: 'rating', | ||
message: `${rating}/5`, | ||
color: floorCountColor(rating, 2, 3, 4), | ||
} | ||
} | ||
return { | ||
label: 'stars', | ||
message: starRating(rating), | ||
color: floorCountColor(rating, 2, 3, 4), | ||
} | ||
} | ||
|
||
async handle({ which, packageName }) { | ||
const { averageRating } = await this.fetch({ packageName }) | ||
return this.constructor.render({ which, score: averageRating }) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
services/vaadin-directory/vaadin-directory-rating.tester.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
|
||
const Joi = require('joi') | ||
const { isStarRating } = require('../test-validators') | ||
const t = (module.exports = require('../tester').createServiceTester()) | ||
|
||
t.create('stars of component displayed in star icons') | ||
.get('/star/vaadinvaadin-grid.json') | ||
.expectBadge({ | ||
label: 'stars', | ||
message: isStarRating, | ||
}) | ||
|
||
t.create('stars of component displayed in star icons') | ||
.get('/stars/vaadinvaadin-grid.json') | ||
.expectBadge({ | ||
label: 'stars', | ||
message: isStarRating, | ||
}) | ||
|
||
t.create('rating of the component (eg: 4.2/5)') | ||
.get('/rating/vaadinvaadin-grid.json') | ||
.expectBadge({ | ||
label: 'rating', | ||
message: Joi.string().regex(/^\d\.\d\/5$/), | ||
}) | ||
|
||
t.create('not found') | ||
.get('/rating/does-not-exist.json') | ||
.expectBadge({ | ||
label: 'rating', | ||
message: 'not found', | ||
}) |
45 changes: 45 additions & 0 deletions
45
services/vaadin-directory/vaadin-directory-release-date.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict' | ||
|
||
const { formatDate } = require('../text-formatters') | ||
const { age: ageColor } = require('../color-formatters') | ||
const { BaseVaadinDirectoryService } = require('./vaadin-directory-base') | ||
|
||
module.exports = class VaadinDirectoryReleaseDate extends BaseVaadinDirectoryService { | ||
static get category() { | ||
return 'activity' | ||
} | ||
|
||
static get route() { | ||
return { | ||
base: 'vaadin-directory', | ||
pattern: ':which(rd|release-date)/:packageName', | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Vaadin Directory', | ||
pattern: 'release-date/:packageName', | ||
namedParams: { packageName: 'vaadinvaadin-grid' }, | ||
staticPreview: this.render({ date: '2018-12-12' }), | ||
keywords: ['vaadin-directory', 'date', 'latest release date'], | ||
}, | ||
] | ||
} | ||
|
||
static get defaultBadgeData() { | ||
return { label: 'latest release date' } | ||
} | ||
|
||
static render({ date }) { | ||
return { message: formatDate(date), color: ageColor(date) } | ||
} | ||
|
||
async handle({ which, packageName }) { | ||
const data = await this.fetch({ packageName }) | ||
return this.constructor.render({ | ||
date: data.latestAvailableRelease.publicationDate, | ||
}) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
services/vaadin-directory/vaadin-directory-release-date.tester.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict' | ||
|
||
const { isFormattedDate } = require('../test-validators') | ||
const t = (module.exports = require('../tester').createServiceTester()) | ||
|
||
t.create('latest release date of the component (format: yyyy-mm-dd)') | ||
.get('/rd/vaadinvaadin-grid.json') | ||
.expectBadge({ | ||
label: 'latest release date', | ||
message: isFormattedDate, | ||
}) | ||
|
||
t.create('latest release date of the component (format: yyyy-mm-dd)') | ||
.get('/release-date/vaadinvaadin-grid.json') | ||
.expectBadge({ | ||
label: 'latest release date', | ||
message: isFormattedDate, | ||
}) | ||
|
||
t.create('not found') | ||
.get('/release-date/does-not-exist.json') | ||
.expectBadge({ label: 'latest release date', message: 'not found' }) |
43 changes: 43 additions & 0 deletions
43
services/vaadin-directory/vaadin-directory-status.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict' | ||
|
||
const { BaseVaadinDirectoryService } = require('./vaadin-directory-base') | ||
|
||
module.exports = class VaadinDirectoryStatus extends BaseVaadinDirectoryService { | ||
static get category() { | ||
return 'other' | ||
} | ||
|
||
static get route() { | ||
return { | ||
base: 'vaadin-directory/status', | ||
pattern: ':packageName', | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Vaadin Directory', | ||
namedParams: { packageName: 'vaadinvaadin-grid' }, | ||
staticPreview: this.render({ status: 'published' }), | ||
keywords: ['vaadin-directory', 'status'], | ||
}, | ||
] | ||
} | ||
|
||
static get defaultBadgeData() { | ||
return { label: 'vaadin directory' } | ||
} | ||
|
||
static render({ status }) { | ||
if (status.toLowerCase() === 'published') { | ||
return { message: 'published', color: '#00b4f0' } | ||
} | ||
return { message: 'unpublished' } | ||
} | ||
|
||
async handle({ packageName }) { | ||
const { status } = await this.fetch({ packageName }) | ||
return this.constructor.render({ status }) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
services/vaadin-directory/vaadin-directory-status.tester.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict' | ||
|
||
const Joi = require('joi') | ||
const t = (module.exports = require('../tester').createServiceTester()) | ||
|
||
t.create('publish status of the component') | ||
.get('/vaadinvaadin-grid.json') | ||
.expectBadge({ | ||
label: 'vaadin directory', | ||
message: Joi.equal('published', 'unpublished'), | ||
}) | ||
|
||
t.create('not found') | ||
.get('/does-not-exist.json') | ||
.expectBadge({ | ||
label: 'vaadin directory', | ||
message: 'not found', | ||
}) |
39 changes: 39 additions & 0 deletions
39
services/vaadin-directory/vaadin-directory-version.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
const { renderVersionBadge } = require('../version') | ||
const { BaseVaadinDirectoryService } = require('./vaadin-directory-base') | ||
|
||
module.exports = class VaadinDirectoryVersion extends BaseVaadinDirectoryService { | ||
static get category() { | ||
return 'version' | ||
} | ||
|
||
static get route() { | ||
return { | ||
base: 'vaadin-directory', | ||
pattern: ':which(v|version)/:packageName', | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Vaadin Directory', | ||
pattern: 'v/:packageName', | ||
namedParams: { packageName: 'vaadinvaadin-grid' }, | ||
staticPreview: renderVersionBadge({ version: 'v5.3.0-alpha4' }), | ||
keywords: ['vaadin-directory', 'version', 'latest version'], | ||
}, | ||
] | ||
} | ||
|
||
static get defaultBadgeData() { | ||
return { label: 'vaadin directory' } | ||
} | ||
|
||
async handle({ which, packageName }) { | ||
const data = await this.fetch({ packageName }) | ||
const lv = data.latestAvailableRelease.name.toLowerCase() | ||
return renderVersionBadge({ version: lv }) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
services/vaadin-directory/vaadin-directory-version.tester.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
const { isSemver } = require('../test-validators') | ||
const t = (module.exports = require('../tester').createServiceTester()) | ||
|
||
t.create('latest version of the component (can have v prefixed or without)') | ||
.get('/v/vaadinvaadin-grid.json') | ||
.expectBadge({ | ||
label: 'vaadin directory', | ||
message: isSemver, | ||
}) | ||
|
||
t.create('latest version of the component (can have v prefixed or without)') | ||
.get('/version/vaadinvaadin-grid.json') | ||
.expectBadge({ | ||
label: 'vaadin directory', | ||
message: isSemver, | ||
}) | ||
|
||
t.create('not found') | ||
.get('/v/does-not-exist.json') | ||
.expectBadge({ | ||
label: 'vaadin directory', | ||
message: 'not found', | ||
}) |
Oops, something went wrong.