-
-
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.
feat: add freecodecamp badge (#6958)
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7b9d1d3
commit b02471f
Showing
2 changed files
with
85 additions
and
0 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,70 @@ | ||
import Joi from 'joi' | ||
import { metric } from '../text-formatters.js' | ||
import { BaseJsonService, InvalidResponse, NotFound } from '../index.js' | ||
|
||
const schema = Joi.object({ | ||
entities: Joi.object({ | ||
user: Joi.object() | ||
.required() | ||
.pattern(/^\w+$/, { | ||
points: Joi.number().allow(null).required(), | ||
}), | ||
}).optional(), | ||
}).required() | ||
|
||
/** | ||
* This badge displays the total number of points a student has accumulated | ||
* from completing challenges on freeCodeCamp. | ||
*/ | ||
export default class FreeCodeCampPoints extends BaseJsonService { | ||
static category = 'other' | ||
static route = { | ||
base: 'freecodecamp/points', | ||
pattern: ':username', | ||
} | ||
|
||
static examples = [ | ||
{ | ||
title: 'freeCodeCamp points', | ||
namedParams: { username: 'sethi' }, | ||
staticPreview: this.render({ points: 934 }), | ||
}, | ||
] | ||
|
||
static defaultBadgeData = { label: 'points', color: 'info' } | ||
|
||
static render({ points }) { | ||
return { message: metric(points) } | ||
} | ||
|
||
async fetch({ username }) { | ||
return this._requestJson({ | ||
schema, | ||
url: `https://api.freecodecamp.org/api/users/get-public-profile`, | ||
options: { | ||
qs: { | ||
username, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
static transform(response, username) { | ||
const { entities } = response | ||
|
||
if (entities === undefined) | ||
throw new NotFound({ prettyMessage: 'profile not found' }) | ||
|
||
const { points } = entities.user[username] | ||
|
||
if (points === null) throw new InvalidResponse({ prettyMessage: 'private' }) | ||
|
||
return points | ||
} | ||
|
||
async handle({ username }) { | ||
const response = await this.fetch({ username }) | ||
const points = this.constructor.transform(response, username) | ||
return this.constructor.render({ points }) | ||
} | ||
} |
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,15 @@ | ||
import { createServiceTester } from '../tester.js' | ||
import { isMetric } from '../test-validators.js' | ||
export const t = await createServiceTester() | ||
|
||
t.create('Total Points Valid') | ||
.get('/sethi.json') | ||
.expectBadge({ label: 'points', message: isMetric }) | ||
|
||
t.create('Total Points Private') | ||
.get('/set.json') | ||
.expectBadge({ label: 'points', message: 'private' }) | ||
|
||
t.create('Total Points Invalid') | ||
.get('/invalid@username.json') | ||
.expectBadge({ label: 'points', message: 'profile not found' }) |