asma-core-helpers 0.27.0 → 0.28.0
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/lib/collaboration/applyOperation.d.ts.map +1 -1
- package/lib/collaboration/applyOperation.js +132 -51
- package/lib/collaboration/applyOperation.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +24 -1
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +26 -4
- package/lib/collaboration/schemas.js.map +1 -1
- package/lib/helpers/createTadaBrowserClient.d.ts +3 -1
- package/lib/helpers/createTadaBrowserClient.d.ts.map +1 -1
- package/lib/helpers/createTadaBrowserClient.js +12 -5
- package/lib/helpers/createTadaBrowserClient.js.map +1 -1
- package/lib/helpers/generateSrvAuthBindings.d.ts +1 -0
- package/lib/helpers/generateSrvAuthBindings.d.ts.map +1 -1
- package/lib/helpers/generateSrvAuthBindings.js +7 -0
- package/lib/helpers/generateSrvAuthBindings.js.map +1 -1
- package/lib/helpers/initTadaBrowserClient.d.ts.map +1 -1
- package/lib/helpers/initTadaBrowserClient.js +2 -1
- package/lib/helpers/initTadaBrowserClient.js.map +1 -1
- package/package.json +1 -1
- package/src/collaboration/applyOperation.test.ts +139 -11
- package/src/collaboration/applyOperation.ts +148 -57
- package/src/collaboration/property.test.ts +4 -1
- package/src/collaboration/schemas.test.ts +49 -0
- package/src/collaboration/schemas.ts +30 -4
- package/src/helpers/createTadaBrowserClient.ts +16 -4
- package/src/helpers/generateSrvAuthBindings.ts +11 -0
- package/src/helpers/initTadaBrowserClient.ts +7 -1
|
@@ -114,6 +114,29 @@ const omitKey = <V>(record: Record<string, V>, key: string): Record<string, V> =
|
|
|
114
114
|
return rest
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The document paths a grid operation owns, and no other op may write.
|
|
119
|
+
*
|
|
120
|
+
* `grid.columnIds` is the ownership array a question's single structural owner is read from
|
|
121
|
+
* (`gridColumn.create`/`move` maintain it), and `presentation.rowEditor.layoutByQuestionId`
|
|
122
|
+
* holds one placement per column (`gridColumn.setLayout` writes exactly one). An op that
|
|
123
|
+
* carries a scalar or a flat primitive array can only write such a map *wholesale*, so
|
|
124
|
+
* reaching either path through `question.updateField` replaces or clears it — columns left
|
|
125
|
+
* owned by nobody, or every other column's placement dropped in one edit. That is precisely
|
|
126
|
+
* the atomicity the grid ops exist to provide, so the paths are reserved for them.
|
|
127
|
+
*/
|
|
128
|
+
const GRID_OWNED_FIELD_PATHS = ['grid.columnIds', 'presentation.rowEditor.layoutByQuestionId'] as const
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The reserved path `field` would write, if any. Both directions count: the path itself, a
|
|
132
|
+
* path *below* it (`grid.columnIds.0`), and an *ancestor* whose write would take the reserved
|
|
133
|
+
* map with it (`grid`, `presentation.rowEditor`).
|
|
134
|
+
*/
|
|
135
|
+
const reservedGridFieldPath = (field: string): string | undefined =>
|
|
136
|
+
GRID_OWNED_FIELD_PATHS.find(
|
|
137
|
+
(owned) => field === owned || field.startsWith(`${owned}.`) || owned.startsWith(`${field}.`),
|
|
138
|
+
)
|
|
139
|
+
|
|
117
140
|
const requireGridQuestion = (doc: QnrTemplateDocument, questionId: QuestionId): QnrQuestion => {
|
|
118
141
|
const question = doc.questionsById?.[questionId]
|
|
119
142
|
if (!question) throw new OperationConflictError(`Unknown question "${questionId}"`)
|
|
@@ -409,6 +432,102 @@ const dropFromGridColumns = (doc: QnrTemplateDocument, questionId: QuestionId):
|
|
|
409
432
|
})
|
|
410
433
|
}
|
|
411
434
|
|
|
435
|
+
/**
|
|
436
|
+
* Every question a grid owns, transitively, the grid itself excluded. `seen` makes it total on
|
|
437
|
+
* malformed data: an ownership cycle is not authorable but is importable, and a delete must
|
|
438
|
+
* terminate on one rather than blow the stack.
|
|
439
|
+
*/
|
|
440
|
+
const collectOwnedColumnIds = (doc: QnrTemplateDocument, gridQuestionId: QuestionId): QuestionId[] => {
|
|
441
|
+
const owned: QuestionId[] = []
|
|
442
|
+
const seen = new Set<QuestionId>([gridQuestionId])
|
|
443
|
+
const queue: QuestionId[] = [gridQuestionId]
|
|
444
|
+
while (queue.length > 0) {
|
|
445
|
+
const current = queue.pop() as QuestionId
|
|
446
|
+
const question = doc.questionsById?.[current]
|
|
447
|
+
// Only a grid owns columns; the per-type bag is open, so a malformed `columnIds` must
|
|
448
|
+
// not crash the reducer — that is a schema violation for validation to report.
|
|
449
|
+
const columnIds = question?.type === 'QuestionGrid' ? question.grid?.columnIds : undefined
|
|
450
|
+
if (!Array.isArray(columnIds)) continue
|
|
451
|
+
for (const columnId of columnIds) {
|
|
452
|
+
if (seen.has(columnId)) continue
|
|
453
|
+
seen.add(columnId)
|
|
454
|
+
owned.push(columnId)
|
|
455
|
+
queue.push(columnId)
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return owned
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Removes one question and everything it alone owns: its alternatives, its own rules, its
|
|
463
|
+
* predefined grid rows, its place in any grid's column list and every binding aimed at it.
|
|
464
|
+
* `question.delete` applies it to the deleted question and to each column that grid owns.
|
|
465
|
+
*/
|
|
466
|
+
const deleteQuestion = (doc: QnrTemplateDocument, questionId: QuestionId): QnrTemplateDocument => {
|
|
467
|
+
// A grid's predefined rows go with it: the order key is dropped below, and a row record
|
|
468
|
+
// no order array reaches is unreachable state that still changes `document_hash`.
|
|
469
|
+
const rowIds = doc.gridRowOrderByQuestionId?.[questionId] ?? []
|
|
470
|
+
let gridRowsById = doc.gridRowsById
|
|
471
|
+
if (gridRowsById) {
|
|
472
|
+
gridRowsById = rowIds.reduce((acc, id) => omitKey(acc, id), gridRowsById)
|
|
473
|
+
}
|
|
474
|
+
const alternativeIds = doc.alternativeOrderByQuestionId?.[questionId] ?? []
|
|
475
|
+
let alternativesById = doc.alternativesById
|
|
476
|
+
if (alternativesById) {
|
|
477
|
+
alternativesById = alternativeIds.reduce((acc, id) => omitKey(acc, id), alternativesById)
|
|
478
|
+
}
|
|
479
|
+
// The question's own rules go with it, the same way its alternatives do: an orphaned
|
|
480
|
+
// rule record is referenced by no order array and only pollutes the hash. Rules of
|
|
481
|
+
// OTHER questions whose condition points at this one are deliberately KEPT — silently
|
|
482
|
+
// dropping a surviving question's authored logic would be data loss, so the dangling
|
|
483
|
+
// `sourceQuestionId` is left for validation to surface instead.
|
|
484
|
+
const visibilityRuleIds = doc.visibilityRuleOrderByQuestionId?.[questionId] ?? []
|
|
485
|
+
let visibilityRulesById = doc.visibilityRulesById
|
|
486
|
+
if (visibilityRulesById) {
|
|
487
|
+
visibilityRulesById = visibilityRuleIds.reduce((acc, id) => omitKey(acc, id), visibilityRulesById)
|
|
488
|
+
}
|
|
489
|
+
const highlightRuleIds = doc.highlightRuleOrderByQuestionId?.[questionId] ?? []
|
|
490
|
+
let highlightRulesById = doc.highlightRulesById
|
|
491
|
+
if (highlightRulesById) {
|
|
492
|
+
highlightRulesById = highlightRuleIds.reduce((acc, id) => omitKey(acc, id), highlightRulesById)
|
|
493
|
+
}
|
|
494
|
+
const narrativeRuleIds = doc.narrativeRuleOrderByQuestionId?.[questionId] ?? []
|
|
495
|
+
let narrativeRulesById = doc.narrativeRulesById
|
|
496
|
+
if (narrativeRulesById) {
|
|
497
|
+
narrativeRulesById = narrativeRuleIds.reduce((acc, id) => omitKey(acc, id), narrativeRulesById)
|
|
498
|
+
}
|
|
499
|
+
const qnrRuleIds = doc.qnrRuleOrderByQuestionId?.[questionId] ?? []
|
|
500
|
+
let qnrRulesById = doc.qnrRulesById
|
|
501
|
+
if (qnrRulesById) {
|
|
502
|
+
qnrRulesById = qnrRuleIds.reduce((acc, id) => omitKey(acc, id), qnrRulesById)
|
|
503
|
+
}
|
|
504
|
+
const withoutQuestion = patchDocument(doc, {
|
|
505
|
+
questionsById: doc.questionsById && omitKey(doc.questionsById, questionId),
|
|
506
|
+
questionOrder: doc.questionOrder.filter((id) => id !== questionId),
|
|
507
|
+
alternativesById,
|
|
508
|
+
alternativeOrderByQuestionId:
|
|
509
|
+
doc.alternativeOrderByQuestionId && omitKey(doc.alternativeOrderByQuestionId, questionId),
|
|
510
|
+
gridRowsById,
|
|
511
|
+
gridRowOrderByQuestionId: doc.gridRowOrderByQuestionId && omitKey(doc.gridRowOrderByQuestionId, questionId),
|
|
512
|
+
visibilityRulesById,
|
|
513
|
+
visibilityRuleOrderByQuestionId:
|
|
514
|
+
doc.visibilityRuleOrderByQuestionId && omitKey(doc.visibilityRuleOrderByQuestionId, questionId),
|
|
515
|
+
highlightRulesById,
|
|
516
|
+
highlightRuleOrderByQuestionId:
|
|
517
|
+
doc.highlightRuleOrderByQuestionId && omitKey(doc.highlightRuleOrderByQuestionId, questionId),
|
|
518
|
+
narrativeRulesById,
|
|
519
|
+
narrativeRuleOrderByQuestionId:
|
|
520
|
+
doc.narrativeRuleOrderByQuestionId && omitKey(doc.narrativeRuleOrderByQuestionId, questionId),
|
|
521
|
+
qnrRulesById,
|
|
522
|
+
qnrRuleOrderByQuestionId:
|
|
523
|
+
doc.qnrRuleOrderByQuestionId && omitKey(doc.qnrRuleOrderByQuestionId, questionId),
|
|
524
|
+
})
|
|
525
|
+
// A binding pointing at a deleted question is an unresolvable reference the
|
|
526
|
+
// compiler would reject at publication; drop it with its target. A grid column
|
|
527
|
+
// list pointing at it is the same class of reference and goes the same way.
|
|
528
|
+
return dropBindingsForQuestion(dropFromGridColumns(withoutQuestion, questionId), questionId)
|
|
529
|
+
}
|
|
530
|
+
|
|
412
531
|
export const applyOperation = (document: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument => {
|
|
413
532
|
const next = reduce(document, op)
|
|
414
533
|
return prune({ ...next, revision: document.revision + 1 })
|
|
@@ -455,6 +574,29 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
455
574
|
if (!question) {
|
|
456
575
|
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
457
576
|
}
|
|
577
|
+
// `type` is the question's identity, not one of its fields: `question.create` sets it and
|
|
578
|
+
// every per-type shape hangs off it. A field write moves it alone — a grid flipped to
|
|
579
|
+
// another type keeps the `grid.columnIds` the document schema admits on no other type,
|
|
580
|
+
// its columns then owned by nobody and no longer cascading with its delete.
|
|
581
|
+
if (op.field === 'type' || op.field.startsWith('type.')) {
|
|
582
|
+
throw new OperationConflictError(
|
|
583
|
+
`Field "${op.field}" writes the question type, which only question.create may set`,
|
|
584
|
+
)
|
|
585
|
+
}
|
|
586
|
+
const reserved = reservedGridFieldPath(op.field)
|
|
587
|
+
if (reserved !== undefined) {
|
|
588
|
+
throw new OperationConflictError(
|
|
589
|
+
`Field "${op.field}" writes "${reserved}", which only the gridColumn operations may author`,
|
|
590
|
+
)
|
|
591
|
+
}
|
|
592
|
+
// `grid` is the grid's own configuration and the document schema admits it on no
|
|
593
|
+
// other type, so authoring it through an ordinary question would store a bag no
|
|
594
|
+
// reader projects and the publication schema then refuses.
|
|
595
|
+
if (question.type !== 'QuestionGrid' && op.field.split('.')[0] === 'grid') {
|
|
596
|
+
throw new OperationConflictError(
|
|
597
|
+
`Question "${op.questionId}" is not a question grid, so "${op.field}" is not authorable on it`,
|
|
598
|
+
)
|
|
599
|
+
}
|
|
458
600
|
return {
|
|
459
601
|
...doc,
|
|
460
602
|
questionsById: {
|
|
@@ -475,63 +617,12 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
475
617
|
}
|
|
476
618
|
|
|
477
619
|
case 'question.delete': {
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
// rule record is referenced by no order array and only pollutes the hash. Rules of
|
|
485
|
-
// OTHER questions whose condition points at this one are deliberately KEPT — silently
|
|
486
|
-
// dropping a surviving question's authored logic would be data loss, so the dangling
|
|
487
|
-
// `sourceQuestionId` is left for validation to surface instead.
|
|
488
|
-
const visibilityRuleIds = doc.visibilityRuleOrderByQuestionId?.[op.questionId] ?? []
|
|
489
|
-
let visibilityRulesById = doc.visibilityRulesById
|
|
490
|
-
if (visibilityRulesById) {
|
|
491
|
-
visibilityRulesById = visibilityRuleIds.reduce((acc, id) => omitKey(acc, id), visibilityRulesById)
|
|
492
|
-
}
|
|
493
|
-
const highlightRuleIds = doc.highlightRuleOrderByQuestionId?.[op.questionId] ?? []
|
|
494
|
-
let highlightRulesById = doc.highlightRulesById
|
|
495
|
-
if (highlightRulesById) {
|
|
496
|
-
highlightRulesById = highlightRuleIds.reduce((acc, id) => omitKey(acc, id), highlightRulesById)
|
|
497
|
-
}
|
|
498
|
-
const narrativeRuleIds = doc.narrativeRuleOrderByQuestionId?.[op.questionId] ?? []
|
|
499
|
-
let narrativeRulesById = doc.narrativeRulesById
|
|
500
|
-
if (narrativeRulesById) {
|
|
501
|
-
narrativeRulesById = narrativeRuleIds.reduce((acc, id) => omitKey(acc, id), narrativeRulesById)
|
|
502
|
-
}
|
|
503
|
-
const qnrRuleIds = doc.qnrRuleOrderByQuestionId?.[op.questionId] ?? []
|
|
504
|
-
let qnrRulesById = doc.qnrRulesById
|
|
505
|
-
if (qnrRulesById) {
|
|
506
|
-
qnrRulesById = qnrRuleIds.reduce((acc, id) => omitKey(acc, id), qnrRulesById)
|
|
507
|
-
}
|
|
508
|
-
const withoutQuestion = patchDocument(doc, {
|
|
509
|
-
questionsById: doc.questionsById && omitKey(doc.questionsById, op.questionId),
|
|
510
|
-
questionOrder: doc.questionOrder.filter((id) => id !== op.questionId),
|
|
511
|
-
alternativesById,
|
|
512
|
-
alternativeOrderByQuestionId:
|
|
513
|
-
doc.alternativeOrderByQuestionId && omitKey(doc.alternativeOrderByQuestionId, op.questionId),
|
|
514
|
-
gridRowOrderByQuestionId:
|
|
515
|
-
doc.gridRowOrderByQuestionId && omitKey(doc.gridRowOrderByQuestionId, op.questionId),
|
|
516
|
-
visibilityRulesById,
|
|
517
|
-
visibilityRuleOrderByQuestionId:
|
|
518
|
-
doc.visibilityRuleOrderByQuestionId && omitKey(doc.visibilityRuleOrderByQuestionId, op.questionId),
|
|
519
|
-
highlightRulesById,
|
|
520
|
-
highlightRuleOrderByQuestionId:
|
|
521
|
-
doc.highlightRuleOrderByQuestionId &&
|
|
522
|
-
omitKey(doc.highlightRuleOrderByQuestionId, op.questionId),
|
|
523
|
-
narrativeRulesById,
|
|
524
|
-
narrativeRuleOrderByQuestionId:
|
|
525
|
-
doc.narrativeRuleOrderByQuestionId &&
|
|
526
|
-
omitKey(doc.narrativeRuleOrderByQuestionId, op.questionId),
|
|
527
|
-
qnrRulesById,
|
|
528
|
-
qnrRuleOrderByQuestionId:
|
|
529
|
-
doc.qnrRuleOrderByQuestionId && omitKey(doc.qnrRuleOrderByQuestionId, op.questionId),
|
|
530
|
-
})
|
|
531
|
-
// A binding pointing at a deleted question is an unresolvable reference the
|
|
532
|
-
// compiler would reject at publication; drop it with its target. A grid column
|
|
533
|
-
// list pointing at it is the same class of reference and goes the same way.
|
|
534
|
-
return dropBindingsForQuestion(dropFromGridColumns(withoutQuestion, op.questionId), op.questionId)
|
|
620
|
+
// A grid owns its columns, so they go with it: they sit in no order array of their
|
|
621
|
+
// own, and leaving them behind is the orphan half of the ownership invariant. The
|
|
622
|
+
// whole ownership subtree is collected first, then each question is removed on its
|
|
623
|
+
// own terms — its alternatives, rules and bindings included.
|
|
624
|
+
const ids = [op.questionId, ...collectOwnedColumnIds(doc, op.questionId)]
|
|
625
|
+
return ids.reduce((acc, id) => deleteQuestion(acc, id), doc)
|
|
535
626
|
}
|
|
536
627
|
|
|
537
628
|
case 'gridColumn.create': {
|
|
@@ -82,7 +82,10 @@ describe('applyOperation determinism', () => {
|
|
|
82
82
|
})
|
|
83
83
|
|
|
84
84
|
describe('DOC-LAW invariants under random op sequences', () => {
|
|
85
|
-
for
|
|
85
|
+
// Seed 4 is here for one reason: it is the smallest seed whose sequence deletes a grid that
|
|
86
|
+
// still owns columns. Without it the ownership assertion below never sees the cascade, which
|
|
87
|
+
// is the one path where a delete can strand questions in no order array at all.
|
|
88
|
+
for (const seed of [4, 7, 13, 99]) {
|
|
86
89
|
it(`holds for seed ${seed}`, () => {
|
|
87
90
|
const { doc } = runSequence(seed, 150)
|
|
88
91
|
|
|
@@ -100,6 +100,55 @@ describe('document schemas', () => {
|
|
|
100
100
|
})
|
|
101
101
|
|
|
102
102
|
describe('question ownership invariant', () => {
|
|
103
|
+
it('admits `grid` on a question grid alone', () => {
|
|
104
|
+
const withGrid = (type: string): QnrTemplateDocument =>
|
|
105
|
+
({
|
|
106
|
+
...emptyTemplateDocument('tpl-1'),
|
|
107
|
+
questionsById: { 'q-1': { type, grid: { columnIds: ['c-1'] } }, 'c-1': { type: 'TextShort' } },
|
|
108
|
+
questionOrder: ['q-1'],
|
|
109
|
+
}) as unknown as QnrTemplateDocument
|
|
110
|
+
|
|
111
|
+
assert.equal(validateTemplateDocument(withGrid('QuestionGrid')).ok, true)
|
|
112
|
+
// The per-type bag is open, so this is the one shape validation must NOT wave through:
|
|
113
|
+
// `columnIds` is ownership, and an ordinary question owning questions is unrepresentable.
|
|
114
|
+
assert.equal(validateTemplateDocument(withGrid('TextShort')).ok, false)
|
|
115
|
+
// The rest of the bag is untouched — an unknown per-type key is still an open extension.
|
|
116
|
+
assert.equal(
|
|
117
|
+
validateTemplateDocument({
|
|
118
|
+
...emptyTemplateDocument('tpl-1'),
|
|
119
|
+
questionsById: { 'q-1': { type: 'TextShort', dropdown: { multi: true } } },
|
|
120
|
+
questionOrder: ['q-1'],
|
|
121
|
+
} as unknown as QnrTemplateDocument).ok,
|
|
122
|
+
true,
|
|
123
|
+
)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('does not read a non-grid question as a column owner', () => {
|
|
127
|
+
// The publication gate refuses on any violation, so a stray `grid` on an ordinary
|
|
128
|
+
// question must not be able to *satisfy* ownership for the question it lists: that would
|
|
129
|
+
// hide the orphan instead of reporting it.
|
|
130
|
+
const doc = {
|
|
131
|
+
documentId: 'tpl-1',
|
|
132
|
+
revision: 0,
|
|
133
|
+
questionsById: {
|
|
134
|
+
'q-1': { type: 'TextShort', grid: { columnIds: ['c-1'] } },
|
|
135
|
+
'c-1': { type: 'TextShort' },
|
|
136
|
+
},
|
|
137
|
+
questionOrder: ['q-1'],
|
|
138
|
+
} as unknown as QnrTemplateDocument
|
|
139
|
+
|
|
140
|
+
assert.deepEqual(findQuestionOwnershipViolations(doc), [
|
|
141
|
+
{
|
|
142
|
+
law: 'QUESTION-OWNERSHIP',
|
|
143
|
+
kind: 'orphan',
|
|
144
|
+
questionId: 'c-1',
|
|
145
|
+
gridQuestionIds: [],
|
|
146
|
+
path: 'questionsById.c-1',
|
|
147
|
+
detail: 'question is neither top-level nor owned by a grid',
|
|
148
|
+
},
|
|
149
|
+
])
|
|
150
|
+
})
|
|
151
|
+
|
|
103
152
|
it('reports orphaned, top-level-and-column, multi-grid, and dangling column questions', () => {
|
|
104
153
|
const doc: QnrTemplateDocument = {
|
|
105
154
|
documentId: 'tpl-1',
|
|
@@ -111,16 +111,37 @@ const questionGridPresentationSchema = type({
|
|
|
111
111
|
...({ '[string]': 'unknown' } as const),
|
|
112
112
|
})
|
|
113
113
|
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
/** Every question field except the discriminator and the grid-only `grid` bag. */
|
|
115
|
+
const qnrQuestionCommonFields = {
|
|
116
116
|
"subtype?": type.enumerated('ORDINARY', 'EMAIL'),
|
|
117
117
|
"label?": 'string',
|
|
118
118
|
"required?": 'boolean',
|
|
119
119
|
"defaultValue?": docScalar,
|
|
120
|
-
"grid?": questionGridConfigSchema,
|
|
121
120
|
"presentation?": questionGridPresentationSchema,
|
|
122
121
|
...({ '[string]': 'unknown' } as const),
|
|
123
|
-
}
|
|
122
|
+
} as const
|
|
123
|
+
|
|
124
|
+
const NON_GRID_QUESTION_TYPES = QUESTION_TYPES.filter((questionType) => questionType !== 'QuestionGrid')
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A question, discriminated on `type` for one reason: `grid` carries `columnIds`, the array a
|
|
128
|
+
* question's structural owner is read from, and only a `QuestionGrid` may own columns. The
|
|
129
|
+
* per-type bag being open (`[string]: unknown`) means omitting the key from the non-grid branch
|
|
130
|
+
* would admit it anyway, so that branch types it `never` — the schema itself then refuses a
|
|
131
|
+
* document where an ordinary question claims to own questions.
|
|
132
|
+
*/
|
|
133
|
+
const qnrQuestionSchema = type.or(
|
|
134
|
+
type({
|
|
135
|
+
type: '"QuestionGrid"',
|
|
136
|
+
"grid?": questionGridConfigSchema,
|
|
137
|
+
...qnrQuestionCommonFields,
|
|
138
|
+
}),
|
|
139
|
+
type({
|
|
140
|
+
type: type.enumerated(...NON_GRID_QUESTION_TYPES),
|
|
141
|
+
"grid?": 'never',
|
|
142
|
+
...qnrQuestionCommonFields,
|
|
143
|
+
}),
|
|
144
|
+
)
|
|
124
145
|
|
|
125
146
|
const qnrAlternativeSchema = type({
|
|
126
147
|
"label?": 'string',
|
|
@@ -628,6 +649,11 @@ export const findQuestionOwnershipViolations = (
|
|
|
628
649
|
): QuestionOwnershipViolation[] => {
|
|
629
650
|
const ownersByQuestionId = new Map<string, string[]>()
|
|
630
651
|
for (const [gridQuestionId, question] of Object.entries(document.questionsById ?? {})) {
|
|
652
|
+
// Only a grid owns columns. `grid` on any other type is a shape the document schema
|
|
653
|
+
// refuses outright, and reading it as ownership here would be worse than ignoring it:
|
|
654
|
+
// a stray key would adopt a top-level question, or cover for a real orphan by naming
|
|
655
|
+
// an owner that owns nothing.
|
|
656
|
+
if (question.type !== 'QuestionGrid') continue
|
|
631
657
|
for (const columnQuestionId of question.grid?.columnIds ?? []) {
|
|
632
658
|
const owners = ownersByQuestionId.get(columnQuestionId) ?? []
|
|
633
659
|
if (!owners.includes(gridQuestionId)) owners.push(gridQuestionId)
|
|
@@ -36,6 +36,8 @@ export interface CreateTadaBrowserClientOptions {
|
|
|
36
36
|
url: string
|
|
37
37
|
/** Resolves the current JWT. Typically `getCachedJwt` from asma-core-react. */
|
|
38
38
|
getJwt: () => Promise<string | undefined>
|
|
39
|
+
/** Returns the synchronously cached JWT. Typically `getJwtToken` from auth bindings. */
|
|
40
|
+
getJwtToken: () => string | undefined
|
|
39
41
|
/** Returns true when the cached JWT is still valid. Typically `isJwtValid` from asma-core-react. */
|
|
40
42
|
isJwtValid: () => boolean
|
|
41
43
|
/** Optional service-specific exchanges injected before fetchExchange. */
|
|
@@ -56,6 +58,7 @@ export interface CreateTadaBrowserClientOptions {
|
|
|
56
58
|
export function createTadaBrowserClient({
|
|
57
59
|
url,
|
|
58
60
|
getJwt,
|
|
61
|
+
getJwtToken,
|
|
59
62
|
isJwtValid,
|
|
60
63
|
resolveExchanges,
|
|
61
64
|
clientOptions,
|
|
@@ -63,7 +66,9 @@ export function createTadaBrowserClient({
|
|
|
63
66
|
let tadaClient: Client | undefined
|
|
64
67
|
|
|
65
68
|
const getClient = async () => {
|
|
66
|
-
tadaClient
|
|
69
|
+
if (!tadaClient) {
|
|
70
|
+
tadaClient = await buildClient({ clientOptions })
|
|
71
|
+
}
|
|
67
72
|
return tadaClient
|
|
68
73
|
}
|
|
69
74
|
|
|
@@ -100,9 +105,14 @@ export function createTadaBrowserClient({
|
|
|
100
105
|
if (!anonymous) {
|
|
101
106
|
_clientOptions.exchanges.unshift(
|
|
102
107
|
authExchange(async (utils) => {
|
|
103
|
-
|
|
108
|
+
let token = await getJwt()
|
|
109
|
+
|
|
104
110
|
return {
|
|
105
111
|
addAuthToOperation(operation) {
|
|
112
|
+
if (!token) {
|
|
113
|
+
return operation
|
|
114
|
+
}
|
|
115
|
+
|
|
106
116
|
return utils.appendHeaders(operation, {
|
|
107
117
|
Authorization: `Bearer ${token}`,
|
|
108
118
|
})
|
|
@@ -111,10 +121,12 @@ export function createTadaBrowserClient({
|
|
|
111
121
|
return error.response.status === 401
|
|
112
122
|
},
|
|
113
123
|
async refreshAuth() {
|
|
114
|
-
|
|
124
|
+
token = await getJwt()
|
|
115
125
|
},
|
|
116
126
|
willAuthError() {
|
|
117
|
-
|
|
127
|
+
const tokenChanged = token !== getJwtToken()
|
|
128
|
+
const tokenInvalid = !isJwtValid()
|
|
129
|
+
return tokenChanged || tokenInvalid
|
|
118
130
|
},
|
|
119
131
|
}
|
|
120
132
|
}),
|
|
@@ -38,6 +38,17 @@ export async function getCachedJwtInternal() {
|
|
|
38
38
|
}
|
|
39
39
|
return getCachedJwt()
|
|
40
40
|
}
|
|
41
|
+
|
|
42
|
+
export function getJwtTokenInternal(): string | undefined {
|
|
43
|
+
const getJwtToken = realWindow.__ASMA__SHELL__?.auth_bindings?.getJwtToken
|
|
44
|
+
|
|
45
|
+
if (!getJwtToken) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
'getJwtToken is not defined! please make sure that generateSrvAuthBindings is called before getJwtToken',
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
return getJwtToken()
|
|
51
|
+
}
|
|
41
52
|
export async function checkForRegisteredSubdomainInternal() {
|
|
42
53
|
const checkForRegisteredSubdomain = realWindow.__ASMA__SHELL__?.auth_bindings?.checkForRegisteredSubdomain
|
|
43
54
|
|
|
@@ -15,7 +15,12 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import { createTadaBrowserClient, type ResolveExchanges } from './createTadaBrowserClient.js'
|
|
17
17
|
import { EnvConfigsFnInternal, type IEnvironmentUrlsGenQLOnly } from './generateEnvConfigsBindings.js'
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
getCachedJwtInternal,
|
|
20
|
+
getJwtTokenInternal,
|
|
21
|
+
getSrvUrlsInternal,
|
|
22
|
+
isJwtValidInternal,
|
|
23
|
+
} from './generateSrvAuthBindings.js'
|
|
19
24
|
|
|
20
25
|
/**
|
|
21
26
|
* Resolves the GraphQL base URL for a service from EnvConfigs, preferring the
|
|
@@ -65,6 +70,7 @@ export function initTadaBrowserClient({
|
|
|
65
70
|
return createTadaBrowserClient({
|
|
66
71
|
url: `${resolveSrvUrl(service)}${path}`,
|
|
67
72
|
getJwt: getCachedJwtInternal,
|
|
73
|
+
getJwtToken: getJwtTokenInternal,
|
|
68
74
|
isJwtValid: isJwtValidInternal,
|
|
69
75
|
resolveExchanges,
|
|
70
76
|
})
|