@petrarca/sonnet-fhir 0.0.1 → 0.9.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.
@@ -0,0 +1,299 @@
1
+ import { BuiltinAnswerType, ValueTypeAnswer, DesignerModel } from '@petrarca/sonnet-schema-designer/questionnaire';
2
+ import { Extension } from '../R4/index.js';
3
+ import { QuestionnaireItem, QuestionnaireItemEnableWhen, QuestionnaireResponseItemAnswer, Questionnaire, QuestionnaireResponse } from '../R4/index.js';
4
+ import { FhirConversionOptions } from '../index.js';
5
+
6
+ /** What a questionnaire conversion reports, whatever the FHIR version. */
7
+ /** A part that did not convert, and why. */
8
+ interface NotConverted {
9
+ linkId: string;
10
+ what: string;
11
+ }
12
+ interface FhirConversion<T> {
13
+ resource: T;
14
+ notConverted: NotConverted[];
15
+ }
16
+
17
+ /**
18
+ * The vocabulary of the questionnaire conversions: the FHIR R4 extension URLs
19
+ * they read and write, and the `x-fhir-*` annotations that carry, on a schema,
20
+ * what FHIR says and JSON Schema does not -- declared once, here (ADR-0003: an
21
+ * `x-` keyword is never a local decision). An annotation is read only by these
22
+ * conversions; the questionnaire designer keeps it untouched, as any keyword it
23
+ * does not understand.
24
+ */
25
+ /** The extension of a value that is missing, and why. */
26
+ declare const DATA_ABSENT_REASON = "http://hl7.org/fhir/StructureDefinition/data-absent-reason";
27
+ /** The extension that says how an item is shown (help, a radio button, ...). */
28
+ declare const ITEM_CONTROL = "http://hl7.org/fhir/StructureDefinition/questionnaire-itemControl";
29
+ /** The code system of the item controls. */
30
+ declare const ITEM_CONTROL_SYSTEM = "http://hl7.org/fhir/questionnaire-item-control";
31
+ /** The extension of a unit a quantity may be given in, one per unit. */
32
+ declare const UNIT_OPTION = "http://hl7.org/fhir/StructureDefinition/questionnaire-unitOption";
33
+ /** The extensions of an answer's limits: its length, pattern, range, count. */
34
+ declare const MIN_LENGTH = "http://hl7.org/fhir/StructureDefinition/minLength";
35
+ declare const REGEX = "http://hl7.org/fhir/StructureDefinition/regex";
36
+ declare const MIN_VALUE = "http://hl7.org/fhir/StructureDefinition/minValue";
37
+ declare const MAX_VALUE = "http://hl7.org/fhir/StructureDefinition/maxValue";
38
+ declare const MIN_OCCURS = "http://hl7.org/fhir/StructureDefinition/questionnaire-minOccurs";
39
+ declare const MAX_OCCURS = "http://hl7.org/fhir/StructureDefinition/questionnaire-maxOccurs";
40
+ /**
41
+ * Our own extension: the option of an open choice that asks for a text of one's
42
+ * own -- "other, please describe" -- its value and label, as a coding. FHIR's
43
+ * open-choice names no such option; written only when it is not the default
44
+ * `other` / "Other". Other tools ignore it.
45
+ */
46
+ declare const OTHER_OPTION_EXTENSION = "https://schemas.petrarca-labs.com/fhir/StructureDefinition/questionnaire-otherOption";
47
+ /** The extension of an example shown in an empty field. */
48
+ declare const ENTRY_FORMAT = "http://hl7.org/fhir/StructureDefinition/entryFormat";
49
+ /**
50
+ * The prefix of every annotation. An element the conversion does not map is
51
+ * kept as `x-fhir-<element>`, its value as FHIR writes it -- an item's
52
+ * `prefix`, `code`, `answerValueSet`, a Questionnaire's `url`, `version`,
53
+ * `status` -- and written back as that element.
54
+ */
55
+ declare const X_FHIR = "x-fhir-";
56
+ /** An item's linkId, when it is not its key path. */
57
+ declare const X_FHIR_LINK_ID = "x-fhir-linkId";
58
+ /** The extensions the conversion does not map, as FHIR writes them. */
59
+ declare const X_FHIR_EXTENSION = "x-fhir-extension";
60
+ /** The code system of a choice's options. */
61
+ declare const X_FHIR_SYSTEM = "x-fhir-system";
62
+ /** The linkId of a question's help item, when it is not `<linkId>.help`. */
63
+ declare const X_FHIR_HELP_LINK_ID = "x-fhir-helpLinkId";
64
+ /** A section's lead question, by key: the section is a question with follow-up questions in FHIR. */
65
+ declare const X_FHIR_LEAD_QUESTION = "x-fhir-leadQuestion";
66
+ /** An item that does not convert, kept whole: written back as it was. */
67
+ declare const X_FHIR_ITEM = "x-fhir-item";
68
+ /**
69
+ * The annotations of a question that do not depend on its answer type -- its
70
+ * linkId, its help's linkId, its prefix, its codes, its definition -- so that a
71
+ * questionnaire read from FHIR and edited in the questionnaire designer goes
72
+ * back with them. Hand them to the designer (`keptOnTypeChange`); the others
73
+ * (a choice's code system, extensions of the old type) go with the type.
74
+ */
75
+ declare const FHIR_TYPE_INDEPENDENT_ANNOTATIONS: readonly string[];
76
+ /** Whether a keyword is an annotation of these conversions. */
77
+ declare function isFhirAnnotation(keyword: string): boolean;
78
+
79
+ /**
80
+ * How a FHIR version spells a questionnaire's items -- the one thing the
81
+ * conversions ask their version: the item type of each built-in answer type,
82
+ * and the answer type of an item. Everything else is version-neutral. Each
83
+ * version implements it in its module (r4.ts, r5.ts).
84
+ */
85
+
86
+ /** An item as a version writes a built-in answer type. */
87
+ interface ItemSpelling {
88
+ type: string;
89
+ repeats?: boolean;
90
+ /** R5: whether a choice takes a text of one's own. */
91
+ answerConstraint?: string;
92
+ }
93
+ interface Spelling {
94
+ /** The item a built-in answer type is written as; `open`: a choice that takes a text of one's own. */
95
+ write(answerType: BuiltinAnswerType, open: boolean): ItemSpelling;
96
+ /** The built-in answer type of an item; none when it has none (a choice with text is read from its shape). */
97
+ answerTypeOf(item: QuestionnaireItem): BuiltinAnswerType | undefined;
98
+ /** Whether an item is a choice of its options only -- one that may carry a note. */
99
+ isChoice(item: QuestionnaireItem): boolean;
100
+ /** Whether an item is a choice that also takes a text of one's own: "other, please describe". */
101
+ isOpen(item: QuestionnaireItem): boolean;
102
+ /** The item elements the spelling itself maps (R5's `answerConstraint`). */
103
+ elements: string[];
104
+ }
105
+
106
+ /**
107
+ * Value types in FHIR -- a value type contributed to the questionnaire
108
+ * conversions: the item type it is, its settings as the item's extensions,
109
+ * its answers. Sonnet contributes the core types the same way an application
110
+ * contributes its own; a value type without one is kept whole, as any item of
111
+ * no answer type. See docs/design/value-types.md (workspace).
112
+ */
113
+
114
+ /** What a condition compares with: a value -- or, `asText`, a text of one's own. */
115
+ interface Comparand {
116
+ value: string;
117
+ asText?: boolean;
118
+ }
119
+ /** What a value type's settings and answers are read and written with. */
120
+ interface ValueTypeContext {
121
+ settings: Record<string, unknown>;
122
+ /** The code system the question keeps (`x-fhir-system`), when it names one. */
123
+ system?: string;
124
+ }
125
+ /**
126
+ * What a value type writes on its item: extensions, and elements of the item
127
+ * (`answerValueSet`) -- and, written without them, what of its settings FHIR
128
+ * cannot say, reported.
129
+ */
130
+ interface ItemParts {
131
+ [element: string]: unknown;
132
+ extension?: Extension[];
133
+ answerValueSet?: string;
134
+ notConverted?: string[];
135
+ }
136
+ /** A value type contributed to the questionnaire conversions. */
137
+ interface FhirValueType {
138
+ /** The answer type it is in the questionnaire designer. */
139
+ answer: ValueTypeAnswer;
140
+ /** The item type it is written as, the same in every version; or `spell` it. */
141
+ itemType?: string;
142
+ /**
143
+ * How a version spells its item -- for a type whose item type differs by
144
+ * version, or when it takes a text of one's own (`orText`: a coded value's
145
+ * open list, R4 `open-choice`, R5 `optionsOrString`). A type that spells its
146
+ * item reads only the items it `claims`.
147
+ */
148
+ spell?(spelling: Spelling, orText: boolean): ItemSpelling;
149
+ /**
150
+ * Whether an item is its own -- needed when it shares the item type with a
151
+ * built-in answer type (a decimal with a currency); default: every item of
152
+ * its `itemType`. An item it does not claim is read as the built-in type.
153
+ */
154
+ claims?(item: QuestionnaireItem, spelling: Spelling): boolean;
155
+ /**
156
+ * Whether it takes several answers -- an array of the type, a repeating
157
+ * item, one answer each; default: one answer, a repeating item kept whole.
158
+ */
159
+ repeats?: boolean;
160
+ /** Its settings as the item's extensions and elements -- and, in `notConverted`, those FHIR cannot say. */
161
+ toItem(context: ValueTypeContext): ItemParts;
162
+ /** Its settings read from an item, with the extensions and elements read; why not, when they cannot be. */
163
+ fromItem(item: QuestionnaireItem, spelling: Spelling): {
164
+ settings: Record<string, unknown>;
165
+ system?: string;
166
+ extensions: Extension[];
167
+ /** The item's elements it read (`answerValueSet`), so they are not kept a second time. */
168
+ elements?: string[];
169
+ } | {
170
+ why: string;
171
+ };
172
+ /** A show-if's comparand -- a value, or a text of one's own -- as an enableWhen answer; default: `answerString`. */
173
+ toEnableAnswer?(comparand: Comparand, context: ValueTypeContext): Partial<QuestionnaireItemEnableWhen>;
174
+ /** An enableWhen answer as a show-if's comparand; why not, when it cannot say it. */
175
+ fromEnableAnswer?(e: QuestionnaireItemEnableWhen, context: ValueTypeContext): Comparand | {
176
+ why: string;
177
+ };
178
+ toAnswer(value: unknown, context: ValueTypeContext): QuestionnaireResponseItemAnswer;
179
+ fromAnswer(answer: QuestionnaireResponseItemAnswer, context: ValueTypeContext): unknown;
180
+ }
181
+ /** The options of the questionnaire conversions: the version, and the value types they know. */
182
+ type QuestionnaireConversionOptions = FhirConversionOptions & {
183
+ /** The value types contributed; default: the core types. */
184
+ valueTypes?: readonly FhirValueType[];
185
+ };
186
+ /** The quantity: item type `quantity`, its units as unit options, its answers `valueQuantity`. */
187
+ declare const QUANTITY_FHIR: FhirValueType;
188
+ /** The value types sonnet contributes: the core types. */
189
+ declare const DEFAULT_FHIR_VALUE_TYPES: readonly FhirValueType[];
190
+ /**
191
+ * The item a value type is written as, in a version; `orText`: it also takes a
192
+ * text of one's own; `multiple`: several answers, a repeating item.
193
+ */
194
+ declare function itemOf(type: FhirValueType, spelling: Spelling, orText: boolean, multiple: boolean): ItemSpelling;
195
+ /** Whether an item is a value type's: its own claim, else its item type. */
196
+ declare function claimsItem(type: FhirValueType, item: QuestionnaireItem, spelling: Spelling): boolean;
197
+ /** The value types a conversion knows: its options', else the core types. */
198
+ declare function valueTypesOf(options: QuestionnaireConversionOptions): readonly FhirValueType[];
199
+ /** The answer types those value types are, for the questionnaire designer's classification. */
200
+ declare function answerTypesOf(types: readonly FhirValueType[]): readonly ValueTypeAnswer[];
201
+ /** The value type of an answer type, when it is one. */
202
+ declare function fhirValueType(answerType: string, types: readonly FhirValueType[]): FhirValueType | undefined;
203
+
204
+ /**
205
+ * The coded value in FHIR: a question whose answers come from a value set --
206
+ * R4 `choice` with `answerValueSet`, R5 `coding`; one that also takes a text of
207
+ * one's own R4 `open-choice`, R5 `optionsOrString`. Its answers are
208
+ * `valueCoding`, a text of one's own `valueString`; a show-if on it compares a
209
+ * coding's system and code, as FHIR's `answerCoding` does. See
210
+ * docs/design/coded-values.md (workspace).
211
+ */
212
+
213
+ /** The coded value: a question on a value set, its answers codings -- or texts of one's own. */
214
+ declare const CODING_FHIR: FhirValueType;
215
+
216
+ /**
217
+ * The codeable concept in a FHIR questionnaire. FHIR has no codeable-concept
218
+ * answer -- an answer is one `Coding`, R4 and R5 alike -- so a codeable concept
219
+ * question is written as a coded value's (`choice` with its first list as
220
+ * `answerValueSet`, R5 `coding`), its answer its first coding, or its text
221
+ * alone as `valueString`. Its further lists and the text beside its codes are
222
+ * ours, more than FHIR says: reported, not owed. A questionnaire read from FHIR
223
+ * has no codeable concept question, so none is ever read as one. In a resource
224
+ * a codeable concept is a `CodeableConcept` (`@petrarca/sonnet-fhir/datatypes`).
225
+ * See docs/design/codeable-concepts.md (workspace).
226
+ */
227
+
228
+ /** The codeable concept: written as a coded value's question, its answer its first coding. */
229
+ declare const CODEABLE_CONCEPT_FHIR: FhirValueType;
230
+
231
+ /**
232
+ * A questionnaire to FHIR R4 -- the schema, read by classifyQuestionnaire, as a
233
+ * `Questionnaire`. Pure, no FHIR library. The `x-fhir-*` annotations are
234
+ * written back as the elements they keep. What does not convert -- a part the
235
+ * questionnaire designer does not understand, a keyword beyond an answer type
236
+ * -- is reported in `notConverted`, never dropped silently. See
237
+ * docs/design/questionnaire-conversion.md.
238
+ */
239
+
240
+ /** The questionnaire as a FHIR R4 `Questionnaire` -- a draft, unless it was read with a status; what did not convert, reported. */
241
+ declare function toFhirQuestionnaire(model: DesignerModel, options: QuestionnaireConversionOptions): FhirConversion<Questionnaire>;
242
+
243
+ /**
244
+ * Answers to FHIR R4 -- one set of a questionnaire's answers as a
245
+ * `QuestionnaireResponse`. Pure, no FHIR library. See
246
+ * docs/design/questionnaire-conversion.md.
247
+ */
248
+
249
+ type Answers$1 = Record<string, unknown>;
250
+ /** One set of answers as a FHIR R4 `QuestionnaireResponse` to the questionnaire. */
251
+ /**
252
+ * A response's own elements, beside its answers -- `id`, `subject`, `author`,
253
+ * `authored`, `meta`, its extensions, ... -- as read (fromFhirQuestionnaireResponse)
254
+ * and written back.
255
+ */
256
+ interface ResponseElements {
257
+ [element: string]: unknown;
258
+ questionnaire?: string;
259
+ status?: QuestionnaireResponse["status"];
260
+ }
261
+ declare function toFhirQuestionnaireResponse(model: DesignerModel, data: Answers$1, options: QuestionnaireConversionOptions & {
262
+ status?: QuestionnaireResponse["status"];
263
+ /** The response's own elements, as read: written back beside the answers. */
264
+ elements?: ResponseElements;
265
+ }): QuestionnaireResponse;
266
+
267
+ /**
268
+ * A FHIR R4 `Questionnaire` as a questionnaire on JSON Schema -- built with
269
+ * the questionnaire designer's own edits, so it reads as one authored there.
270
+ * Pure, no FHIR library. What JSON Schema does not say is kept in `x-fhir-*`
271
+ * annotations; an item of no answer type is kept whole -- shown locked, written
272
+ * back as it came. What is kept but does not work as in FHIR is reported in
273
+ * `notConverted`. See docs/design/questionnaire-conversion.md.
274
+ */
275
+
276
+ /** A FHIR R4 `Questionnaire` as a questionnaire on JSON Schema; what does not work as in FHIR, reported. */
277
+ declare function fromFhirQuestionnaire(questionnaire: Questionnaire, options: QuestionnaireConversionOptions): {
278
+ model: DesignerModel;
279
+ notConverted: NotConverted[];
280
+ };
281
+
282
+ /**
283
+ * A FHIR R4 `QuestionnaireResponse` as one set of a questionnaire's answers --
284
+ * the reverse of toFhirQuestionnaireResponse. Pure, no FHIR library. An answer
285
+ * to no question of the questionnaire, or to one of no answer type, is reported
286
+ * in `notConverted`. See docs/design/questionnaire-conversion.md.
287
+ */
288
+
289
+ type Answers = Record<string, unknown>;
290
+ /** A FHIR R4 `QuestionnaireResponse` as the answers to the questionnaire; what has no place, reported. */
291
+ declare function fromFhirQuestionnaireResponse(model: DesignerModel, response: QuestionnaireResponse, options: QuestionnaireConversionOptions): {
292
+ data: Answers;
293
+ /** The response's own elements -- id, subject, author, authored, ... -- for writing it back. */
294
+ elements: ResponseElements;
295
+ notConverted: NotConverted[];
296
+ };
297
+
298
+ export { CODEABLE_CONCEPT_FHIR, CODING_FHIR, DATA_ABSENT_REASON, DEFAULT_FHIR_VALUE_TYPES, ENTRY_FORMAT, FHIR_TYPE_INDEPENDENT_ANNOTATIONS, ITEM_CONTROL, ITEM_CONTROL_SYSTEM, MAX_OCCURS, MAX_VALUE, MIN_LENGTH, MIN_OCCURS, MIN_VALUE, OTHER_OPTION_EXTENSION, QUANTITY_FHIR, REGEX, UNIT_OPTION, X_FHIR, X_FHIR_EXTENSION, X_FHIR_HELP_LINK_ID, X_FHIR_ITEM, X_FHIR_LEAD_QUESTION, X_FHIR_LINK_ID, X_FHIR_SYSTEM, answerTypesOf, claimsItem, fhirValueType, fromFhirQuestionnaire, fromFhirQuestionnaireResponse, isFhirAnnotation, itemOf, toFhirQuestionnaire, toFhirQuestionnaireResponse, valueTypesOf };
299
+ export type { Comparand, FhirConversion, FhirValueType, ItemParts, NotConverted, QuestionnaireConversionOptions, ResponseElements, ValueTypeContext };