Skip to content

Commit

Permalink
msw/session: Implement apiToken model
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Jan 17, 2025
1 parent 235c5c3 commit 07f7652
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/crates-io-msw/models/api-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { nullable, oneOf, primaryKey } from '@mswjs/data';

import { applyDefault } from './-utils.js';

export const apiToken = {
id: primaryKey(Number),

crateScopes: nullable(Array),
createdAt: '2017-11-19T17:59:22',
endpointScopes: nullable(Array),
expiredAt: nullable(String),
lastUsedAt: nullable(String),
name: i => `API Token ${i + 1}`,
token: String,

user: oneOf('user'),

preCreate(attrs, counter) {
applyDefault(attrs, 'id', () => counter);
applyDefault(attrs, 'crateScopes', () => null);
applyDefault(attrs, 'endpointScopes', () => null);
applyDefault(attrs, 'expiredAt', () => null);
applyDefault(attrs, 'lastUsedAt', () => null);
applyDefault(attrs, 'name', attrs => `API Token ${attrs.id}`);
applyDefault(attrs, 'token', () => generateToken());

if (!attrs.user) {
throw new Error('Missing `user` relationship on `api-token`');
}
},
};

function generateToken() {
return Math.random().toString().slice(2);
}
2 changes: 2 additions & 0 deletions packages/crates-io-msw/models/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { apiToken } from './api-token.js';
import { mswSession } from './msw-session.js';
import { user } from './user.js';

export const models = {
apiToken,
mswSession,
user,
};

0 comments on commit 07f7652

Please sign in to comment.