Skip to content

Commit 368e52d

Browse files
committed
msw: Migrate models to new @msw/data API
1 parent 2a57215 commit 368e52d

32 files changed

+661
-601
lines changed

packages/crates-io-msw/index.js

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,6 @@ import teamHandlers from './handlers/teams.js';
1414
import trustpubHandlers from './handlers/trustpub.js';
1515
import userHandlers from './handlers/users.js';
1616
import versionHandlers from './handlers/versions.js';
17-
import apiToken from './models/api-token.js';
18-
import category from './models/category.js';
19-
import crateOwnerInvitation from './models/crate-owner-invitation.js';
20-
import crateOwnership from './models/crate-ownership.js';
21-
import crate from './models/crate.js';
22-
import dependency from './models/dependency.js';
23-
import keyword from './models/keyword.js';
24-
import mswSession from './models/msw-session.js';
25-
import team from './models/team.js';
26-
import trustpubGithubConfig from './models/trustpub/github-config.js';
27-
import trustpubGitlabConfig from './models/trustpub/gitlab-config.js';
28-
import user from './models/user.js';
29-
import versionDownload from './models/version-download.js';
30-
import version from './models/version.js';
31-
import { factory } from './utils/factory.js';
3217

3318
export const handlers = [
3419
...apiTokenHandlers,
@@ -49,19 +34,4 @@ export const handlers = [
4934
...versionHandlers,
5035
];
5136

52-
export const db = factory({
53-
apiToken,
54-
category,
55-
crateOwnerInvitation,
56-
crateOwnership,
57-
crate,
58-
dependency,
59-
keyword,
60-
mswSession,
61-
team,
62-
trustpubGithubConfig,
63-
trustpubGitlabConfig,
64-
user,
65-
versionDownload,
66-
version,
67-
});
37+
export { db } from './models/index.js';
Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
1-
import { nullable, oneOf, primaryKey } from '@mswjs/data';
1+
import { Collection } from '@msw/data';
2+
import { z } from 'zod';
23

34
import { applyDefault } from '../utils/defaults.js';
5+
import { preCreateExtension } from '../utils/pre-create-extension.js';
46
import { seededRandom } from '../utils/random.js';
57

6-
export default {
7-
id: primaryKey(Number),
8-
9-
crateScopes: nullable(Array),
10-
createdAt: String,
11-
endpointScopes: nullable(Array),
12-
expiredAt: nullable(String),
13-
lastUsedAt: nullable(String),
14-
name: String,
15-
token: String,
16-
revoked: Boolean,
17-
18-
user: oneOf('user'),
19-
20-
preCreate(attrs, counter) {
21-
applyDefault(attrs, 'id', () => counter);
22-
applyDefault(attrs, 'crateScopes', () => null);
23-
applyDefault(attrs, 'createdAt', () => '2017-11-19T17:59:22Z');
24-
applyDefault(attrs, 'endpointScopes', () => null);
25-
applyDefault(attrs, 'expiredAt', () => null);
26-
applyDefault(attrs, 'lastUsedAt', () => null);
27-
applyDefault(attrs, 'name', () => `API Token ${attrs.id}`);
28-
applyDefault(attrs, 'token', () => generateToken(counter));
29-
applyDefault(attrs, 'revoked', () => false);
30-
31-
if (!attrs.user) {
32-
throw new Error('Missing `user` relationship on `api-token`');
33-
}
34-
},
35-
};
8+
const schema = z.object({
9+
id: z.number(),
10+
11+
crateScopes: z.array(z.any()).nullable(),
12+
createdAt: z.string(),
13+
endpointScopes: z.array(z.any()).nullable(),
14+
expiredAt: z.string().nullable(),
15+
lastUsedAt: z.string().nullable(),
16+
name: z.string(),
17+
token: z.string(),
18+
revoked: z.boolean(),
19+
20+
user: z.any(),
21+
});
22+
23+
function preCreate(attrs, counter) {
24+
applyDefault(attrs, 'id', () => counter);
25+
applyDefault(attrs, 'crateScopes', () => null);
26+
applyDefault(attrs, 'createdAt', () => '2017-11-19T17:59:22Z');
27+
applyDefault(attrs, 'endpointScopes', () => null);
28+
applyDefault(attrs, 'expiredAt', () => null);
29+
applyDefault(attrs, 'lastUsedAt', () => null);
30+
applyDefault(attrs, 'name', () => `API Token ${attrs.id}`);
31+
applyDefault(attrs, 'token', () => generateToken(counter));
32+
applyDefault(attrs, 'revoked', () => false);
33+
34+
if (!attrs.user) {
35+
throw new Error('Missing `user` relationship on `api-token`');
36+
}
37+
}
3638

3739
function generateToken(seed) {
3840
return seededRandom(seed).toString().slice(2);
3941
}
42+
43+
const collection = new Collection({
44+
schema,
45+
extensions: [preCreateExtension(preCreate)],
46+
});
47+
48+
export default collection;

packages/crates-io-msw/models/api-token.test.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { test } from 'vitest';
22

33
import { db } from '../index.js';
44

5-
test('throws if `user` is not set', ({ expect }) => {
6-
expect(() => db.apiToken.create()).toThrowErrorMatchingInlineSnapshot(
5+
test('throws if `user` is not set', async ({ expect }) => {
6+
await expect(() => db.apiToken.create()).rejects.toThrowErrorMatchingInlineSnapshot(
77
`[Error: Missing \`user\` relationship on \`api-token\`]`,
88
);
99
});
1010

11-
test('happy path', ({ expect }) => {
12-
let user = db.user.create();
13-
let session = db.apiToken.create({ user });
11+
test('happy path', async ({ expect }) => {
12+
let user = await db.user.create();
13+
let session = await db.apiToken.create({ user });
1414
expect(session).toMatchInlineSnapshot(`
1515
{
1616
"crateScopes": null,
@@ -34,11 +34,7 @@ test('happy path', ({ expect }) => {
3434
"name": "User 1",
3535
"publishNotifications": true,
3636
"url": "https://github.com/user-1",
37-
Symbol(type): "user",
38-
Symbol(primaryKey): "id",
3937
},
40-
Symbol(type): "apiToken",
41-
Symbol(primaryKey): "id",
4238
}
4339
`);
4440
});
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
import { nullable, primaryKey } from '@mswjs/data';
1+
import { Collection } from '@msw/data';
2+
import { z } from 'zod';
23

34
import { applyDefault } from '../utils/defaults.js';
5+
import { preCreateExtension } from '../utils/pre-create-extension.js';
46
import { dasherize } from '../utils/strings.js';
57

6-
export default {
7-
id: primaryKey(String),
8+
const schema = z.object({
9+
id: z.string(),
810

9-
category: String,
10-
slug: String,
11-
description: String,
12-
created_at: String,
13-
crates_cnt: nullable(Number),
11+
category: z.string(),
12+
slug: z.string(),
13+
description: z.string(),
14+
created_at: z.string(),
15+
crates_cnt: z.number().nullable(),
16+
});
1417

15-
preCreate(attrs, counter) {
16-
applyDefault(attrs, 'category', () => `Category ${counter}`);
17-
applyDefault(attrs, 'slug', () => dasherize(attrs.category));
18-
applyDefault(attrs, 'id', () => attrs.slug);
19-
applyDefault(attrs, 'description', () => `This is the description for the category called "${attrs.category}"`);
20-
applyDefault(attrs, 'created_at', () => '2010-06-16T21:30:45Z');
21-
applyDefault(attrs, 'crates_cnt', () => null);
22-
},
23-
};
18+
function preCreate(attrs, counter) {
19+
applyDefault(attrs, 'category', () => `Category ${counter}`);
20+
applyDefault(attrs, 'slug', () => dasherize(attrs.category));
21+
applyDefault(attrs, 'id', () => attrs.slug);
22+
applyDefault(attrs, 'description', () => `This is the description for the category called "${attrs.category}"`);
23+
applyDefault(attrs, 'created_at', () => '2010-06-16T21:30:45Z');
24+
applyDefault(attrs, 'crates_cnt', () => null);
25+
}
26+
27+
const collection = new Collection({
28+
schema,
29+
extensions: [preCreateExtension(preCreate)],
30+
});
31+
32+
export default collection;

packages/crates-io-msw/models/category.test.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { test } from 'vitest';
22

33
import { db } from '../index.js';
44

5-
test('default are applied', ({ expect }) => {
6-
let category = db.category.create();
5+
test('default are applied', async ({ expect }) => {
6+
let category = await db.category.create();
77
expect(category).toMatchInlineSnapshot(`
88
{
99
"category": "Category 1",
@@ -12,14 +12,12 @@ test('default are applied', ({ expect }) => {
1212
"description": "This is the description for the category called "Category 1"",
1313
"id": "category-1",
1414
"slug": "category-1",
15-
Symbol(type): "category",
16-
Symbol(primaryKey): "id",
1715
}
1816
`);
1917
});
2018

21-
test('name can be set', ({ expect }) => {
22-
let category = db.category.create({ category: 'Network programming' });
19+
test('name can be set', async ({ expect }) => {
20+
let category = await db.category.create({ category: 'Network programming' });
2321
expect(category).toMatchInlineSnapshot(`
2422
{
2523
"category": "Network programming",
@@ -28,8 +26,6 @@ test('name can be set', ({ expect }) => {
2826
"description": "This is the description for the category called "Network programming"",
2927
"id": "network-programming",
3028
"slug": "network-programming",
31-
Symbol(type): "category",
32-
Symbol(primaryKey): "id",
3329
}
3430
`);
3531
});
Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
import { oneOf, primaryKey } from '@mswjs/data';
1+
import { Collection } from '@msw/data';
2+
import { z } from 'zod';
23

34
import { applyDefault } from '../utils/defaults.js';
5+
import { preCreateExtension } from '../utils/pre-create-extension.js';
46

5-
export default {
6-
id: primaryKey(Number),
7-
8-
createdAt: String,
9-
expiresAt: String,
10-
token: String,
11-
12-
crate: oneOf('crate'),
13-
invitee: oneOf('user'),
14-
inviter: oneOf('user'),
15-
16-
preCreate(attrs, counter) {
17-
applyDefault(attrs, 'id', () => counter);
18-
applyDefault(attrs, 'createdAt', () => '2016-12-24T12:34:56Z');
19-
applyDefault(attrs, 'expiresAt', () => '2017-01-24T12:34:56Z');
20-
applyDefault(attrs, 'token', () => `secret-token-${attrs.id}`);
21-
22-
if (!attrs.crate) {
23-
throw new Error(`Missing \`crate\` relationship on \`crate-owner-invitation\``);
24-
}
25-
if (!attrs.invitee) {
26-
throw new Error(`Missing \`invitee\` relationship on \`crate-owner-invitation\``);
27-
}
28-
if (!attrs.inviter) {
29-
throw new Error(`Missing \`inviter\` relationship on \`crate-owner-invitation\``);
30-
}
31-
},
32-
};
7+
const schema = z.object({
8+
id: z.number(),
9+
10+
createdAt: z.string(),
11+
expiresAt: z.string(),
12+
token: z.string(),
13+
14+
crate: z.any(),
15+
invitee: z.any(),
16+
inviter: z.any(),
17+
});
18+
19+
function preCreate(attrs, counter) {
20+
applyDefault(attrs, 'id', () => counter);
21+
applyDefault(attrs, 'createdAt', () => '2016-12-24T12:34:56Z');
22+
applyDefault(attrs, 'expiresAt', () => '2017-01-24T12:34:56Z');
23+
applyDefault(attrs, 'token', () => `secret-token-${attrs.id}`);
24+
25+
if (!attrs.crate) {
26+
throw new Error(`Missing \`crate\` relationship on \`crate-owner-invitation\``);
27+
}
28+
if (!attrs.invitee) {
29+
throw new Error(`Missing \`invitee\` relationship on \`crate-owner-invitation\``);
30+
}
31+
if (!attrs.inviter) {
32+
throw new Error(`Missing \`inviter\` relationship on \`crate-owner-invitation\``);
33+
}
34+
}
35+
36+
const collection = new Collection({
37+
schema,
38+
extensions: [preCreateExtension(preCreate)],
39+
});
40+
41+
export default collection;

packages/crates-io-msw/models/crate-owner-invitation.test.js

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@ import { test } from 'vitest';
22

33
import { db } from '../index.js';
44

5-
test('throws if `crate` is not set', ({ expect }) => {
6-
let inviter = db.user.create();
7-
let invitee = db.user.create();
8-
expect(() => db.crateOwnerInvitation.create({ inviter, invitee })).toThrowErrorMatchingInlineSnapshot(
5+
test('throws if `crate` is not set', async ({ expect }) => {
6+
let inviter = await db.user.create();
7+
let invitee = await db.user.create();
8+
await expect(() => db.crateOwnerInvitation.create({ inviter, invitee })).rejects.toThrowErrorMatchingInlineSnapshot(
99
`[Error: Missing \`crate\` relationship on \`crate-owner-invitation\`]`,
1010
);
1111
});
1212

13-
test('throws if `inviter` is not set', ({ expect }) => {
14-
let crate = db.crate.create();
15-
let invitee = db.user.create();
16-
expect(() => db.crateOwnerInvitation.create({ crate, invitee })).toThrowErrorMatchingInlineSnapshot(
13+
test('throws if `inviter` is not set', async ({ expect }) => {
14+
let crate = await db.crate.create();
15+
let invitee = await db.user.create();
16+
await expect(() => db.crateOwnerInvitation.create({ crate, invitee })).rejects.toThrowErrorMatchingInlineSnapshot(
1717
`[Error: Missing \`inviter\` relationship on \`crate-owner-invitation\`]`,
1818
);
1919
});
2020

21-
test('throws if `invitee` is not set', ({ expect }) => {
22-
let crate = db.crate.create();
23-
let inviter = db.user.create();
24-
expect(() => db.crateOwnerInvitation.create({ crate, inviter })).toThrowErrorMatchingInlineSnapshot(
21+
test('throws if `invitee` is not set', async ({ expect }) => {
22+
let crate = await db.crate.create();
23+
let inviter = await db.user.create();
24+
await expect(() => db.crateOwnerInvitation.create({ crate, inviter })).rejects.toThrowErrorMatchingInlineSnapshot(
2525
`[Error: Missing \`invitee\` relationship on \`crate-owner-invitation\`]`,
2626
);
2727
});
2828

29-
test('happy path', ({ expect }) => {
30-
let crate = db.crate.create();
31-
let inviter = db.user.create();
32-
let invitee = db.user.create();
33-
let invite = db.crateOwnerInvitation.create({ crate, inviter, invitee });
29+
test('happy path', async ({ expect }) => {
30+
let crate = await db.crate.create();
31+
let inviter = await db.user.create();
32+
let invitee = await db.user.create();
33+
let invite = await db.crateOwnerInvitation.create({ crate, inviter, invitee });
3434
expect(invite).toMatchInlineSnapshot(`
3535
{
3636
"crate": {
@@ -49,8 +49,6 @@ test('happy path', ({ expect }) => {
4949
"repository": null,
5050
"trustpubOnly": false,
5151
"updated_at": "2017-02-24T12:34:56Z",
52-
Symbol(type): "crate",
53-
Symbol(primaryKey): "id",
5452
},
5553
"createdAt": "2016-12-24T12:34:56Z",
5654
"expiresAt": "2017-01-24T12:34:56Z",
@@ -67,8 +65,6 @@ test('happy path', ({ expect }) => {
6765
"name": "User 2",
6866
"publishNotifications": true,
6967
"url": "https://github.com/user-2",
70-
Symbol(type): "user",
71-
Symbol(primaryKey): "id",
7268
},
7369
"inviter": {
7470
"avatar": "https://avatars1.githubusercontent.com/u/14631425?v=4",
@@ -82,12 +78,8 @@ test('happy path', ({ expect }) => {
8278
"name": "User 1",
8379
"publishNotifications": true,
8480
"url": "https://github.com/user-1",
85-
Symbol(type): "user",
86-
Symbol(primaryKey): "id",
8781
},
8882
"token": "secret-token-1",
89-
Symbol(type): "crateOwnerInvitation",
90-
Symbol(primaryKey): "id",
9183
}
9284
`);
9385
});

0 commit comments

Comments
 (0)