|
1 | 1 | import { Collection } from '@msw/data'; |
2 | 2 | import * as v from 'valibot'; |
3 | 3 |
|
4 | | -import { applyDefault } from '../utils/defaults.js'; |
5 | | -import { preCreateExtension } from '../utils/pre-create-extension.js'; |
| 4 | +import * as counters from '../utils/counters.js'; |
6 | 5 |
|
7 | | -const schema = v.object({ |
8 | | - // `v.string()` is used to support some of our old fixtures that use strings here for some reason |
9 | | - id: v.union([v.number(), v.string()]), |
| 6 | +const schema = v.pipe( |
| 7 | + v.object({ |
| 8 | + // `v.string()` is used to support some of our old fixtures that use strings here for some reason |
| 9 | + id: v.optional(v.union([v.number(), v.string()])), |
10 | 10 |
|
11 | | - name: v.string(), |
12 | | - description: v.string(), |
13 | | - downloads: v.number(), |
14 | | - recent_downloads: v.number(), |
15 | | - documentation: v.nullable(v.string()), |
16 | | - homepage: v.nullable(v.string()), |
17 | | - repository: v.nullable(v.string()), |
18 | | - created_at: v.string(), |
19 | | - updated_at: v.string(), |
20 | | - badges: v.array(v.any()), |
21 | | - _extra_downloads: v.array(v.any()), |
22 | | - trustpubOnly: v.boolean(), |
| 11 | + name: v.optional(v.string()), |
| 12 | + description: v.optional(v.string()), |
| 13 | + downloads: v.optional(v.number()), |
| 14 | + recent_downloads: v.optional(v.number()), |
| 15 | + documentation: v.optional(v.nullable(v.string()), null), |
| 16 | + homepage: v.optional(v.nullable(v.string()), null), |
| 17 | + repository: v.optional(v.nullable(v.string()), null), |
| 18 | + created_at: v.optional(v.string(), '2010-06-16T21:30:45Z'), |
| 19 | + updated_at: v.optional(v.string(), '2017-02-24T12:34:56Z'), |
| 20 | + badges: v.optional(v.array(v.any()), []), |
| 21 | + _extra_downloads: v.optional(v.array(v.any()), []), |
| 22 | + trustpubOnly: v.optional(v.boolean(), false), |
23 | 23 |
|
24 | | - categories: v.optional(v.array(v.any()), () => []), |
25 | | - keywords: v.optional(v.array(v.any()), () => []), |
26 | | -}); |
| 24 | + categories: v.optional(v.array(v.any()), []), |
| 25 | + keywords: v.optional(v.array(v.any()), []), |
| 26 | + }), |
| 27 | + v.transform(function (input) { |
| 28 | + let counter = counters.increment('crate'); |
| 29 | + let id = input.id ?? counter; |
| 30 | + let name = input.name ?? `crate-${id}`; |
| 31 | + let description = input.description ?? `This is the description for the crate called "${name}"`; |
| 32 | + let downloads = input.downloads ?? (((id + 13) * 42) % 13) * 12_345; |
| 33 | + let recent_downloads = input.recent_downloads ?? (((id + 7) * 31) % 13) * 321; |
| 34 | + return { ...input, id, name, description, downloads, recent_downloads }; |
| 35 | + }), |
| 36 | +); |
27 | 37 |
|
28 | | -function preCreate(attrs, counter) { |
29 | | - applyDefault(attrs, 'id', () => counter); |
30 | | - applyDefault(attrs, 'name', () => `crate-${attrs.id}`); |
31 | | - applyDefault(attrs, 'description', () => `This is the description for the crate called "${attrs.name}"`); |
32 | | - applyDefault(attrs, 'downloads', () => (((attrs.id + 13) * 42) % 13) * 12_345); |
33 | | - applyDefault(attrs, 'recent_downloads', () => (((attrs.id + 7) * 31) % 13) * 321); |
34 | | - applyDefault(attrs, 'documentation', () => null); |
35 | | - applyDefault(attrs, 'homepage', () => null); |
36 | | - applyDefault(attrs, 'repository', () => null); |
37 | | - applyDefault(attrs, 'created_at', () => '2010-06-16T21:30:45Z'); |
38 | | - applyDefault(attrs, 'updated_at', () => '2017-02-24T12:34:56Z'); |
39 | | - applyDefault(attrs, 'badges', () => []); |
40 | | - applyDefault(attrs, '_extra_downloads', () => []); |
41 | | - applyDefault(attrs, 'trustpubOnly', () => false); |
42 | | -} |
43 | | - |
44 | | -const collection = new Collection({ |
45 | | - schema, |
46 | | - extensions: [preCreateExtension(preCreate)], |
47 | | -}); |
| 38 | +const collection = new Collection({ schema }); |
48 | 39 |
|
49 | 40 | export default collection; |
0 commit comments