Skip to content

Commit

Permalink
mirage: Add include=default_version support for `GET /api/v1/crates…
Browse files Browse the repository at this point in the history
…/:id` (#10301)
  • Loading branch information
eth3lbert authored Jan 2, 2025
1 parent 50b1bc2 commit 9127efd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
8 changes: 8 additions & 0 deletions mirage/route-handlers/crates.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export function register(server) {
let crate = schema.crates.findBy({ name });
if (!crate) return notFound();
let serialized = this.serialize(crate);
let includes = request.queryParams?.include ?? '';
let includeDefaultVersion = includes.includes('default_version') && !includes.includes('versions');
return {
categories: null,
keywords: null,
Expand All @@ -67,6 +69,12 @@ export function register(server) {
...(serialized.crate.categories && this.serialize(crate.categories)),
...(serialized.crate.keywords && this.serialize(crate.keywords)),
...(serialized.crate.versions && this.serialize(crate.versions.sort((a, b) => Number(b.id) - Number(a.id)))),
// `default_version` share the same key `versions`
...(includeDefaultVersion && {
versions: [serialized.crate.default_version].map(
num => this.serialize(schema.versions.findBy({ num })).version,
),
}),
};
});

Expand Down
8 changes: 7 additions & 1 deletion mirage/serializers/crate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import semverSort from 'semver/functions/rsort';
import { compareIsoDates } from '../route-handlers/-utils';
import BaseSerializer from './application';

const VALID_INCLUDE_MODEL = new Set(['versions', 'keywords', 'categories' /*, 'badges', 'downloads' */]);
const VALID_INCLUDE_MODEL = new Set([
'versions',
'default_version',
'keywords',
'categories',
/*, 'badges', 'downloads' */
]);

export default BaseSerializer.extend({
include(request) {
Expand Down
43 changes: 43 additions & 0 deletions tests/mirage/crates/get-by-id-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,47 @@ module('Mirage | GET /api/v1/crates/:id', function (hooks) {
versions: null,
});
});
test('includes default_version', async function (assert) {
let crate = this.server.create('crate', { name: 'rand' });
this.server.create('version', { crate, num: '1.0.0' });
this.server.create('version', { crate, num: '1.1.0' });
this.server.create('version', { crate, num: '1.2.0' });

let req = await fetch('/api/v1/crates/rand');
let expected = await req.json();

let response = await fetch('/api/v1/crates/rand?include=default_version');
assert.strictEqual(response.status, 200);

let responsePayload = await response.json();
let default_version = expected.versions.find(it => it.num === responsePayload.crate.default_version);
assert.deepEqual(responsePayload, {
...expected,
crate: {
...expected.crate,
categories: null,
keywords: null,
max_version: '0.0.0',
newest_version: '0.0.0',
max_stable_version: null,
versions: null,
},
categories: null,
keywords: null,
versions: [default_version],
});

let resp_both = await fetch('/api/v1/crates/rand?include=versions,default_version');
assert.strictEqual(response.status, 200);
assert.deepEqual(await resp_both.json(), {
...expected,
crate: {
...expected.crate,
categories: null,
keywords: null,
},
categories: null,
keywords: null,
});
});
});

0 comments on commit 9127efd

Please sign in to comment.