Skip to content

Commit

Permalink
Add [Raycast] Badge (#9801)
Browse files Browse the repository at this point in the history
* feat: add Raycast extension installs Badge

* fix: capitalize the first letter

* fix: change to use the offical public API

* fix: change category to downloads
  • Loading branch information
Fatpandac authored Dec 20, 2023
1 parent c44e8c4 commit 6e581b7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
55 changes: 55 additions & 0 deletions services/raycast/installs.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Joi from 'joi'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService, pathParams } from '../index.js'
import { renderDownloadsBadge } from '../downloads.js'

const schema = Joi.object({
download_count: nonNegativeInteger,
}).required()

export default class RaycastInstalls extends BaseJsonService {
static category = 'downloads'

static route = {
base: 'raycast/dt',
pattern: ':user/:extension',
}

static openApi = {
'/raycast/dt/{user}/{extension}': {
get: {
summary: 'Raycast extension downloads count',
parameters: pathParams(
{ name: 'user', example: 'Fatpandac' },
{ name: 'extension', example: 'bilibili' },
),
},
},
}

static render({ downloads }) {
return renderDownloadsBadge({ downloads })
}

async fetch({ user, extension }) {
return this._requestJson({
schema,
url: `https://www.raycast.com/api/v1/extensions/${user}/${extension}`,
httpErrors: {
404: 'user/extension not found',
},
})
}

transform(json) {
const downloads = json.download_count

return { downloads }
}

async handle({ user, extension }) {
const json = await this.fetch({ user, extension })
const { downloads } = this.transform(json)
return this.constructor.render({ downloads })
}
}
30 changes: 30 additions & 0 deletions services/raycast/installs.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createServiceTester } from '../tester.js'
import { isMetric } from '../test-validators.js'

export const t = await createServiceTester()

t.create('installs (invalid user)')
.get('/fatpandac/bilibili.json')
.expectBadge({
label: 'downloads',
message: 'user/extension not found',
})

t.create('installs (not existing extension)')
.get('/Fatpandac/safdsaklfhe.json')
.expectBadge({
label: 'downloads',
message: 'user/extension not found',
})

t.create('installs (not existing user and extension)')
.get('/fatpandac/safdsaklfhe.json')
.expectBadge({
label: 'downloads',
message: 'user/extension not found',
})

t.create('installs (valid)').get('/Fatpandac/bilibili.json').expectBadge({
label: 'downloads',
message: isMetric,
})

0 comments on commit 6e581b7

Please sign in to comment.