Skip to content

Commit 55805dd

Browse files
committed
feat(wakatime): add time-based badges for user and project
fix: resolve lint issues
1 parent 7599611 commit 55805dd

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

services/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from '../core/base-service/index.js'
2+
export { default as WakaTimeBadge } from './wakatime/wakatime-time.service.js'
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { BaseService, pathParams } from '../index.js'
2+
3+
export default class WakaTimeBadge extends BaseService {
4+
static category = 'activity'
5+
6+
static route = {
7+
base: 'wakatime',
8+
pattern: ':type(user|project)/:id',
9+
}
10+
11+
static openApi = {
12+
'/wakatime/{type}/{id}': {
13+
get: {
14+
summary: 'WakaTime coding time badge',
15+
parameters: [
16+
...pathParams({
17+
name: 'type',
18+
example: 'user',
19+
}),
20+
...pathParams({
21+
name: 'id',
22+
example: '73d84531-5bb3-4938-91c9-5ca9e6507df9',
23+
}),
24+
],
25+
},
26+
},
27+
}
28+
29+
static _cacheLength = 3600
30+
31+
static defaultBadgeData = {
32+
label: 'wakatime',
33+
color: 'blue',
34+
}
35+
36+
async fetch({ type, id }) {
37+
const url = `https://wakatime.com/badge/${type}/${id}.svg`
38+
39+
return this._request({
40+
url,
41+
parseJson: false,
42+
})
43+
}
44+
45+
extractTime(svg) {
46+
const match = svg.match(/>([\d,]+\s+hrs?.*?)</i)
47+
return match ? match[1] : null
48+
}
49+
50+
async handle({ type, id }) {
51+
const svg = await this.fetch({ type, id })
52+
const time = this.extractTime(svg)
53+
54+
if (!time) {
55+
return this.constructor.render({
56+
message: 'invalid',
57+
color: 'red',
58+
})
59+
}
60+
61+
return this.constructor.render({
62+
message: time,
63+
color: 'blue',
64+
})
65+
}
66+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createServiceTester } from '../tester.js'
2+
3+
export const t = await createServiceTester()
4+
5+
t.create('user badge')
6+
.get('/user/test-id.json')
7+
.intercept(nock =>
8+
nock('https://wakatime.com')
9+
.get('/badge/user/test-id.svg')
10+
.reply(200, `<svg><text>1,200 hrs 10 mins</text></svg>`),
11+
)
12+
.expectBadge({
13+
label: 'wakatime',
14+
message: '1,200 hrs 10 mins',
15+
})
16+
17+
t.create('invalid response')
18+
.get('/user/test-id.json')
19+
.intercept(nock =>
20+
nock('https://wakatime.com')
21+
.get('/badge/user/test-id.svg')
22+
.reply(200, `<svg></svg>`),
23+
)
24+
.expectBadge({
25+
label: 'wakatime',
26+
message: 'invalid',
27+
})

0 commit comments

Comments
 (0)