Skip to content

Commit

Permalink
mirage: Add meta field in GET /api/v1/crates/:id/versions (#10294)
Browse files Browse the repository at this point in the history
  • Loading branch information
eth3lbert authored Dec 30, 2024
1 parent 7a140d1 commit 7e215ef
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
16 changes: 16 additions & 0 deletions mirage/route-handlers/-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Response } from 'miragejs';
import semverParse from 'semver/functions/parse';
import semverSort from 'semver/functions/rsort';

export function notFound() {
return new Response(
Expand Down Expand Up @@ -31,3 +33,17 @@ export function compareIsoDates(a, b) {
let bDate = new Date(b);
return aDate < bDate ? -1 : aDate > bDate ? 1 : 0;
}

export function releaseTracks(versions) {
let versionNums = versions.models.filter(it => !it.yanked).map(it => it.num);
semverSort(versionNums, { loose: true });
let tracks = {};
for (let num of versionNums) {
let semver = semverParse(num, { loose: true });
if (!semver || semver.prerelease.length !== 0) continue;
let name = semver.major == 0 ? `0.${semver.minor}` : `${semver.major}`;
if (name in tracks) continue;
tracks[name] = { highest: num };
}
return tracks;
}
17 changes: 14 additions & 3 deletions mirage/route-handlers/crates.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Response } from 'miragejs';

import { getSession } from '../utils/session';
import { compareIsoDates, compareStrings, notFound, pageParams } from './-utils';
import { compareIsoDates, compareStrings, notFound, pageParams, releaseTracks } from './-utils';

export function list(schema, request) {
const { start, end } = pageParams(request);
Expand Down Expand Up @@ -140,12 +140,23 @@ export function register(server) {
return { ok: true };
});

server.get('/api/v1/crates/:name/versions', (schema, request) => {
server.get('/api/v1/crates/:name/versions', function (schema, request) {
let { name } = request.params;
let crate = schema.crates.findBy({ name });
if (!crate) return notFound();

return crate.versions.sort((a, b) => compareIsoDates(b.created_at, a.created_at));
let versions = crate.versions.sort((a, b) => compareIsoDates(b.created_at, a.created_at));
let total = versions.length;
let include = request.queryParams?.include ?? '';
let release_tracks = include.split(',').includes('release_tracks') && releaseTracks(crate.versions);
let resp = {
...this.serialize(versions),
meta: { total, next_page: null },
};
if (release_tracks && Object.keys(release_tracks).length !== 0) {
resp.meta.release_tracks = release_tracks;
}
return resp;
});

server.get('/api/v1/crates/:name/:version_num/authors', (schema, request) => {
Expand Down
32 changes: 32 additions & 0 deletions tests/mirage/crates/versions/list-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module('Mirage | GET /api/v1/crates/:id/versions', function (hooks) {
assert.strictEqual(response.status, 200);
assert.deepEqual(await response.json(), {
versions: [],
meta: { total: 0, next_page: null },
});
});

Expand Down Expand Up @@ -103,6 +104,37 @@ module('Mirage | GET /api/v1/crates/:id/versions', function (hooks) {
yank_message: null,
},
],
meta: { total: 3, next_page: null },
});
});

test('include `release_tracks` meta', async function (assert) {
let user = this.server.create('user');
let crate = this.server.create('crate', { name: 'rand' });
this.server.create('version', { crate, num: '0.0.1' });
this.server.create('version', { crate, num: '0.0.2', yanked: true });
this.server.create('version', { crate, num: '1.0.0' });
this.server.create('version', { crate, num: '1.1.0', publishedBy: user });
this.server.create('version', { crate, num: '1.2.0', rust_version: '1.69', yanked: true });

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

let response = await fetch('/api/v1/crates/rand/versions?include=release_tracks');
assert.strictEqual(response.status, 200);
assert.deepEqual(await response.json(), {
...expected,
meta: {
...expected.meta,
release_tracks: {
'0.0': {
highest: '0.0.1',
},
1: {
highest: '1.1.0',
},
},
},
});
});
});

0 comments on commit 7e215ef

Please sign in to comment.