Skip to content

Commit 94abc3f

Browse files
committed
mirage: Allow non-canonical names in GET /api/v1/crates/:name route handler
1 parent d5dc56a commit 94abc3f

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

mirage/route-handlers/crates.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { Response } from 'miragejs';
33
import { getSession } from '../utils/session';
44
import { compareIsoDates, compareStrings, notFound, pageParams, releaseTracks } from './-utils';
55

6+
function toCanonicalName(name) {
7+
return name.toLowerCase().replace(/-/g, '_');
8+
}
9+
610
export function list(schema, request) {
711
const { start, end } = pageParams(request);
812

@@ -56,7 +60,8 @@ export function register(server) {
5660

5761
server.get('/api/v1/crates/:name', function (schema, request) {
5862
let { name } = request.params;
59-
let crate = schema.crates.findBy({ name });
63+
let canonicalName = toCanonicalName(name);
64+
let crate = schema.crates.all().models.find(it => toCanonicalName(it.name) === canonicalName);
6065
if (!crate) return notFound();
6166
let serialized = this.serialize(crate);
6267
let includes = request.queryParams?.include ?? '';

tests/mirage/crates/get-by-id-test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,67 @@ module('Mirage | GET /api/v1/crates/:id', function (hooks) {
7676
});
7777
});
7878

79+
test('works for non-canonical names', async function (assert) {
80+
let crate = this.server.create('crate', { name: 'foo-bar' });
81+
this.server.create('version', { crate, num: '1.0.0-beta.1' });
82+
83+
let response = await fetch('/api/v1/crates/foo_bar');
84+
assert.strictEqual(response.status, 200);
85+
assert.deepEqual(await response.json(), {
86+
categories: [],
87+
crate: {
88+
badges: [],
89+
categories: [],
90+
created_at: '2010-06-16T21:30:45Z',
91+
default_version: '1.0.0-beta.1',
92+
description: 'This is the description for the crate called "foo-bar"',
93+
documentation: null,
94+
downloads: 0,
95+
homepage: null,
96+
id: 'foo-bar',
97+
keywords: [],
98+
links: {
99+
owner_team: '/api/v1/crates/foo-bar/owner_team',
100+
owner_user: '/api/v1/crates/foo-bar/owner_user',
101+
reverse_dependencies: '/api/v1/crates/foo-bar/reverse_dependencies',
102+
version_downloads: '/api/v1/crates/foo-bar/downloads',
103+
versions: '/api/v1/crates/foo-bar/versions',
104+
},
105+
max_version: '1.0.0-beta.1',
106+
max_stable_version: null,
107+
name: 'foo-bar',
108+
newest_version: '1.0.0-beta.1',
109+
repository: null,
110+
updated_at: '2017-02-24T12:34:56Z',
111+
versions: ['1'],
112+
yanked: false,
113+
},
114+
keywords: [],
115+
versions: [
116+
{
117+
id: '1',
118+
crate: 'foo-bar',
119+
crate_size: 0,
120+
created_at: '2010-06-16T21:30:45Z',
121+
dl_path: '/api/v1/crates/foo-bar/1.0.0-beta.1/download',
122+
downloads: 0,
123+
license: 'MIT/Apache-2.0',
124+
links: {
125+
dependencies: '/api/v1/crates/foo-bar/1.0.0-beta.1/dependencies',
126+
version_downloads: '/api/v1/crates/foo-bar/1.0.0-beta.1/downloads',
127+
},
128+
num: '1.0.0-beta.1',
129+
published_by: null,
130+
readme_path: '/api/v1/crates/foo-bar/1.0.0-beta.1/readme',
131+
rust_version: null,
132+
updated_at: '2017-02-24T12:34:56Z',
133+
yanked: false,
134+
yank_message: null,
135+
},
136+
],
137+
});
138+
});
139+
79140
test('includes related versions', async function (assert) {
80141
let crate = this.server.create('crate', { name: 'rand' });
81142
this.server.create('version', { crate, num: '1.0.0' });

0 commit comments

Comments
 (0)