|
| 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 | +} |
0 commit comments