@photonhealth/client 0.0.0-stage → 2.0.0-rc.1

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.
Files changed (178) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +89 -2
  3. package/dist/api/changes/applyOption.d.ts +41 -0
  4. package/dist/api/changes/applyOption.d.ts.map +1 -0
  5. package/dist/api/changes/applyOption.js +76 -0
  6. package/dist/api/changes/applyOption.js.map +1 -0
  7. package/dist/api/changes/classifyChanges.d.ts +9 -0
  8. package/dist/api/changes/classifyChanges.d.ts.map +1 -0
  9. package/dist/api/changes/classifyChanges.js +2 -0
  10. package/dist/api/changes/classifyChanges.js.map +1 -0
  11. package/dist/api/changes/index.d.ts +3 -0
  12. package/dist/api/changes/index.d.ts.map +1 -0
  13. package/dist/api/changes/index.js +3 -0
  14. package/dist/api/changes/index.js.map +1 -0
  15. package/dist/api/client.d.ts +43 -0
  16. package/dist/api/client.d.ts.map +1 -0
  17. package/dist/api/client.js +77 -0
  18. package/dist/api/client.js.map +1 -0
  19. package/dist/api/health.d.ts +4 -0
  20. package/dist/api/health.d.ts.map +1 -0
  21. package/dist/api/health.js +6 -0
  22. package/dist/api/health.js.map +1 -0
  23. package/dist/api/index.d.ts +22 -0
  24. package/dist/api/index.d.ts.map +1 -0
  25. package/dist/api/index.js +21 -0
  26. package/dist/api/index.js.map +1 -0
  27. package/dist/api/order.d.ts +33 -0
  28. package/dist/api/order.d.ts.map +1 -0
  29. package/dist/api/order.js +33 -0
  30. package/dist/api/order.js.map +1 -0
  31. package/dist/api/patient.d.ts +33 -0
  32. package/dist/api/patient.d.ts.map +1 -0
  33. package/dist/api/patient.js +33 -0
  34. package/dist/api/patient.js.map +1 -0
  35. package/dist/api/prescription.d.ts +31 -0
  36. package/dist/api/prescription.d.ts.map +1 -0
  37. package/dist/api/prescription.js +30 -0
  38. package/dist/api/prescription.js.map +1 -0
  39. package/dist/api/results.d.ts +30 -0
  40. package/dist/api/results.d.ts.map +1 -0
  41. package/dist/api/results.js +26 -0
  42. package/dist/api/results.js.map +1 -0
  43. package/dist/gql/gql.d.ts +21 -0
  44. package/dist/gql/gql.d.ts.map +1 -0
  45. package/dist/gql/gql.js +13 -0
  46. package/dist/gql/gql.js.map +1 -0
  47. package/dist/gql/graphql.d.ts +9674 -0
  48. package/dist/gql/graphql.d.ts.map +1 -0
  49. package/dist/gql/graphql.js +4030 -0
  50. package/dist/gql/graphql.js.map +1 -0
  51. package/dist/gql/index.d.ts +2 -0
  52. package/dist/gql/index.d.ts.map +1 -0
  53. package/dist/gql/index.js +3 -0
  54. package/dist/gql/index.js.map +1 -0
  55. package/dist/index.d.ts +15 -0
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +15 -0
  58. package/dist/index.js.map +1 -0
  59. package/dist/photon/auth.d.ts +48 -0
  60. package/dist/photon/auth.d.ts.map +1 -0
  61. package/dist/photon/auth.js +34 -0
  62. package/dist/photon/auth.js.map +1 -0
  63. package/dist/photon/choices.d.ts +44 -0
  64. package/dist/photon/choices.d.ts.map +1 -0
  65. package/dist/photon/choices.js +58 -0
  66. package/dist/photon/choices.js.map +1 -0
  67. package/dist/photon/context.d.ts +29 -0
  68. package/dist/photon/context.d.ts.map +1 -0
  69. package/dist/photon/context.js +2 -0
  70. package/dist/photon/context.js.map +1 -0
  71. package/dist/photon/environments.d.ts +26 -0
  72. package/dist/photon/environments.d.ts.map +1 -0
  73. package/dist/photon/environments.js +26 -0
  74. package/dist/photon/environments.js.map +1 -0
  75. package/dist/photon/errors.d.ts +26 -0
  76. package/dist/photon/errors.d.ts.map +1 -0
  77. package/dist/photon/errors.js +42 -0
  78. package/dist/photon/errors.js.map +1 -0
  79. package/dist/photon/index.d.ts +23 -0
  80. package/dist/photon/index.d.ts.map +1 -0
  81. package/dist/photon/index.js +11 -0
  82. package/dist/photon/index.js.map +1 -0
  83. package/dist/photon/inputs.d.ts +7 -0
  84. package/dist/photon/inputs.d.ts.map +1 -0
  85. package/dist/photon/inputs.js +25 -0
  86. package/dist/photon/inputs.js.map +1 -0
  87. package/dist/photon/links.d.ts +68 -0
  88. package/dist/photon/links.d.ts.map +1 -0
  89. package/dist/photon/links.js +187 -0
  90. package/dist/photon/links.js.map +1 -0
  91. package/dist/photon/live.d.ts +62 -0
  92. package/dist/photon/live.d.ts.map +1 -0
  93. package/dist/photon/live.js +107 -0
  94. package/dist/photon/live.js.map +1 -0
  95. package/dist/photon/map.d.ts +10 -0
  96. package/dist/photon/map.d.ts.map +1 -0
  97. package/dist/photon/map.js +211 -0
  98. package/dist/photon/map.js.map +1 -0
  99. package/dist/photon/order.d.ts +148 -0
  100. package/dist/photon/order.d.ts.map +1 -0
  101. package/dist/photon/order.js +491 -0
  102. package/dist/photon/order.js.map +1 -0
  103. package/dist/photon/patient.d.ts +78 -0
  104. package/dist/photon/patient.d.ts.map +1 -0
  105. package/dist/photon/patient.js +316 -0
  106. package/dist/photon/patient.js.map +1 -0
  107. package/dist/photon/photon.d.ts +150 -0
  108. package/dist/photon/photon.d.ts.map +1 -0
  109. package/dist/photon/photon.js +315 -0
  110. package/dist/photon/photon.js.map +1 -0
  111. package/dist/photon/prescription.d.ts +132 -0
  112. package/dist/photon/prescription.d.ts.map +1 -0
  113. package/dist/photon/prescription.js +381 -0
  114. package/dist/photon/prescription.js.map +1 -0
  115. package/dist/photon/session.d.ts +84 -0
  116. package/dist/photon/session.d.ts.map +1 -0
  117. package/dist/photon/session.js +350 -0
  118. package/dist/photon/session.js.map +1 -0
  119. package/dist/photon/types.d.ts +411 -0
  120. package/dist/photon/types.d.ts.map +1 -0
  121. package/dist/photon/types.js +7 -0
  122. package/dist/photon/types.js.map +1 -0
  123. package/dist/test-utils/msw.d.ts +23 -0
  124. package/dist/test-utils/msw.d.ts.map +1 -0
  125. package/dist/test-utils/msw.js +51 -0
  126. package/dist/test-utils/msw.js.map +1 -0
  127. package/dist/testing/fixtures.d.ts +51 -0
  128. package/dist/testing/fixtures.d.ts.map +1 -0
  129. package/dist/testing/fixtures.js +166 -0
  130. package/dist/testing/fixtures.js.map +1 -0
  131. package/dist/testing/scriptedClient.d.ts +38 -0
  132. package/dist/testing/scriptedClient.d.ts.map +1 -0
  133. package/dist/testing/scriptedClient.js +52 -0
  134. package/dist/testing/scriptedClient.js.map +1 -0
  135. package/dist/testing.d.ts +11 -0
  136. package/dist/testing.d.ts.map +1 -0
  137. package/dist/testing.js +10 -0
  138. package/dist/testing.js.map +1 -0
  139. package/package.json +67 -4
  140. package/src/api/changes/applyOption.ts +103 -0
  141. package/src/api/changes/classifyChanges.ts +10 -0
  142. package/src/api/changes/index.ts +2 -0
  143. package/src/api/client.ts +124 -0
  144. package/src/api/health.ts +7 -0
  145. package/src/api/index.ts +22 -0
  146. package/src/api/order.ts +64 -0
  147. package/src/api/patient.ts +58 -0
  148. package/src/api/prescription.ts +68 -0
  149. package/src/api/results.ts +45 -0
  150. package/src/gql/gql.ts +56 -0
  151. package/src/gql/graphql.ts +5344 -0
  152. package/src/gql/index.ts +2 -0
  153. package/src/index.ts +14 -0
  154. package/src/operations/fragments.graphql +351 -0
  155. package/src/operations/health.graphql +4 -0
  156. package/src/operations/order.graphql +42 -0
  157. package/src/operations/patient.graphql +36 -0
  158. package/src/operations/prescription.graphql +70 -0
  159. package/src/photon/auth.ts +70 -0
  160. package/src/photon/choices.ts +79 -0
  161. package/src/photon/context.ts +30 -0
  162. package/src/photon/environments.ts +47 -0
  163. package/src/photon/errors.ts +56 -0
  164. package/src/photon/index.ts +35 -0
  165. package/src/photon/inputs.ts +24 -0
  166. package/src/photon/links.ts +197 -0
  167. package/src/photon/live.ts +135 -0
  168. package/src/photon/map.ts +255 -0
  169. package/src/photon/order.ts +581 -0
  170. package/src/photon/patient.ts +346 -0
  171. package/src/photon/photon.ts +513 -0
  172. package/src/photon/prescription.ts +447 -0
  173. package/src/photon/session.ts +445 -0
  174. package/src/photon/types.ts +329 -0
  175. package/src/test-utils/msw.ts +61 -0
  176. package/src/testing/fixtures.ts +191 -0
  177. package/src/testing/scriptedClient.ts +52 -0
  178. package/src/testing.ts +22 -0
