fix(modrinth): using path param instead of query string since the que… (#14367)

* 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

---------
This commit is contained in:
SettingDust 2024-02-03 10:35:11 +08:00 committed by GitHub
parent d037d5a31f
commit f892480be7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 28 deletions

View File

@ -1,3 +1,3 @@
module.exports = {
'/project/:id/versions': ['SettingDust'],
'/project/:id/versions/:routeParams?': ['SettingDust'],
};

View File

@ -1,3 +1,3 @@
module.exports = (router) => {
router.get('/project/:id/versions', require('./versions'));
router.get('/project/:id/versions/:routeParams?', require('./versions'));
};

View File

@ -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`, {

View File

@ -341,15 +341,13 @@
### Project versions {#modrinth-project-versions}
<Route author="SettingDust" example="/project/modrinth/sodium/versions" path="/project/modrinth/:id/versions" paramsDesc={['Id or slug of the Modrinth project']} radar="1" />
<Route author="SettingDust" example="/modrinth/project/sodium/versions" path="/modrinth/project/:id/versions/:routeParams?" paramsDesc={['Id or slug of the Modrinth project','Extra route params. See the table below for options']} radar="1" />
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}