1
0
mirror of https://github.com/badges/shields.git synced 2025-04-18 19:44:04 +03:00

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 <chris48s@users.noreply.github.com>
Co-authored-by: chris48s <git@chris-shaw.dev>
This commit is contained in:
Sepehr Safari 2024-01-11 23:05:03 +03:30 committed by GitHub
parent bfcbaea116
commit 4a55c3eaa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,67 @@
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { BaseJsonService, pathParams } from '../index.js'
const npubSchema = Joi.object({
followers_pubkey_count: Joi.number().required(),
}).required()
const mainSchema = Joi.object({
stats: Joi.object()
.pattern(Joi.string(), npubSchema)
.min(1)
.max(1)
.required(),
}).required()
export default class NostrBandFollowers extends BaseJsonService {
static category = 'social'
static route = {
base: 'nostr-band/followers',
pattern: ':npub',
}
static openApi = {
'/nostr-band/followers/{npub}': {
get: {
summary: 'Nostr.band Followers',
description:
'Returns the number of followers for a Nostr pubkey using the Nostr.band API.',
parameters: pathParams({
name: 'npub',
description: 'Nostr pubkey in (npub1...) format or hex.',
example:
'npub18c556t7n8xa3df2q82rwxejfglw5przds7sqvefylzjh8tjne28qld0we7',
}),
},
},
}
static defaultBadgeData = { label: 'followers' }
static render({ followers }) {
return {
message: metric(followers),
style: 'social',
}
}
async fetch({ npub }) {
const data = await this._requestJson({
url: `https://api.nostr.band/v0/stats/profile/${npub}`,
schema: mainSchema,
httpErrors: {
400: 'invalid pubkey',
},
})
const stats = data.stats
const firstKey = Object.keys(stats)[0]
return stats[firstKey].followers_pubkey_count
}
async handle({ npub }) {
const followers = await this.fetch({ npub })
return this.constructor.render({ followers })
}
}

View File

@ -0,0 +1,15 @@
import { isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()
t.create('fetch: valid npub')
.get('/npub18c556t7n8xa3df2q82rwxejfglw5przds7sqvefylzjh8tjne28qld0we7.json')
.expectBadge({
label: 'followers',
message: isMetric,
})
t.create('fetch: invalid npub').get('/invalidnpub.json').expectBadge({
label: 'followers',
message: 'invalid pubkey',
})