Skip to content

Commit

Permalink
feat: add a query param into plugin-registry
Browse files Browse the repository at this point in the history
This param 'showAll' allow to get all plugins and their versions (distTag)
  • Loading branch information
BatLeDev committed Mar 26, 2024
1 parent bc1a377 commit b50459f
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 206 deletions.
3 changes: 1 addition & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"dev": "NODE_ENV=development DEBUG=upgrade nodemon api"
},
"devDependencies": {
"@types/memoizee": "^0.4.11",
"@types/ws": "^8.5.10"
},
"dependencies": {
Expand All @@ -16,7 +15,7 @@
"http-terminator": "^3.2.0",
"jsonwebtoken": "9",
"jwks-rsa": "3",
"memoizee": "^0.4.15",
"memoize": "^10.0.0",
"prom-client": "15",
"ws": "^8.9.0"
}
Expand Down
43 changes: 31 additions & 12 deletions api/src/routers/plugins-registry.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,54 @@
import { asyncHandler } from '@data-fair/lib/express/index.js'
import { Router } from 'express'
import axios from '@data-fair/lib/node/axios.js'
import memoize from 'memoizee'
import memoize from 'memoize'

const router = Router()
export default router

const search = memoize(async (/** @type {any} */q, res) => {
/**
* Search for plugins in the npm registry
* @param {string} q - search query
* @param {boolean} showAll - if true, return all versions of each plugin
*/
const search = memoize(async (q, showAll) => {
// see https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#get-v1search
const response = await axios.get('https://registry.npmjs.org/-/v1/search', {
const res = await axios.get('https://registry.npmjs.org/-/v1/search', {
params: {
size: 250,
text: `keywords:data-fair-processings-plugin ${q || ''}`
}
})

for (const o of response.data.objects) {
const results = []
for (const o of res.data.objects) {
if (!o.package.keywords || !o.package.keywords.includes('data-fair-processings-plugin')) continue
const distTags = (await axios.get('https://registry.npmjs.org/-/package/' + o.package.name + '/dist-tags')).data
const plugin = { name: o.package.name, description: o.package.description }
for (const distTag in distTags) {
const result = { ...plugin, version: distTags[distTag], distTag }
res.write(JSON.stringify(result) + '\n') // send data after each plugin loaded

if (showAll) {
const distTags = (await axios.get('https://registry.npmjs.org/-/package/' + o.package.name + '/dist-tags')).data
for (const distTag in distTags) {
results.push({ ...plugin, version: distTags[distTag], distTag })
}
} else {
results.push({ ...plugin, distTag: 'latest' })
}
}
res.end()
return {
count: results.length,
results
}
}, {
cacheKey: arguments_ => arguments_.join(','),
maxAge: 5 * 60 * 1000 // cached for 5 minutes to be polite with npmjs
})

/**
* Search for plugins in the npm registry
* @route GET /plugins-registry
* @param {string} q.query - search query
* @param {boolean} showAll.query - if true, return all versions of each plugin
* @returns {object} 200 - An object with the count of results and an array of plugins
*/
router.get('/', asyncHandler(async (req, res) => {
res.setHeader('Content-Type', 'application/json')
await search(req.query.q, res)
res.send(await search(req.query.q, req.params.showAll === 'true' || false))
}))
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ Performs a search query against the npm registry for plugins tagged with `data-f
**Parameters**

- `q` (query, optional) : A search query to filter plugins based on their name, description, or other characteristics. The search is further refined to only include packages with the `data-fair-processings-plugin` keyword.
- `showAll` (query, optional) : A boolean to show plugins and all their versions (differents distTag).

**Caching**

Expand Down
Loading

0 comments on commit b50459f

Please sign in to comment.