@oystehr/sdk 4.0.0-alpha.6 → 4.0.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.
Files changed (158) hide show
  1. package/dist/cjs/client/client.cjs +259 -0
  2. package/dist/cjs/client/client.cjs.map +1 -0
  3. package/dist/cjs/errors/index.cjs +58 -0
  4. package/dist/cjs/errors/index.cjs.map +1 -0
  5. package/dist/cjs/index.cjs +11 -1980
  6. package/dist/cjs/index.cjs.map +1 -1
  7. package/dist/cjs/index.min.cjs +1 -1
  8. package/dist/cjs/index.min.cjs.map +1 -1
  9. package/dist/cjs/resources/classes/application.cjs +88 -0
  10. package/dist/cjs/resources/classes/application.cjs.map +1 -0
  11. package/dist/cjs/resources/classes/charge.cjs +44 -0
  12. package/dist/cjs/resources/classes/charge.cjs.map +1 -0
  13. package/dist/cjs/resources/classes/conversation-ext.cjs +31 -0
  14. package/dist/cjs/resources/classes/conversation-ext.cjs.map +1 -0
  15. package/dist/cjs/resources/classes/conversation.cjs +84 -0
  16. package/dist/cjs/resources/classes/conversation.cjs.map +1 -0
  17. package/dist/cjs/resources/classes/developer.cjs +72 -0
  18. package/dist/cjs/resources/classes/developer.cjs.map +1 -0
  19. package/dist/cjs/resources/classes/erx.cjs +88 -0
  20. package/dist/cjs/resources/classes/erx.cjs.map +1 -0
  21. package/dist/cjs/resources/classes/erx.d.ts +5 -1
  22. package/dist/cjs/resources/classes/erxV1.cjs +46 -0
  23. package/dist/cjs/resources/classes/erxV1.cjs.map +1 -0
  24. package/dist/cjs/resources/classes/erxV2.cjs +22 -0
  25. package/dist/cjs/resources/classes/erxV2.cjs.map +1 -0
  26. package/dist/cjs/resources/classes/fax.cjs +46 -0
  27. package/dist/cjs/resources/classes/fax.cjs.map +1 -0
  28. package/dist/cjs/resources/classes/fhir-ext.cjs +200 -0
  29. package/dist/cjs/resources/classes/fhir-ext.cjs.map +1 -0
  30. package/dist/cjs/resources/classes/fhir-ext.d.ts +7 -7
  31. package/dist/cjs/resources/classes/fhir.cjs +34 -0
  32. package/dist/cjs/resources/classes/fhir.cjs.map +1 -0
  33. package/dist/cjs/resources/classes/fhir.d.ts +2 -2
  34. package/dist/cjs/resources/classes/index.cjs +88 -0
  35. package/dist/cjs/resources/classes/index.cjs.map +1 -0
  36. package/dist/cjs/resources/classes/lab.cjs +76 -0
  37. package/dist/cjs/resources/classes/lab.cjs.map +1 -0
  38. package/dist/cjs/resources/classes/m2m.cjs +81 -0
  39. package/dist/cjs/resources/classes/m2m.cjs.map +1 -0
  40. package/dist/cjs/resources/classes/messaging.cjs +22 -0
  41. package/dist/cjs/resources/classes/messaging.cjs.map +1 -0
  42. package/dist/cjs/resources/classes/paymentMethod.cjs +88 -0
  43. package/dist/cjs/resources/classes/paymentMethod.cjs.map +1 -0
  44. package/dist/cjs/resources/classes/project.cjs +34 -0
  45. package/dist/cjs/resources/classes/project.cjs.map +1 -0
  46. package/dist/cjs/resources/classes/rcm.cjs +55 -0
  47. package/dist/cjs/resources/classes/rcm.cjs.map +1 -0
  48. package/dist/cjs/resources/classes/role.cjs +62 -0
  49. package/dist/cjs/resources/classes/role.cjs.map +1 -0
  50. package/dist/cjs/resources/classes/secret.cjs +52 -0
  51. package/dist/cjs/resources/classes/secret.cjs.map +1 -0
  52. package/dist/cjs/resources/classes/telemed.cjs +43 -0
  53. package/dist/cjs/resources/classes/telemed.cjs.map +1 -0
  54. package/dist/cjs/resources/classes/transactionalSMS.cjs +34 -0
  55. package/dist/cjs/resources/classes/transactionalSMS.cjs.map +1 -0
  56. package/dist/cjs/resources/classes/user.cjs +80 -0
  57. package/dist/cjs/resources/classes/user.cjs.map +1 -0
  58. package/dist/cjs/resources/classes/version.cjs +22 -0
  59. package/dist/cjs/resources/classes/version.cjs.map +1 -0
  60. package/dist/cjs/resources/classes/z3-ext.cjs +81 -0
  61. package/dist/cjs/resources/classes/z3-ext.cjs.map +1 -0
  62. package/dist/cjs/resources/classes/z3.cjs +88 -0
  63. package/dist/cjs/resources/classes/z3.cjs.map +1 -0
  64. package/dist/cjs/resources/classes/zambda-ext.cjs +15 -0
  65. package/dist/cjs/resources/classes/zambda-ext.cjs.map +1 -0
  66. package/dist/cjs/resources/classes/zambda.cjs +89 -0
  67. package/dist/cjs/resources/classes/zambda.cjs.map +1 -0
  68. package/dist/cjs/resources/classes/zambdaLogStream.cjs +43 -0
  69. package/dist/cjs/resources/classes/zambdaLogStream.cjs.map +1 -0
  70. package/dist/cjs/resources/types/ErxGetConfigurationResponse.d.ts +9 -0
  71. package/dist/cjs/resources/types/ZambdaCreateParams.d.ts +5 -1
  72. package/dist/cjs/resources/types/ZambdaFunction.d.ts +1 -1
  73. package/dist/cjs/resources/types/ZambdaUpdateParams.d.ts +1 -1
  74. package/dist/cjs/resources/types/index.d.ts +1 -0
  75. package/dist/esm/client/client.js +68 -67
  76. package/dist/esm/client/client.js.map +1 -1
  77. package/dist/esm/errors/index.js +3 -2
  78. package/dist/esm/errors/index.js.map +1 -1
  79. package/dist/esm/index.js +2 -2
  80. package/dist/esm/index.js.map +1 -1
  81. package/dist/esm/index.min.js +1 -1
  82. package/dist/esm/index.min.js.map +1 -1
  83. package/dist/esm/node_modules/tslib/package.json +1 -0
  84. package/dist/esm/resources/classes/application.js +11 -15
  85. package/dist/esm/resources/classes/application.js.map +1 -1
  86. package/dist/esm/resources/classes/charge.js +5 -9
  87. package/dist/esm/resources/classes/charge.js.map +1 -1
  88. package/dist/esm/resources/classes/conversation-ext.js +4 -5
  89. package/dist/esm/resources/classes/conversation-ext.js.map +1 -1
  90. package/dist/esm/resources/classes/conversation.js +16 -20
  91. package/dist/esm/resources/classes/conversation.js.map +1 -1
  92. package/dist/esm/resources/classes/developer.js +9 -13
  93. package/dist/esm/resources/classes/developer.js.map +1 -1
  94. package/dist/esm/resources/classes/erx.d.ts +5 -1
  95. package/dist/esm/resources/classes/erx.js +20 -18
  96. package/dist/esm/resources/classes/erx.js.map +1 -1
  97. package/dist/esm/resources/classes/erxV1.js +8 -12
  98. package/dist/esm/resources/classes/erxV1.js.map +1 -1
  99. package/dist/esm/resources/classes/erxV2.js +4 -8
  100. package/dist/esm/resources/classes/erxV2.js.map +1 -1
  101. package/dist/esm/resources/classes/fax.js +6 -10
  102. package/dist/esm/resources/classes/fax.js.map +1 -1
  103. package/dist/esm/resources/classes/fhir-ext.d.ts +7 -7
  104. package/dist/esm/resources/classes/fhir-ext.js +71 -62
  105. package/dist/esm/resources/classes/fhir-ext.js.map +1 -1
  106. package/dist/esm/resources/classes/fhir.d.ts +2 -2
  107. package/dist/esm/resources/classes/fhir.js +20 -20
  108. package/dist/esm/resources/classes/fhir.js.map +1 -1
  109. package/dist/esm/resources/classes/index.js +28 -5
  110. package/dist/esm/resources/classes/index.js.map +1 -1
  111. package/dist/esm/resources/classes/lab.js +9 -13
  112. package/dist/esm/resources/classes/lab.js.map +1 -1
  113. package/dist/esm/resources/classes/m2m.js +10 -14
  114. package/dist/esm/resources/classes/m2m.js.map +1 -1
  115. package/dist/esm/resources/classes/messaging.js +4 -8
  116. package/dist/esm/resources/classes/messaging.js.map +1 -1
  117. package/dist/esm/resources/classes/paymentMethod.js +7 -11
  118. package/dist/esm/resources/classes/paymentMethod.js.map +1 -1
  119. package/dist/esm/resources/classes/project.js +5 -9
  120. package/dist/esm/resources/classes/project.js.map +1 -1
  121. package/dist/esm/resources/classes/rcm.js +6 -10
  122. package/dist/esm/resources/classes/rcm.js.map +1 -1
  123. package/dist/esm/resources/classes/role.js +8 -12
  124. package/dist/esm/resources/classes/role.js.map +1 -1
  125. package/dist/esm/resources/classes/secret.js +7 -11
  126. package/dist/esm/resources/classes/secret.js.map +1 -1
  127. package/dist/esm/resources/classes/telemed.js +5 -9
  128. package/dist/esm/resources/classes/telemed.js.map +1 -1
  129. package/dist/esm/resources/classes/transactionalSMS.js +4 -8
  130. package/dist/esm/resources/classes/transactionalSMS.js.map +1 -1
  131. package/dist/esm/resources/classes/user.js +10 -14
  132. package/dist/esm/resources/classes/user.js.map +1 -1
  133. package/dist/esm/resources/classes/version.js +4 -8
  134. package/dist/esm/resources/classes/version.js.map +1 -1
  135. package/dist/esm/resources/classes/z3-ext.js +47 -56
  136. package/dist/esm/resources/classes/z3-ext.js.map +1 -1
  137. package/dist/esm/resources/classes/z3.js +26 -30
  138. package/dist/esm/resources/classes/z3.js.map +1 -1
  139. package/dist/esm/resources/classes/zambda-ext.js +6 -11
  140. package/dist/esm/resources/classes/zambda-ext.js.map +1 -1
  141. package/dist/esm/resources/classes/zambda.js +12 -16
  142. package/dist/esm/resources/classes/zambda.js.map +1 -1
  143. package/dist/esm/resources/classes/zambdaLogStream.js +6 -10
  144. package/dist/esm/resources/classes/zambdaLogStream.js.map +1 -1
  145. package/dist/esm/resources/types/ErxGetConfigurationResponse.d.ts +9 -0
  146. package/dist/esm/resources/types/ZambdaCreateParams.d.ts +5 -1
  147. package/dist/esm/resources/types/ZambdaFunction.d.ts +1 -1
  148. package/dist/esm/resources/types/ZambdaUpdateParams.d.ts +1 -1
  149. package/dist/esm/resources/types/index.d.ts +1 -0
  150. package/package.json +4 -3
  151. package/rollup.config-cjs.mjs +97 -18
  152. package/src/resources/classes/erx.ts +7 -0
  153. package/src/resources/classes/fhir-ext.ts +5 -5
  154. package/src/resources/types/ErxGetConfigurationResponse.ts +11 -0
  155. package/src/resources/types/ZambdaCreateParams.ts +5 -1
  156. package/src/resources/types/ZambdaFunction.ts +1 -1
  157. package/src/resources/types/ZambdaUpdateParams.ts +1 -1
  158. package/src/resources/types/index.ts +1 -0
