@health-samurai/aidbox-client 0.0.0-alpha.1 → 0.0.0-alpha.2

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.
@@ -202,15 +202,28 @@ import { coerceBody } from "./utils";
202
202
  var isInternalErrorResponse = function(resp) {
203
203
  return "error" in resp;
204
204
  };
205
+ var makeUrl = function(parts) {
206
+ return "/".concat(parts.map(function(part) {
207
+ return encodeURIComponent(part);
208
+ }).join("/"));
209
+ };
210
+ var basePath = "fhir";
205
211
  /**
206
212
  * Create a client to the FHIR server.
207
213
  *
208
214
  * ```typescript
209
- * const client = makeClient({ baseurl: "https://fhir-server.address" })
215
+ * const baseUrl = "https://fhir-server.address";
216
+ * const client = makeClient({
217
+ * baseUrl,
218
+ * authProvider: new BrowserAuthProvider(baseUrl);
219
+ * });
210
220
  *
211
221
  * // alternatively, specify different FHIR types:
212
222
  * import type { Bundle, OperationOutcome } from "hl7-fhir-r5-core";
213
- * const client = makeClient<Bundle, OperationOutcome>({ baseurl: "https://fhir-server.address" })
223
+ * const client = makeClient<Bundle, OperationOutcome, User>({
224
+ * baseUrl,
225
+ * authProvider: new BrowserAuthProvider(baseUrl);
226
+ * });
214
227
  * ```
215
228
  *
216
229
  * Main client functions are `request` for typed interactions, and `rawRequest` for manual response processing.
@@ -218,37 +231,54 @@ var isInternalErrorResponse = function(resp) {
218
231
  * This client also provides a set of convenience methods for accessing FHIR operations:
219
232
  *
220
233
  * - `read`
221
- * - `search`
234
+ * - `vread`
235
+ * - `searchType`
236
+ * - `searchSystem`
237
+ * - `searchCompartment`
222
238
  * - `create`
239
+ * - `conditionalCreate`
240
+ * - `update`
241
+ * - `conditionalUpdate`
242
+ * - `patch`
243
+ * - `conditionalPatch`
244
+ * - `delete: deleteOp`
245
+ * - `deleteHistory`
246
+ * - `deleteHistoryVersion`
247
+ * - `conditionalDelete`
248
+ * - `historyInstance`
249
+ * - `historyType`
250
+ * - `historySystem`
251
+ * - `capabilities`
252
+ * - `batch`
253
+ * - `transaction`
223
254
  * - `operation`
224
255
  * - `validate`
225
- *
226
256
  */ export function makeClient(param) {
227
- var baseurl = param.baseurl, _param_onResponse = param.onResponse, onResponse = _param_onResponse === void 0 ? undefined : _param_onResponse;
228
- var getBaseURL = function() {
229
- return baseurl;
257
+ var baseUrl = param.baseUrl, authProvider = param.authProvider;
258
+ var getBaseUrl = function() {
259
+ return baseUrl;
230
260
  };
231
261
  // TODO: async response pattern
232
262
  var internalRawRequest = function(requestParams) {
233
263
  return _async_to_generator(function() {
234
- var startTime, baseURL, method, url, _requestParams_headers, headers, _requestParams_params, params, body, urlObj, requestHeaders, request, response, responseHeaders, e;
264
+ var startTime, baseUrl, method, url, _requestParams_headers, headers, _requestParams_params, params, body, urlObj, requestHeaders, request, response, responseHeaders, e;
235
265
  return _ts_generator(this, function(_state) {
236
266
  switch(_state.label){
237
267
  case 0:
238
- startTime = Date.now();
239
- baseURL = getBaseURL();
268
+ startTime = performance.now();
269
+ baseUrl = getBaseUrl();
240
270
  if (!requestParams.url.startsWith("/")) return [
241
271
  2,
242
272
  {
243
- error: new RequestError("url must start with a forward slash", {
273
+ error: new RequestError("URL must start with a forward slash", {
244
274
  request: requestParams
245
275
  }),
246
- duration: Date.now() - startTime,
276
+ duration: performance.now() - startTime,
247
277
  request: requestParams
248
278
  }
249
279
  ];
250
280
  method = requestParams.method, url = requestParams.url, _requestParams_headers = requestParams.headers, headers = _requestParams_headers === void 0 ? {} : _requestParams_headers, _requestParams_params = requestParams.params, params = _requestParams_params === void 0 ? [] : _requestParams_params, body = requestParams.body;
251
- urlObj = new URL(url, baseURL);
281
+ urlObj = new URL(url, baseUrl);
252
282
  params.forEach(function(param) {
253
283
  var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
254
284
  urlObj.searchParams.append(key, value);
@@ -278,11 +308,10 @@ var isInternalErrorResponse = function(resp) {
278
308
  ]);
279
309
  return [
280
310
  4,
281
- fetch(urlObj.toString(), {
311
+ authProvider.fetch(urlObj.toString(), {
282
312
  method: method,
283
313
  headers: requestHeaders,
284
314
  body: body || null,
285
- credentials: "include",
286
315
  cache: "no-store"
287
316
  })
288
317
  ];
@@ -297,7 +326,7 @@ var isInternalErrorResponse = function(resp) {
297
326
  {
298
327
  response: response,
299
328
  responseHeaders: responseHeaders,
300
- duration: Date.now() - startTime,
329
+ duration: performance.now() - startTime,
301
330
  request: request
302
331
  }
303
332
  ];
@@ -310,7 +339,7 @@ var isInternalErrorResponse = function(resp) {
310
339
  cause: e,
311
340
  request: request
312
341
  }),
313
- duration: Date.now() - startTime,
342
+ duration: performance.now() - startTime,
314
343
  request: request
315
344
  }
316
345
  ];
@@ -335,7 +364,6 @@ var isInternalErrorResponse = function(resp) {
335
364
  case 1:
336
365
  result = _state.sent();
337
366
  if (isInternalErrorResponse(result)) throw result.error;
338
- if (onResponse) onResponse(result.response.clone());
339
367
  if (!result.response.ok) throw new ErrorResponse("HTTP ".concat(result.response.status, ": ").concat(result.response.statusText), result);
340
368
  return [
341
369
  2,
@@ -358,7 +386,6 @@ var isInternalErrorResponse = function(resp) {
358
386
  case 1:
359
387
  response = _state.sent();
360
388
  if (isInternalErrorResponse(response)) throw response.error;
361
- if (onResponse) onResponse(response.response.clone());
362
389
  return [
363
390
  4,
364
391
  coerceBody(response)
@@ -447,7 +474,11 @@ var isInternalErrorResponse = function(resp) {
447
474
  return [
448
475
  4,
449
476
  request({
450
- url: "/fhir/".concat(opts.type, "/").concat(opts.id),
477
+ url: makeUrl([
478
+ basePath,
479
+ opts.type,
480
+ opts.id
481
+ ]),
451
482
  method: "GET"
452
483
  })
453
484
  ];
@@ -468,7 +499,13 @@ var isInternalErrorResponse = function(resp) {
468
499
  return [
469
500
  4,
470
501
  request({
471
- url: "/fhir/".concat(opts.type, "/").concat(opts.id, "/_history/").concat(opts.vid),
502
+ url: makeUrl([
503
+ basePath,
504
+ opts.type,
505
+ opts.id,
506
+ "_history",
507
+ opts.vid
508
+ ]),
472
509
  method: "GET"
473
510
  })
474
511
  ];
@@ -481,30 +518,87 @@ var isInternalErrorResponse = function(resp) {
481
518
  });
482
519
  })();
483
520
  };
484
- var search = function(opts) {
521
+ var searchType = function(opts) {
485
522
  return _async_to_generator(function() {
486
523
  var url, requestParams;
487
524
  return _ts_generator(this, function(_state) {
488
525
  switch(_state.label){
489
526
  case 0:
490
527
  url = [
491
- "/fhir"
528
+ basePath,
529
+ opts.type
492
530
  ];
493
- if (opts.compartment) url.push(opts.compartment);
494
- if (opts.compartmentId) url.push(opts.compartmentId);
495
- if (opts.type) url.push(opts.type);
496
- // TODO: supports POST to /fhir/.../_search
497
531
  requestParams = {
498
- url: url.join("/"),
532
+ url: makeUrl(url),
533
+ method: "GET",
534
+ headers: {
535
+ "Content-Type": "application/x-www-form-urlencoded"
536
+ },
537
+ params: opts.query
538
+ };
539
+ return [
540
+ 4,
541
+ request(requestParams)
542
+ ];
543
+ case 1:
544
+ return [
545
+ 2,
546
+ _state.sent()
547
+ ];
548
+ }
549
+ });
550
+ })();
551
+ };
552
+ var searchSystem = function(opts) {
553
+ return _async_to_generator(function() {
554
+ var url, requestParams;
555
+ return _ts_generator(this, function(_state) {
556
+ switch(_state.label){
557
+ case 0:
558
+ url = [
559
+ basePath
560
+ ];
561
+ requestParams = {
562
+ url: makeUrl(url),
563
+ method: "GET",
564
+ headers: {
565
+ "Content-Type": "application/x-www-form-urlencoded"
566
+ },
567
+ params: opts.query
568
+ };
569
+ return [
570
+ 4,
571
+ request(requestParams)
572
+ ];
573
+ case 1:
574
+ return [
575
+ 2,
576
+ _state.sent()
577
+ ];
578
+ }
579
+ });
580
+ })();
581
+ };
582
+ var searchCompartment = function(opts) {
583
+ return _async_to_generator(function() {
584
+ var url, requestParams;
585
+ return _ts_generator(this, function(_state) {
586
+ switch(_state.label){
587
+ case 0:
588
+ url = [
589
+ basePath,
590
+ opts.compartment,
591
+ opts.compartmentId,
592
+ opts.type
593
+ ];
594
+ requestParams = {
595
+ url: makeUrl(url),
499
596
  method: "GET",
500
597
  headers: {
501
598
  "Content-Type": "application/x-www-form-urlencoded"
502
599
  },
503
600
  params: opts.query
504
601
  };
505
- if (opts.compartmentId && !opts.compartment) throw new RequestError("compartment must be specified if compartment ID is provided", {
506
- request: requestParams
507
- });
508
602
  return [
509
603
  4,
510
604
  request(requestParams)
@@ -526,7 +620,10 @@ var isInternalErrorResponse = function(resp) {
526
620
  return [
527
621
  4,
528
622
  request({
529
- url: "/fhir/".concat(opts.type, "/"),
623
+ url: makeUrl([
624
+ basePath,
625
+ opts.type
626
+ ]),
530
627
  method: "POST",
531
628
  body: JSON.stringify(opts.resource)
532
629
  })
@@ -548,13 +645,13 @@ var isInternalErrorResponse = function(resp) {
548
645
  return [
549
646
  4,
550
647
  request({
551
- url: "/fhir/".concat(opts.type, "/"),
648
+ url: makeUrl([
649
+ basePath,
650
+ opts.type
651
+ ]),
552
652
  method: "POST",
553
653
  headers: {
554
- "If-None-Exist": opts.searchParameters.map(function(param) {
555
- var _param = _sliced_to_array(param, 2), name = _param[0], _$param = _param[1];
556
- return "".concat(name, "=").concat(_$param);
557
- }).join("&")
654
+ "If-None-Exist": new URLSearchParams(opts.searchParameters).toString()
558
655
  },
559
656
  body: JSON.stringify(opts.resource)
560
657
  })
@@ -576,7 +673,11 @@ var isInternalErrorResponse = function(resp) {
576
673
  return [
577
674
  4,
578
675
  request({
579
- url: "/fhir/".concat(opts.type, "/").concat(opts.id),
676
+ url: makeUrl([
677
+ basePath,
678
+ opts.type,
679
+ opts.id
680
+ ]),
580
681
  method: "PUT",
581
682
  body: JSON.stringify(opts.resource)
582
683
  })
@@ -598,7 +699,10 @@ var isInternalErrorResponse = function(resp) {
598
699
  return [
599
700
  4,
600
701
  request({
601
- url: "/fhir/".concat(opts.type),
702
+ url: makeUrl([
703
+ basePath,
704
+ opts.type
705
+ ]),
602
706
  method: "PUT",
603
707
  body: JSON.stringify(opts.resource),
604
708
  params: opts.searchParameters
@@ -621,7 +725,11 @@ var isInternalErrorResponse = function(resp) {
621
725
  return [
622
726
  4,
623
727
  request({
624
- url: "/fhir/".concat(opts.type, "/").concat(opts.id),
728
+ url: makeUrl([
729
+ basePath,
730
+ opts.type,
731
+ opts.id
732
+ ]),
625
733
  method: "PATCH",
626
734
  headers: {
627
735
  "Content-Type": "application/json-patch+json"
@@ -646,7 +754,10 @@ var isInternalErrorResponse = function(resp) {
646
754
  return [
647
755
  4,
648
756
  request({
649
- url: "/fhir/".concat(opts.type),
757
+ url: makeUrl([
758
+ basePath,
759
+ opts.type
760
+ ]),
650
761
  method: "PATCH",
651
762
  headers: {
652
763
  "Content-Type": "application/json-patch+json"
@@ -666,45 +777,50 @@ var isInternalErrorResponse = function(resp) {
666
777
  };
667
778
  var deleteOp = function(opts) {
668
779
  return _async_to_generator(function() {
669
- var result, body;
780
+ var response, body;
670
781
  return _ts_generator(this, function(_state) {
671
782
  switch(_state.label){
672
783
  case 0:
673
784
  return [
674
785
  4,
675
- rawRequest({
676
- url: "/fhir/".concat(opts.type, "/").concat(opts.id),
786
+ internalRawRequest({
787
+ url: makeUrl([
788
+ basePath,
789
+ opts.type,
790
+ opts.id
791
+ ]),
677
792
  method: "DELETE"
678
793
  })
679
794
  ];
680
795
  case 1:
681
- result = _state.sent();
682
- if (result.response.status === 204) return [
796
+ response = _state.sent();
797
+ if (isInternalErrorResponse(response)) throw response.error;
798
+ if (response.response.status === 204) return [
683
799
  2,
684
800
  Ok(_object_spread({
685
801
  resource: undefined
686
- }, result))
802
+ }, response))
687
803
  ];
688
804
  return [
689
805
  4,
690
- coerceBody(result)
806
+ coerceBody(response)
691
807
  ];
692
808
  case 2:
693
809
  body = _state.sent();
694
- if (!result.response.ok) {
810
+ if (!response.response.ok) {
695
811
  if (body.resourceType === "OperationOutcome") return [
696
812
  2,
697
813
  Err(_object_spread({
698
814
  resource: body
699
- }, result))
815
+ }, response))
700
816
  ];
701
- throw new ErrorResponse("HTTP ".concat(result.response.status, ": ").concat(result.response.statusText), result);
817
+ throw new ErrorResponse("HTTP ".concat(response.response.status, ": ").concat(response.response.statusText), response);
702
818
  }
703
819
  return [
704
820
  2,
705
821
  Ok(_object_spread({
706
822
  resource: body
707
- }, result))
823
+ }, response))
708
824
  ];
709
825
  }
710
826
  });
@@ -718,7 +834,12 @@ var isInternalErrorResponse = function(resp) {
718
834
  return [
719
835
  4,
720
836
  request({
721
- url: "/fhir/".concat(opts.type, "/").concat(opts.id, "/_history"),
837
+ url: makeUrl([
838
+ basePath,
839
+ opts.type,
840
+ opts.id,
841
+ "_history"
842
+ ]),
722
843
  method: "DELETE"
723
844
  })
724
845
  ];
@@ -739,7 +860,13 @@ var isInternalErrorResponse = function(resp) {
739
860
  return [
740
861
  4,
741
862
  request({
742
- url: "/fhir/".concat(opts.type, "/").concat(opts.id, "/_history/").concat(opts.vid),
863
+ url: makeUrl([
864
+ basePath,
865
+ opts.type,
866
+ opts.id,
867
+ "_history",
868
+ opts.vid
869
+ ]),
743
870
  method: "DELETE"
744
871
  })
745
872
  ];
@@ -759,11 +886,11 @@ var isInternalErrorResponse = function(resp) {
759
886
  switch(_state.label){
760
887
  case 0:
761
888
  url = [
762
- "/fhir"
889
+ basePath
763
890
  ];
764
891
  if (opts.type) url.push(opts.type);
765
892
  requestParams = {
766
- url: url.join("/"),
893
+ url: makeUrl(url),
767
894
  method: "DELETE",
768
895
  params: opts.searchParameters
769
896
  };
@@ -780,25 +907,77 @@ var isInternalErrorResponse = function(resp) {
780
907
  });
781
908
  })();
782
909
  };
783
- var history = function(opts) {
910
+ var historyInstance = function(opts) {
784
911
  return _async_to_generator(function() {
785
912
  var url, requestParams;
786
913
  return _ts_generator(this, function(_state) {
787
914
  switch(_state.label){
788
915
  case 0:
789
916
  url = [
790
- "/fhir"
917
+ basePath,
918
+ opts.type,
919
+ opts.id,
920
+ "_history"
791
921
  ];
792
- if (opts.type) url.push(opts.type);
793
- if (opts.id) url.push(opts.id);
794
- url.push("_history");
795
922
  requestParams = {
796
- url: url.join("/"),
923
+ url: makeUrl(url),
924
+ method: "GET"
925
+ };
926
+ return [
927
+ 4,
928
+ request(requestParams)
929
+ ];
930
+ case 1:
931
+ return [
932
+ 2,
933
+ _state.sent()
934
+ ];
935
+ }
936
+ });
937
+ })();
938
+ };
939
+ var historySystem = function(_) {
940
+ return _async_to_generator(function() {
941
+ var url, requestParams;
942
+ return _ts_generator(this, function(_state) {
943
+ switch(_state.label){
944
+ case 0:
945
+ url = [
946
+ basePath,
947
+ "_history"
948
+ ];
949
+ requestParams = {
950
+ url: makeUrl(url),
951
+ method: "GET"
952
+ };
953
+ return [
954
+ 4,
955
+ request(requestParams)
956
+ ];
957
+ case 1:
958
+ return [
959
+ 2,
960
+ _state.sent()
961
+ ];
962
+ }
963
+ });
964
+ })();
965
+ };
966
+ var historyType = function(opts) {
967
+ return _async_to_generator(function() {
968
+ var url, requestParams;
969
+ return _ts_generator(this, function(_state) {
970
+ switch(_state.label){
971
+ case 0:
972
+ url = [
973
+ basePath,
974
+ opts.type,
975
+ "_history"
976
+ ];
977
+ requestParams = {
978
+ url: makeUrl(url),
797
979
  method: "GET"
798
980
  };
799
- if (opts.id && !opts.type) throw new RequestError("resource type must be specified if ID is provided", {
800
- request: requestParams
801
- });
802
981
  return [
803
982
  4,
804
983
  request(requestParams)
@@ -820,8 +999,14 @@ var isInternalErrorResponse = function(resp) {
820
999
  return [
821
1000
  4,
822
1001
  request({
823
- url: "/fhir/metadata",
1002
+ url: makeUrl([
1003
+ basePath,
1004
+ "metadata"
1005
+ ]),
824
1006
  method: "GET",
1007
+ headers: {
1008
+ Accept: "application/fhir+json"
1009
+ },
825
1010
  params: [
826
1011
  [
827
1012
  "mode",
@@ -851,7 +1036,9 @@ var isInternalErrorResponse = function(resp) {
851
1036
  return [
852
1037
  4,
853
1038
  request({
854
- url: "/fhir",
1039
+ url: makeUrl([
1040
+ basePath
1041
+ ]),
855
1042
  method: "POST",
856
1043
  params: [
857
1044
  [
@@ -879,7 +1066,9 @@ var isInternalErrorResponse = function(resp) {
879
1066
  return [
880
1067
  4,
881
1068
  request({
882
- url: "/fhir",
1069
+ url: makeUrl([
1070
+ basePath
1071
+ ]),
883
1072
  method: "POST",
884
1073
  params: [
885
1074
  [
@@ -906,13 +1095,13 @@ var isInternalErrorResponse = function(resp) {
906
1095
  switch(_state.label){
907
1096
  case 0:
908
1097
  url = [
909
- "/fhir"
1098
+ basePath
910
1099
  ];
911
1100
  if (opts.type) url.push(opts.type);
912
1101
  if (opts.id) url.push(opts.id);
913
1102
  url.push(opts.operation);
914
1103
  requestParams = {
915
- url: url.join("/"),
1104
+ url: makeUrl(url),
916
1105
  method: "POST"
917
1106
  };
918
1107
  if (opts.resource) requestParams.body = JSON.stringify(opts.resource);
@@ -951,7 +1140,7 @@ var isInternalErrorResponse = function(resp) {
951
1140
  };
952
1141
  return {
953
1142
  // General
954
- getBaseURL: getBaseURL,
1143
+ getBaseUrl: getBaseUrl,
955
1144
  rawRequest: rawRequest,
956
1145
  request: request,
957
1146
  // Aidbox
@@ -960,7 +1149,9 @@ var isInternalErrorResponse = function(resp) {
960
1149
  // FHIR HTTP
961
1150
  read: read,
962
1151
  vread: vread,
963
- search: search,
1152
+ searchType: searchType,
1153
+ searchSystem: searchSystem,
1154
+ searchCompartment: searchCompartment,
964
1155
  create: create,
965
1156
  conditionalCreate: conditionalCreate,
966
1157
  update: update,
@@ -971,7 +1162,9 @@ var isInternalErrorResponse = function(resp) {
971
1162
  deleteHistory: deleteHistory,
972
1163
  deleteHistoryVersion: deleteHistoryVersion,
973
1164
  conditionalDelete: conditionalDelete,
974
- history: history,
1165
+ historyInstance: historyInstance,
1166
+ historyType: historyType,
1167
+ historySystem: historySystem,
975
1168
  capabilities: capabilities,
976
1169
  batch: batch,
977
1170
  transaction: transaction,