Skip to content

Commit c213e9e

Browse files
committed
msw: Implement mswSession model
1 parent 012d9d3 commit c213e9e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { mswSession } from './msw-session';
12
import { user } from './user';
23

34
export const models = {
5+
mswSession,
46
user,
57
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { oneOf, primaryKey } from '@mswjs/data';
2+
3+
import { applyDefault } from './-utils';
4+
5+
/**
6+
* This is a mirage-only model, that is used to keep track of the current
7+
* session and the associated `user` model, because in route handlers we don't
8+
* have access to the cookie data that the actual API is using for these things.
9+
*
10+
* This mock implementation means that there can only ever exist one
11+
* session at a time.
12+
*/
13+
export const mswSession = {
14+
id: primaryKey(Number),
15+
user: oneOf('user'),
16+
17+
preCreate(attrs, counter) {
18+
applyDefault(attrs, 'id', () => counter);
19+
20+
if (!attrs.user) {
21+
throw new Error('Missing `user` relationship');
22+
}
23+
},
24+
};

0 commit comments

Comments
 (0)