From f892480be7f3795fae6ec2c8b60fd598670986f3 Mon Sep 17 00:00:00 2001 From: SettingDust Date: Sat, 3 Feb 2024 10:35:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(modrinth):=20using=20path=20param=20instead?= =?UTF-8?q?=20of=20query=20string=20since=20the=20que=E2=80=A6=20(#14367)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(modrinth): using path param instead of query string since the query won't affect cached result * fix(modrinth): use `URLSearchParams` instead of querystring * docs(modrinth): typo * fix(modrinth): avoid empty array query when no value --------- --- lib/v2/modrinth/maintainer.js | 2 +- lib/v2/modrinth/router.js | 2 +- lib/v2/modrinth/versions.js | 33 +++++++++++++++------------------ website/docs/routes/game.mdx | 14 ++++++-------- 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/lib/v2/modrinth/maintainer.js b/lib/v2/modrinth/maintainer.js index 56b5f95fc..c0bf24944 100644 --- a/lib/v2/modrinth/maintainer.js +++ b/lib/v2/modrinth/maintainer.js @@ -1,3 +1,3 @@ module.exports = { - '/project/:id/versions': ['SettingDust'], + '/project/:id/versions/:routeParams?': ['SettingDust'], }; diff --git a/lib/v2/modrinth/router.js b/lib/v2/modrinth/router.js index 98b0a2848..e95fb268f 100644 --- a/lib/v2/modrinth/router.js +++ b/lib/v2/modrinth/router.js @@ -1,3 +1,3 @@ module.exports = (router) => { - router.get('/project/:id/versions', require('./versions')); + router.get('/project/:id/versions/:routeParams?', require('./versions')); }; diff --git a/lib/v2/modrinth/versions.js b/lib/v2/modrinth/versions.js index 32148d1cf..3dca9eb7b 100644 --- a/lib/v2/modrinth/versions.js +++ b/lib/v2/modrinth/versions.js @@ -2,6 +2,7 @@ const { parseDate } = require('@/utils/parse-date'); const { art } = require('@/utils/render'); const path = require('path'); const { value: config } = require('@/config'); + const got = require('@/utils/got').extend({ headers: { // https://docs.modrinth.com/#section/User-Agents @@ -19,32 +20,28 @@ module.exports = async ( const { /** @type string */ id, + /** @type {string | undefined} */ + routeParams, } = ctx.params; - let { - /** @type {string | string[] | undefined} */ - loaders, - /** @type {string | string[] | undefined} */ - game_versions, - } = ctx.query; - const { featured } = ctx.query; - if (typeof loaders === 'string') { - loaders = [loaders]; - } - if (typeof game_versions === 'string') { - game_versions = [game_versions]; - } + /** + * /@type {{ + * loaders: string | string[] | undefined + * game_versions: string | string[] | undefined + * featured: string | undefined + * }} + */ + const parsedQuery = new URLSearchParams(routeParams); + + parsedQuery.set('loaders', parsedQuery.has('loaders') ? JSON.stringify(parsedQuery.getAll('loaders')) : ''); + parsedQuery.set('game_versions', parsedQuery.has('game_versions') ? JSON.stringify(parsedQuery.getAll('game_versions')) : ''); try { /** @type {import('./api').Project} */ const project = await got(`https://api.modrinth.com/v2/project/${id}`).json(); /** @type {import('./api').Version[]} */ const versions = await got(`https://api.modrinth.com/v2/project/${id}/version`, { - searchParams: { - loaders: loaders ? JSON.stringify(loaders) : undefined, - game_versions: game_versions ? JSON.stringify(game_versions) : undefined, - featured, - }, + searchParams: parsedQuery, }).json(); /** @type {import('./api').Author[]} */ const authors = await got(`https://api.modrinth.com/v2/users`, { diff --git a/website/docs/routes/game.mdx b/website/docs/routes/game.mdx index a6bdc908a..c0e37c9ad 100644 --- a/website/docs/routes/game.mdx +++ b/website/docs/routes/game.mdx @@ -341,15 +341,13 @@ ### Project versions {#modrinth-project-versions} - + -Addition query parameters - -| Name | Example | -| -------------- | --------------------------------------------- | -| loaders | ?loaders=fabric\&loaders=quilt\&loaders=forge | -| game\_versions | ?game\_versions=1.20.1\&game\_versions=1.20.2 | -| featured | ?featured=true | +| Name | Example | +| -------------- | -------------------------------------------- | +| loaders | loaders=fabric\&loaders=quilt\&loaders=forge | +| game\_versions | game\_versions=1.20.1\&game\_versions=1.20.2 | +| featured | featured=true | ## Nintendo {#nintendo}