@@ -0,0 +1,259 @@
1
+ 'use strict';
2
+
3
+ var uuid = require('uuid');
4
+ var index = require('../errors/index.cjs');
5
+
6
+ const defaultProjectApiUrl = 'https://project-api.zapehr.com/v1';
7
+ const defaultFhirApiUrl = 'https://fhir-api.zapehr.com';
8
+ const STATUS_CODES_TO_RETRY = [408, 429, 500, 502, 503, 504];
9
+ const ERROR_CODES_TO_RETRY = [
10
+ 'ECONNRESET',
11
+ 'ECONNREFUSED',
12
+ 'EPIPE',
13
+ 'ETIMEDOUT',
14
+ 'UND_ERR_CONNECT_TIMEOUT',
15
+ 'UND_ERR_HEADERS_TIMEOUT',
16
+ 'UND_ERR_HEADERS_TIMEOUT',
17
+ 'UND_ERR_SOCKET',
18
+ ];
19
+ class SDKResource {
20
+ config;
21
+ constructor(config) {
22
+ this.config = config;
23
+ }
24
+ request(path, method, baseUrlThunk) {
25
+ return async (params, request) => {
26
+ const configThunk = () => this.config;
27
+ try {
28
+ return await fetcher(baseUrlThunk, configThunk, path, method)(params, request);
29
+ }
30
+ catch (err) {
31
+ const error = err;
32
+ throw new index.OystehrSdkError({ message: error.message, code: error.code, cause: error.cause });
33
+ }
34
+ };
35
+ }
36
+ fhirRequest(path, method) {
37
+ return async (params, request) => {
38
+ try {
39
+ const baseUrlThunk = () => this.config.services?.fhirApiUrl ?? defaultFhirApiUrl;
40
+ const configThunk = () => this.config;
41
+ // must await here to catch
42
+ return await fetcher(baseUrlThunk, configThunk, path, method)(params, request);
43
+ }
44
+ catch (err) {
45
+ // FHIR API error messages are JSON strings
46
+ const fullError = err;
47
+ if (typeof fullError.message === 'string') {
48
+ throw new index.OystehrSdkError({
49
+ message: fullError.message,
50
+ code: fullError.code,
51
+ cause: fullError.cause,
52
+ });
53
+ }
54
+ throw new index.OystehrFHIRError({
55
+ error: fullError.message,
56
+ code: fullError.code,
57
+ });
58
+ }
59
+ };
60
+ }
61
+ }
62
+ function isInternalClientRequest(request) {
63
+ return 'accessToken' in request;
64
+ }
65
+ function fetcher(baseUrlThunk, configThunk, path, methodParam) {
66
+ return async (params, request) => {
67
+ // this function supports multiple signatures. fetcher(baseUrl, path, method)(params, request) or fetcher(baseUrl, path, method)(request)
68
+ // or fetcher(baseUrl, path, method)(params) or fetcher(baseUrl, path, method)(). the types for this are handled by Client<Path, Methods>
69
+ // and this is the backend implementation behind it. the heuristic we're using is that if the first param is an object with an accessToken
70
+ // and there is no second param, assume the first one is the request object instead
71
+ const providedParams = !!params && !request && !Array.isArray(params) && isInternalClientRequest(params)
72
+ ? {}
73
+ : params ?? {};
74
+ const requestCtx = !!params && !request && !Array.isArray(params) && isInternalClientRequest(params)
75
+ ? params
76
+ : request;
77
+ const method = methodParam.toLowerCase();
78
+ const config = configThunk();
79
+ const fetchImpl = config.fetch ?? fetch;
80
+ const accessToken = requestCtx?.accessToken ?? config.accessToken;
81
+ const projectId = requestCtx?.projectId ?? configThunk().projectId;
82
+ let finalPath = path;
83
+ let finalParams = providedParams;
84
+ if (!Array.isArray(providedParams)) {
85
+ const [subbedPath, addlParams] = subParamsInPath(path, providedParams);
86
+ finalPath = subbedPath;
87
+ finalParams = addlParams;
88
+ }
89
+ finalPath = finalPath.replace(/^\//, ''); // remove leading slash
90
+ const baseUrlEvaluated = baseUrlThunk();
91
+ const fullBaseUrl = baseUrlEvaluated.endsWith('/') ? baseUrlEvaluated : baseUrlEvaluated + '/';
92
+ const url = new URL(finalPath, fullBaseUrl);
93
+ let body;
94
+ if (Array.isArray(finalParams)) {
95
+ body = JSON.stringify(finalParams);
96
+ }
97
+ else if (Object.keys(finalParams).length) {
98
+ if (method === 'get') {
99
+ addParamsToSearch(finalParams, url.searchParams);
100
+ }
101
+ else if (requestCtx?.contentType === 'application/x-www-form-urlencoded') {
102
+ const search = new URLSearchParams();
103
+ addParamsToSearch(finalParams, search);
104
+ body = search.toString();
105
+ }
106
+ else {
107
+ body = JSON.stringify(finalParams);
108
+ }
109
+ }
110
+ else {
111
+ // override for rpc call
112
+ if (requestCtx?.contentType !== 'application/x-www-form-urlencoded' && method === 'post') {
113
+ body = '{}';
114
+ }
115
+ }
116
+ const headers = Object.assign(projectId
117
+ ? {
118
+ 'x-zapehr-project-id': projectId,
119
+ 'x-oystehr-project-id': projectId,
120
+ }
121
+ : {}, {
122
+ 'content-type': requestCtx?.contentType ?? 'application/json',
123
+ }, accessToken ? { Authorization: `Bearer ${accessToken}` } : {}, requestCtx?.ifMatch ? { 'If-Match': requestCtx.ifMatch } : {}, { 'x-oystehr-request-id': requestCtx?.requestId ?? uuid.v4() });
124
+ const retryConfig = {
125
+ retries: config.retry?.retries ?? 3,
126
+ jitter: config.retry?.jitter ?? 20,
127
+ delay: config.retry?.delay ?? 100,
128
+ onRetry: config.retry?.onRetry,
129
+ // Using array instead of set because the length is too short for uniqueness to be important
130
+ retryOn: [...(config.retry?.retryOn ?? []), ...STATUS_CODES_TO_RETRY],
131
+ };
132
+ retryConfig.retryOn.push(...STATUS_CODES_TO_RETRY);
133
+ return retry(async () => {
134
+ const response = await fetchImpl(new Request(url, {
135
+ method: method.toUpperCase(),
136
+ body,
137
+ headers,
138
+ }));
139
+ const responseBody = response.body ? await response.text() : null;
140
+ let responseJson;
141
+ try {
142
+ responseJson =
143
+ responseBody &&
144
+ (response.headers.get('content-type')?.includes('application/json') ||
145
+ response.headers.get('content-type')?.includes('application/fhir+json'))
146
+ ? JSON.parse(responseBody)
147
+ : null;
148
+ }
149
+ catch (_err) {
150
+ // ignore JSON.parse errors
151
+ responseJson = null;
152
+ }
153
+ const isError = !response.ok || response.status >= 400;
154
+ if (isError) {
155
+ const errObj = {
156
+ message: responseJson?.output?.message ?? // official zambda output format
157
+ responseJson?.message ?? // normal endpoint output format
158
+ responseJson ?? // parsable json
159
+ responseBody ?? // raw response
160
+ response.statusText, // fallback to status text
161
+ code: responseJson?.output?.code ?? // official zambda output format
162
+ responseJson?.code ?? // normal endpoint output format
163
+ response.status, // fallback to status code
164
+ response,
165
+ };
166
+ throw errObj;
167
+ }
168
+ return responseJson;
169
+ }, retryConfig);
170
+ };
171
+ }
172
+ async function retry(work, config) {
173
+ let lastErr;
174
+ for (const attempt of Array.from({ length: (config.retries ?? 0) + 1 }, (_, index) => index)) {
175
+ try {
176
+ return await work(attempt);
177
+ }
178
+ catch (e) {
179
+ let isRetryable = false;
180
+ if ('response' in e) {
181
+ // error from API
182
+ const err = e;
183
+ isRetryable = config.retryOn.includes(err.code);
184
+ // Removes response
185
+ lastErr = { message: e.message, code: e.code };
186
+ }
187
+ else {
188
+ lastErr = e;
189
+ // error from fetch
190
+ if ('code' in e && typeof e.code === 'string') {
191
+ const err = e;
192
+ isRetryable = ERROR_CODES_TO_RETRY.includes(err.code);
193
+ }
194
+ }
195
+ if (!isRetryable) {
196
+ break;
197
+ }
198
+ const jitter = Math.floor(Math.random() * (config.jitter + 1));
199
+ await new Promise((resolve) => setTimeout(resolve, config.delay + jitter));
200
+ if (config.onRetry && attempt !== (config.retries ?? 0)) {
201
+ config.onRetry(attempt + 1);
202
+ }
203
+ }
204
+ }
205
+ throw lastErr;
206
+ }
207
+ /**
208
+ * Substitutes params in a path and returns the path with params substituted and any unused params.
209
+ *
210
+ * Uses the property names in the params object to determine the param to substitute in the path.
211
+ *
212
+ * @param path JSON API resource URI
213
+ * @param params all params provided to the client method
214
+ * @returns resource URI with params substituted and any unused params
215
+ */
216
+ function subParamsInPath(path, params) {
217
+ const unusedParams = { ...params };
218
+ // capture everything of the form `{paramName}` and replace with the value of `params[paramName]`
219
+ const subbedPath = path.replace(/\{([^}]+)\}/g, (_, paramName) => {
220
+ delete unusedParams[paramName];
221
+ // override for path params that are paths, indicated by a `+` at the end
222
+ if (paramName.match(/^.*\+$/)) {
223
+ return params[paramName] + '';
224
+ }
225
+ // error if param value is empty
226
+ if (!params[paramName] || params[paramName] === '') {
227
+ throw new index.OystehrSdkError({ message: `Required path parameter is an empty string: ${paramName}`, code: 400 });
228
+ }
229
+ // encode search params
230
+ if (params[paramName]) {
231
+ return encodeURIComponent(params[paramName] + ''); // coerce to string
232
+ }
233
+ return '';
234
+ });
235
+ const unusedKeys = Object.keys(unusedParams);
236
+ const addlParams = unusedKeys.length
237
+ ? unusedKeys.reduce((acc, key) => ({ ...acc, [key]: unusedParams[key] }), {})
238
+ : {};
239
+ return [subbedPath, addlParams];
240
+ }
241
+ /**
242
+ * Adds params to a URLSearchParams object in such a way as to preserve array values.
243
+ * @param params params
244
+ * @param search URLSearchParams object
245
+ */
246
+ function addParamsToSearch(params, search) {
247
+ for (const [key, value] of Object.entries(params)) {
248
+ if (Array.isArray(value)) {
249
+ value.forEach((v) => search.append(key, v));
250
+ continue;
251
+ }
252
+ search.append(key, value);
253
+ }
254
+ }
255
+
256
+ exports.SDKResource = SDKResource;
257
+ exports.addParamsToSearch = addParamsToSearch;
258
+ exports.defaultProjectApiUrl = defaultProjectApiUrl;
259
+ //# sourceMappingURL=client.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.cjs","sources":["../../../src/client/client.ts"],"sourcesContent":["import { v4 as uuidv4 } from 'uuid';\nimport { OystehrConfig } from '../config';\nimport { OystehrFHIRError, OystehrSdkError } from '../errors';\nimport { FhirBundle, FhirResource, OperationOutcome } from '../resources/types';\n\ntype HttpMethod = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace';\nexport const defaultProjectApiUrl = 'https://project-api.zapehr.com/v1';\nconst defaultFhirApiUrl = 'https://fhir-api.zapehr.com';\nconst STATUS_CODES_TO_RETRY = [408, 429, 500, 502, 503, 504];\nconst ERROR_CODES_TO_RETRY = [\n 'ECONNRESET',\n 'ECONNREFUSED',\n 'EPIPE',\n 'ETIMEDOUT',\n 'UND_ERR_CONNECT_TIMEOUT',\n 'UND_ERR_HEADERS_TIMEOUT',\n 'UND_ERR_HEADERS_TIMEOUT',\n 'UND_ERR_SOCKET',\n];\n\n/**\n * Optional parameter that can be passed to the client methods. It allows\n * overriding the access token or project ID, and setting various headers,\n * such as 'Content-Type'.\n */\nexport interface OystehrClientRequest {\n /**\n * The access token to use for the request. If not provided, the access token from `oystehr.init()` will be used.\n */\n accessToken?: string;\n /**\n * The project ID to use for the request. If not provided, the project ID from `oystehr.init()` will be used.\n */\n projectId?: string;\n /**\n * The value of the 'Content-Type' header to use for the request.\n */\n contentType?: string;\n /**\n * Unique identifier for this request.\n */\n requestId?: string;\n}\n\ninterface InternalClientRequest extends OystehrClientRequest {\n ifMatch?: string;\n}\n\ntype FhirData<T extends FhirResource> = T | T[] | FhirBundle<T>;\nexport type FhirFetcherResponse<T extends FhirData<FhirResource> = any> = T;\n\nexport class SDKResource {\n protected readonly config: OystehrConfig;\n constructor(config: OystehrConfig) {\n this.config = config;\n }\n\n protected request(path: string, method: string, baseUrlThunk: () => string): FetcherFunction {\n return async (params: any, request?: InternalClientRequest): Promise<FetcherResponse> => {\n const configThunk = (): OystehrConfig => this.config;\n try {\n return await fetcher(baseUrlThunk, configThunk, path, method)(params, request);\n } catch (err: any) {\n const error = err as { message: string; code: number; cause?: unknown };\n throw new OystehrSdkError({ message: error.message, code: error.code, cause: error.cause });\n }\n };\n }\n\n protected fhirRequest<T extends FhirResource = any>(path: string, method: string) {\n return async (params: any, request?: InternalClientRequest): Promise<FhirFetcherResponse<T>> => {\n try {\n const baseUrlThunk = (): string => this.config.services?.fhirApiUrl ?? defaultFhirApiUrl;\n const configThunk = (): OystehrConfig => this.config;\n // must await here to catch\n return await fetcher(baseUrlThunk, configThunk, path, method)(params, request);\n } catch (err: unknown) {\n // FHIR API error messages are JSON strings\n const fullError = err as { message: string | Record<string, any>; code: number; cause?: unknown };\n if (typeof fullError.message === 'string') {\n throw new OystehrSdkError({\n message: fullError.message,\n code: fullError.code,\n cause: fullError.cause,\n });\n }\n throw new OystehrFHIRError({\n error: fullError.message as OperationOutcome,\n code: fullError.code,\n });\n }\n };\n }\n}\n\nexport type FetcherError = { message: string; code: number };\nexport type FetcherResponse = any;\nexport type FetcherFunction = (\n params?: Record<string, any> | [any] | InternalClientRequest,\n request?: InternalClientRequest\n) => Promise<FetcherResponse>;\n\nfunction isInternalClientRequest(request: Record<string, any>): request is InternalClientRequest {\n return 'accessToken' in request;\n}\n\nfunction fetcher(\n baseUrlThunk: () => string,\n configThunk: () => OystehrConfig,\n path: string,\n methodParam: string\n): FetcherFunction {\n return async (\n params?: Record<string, unknown> | [any] | InternalClientRequest,\n request?: InternalClientRequest\n ): Promise<FetcherResponse> => {\n // this function supports multiple signatures. fetcher(baseUrl, path, method)(params, request) or fetcher(baseUrl, path, method)(request)\n // or fetcher(baseUrl, path, method)(params) or fetcher(baseUrl, path, method)(). the types for this are handled by Client<Path, Methods>\n // and this is the backend implementation behind it. the heuristic we're using is that if the first param is an object with an accessToken\n // and there is no second param, assume the first one is the request object instead\n const providedParams: Record<string, unknown> | [any] =\n !!params && !request && !Array.isArray(params) && isInternalClientRequest(params)\n ? {}\n : (params as Record<string, unknown>) ?? {};\n const requestCtx =\n !!params && !request && !Array.isArray(params) && isInternalClientRequest(params)\n ? (params as InternalClientRequest)\n : request;\n const method = methodParam.toLowerCase() as HttpMethod;\n const config = configThunk();\n const fetchImpl = config.fetch ?? fetch;\n const accessToken = requestCtx?.accessToken ?? config.accessToken;\n const projectId = requestCtx?.projectId ?? configThunk().projectId;\n let finalPath = path;\n let finalParams = providedParams;\n if (!Array.isArray(providedParams)) {\n const [subbedPath, addlParams] = subParamsInPath(path, providedParams);\n finalPath = subbedPath;\n finalParams = addlParams;\n }\n finalPath = finalPath.replace(/^\\//, ''); // remove leading slash\n const baseUrlEvaluated = baseUrlThunk();\n const fullBaseUrl = baseUrlEvaluated.endsWith('/') ? baseUrlEvaluated : baseUrlEvaluated + '/';\n const url = new URL(finalPath, fullBaseUrl);\n let body: any;\n\n if (Array.isArray(finalParams)) {\n body = JSON.stringify(finalParams);\n } else if (Object.keys(finalParams).length) {\n if (method === 'get') {\n addParamsToSearch(finalParams, url.searchParams);\n } else if (requestCtx?.contentType === 'application/x-www-form-urlencoded') {\n const search = new URLSearchParams();\n addParamsToSearch(finalParams, search);\n body = search.toString();\n } else {\n body = JSON.stringify(finalParams);\n }\n } else {\n // override for rpc call\n if (requestCtx?.contentType !== 'application/x-www-form-urlencoded' && method === 'post') {\n body = '{}';\n }\n }\n\n const headers: Record<string, string> = Object.assign(\n projectId\n ? {\n 'x-zapehr-project-id': projectId,\n 'x-oystehr-project-id': projectId,\n }\n : {},\n {\n 'content-type': requestCtx?.contentType ?? 'application/json',\n },\n accessToken ? { Authorization: `Bearer ${accessToken}` } : {},\n requestCtx?.ifMatch ? { 'If-Match': requestCtx.ifMatch } : {},\n { 'x-oystehr-request-id': requestCtx?.requestId ?? uuidv4() }\n );\n const retryConfig: ConstructedRetryConfig = {\n retries: config.retry?.retries ?? 3,\n jitter: config.retry?.jitter ?? 20,\n delay: config.retry?.delay ?? 100,\n onRetry: config.retry?.onRetry,\n // Using array instead of set because the length is too short for uniqueness to be important\n retryOn: [...(config.retry?.retryOn ?? []), ...STATUS_CODES_TO_RETRY],\n };\n retryConfig.retryOn.push(...STATUS_CODES_TO_RETRY);\n return retry(async () => {\n const response = await fetchImpl(\n new Request(url, {\n method: method.toUpperCase(),\n body,\n headers,\n })\n );\n const responseBody = response.body ? await response.text() : null;\n let responseJson: Record<string, unknown> | null;\n try {\n responseJson =\n responseBody &&\n (response.headers.get('content-type')?.includes('application/json') ||\n response.headers.get('content-type')?.includes('application/fhir+json'))\n ? JSON.parse(responseBody)\n : null;\n } catch (_err) {\n // ignore JSON.parse errors\n responseJson = null;\n }\n const isError = !response.ok || response.status >= 400;\n if (isError) {\n const errObj = {\n message:\n (responseJson?.output as Record<string, unknown>)?.message ?? // official zambda output format\n responseJson?.message ?? // normal endpoint output format\n responseJson ?? // parsable json\n responseBody ?? // raw response\n response.statusText, // fallback to status text\n code:\n (responseJson?.output as Record<string, unknown>)?.code ?? // official zambda output format\n responseJson?.code ?? // normal endpoint output format\n response.status, // fallback to status code\n response,\n };\n throw errObj;\n }\n return responseJson;\n }, retryConfig);\n };\n}\n\ntype ConstructedRetryConfig = Omit<NonNullable<OystehrConfig['retry']>, 'retryOn'> & {\n jitter: NonNullable<NonNullable<OystehrConfig['retry']>['jitter']>;\n delay: NonNullable<NonNullable<OystehrConfig['retry']>['delay']>;\n retryOn: NonNullable<NonNullable<OystehrConfig['retry']>['retryOn']>;\n};\nasync function retry<T>(work: (attempt: number) => Promise<T>, config: ConstructedRetryConfig): Promise<T> {\n let lastErr;\n for (const attempt of Array.from({ length: (config.retries ?? 0) + 1 }, (_, index) => index)) {\n try {\n return await work(attempt);\n } catch (e: any) {\n let isRetryable = false;\n if ('response' in e) {\n // error from API\n const err = e as FetcherError;\n isRetryable = config.retryOn.includes(err.code);\n // Removes response\n lastErr = { message: e.message, code: e.code };\n } else {\n lastErr = e;\n // error from fetch\n if ('code' in e && typeof e.code === 'string') {\n const err = e as { code: string };\n isRetryable = ERROR_CODES_TO_RETRY.includes(err.code);\n }\n }\n if (!isRetryable) {\n break;\n }\n const jitter = Math.floor(Math.random() * (config.jitter + 1));\n await new Promise((resolve) => setTimeout(resolve, config.delay + jitter));\n if (config.onRetry && attempt !== (config.retries ?? 0)) {\n config.onRetry(attempt + 1);\n }\n }\n }\n throw lastErr;\n}\n\n/**\n * Substitutes params in a path and returns the path with params substituted and any unused params.\n *\n * Uses the property names in the params object to determine the param to substitute in the path.\n *\n * @param path JSON API resource URI\n * @param params all params provided to the client method\n * @returns resource URI with params substituted and any unused params\n */\nfunction subParamsInPath(path: string, params: Record<string, unknown>): [string, Record<string, string>] {\n const unusedParams = { ...params };\n // capture everything of the form `{paramName}` and replace with the value of `params[paramName]`\n const subbedPath = path.replace(/\\{([^}]+)\\}/g, (_, paramName) => {\n delete unusedParams[paramName];\n // override for path params that are paths, indicated by a `+` at the end\n if (paramName.match(/^.*\\+$/)) {\n return params[paramName] + '';\n }\n // error if param value is empty\n if (!params[paramName] || params[paramName] === '') {\n throw new OystehrSdkError({ message: `Required path parameter is an empty string: ${paramName}`, code: 400 });\n }\n // encode search params\n if (params[paramName]) {\n return encodeURIComponent(params[paramName] + ''); // coerce to string\n }\n return '';\n });\n\n const unusedKeys = Object.keys(unusedParams);\n const addlParams = unusedKeys.length\n ? unusedKeys.reduce((acc, key) => ({ ...acc, [key]: unusedParams[key] }), {})\n : {};\n return [subbedPath, addlParams];\n}\n\n/**\n * Adds params to a URLSearchParams object in such a way as to preserve array values.\n * @param params params\n * @param search URLSearchParams object\n */\nexport function addParamsToSearch(params: Record<string, unknown>, search: URLSearchParams): void {\n for (const [key, value] of Object.entries(params)) {\n if (Array.isArray(value)) {\n value.forEach((v) => search.append(key, v as string));\n continue;\n }\n search.append(key, value as string);\n }\n}\n"],"names":["OystehrSdkError","OystehrFHIRError","uuidv4"],"mappings":";;;;;AAMO,MAAM,oBAAoB,GAAG,oCAAoC;AACxE,MAAM,iBAAiB,GAAG,6BAA6B,CAAC;AACxD,MAAM,qBAAqB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAC7D,MAAM,oBAAoB,GAAG;IAC3B,YAAY;IACZ,cAAc;IACd,OAAO;IACP,WAAW;IACX,yBAAyB;IACzB,yBAAyB;IACzB,yBAAyB;IACzB,gBAAgB;CACjB,CAAC;MAiCW,WAAW,CAAA;AACH,IAAA,MAAM,CAAgB;AACzC,IAAA,WAAA,CAAY,MAAqB,EAAA;AAC/B,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;KACtB;AAES,IAAA,OAAO,CAAC,IAAY,EAAE,MAAc,EAAE,YAA0B,EAAA;AACxE,QAAA,OAAO,OAAO,MAAW,EAAE,OAA+B,KAA8B;YACtF,MAAM,WAAW,GAAG,MAAqB,IAAI,CAAC,MAAM,CAAC;AACrD,YAAA,IAAI;AACF,gBAAA,OAAO,MAAM,OAAO,CAAC,YAAY,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;aAChF;YAAC,OAAO,GAAQ,EAAE;gBACjB,MAAM,KAAK,GAAG,GAAyD,CAAC;gBACxE,MAAM,IAAIA,qBAAe,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;aAC7F;AACH,SAAC,CAAC;KACH;IAES,WAAW,CAA+B,IAAY,EAAE,MAAc,EAAA;A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AAe,CAAC,EAAE,OAAO,EAAE,CAAA,4CAAA,EAA+C,SAAS,CAAA,CAAE,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC;SAC/G;;AAED,QAAA,IAAI,MAAM,CAAC,SAAS,CAAC,EAAE;YACrB,OAAO,kBAAkB,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC;SACnD;AACD,QAAA,OAAO,EAAE,CAAC;AACZ,KAAC,CAAC,CAAC;IAEH,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;AAC7C,IAAA,MAAM,UAAU,GAAG,UAAU,CAAC,MAAM;AAClC,UAAE,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,MAAM,EAAE,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;UAC3E,EAAE,CAAC;AACP,IAAA,OAAO,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;AAClC,CAAC;AAED;;;;AAIG;AACa,SAAA,iBAAiB,CAAC,MAA+B,EAAE,MAAuB,EAAA;AACxF,IAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;AACjD,QAAA,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;AACxB,YAAA,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAW,CAAC,CAAC,CAAC;YACtD,SAAS;SACV;AACD,QAAA,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,KAAe,CAAC,CAAC;KACrC;AACH;;;;;;"}
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ class OystehrSdkError extends Error {
4
+ code;
5
+ constructor({ message, code, cause }) {
6
+ super(message, { cause });
7
+ Object.setPrototypeOf(this, OystehrSdkError.prototype);
8
+ this.code = code;
9
+ this.name = 'OystehrSdkError';
10
+ }
11
+ toString() {
12
+ return `${this.name}: ${this.message} (code: ${this.code})`;
13
+ }
14
+ toJSON() {
15
+ return {
16
+ name: this.name,
17
+ message: this.message,
18
+ code: this.code,
19
+ cause: this.cause,
20
+ };
21
+ }
22
+ }
23
+ function transformOperationOutcomeToErrorMessage(cause) {
24
+ const causes = [];
25
+ for (const issue of cause.issue ?? []) {
26
+ if (issue.details && issue.details.text) {
27
+ causes.push(issue.details.text);
28
+ }
29
+ }
30
+ if (!causes.length) {
31
+ causes.push('Unknown FHIR error');
32
+ }
33
+ return causes.join(',');
34
+ }
35
+ class OystehrFHIRError extends OystehrSdkError {
36
+ cause;
37
+ constructor({ error, code }) {
38
+ super({
39
+ message: transformOperationOutcomeToErrorMessage(error),
40
+ code,
41
+ });
42
+ Object.setPrototypeOf(this, OystehrFHIRError.prototype);
43
+ this.cause = error;
44
+ this.name = 'OystehrFHIRError';
45
+ }
46
+ toJSON() {
47
+ return {
48
+ name: this.name,
49
+ message: this.message,
50
+ code: this.code,
51
+ cause: this.cause,
52
+ };
53
+ }
54
+ }
55
+
56
+ exports.OystehrFHIRError = OystehrFHIRError;
57
+ exports.OystehrSdkError = OystehrSdkError;
58
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../../../src/errors/index.ts"],"sourcesContent":["import { OperationOutcome as OperationOutcomeR4B } from 'fhir/r4b';\nimport { OperationOutcome as OperationOutcomeR5 } from 'fhir/r5';\n\nexport class OystehrSdkError extends Error {\n code: number;\n constructor({ message, code, cause }: { message: string; code: number; cause?: unknown }) {\n super(message, { cause });\n Object.setPrototypeOf(this, OystehrSdkError.prototype);\n this.code = code;\n this.name = 'OystehrSdkError';\n }\n toString(): string {\n return `${this.name}: ${this.message} (code: ${this.code})`;\n }\n toJSON(): any {\n return {\n name: this.name,\n message: this.message,\n code: this.code,\n cause: this.cause,\n };\n }\n}\n\nfunction transformOperationOutcomeToErrorMessage(cause: OperationOutcomeR4B | OperationOutcomeR5): string {\n const causes = [];\n for (const issue of cause.issue ?? []) {\n if (issue.details && issue.details.text) {\n causes.push(issue.details.text);\n }\n }\n if (!causes.length) {\n causes.push('Unknown FHIR error');\n }\n return causes.join(',');\n}\n\nexport class OystehrFHIRError extends OystehrSdkError {\n cause: OperationOutcomeR4B | OperationOutcomeR5;\n constructor({ error, code }: { error: OperationOutcomeR4B | OperationOutcomeR5; code: number }) {\n super({\n message: transformOperationOutcomeToErrorMessage(error as OperationOutcomeR4B | OperationOutcomeR5),\n code,\n });\n Object.setPrototypeOf(this, OystehrFHIRError.prototype);\n this.cause = error as OperationOutcomeR4B | OperationOutcomeR5;\n this.name = 'OystehrFHIRError';\n }\n toJSON(): any {\n return {\n name: this.name,\n message: this.message,\n code: this.code,\n cause: this.cause,\n };\n }\n}\n"],"names":[],"mappings":";;AAGM,MAAO,eAAgB,SAAQ,KAAK,CAAA;AACxC,IAAA,IAAI,CAAS;AACb,IAAA,WAAA,CAAY,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAsD,EAAA;AACtF,QAAA,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC1B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC;AACvD,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;AACjB,QAAA,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;KAC/B;IACD,QAAQ,GAAA;AACN,QAAA,OAAO,CAAG,EAAA,IAAI,CAAC,IAAI,CAAK,EAAA,EAAA,IAAI,CAAC,OAAO,CAAW,QAAA,EAAA,IAAI,CAAC,IAAI,GAAG,CAAC;KAC7D;IACD,MAAM,GAAA;QACJ,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,IAAI,EAAE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