Skip to content

Commit 4a55c3e

Browse files
Add Support for [Nostr] Followers (#9870)
* add nostr followers with tests * fix broken test * fix http error handler * rename route from nostr to nostr-band * remove unnecessary test cases * edit test expectation * validate data schema ensuring exact one key * remove unavailable named logo * migrate examples to openApi * Update services/nostr-band/nostr-band-followers.tester.js * remove unused import --------- Co-authored-by: chris48s <[email protected]> Co-authored-by: chris48s <[email protected]>
1 parent bfcbaea commit 4a55c3e

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Joi from 'joi'
2+
import { metric } from '../text-formatters.js'
3+
import { BaseJsonService, pathParams } from '../index.js'
4+
5+
const npubSchema = Joi.object({
6+
followers_pubkey_count: Joi.number().required(),
7+
}).required()
8+
9+
const mainSchema = Joi.object({
10+
stats: Joi.object()
11+
.pattern(Joi.string(), npubSchema)
12+
.min(1)
13+
.max(1)
14+
.required(),
15+
}).required()
16+
17+
export default class NostrBandFollowers extends BaseJsonService {
18+
static category = 'social'
19+
20+
static route = {
21+
base: 'nostr-band/followers',
22+
pattern: ':npub',
23+
}
24+
25+
static openApi = {
26+
'/nostr-band/followers/{npub}': {
27+
get: {
28+
summary: 'Nostr.band Followers',
29+
description:
30+
'Returns the number of followers for a Nostr pubkey using the Nostr.band API.',
31+
parameters: pathParams({
32+
name: 'npub',
33+
description: 'Nostr pubkey in (npub1...) format or hex.',
34+
example:
35+
'npub18c556t7n8xa3df2q82rwxejfglw5przds7sqvefylzjh8tjne28qld0we7',
36+
}),
37+
},
38+
},
39+
}
40+
41+
static defaultBadgeData = { label: 'followers' }
42+
43+
static render({ followers }) {
44+
return {
45+
message: metric(followers),
46+
style: 'social',
47+
}
48+
}
49+
50+
async fetch({ npub }) {
51+
const data = await this._requestJson({
52+
url: `https://api.nostr.band/v0/stats/profile/${npub}`,
53+
schema: mainSchema,
54+
httpErrors: {
55+
400: 'invalid pubkey',
56+
},
57+
})
58+
const stats = data.stats
59+
const firstKey = Object.keys(stats)[0]
60+
return stats[firstKey].followers_pubkey_count
61+
}
62+
63+
async handle({ npub }) {
64+
const followers = await this.fetch({ npub })
65+
return this.constructor.render({ followers })
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { isMetric } from '../test-validators.js'
2+
import { createServiceTester } from '../tester.js'
3+
export const t = await createServiceTester()
4+
5+
t.create('fetch: valid npub')
6+
.get('/npub18c556t7n8xa3df2q82rwxejfglw5przds7sqvefylzjh8tjne28qld0we7.json')
7+
.expectBadge({
8+
label: 'followers',
9+
message: isMetric,
10+
})
11+
12+
t.create('fetch: invalid npub').get('/invalidnpub.json').expectBadge({
13+
label: 'followers',
14+
message: 'invalid pubkey',
15+
})

0 commit comments

Comments
 (0)