@@ -0,0 +1,255 @@
1
+ import type {
2
+ ChangeFieldsFragment,
3
+ ListDocumentationStatus,
4
+ OrderCandidateFieldsFragment,
5
+ OrderFieldsFragment,
6
+ PatientFieldsFragment,
7
+ PharmacyFieldsFragment,
8
+ PharmacyOfferFieldsFragment,
9
+ PrescriptionFieldsFragment,
10
+ ScreeningAlertFieldsFragment,
11
+ TreatmentFieldsFragment,
12
+ } from "../gql/graphql.ts";
13
+ import { isInlineOption, optionPath, parseOptionValue, type ChangeSet } from "../api/index.ts";
14
+ import type {
15
+ Change,
16
+ ChangeOption,
17
+ Changes,
18
+ OrderCandidate,
19
+ OrderRecord,
20
+ PatientRecord,
21
+ Pharmacy,
22
+ PharmacyOffer,
23
+ PrescriptionRecord,
24
+ ScreeningAlert,
25
+ Treatment,
26
+ } from "./types.ts";
27
+
28
+ /**
29
+ * Generated GraphQL shapes → core's records. The one place the two meet: every field is picked by name,
30
+ * so a schema change stops here (and typecheck says where).
31
+ */
32
+
33
+ const offer = (o: PharmacyOfferFieldsFragment): PharmacyOffer => ({
34
+ source: o.source,
35
+ isPromoted: o.isPromoted,
36
+ attributeTags: o.attributeTags.map(({ kind, label }) => ({ kind, label })),
37
+ priceSummary: o.priceSummary && {
38
+ priceType: o.priceSummary.priceType,
39
+ totalAmount: o.priceSummary.totalAmount,
40
+ totalRetailAmount: o.priceSummary.totalRetailAmount,
41
+ totalSavings: o.priceSummary.totalSavings,
42
+ },
43
+ prescriptionDeliveryEstimates: o.prescriptionDeliveryEstimates.map((d) => ({
44
+ prescriptionId: d.prescriptionId,
45
+ deliveryPromise: d.deliveryPromise,
46
+ deliveryPromiseRangeStart: d.deliveryPromiseRangeStart,
47
+ deliveryPromiseRangeEnd: d.deliveryPromiseRangeEnd,
48
+ })),
49
+ });
50
+
51
+ const pharmacy = (p: PharmacyFieldsFragment | null): Pharmacy | null =>
52
+ p && {
53
+ id: p.id,
54
+ name: p.name,
55
+ address: p.address && {
56
+ street1: p.address.street1,
57
+ street2: p.address.street2,
58
+ city: p.address.city,
59
+ state: p.address.state,
60
+ postalCode: p.address.postalCode,
61
+ },
62
+ latitude: p.latitude,
63
+ longitude: p.longitude,
64
+ fulfillmentType: p.fulfillmentType,
65
+ offers: (p.offers ?? []).map(offer),
66
+ };
67
+
68
+ /**
69
+ * A patient's list: null when not recorded, [] when "none" is. A list without a status (medications, diagnoses,
70
+ * benefits until network-api stores their "none") reads empty as not recorded.
71
+ */
72
+ const recorded = <T>({ status, entries }: { status?: ListDocumentationStatus | null; entries: T[] }): T[] | null =>
73
+ status === "NO_KNOWN_CONFIRMED" ? [] : entries.length ? entries : null;
74
+
75
+ export function toPatient(p: PatientFieldsFragment): PatientRecord {
76
+ const d = p.demographic;
77
+ const c = p.clinical;
78
+ return {
79
+ id: p.id,
80
+ demographic: d && {
81
+ name: { first: d.name.first, middle: d.name.middle, last: d.name.last },
82
+ dateOfBirth: d.dateOfBirth,
83
+ sex: d.sex,
84
+ gender: d.gender,
85
+ email: d.email,
86
+ phone: d.phone,
87
+ address: d.address && {
88
+ street1: d.address.street1,
89
+ street2: d.address.street2,
90
+ city: d.address.city,
91
+ state: d.address.state,
92
+ postalCode: d.address.postalCode,
93
+ country: d.address.country,
94
+ },
95
+ benefits:
96
+ recorded(d.benefits)?.map((b) => ({
97
+ id: b.id,
98
+ bin: b.bin,
99
+ pcn: b.pcn,
100
+ groupId: b.groupId,
101
+ memberId: b.memberId,
102
+ })) ?? null,
103
+ preferredPharmacy: pharmacy(d.preferredPharmacy),
104
+ },
105
+ clinical: c && {
106
+ allergies: recorded(c.allergies)?.map((a) => ({ id: a.id, name: a.name, rxNormId: a.rxNormId })) ?? null,
107
+ medications:
108
+ recorded(c.medications)?.map((m) => ({ id: m.id, name: m.name, rxNormId: m.rxNormId, status: m.status })) ??
109
+ null,
110
+ diagnoses: recorded(c.diagnoses)?.map((x) => ({ id: x.id, name: x.name, icd10Code: x.icd10Code })) ?? null,
111
+ notes: c.notes,
112
+ },
113
+ };
114
+ }
115
+
116
+ const toScreeningAlert = (a: ScreeningAlertFieldsFragment): ScreeningAlert => ({
117
+ severity: a.severity,
118
+ type: a.type,
119
+ description: a.description,
120
+ involvedEntities: a.involvedEntities.map((e) => ({ id: e.id, name: e.name })),
121
+ });
122
+
123
+ const toTreatment = (t: TreatmentFieldsFragment): Treatment => ({
124
+ id: String(t.id),
125
+ name: t.name,
126
+ prescribableName: t.prescribableName,
127
+ rxNormId: t.rxNormId,
128
+ ndc: t.ndc,
129
+ upc: t.upc,
130
+ entityKind: t.entityKind,
131
+ brandName: t.brandName,
132
+ genericName: t.genericName,
133
+ form: t.form,
134
+ route: t.route,
135
+ strength: t.strength,
136
+ medicationType: t.medicationType,
137
+ schedule: t.schedule,
138
+ recommendedDispenseUnits: t.recommendedDispenseUnits ? [...t.recommendedDispenseUnits] : null,
139
+ });
140
+
141
+ export function toPrescription(rx: PrescriptionFieldsFragment): PrescriptionRecord {
142
+ return {
143
+ id: rx.id,
144
+ templateId: rx.templateId,
145
+ patient: rx.patient && { id: rx.patient.id },
146
+ treatment: toTreatment(rx.treatment),
147
+ instructions: rx.instructions,
148
+ dispense: rx.dispense && {
149
+ quantity: rx.dispense.quantity,
150
+ unit: rx.dispense.unit,
151
+ daysSupply: rx.dispense.daysSupply,
152
+ refillsAllowed: rx.dispense.refillsAllowed,
153
+ dispenseAsWritten: rx.dispense.dispenseAsWritten,
154
+ },
155
+ clinical: rx.clinical && {
156
+ diagnoses: rx.clinical.diagnoses.map((x) => ({ id: x.id, name: x.name, icd10Code: x.icd10Code })),
157
+ notes: rx.clinical.notes,
158
+ },
159
+ screeningAlerts: (rx.screeningAlerts ?? []).map(toScreeningAlert),
160
+ signing: {
161
+ state: rx.signing.state,
162
+ contentHash: rx.signing.contentHash,
163
+ message: rx.signing.message,
164
+ signedAt: rx.signing.signedAt,
165
+ },
166
+ };
167
+ }
168
+
169
+ export function toOrder(o: OrderFieldsFragment): OrderRecord {
170
+ return {
171
+ id: o.id,
172
+ state: o.state,
173
+ patient: o.patient?.__typename === "Patient" ? toPatient(o.patient) : null,
174
+ prescriptions: o.prescriptions.map(toPrescription),
175
+ screeningAlerts: (o.screeningAlerts ?? []).map(toScreeningAlert),
176
+ pharmacy: pharmacy(o.pharmacy),
177
+ exceptions: o.exceptions.map((x) => ({ id: x.id, type: x.type, message: x.message, createdAt: x.createdAt })),
178
+ };
179
+ }
180
+
181
+ export function toOrderCandidate(c: OrderCandidateFieldsFragment): OrderCandidate {
182
+ return { id: c.id, pharmacy: pharmacy(c.pharmacy), prescriptions: c.prescriptions.map(toPrescription) };
183
+ }
184
+
185
+ /** The record an option picks: an object holding its id (`{"id":…}`), or an id at a `….id` path. */
186
+ function pickedId(path: string | null, value: unknown): string | null {
187
+ if (value && typeof value === "object" && typeof (value as { id?: unknown }).id === "string")
188
+ return (value as { id: string }).id;
189
+ return path?.split(".").at(-1) === "id" && typeof value === "string" && value ? value : null;
190
+ }
191
+
192
+ type Choice = NonNullable<ChangeFieldsFragment["after"]>;
193
+
194
+ const choicePath = (o: Choice) => (isInlineOption(o) ? optionPath(o) : null);
195
+
196
+ const choice = (o: Choice): ChangeOption => {
197
+ const path = choicePath(o);
198
+ const value = parseOptionValue(o.value);
199
+ return {
200
+ displayName: o.displayName,
201
+ reason: o.reason,
202
+ path,
203
+ value,
204
+ id: pickedId(path, value),
205
+ treatment: o.entity?.__typename === "Treatment" ? toTreatment(o.entity) : null,
206
+ pharmacy: o.entity?.__typename === "Pharmacy" ? pharmacy(o.entity) : null,
207
+ operation: o.operation,
208
+ matchReasons: (o.matchReasons ?? []).map((r) => ({
209
+ type: r.type,
210
+ label: r.label,
211
+ confidence: r.confidence ?? null,
212
+ })),
213
+ };
214
+ };
215
+
216
+ const change = (c: ChangeFieldsFragment, sent: unknown): Change => {
217
+ const after = c.after ? choice(c.after) : null;
218
+ return {
219
+ key: c.key,
220
+ label: c.label,
221
+ status: c.status,
222
+ severity: c.severity,
223
+ reason: c.reason,
224
+ category: c.category,
225
+ operation: c.operation,
226
+ options: (c.options ?? []).map(choice),
227
+ inputText: inputTextOf(c, sent),
228
+ resolvedId: c.status === "APPLIED" ? (after?.id ?? after?.treatment?.id ?? after?.pharmacy?.id ?? null) : null,
229
+ };
230
+ };
231
+
232
+ /**
233
+ * The text the server tried to resolve, read back from what was sent: network-api doesn't echo it, but each
234
+ * choice's path says where it went (`treatment.id` → the `treatment` sent: `{ name: "lisinopril" }`).
235
+ */
236
+ function inputTextOf(c: ChangeFieldsFragment, sent: unknown): string | null {
237
+ const choices = [...(c.options ?? []), c.after, c.before].filter((o): o is Choice => Boolean(o));
238
+ for (const path of choices.map(choicePath)) {
239
+ const segments = path?.split(".") ?? [];
240
+ if (segments.at(-1) === "id") segments.pop();
241
+ const at = segments.reduce<unknown>((node, key) => (node as Record<string, unknown> | null)?.[key], sent);
242
+ if (typeof at === "string") return at;
243
+ const reference = (at ?? {}) as { name?: unknown; rxNormId?: unknown; icd10Code?: unknown };
244
+ const text = reference.name ?? reference.rxNormId ?? reference.icd10Code;
245
+ if (typeof text === "string") return text;
246
+ }
247
+ return null;
248
+ }
249
+
250
+ export const toChanges = (set: ChangeSet | null | undefined): Changes => {
251
+ const all = (set?.changes ?? []).map((c) => change(c, set?.sent));
252
+ return { applied: all.filter((c) => c.status === "APPLIED"), unapplied: all.filter((c) => c.status !== "APPLIED") };
253
+ };
254
+
255
+ export const noChanges: Changes = { applied: [], unapplied: [] };