@uniweb/api 0.2.12 → 0.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/mock/index.js +5 -2
- package/src/mock/store.js +23 -1
package/package.json
CHANGED
package/src/mock/index.js
CHANGED
|
@@ -38,11 +38,14 @@ import { DEFAULT_SEED } from './seed.js'
|
|
|
38
38
|
*
|
|
39
39
|
* @param {object} [options]
|
|
40
40
|
* @param {object} [options.seed] - accounts, schemas and entities to start from
|
|
41
|
+
* @param {string} [options.signedInAs] - a seeded `username` to start signed in as, for a
|
|
42
|
+
* demo whose subject is the signed-in view. Development-only by construction, since
|
|
43
|
+
* this whole module is; throws if the username is not seeded.
|
|
41
44
|
* @param {string} [options.prefix] - the path the API is mounted under (default `/api`)
|
|
42
45
|
* @returns {{ fetch: (request: Request) => Promise<Response>, store: MockStore }}
|
|
43
46
|
*/
|
|
44
|
-
export function createMockBackend({ seed = DEFAULT_SEED, prefix = '/api' } = {}) {
|
|
45
|
-
const store = new MockStore(seed)
|
|
47
|
+
export function createMockBackend({ seed = DEFAULT_SEED, prefix = '/api', signedInAs = null } = {}) {
|
|
48
|
+
const store = new MockStore(seed, { signedInAs })
|
|
46
49
|
|
|
47
50
|
const json = (status, body) =>
|
|
48
51
|
new Response(body === undefined ? null : JSON.stringify(body), {
|
package/src/mock/store.js
CHANGED
|
@@ -33,8 +33,10 @@ export class MockStore {
|
|
|
33
33
|
* @param {object[]} [seed.accounts] - `{ username, password, handle, roles?, units? }`
|
|
34
34
|
* @param {object} [seed.schemas] - `{ '@/session': { creatable_by?, append_only? } }`
|
|
35
35
|
* @param {object[]} [seed.entities] - `{ uuid?, model, data?, items? }`
|
|
36
|
+
* @param {object} [options]
|
|
37
|
+
* @param {string} [options.signedInAs] - start with this account already signed in.
|
|
36
38
|
*/
|
|
37
|
-
constructor(seed = {}) {
|
|
39
|
+
constructor(seed = {}, { signedInAs = null } = {}) {
|
|
38
40
|
this.accounts = (seed.accounts || []).map((a) => ({
|
|
39
41
|
uuid: a.uuid || nextId('acct'),
|
|
40
42
|
username: a.username,
|
|
@@ -52,6 +54,26 @@ export class MockStore {
|
|
|
52
54
|
/** The one session. A mock serves one developer, so one is the honest number. */
|
|
53
55
|
this.session = null
|
|
54
56
|
this.resets = new Map()
|
|
57
|
+
|
|
58
|
+
// ⭐ `signedInAs` — start already signed in, for a demo whose whole point is the
|
|
59
|
+
// signed-in view. Without it a visitor must type credentials before seeing
|
|
60
|
+
// anything, which for a lived-in demo is the experience itself.
|
|
61
|
+
//
|
|
62
|
+
// ⛔ THROWS on an unknown username rather than leaving the session null. A typo
|
|
63
|
+
// here produces "why am I not logged in?" — a question with no visible cause, in
|
|
64
|
+
// the one place where the answer is a string three lines away. The mock is
|
|
65
|
+
// development-only, so failing at construction costs nothing and a silent
|
|
66
|
+
// anonymous session costs an afternoon.
|
|
67
|
+
if (signedInAs) {
|
|
68
|
+
const account = this.accounts.find((a) => a.username === signedInAs)
|
|
69
|
+
if (!account) {
|
|
70
|
+
const known = this.accounts.map((a) => a.username).join(', ') || '(none)'
|
|
71
|
+
throw new Error(
|
|
72
|
+
`[uniweb/api mock] signedInAs: '${signedInAs}' is not a seeded account. Seeded: ${known}`
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
this.session = { account, at: now() }
|
|
76
|
+
}
|
|
55
77
|
}
|
|
56
78
|
|
|
57
79
|
seedEntity({ uuid, model, data = {}, items = [], owner = null }) {
|