@spoketech/hub-client 0.1.5 → 0.1.8

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/dist/index.js CHANGED
@@ -3711,6 +3711,32 @@ var HubApi = HttpApi.make("hub").add(authGroup).add(organizationsGroup).add(memb
3711
3711
  );
3712
3712
 
3713
3713
  // src/client.ts
3714
+ var SpokeHubClientError = class extends Error {
3715
+ kind;
3716
+ code;
3717
+ body;
3718
+ cause;
3719
+ constructor(input) {
3720
+ super(input.message);
3721
+ this.name = "SpokeHubClientError";
3722
+ this.kind = input.kind;
3723
+ this.code = input.code;
3724
+ this.body = input.body;
3725
+ this.cause = input.cause;
3726
+ }
3727
+ };
3728
+ var SpokeHubApiError = class extends SpokeHubClientError {
3729
+ constructor(body, cause) {
3730
+ super({
3731
+ kind: "api",
3732
+ message: body.message ?? body.code ?? "Spoke Hub API request failed",
3733
+ code: body.code,
3734
+ body,
3735
+ cause
3736
+ });
3737
+ this.name = "SpokeHubApiError";
3738
+ }
3739
+ };
3714
3740
  var defaultApiBasePath = "/api";
3715
3741
  function trimSlashes(value) {
3716
3742
  return value.replace(/^\/+|\/+$/g, "");
@@ -3746,6 +3772,20 @@ function normalizeSignedQuery(url) {
3746
3772
  );
3747
3773
  return entries.map(([key, value]) => `${encodeQueryComponent(key)}=${encodeQueryComponent(value)}`).join("&");
3748
3774
  }
3775
+ function resolveSignedRequestUrl(requestUrl, apiBaseUrl) {
3776
+ const baseUrl = new URL(apiBaseUrl);
3777
+ try {
3778
+ return new URL(requestUrl);
3779
+ } catch {
3780
+ const relativeUrl = new URL(requestUrl, "http://spoke.local");
3781
+ const basePath = baseUrl.pathname.replace(/\/+$/, "");
3782
+ const requestPath = relativeUrl.pathname.replace(/^\/+/, "");
3783
+ baseUrl.pathname = `${basePath}/${requestPath}`.replace(/\/{2,}/g, "/");
3784
+ baseUrl.search = relativeUrl.search;
3785
+ baseUrl.hash = "";
3786
+ return baseUrl;
3787
+ }
3788
+ }
3749
3789
  function signRequestWithPrivateKey(input) {
3750
3790
  const payload = [
3751
3791
  input.method.trim().toUpperCase(),
@@ -3764,7 +3804,7 @@ function signRequestWithPrivateKey(input) {
3764
3804
  function configureSignedHttpClient(client, options) {
3765
3805
  return client.pipe(
3766
3806
  HttpClient.mapRequest((request) => {
3767
- const url = new URL(request.url, options.apiBaseUrl);
3807
+ const url = resolveSignedRequestUrl(request.url, options.apiBaseUrl);
3768
3808
  for (const [key, value] of request.urlParams) {
3769
3809
  url.searchParams.append(key, value);
3770
3810
  }
@@ -3789,6 +3829,82 @@ function configureSignedHttpClient(client, options) {
3789
3829
  })
3790
3830
  );
3791
3831
  }
3832
+ function hasApiErrorBody(value) {
3833
+ if (value instanceof Error) {
3834
+ return false;
3835
+ }
3836
+ const body = value;
3837
+ return !!value && typeof value === "object" && (typeof body.message === "string" || typeof body.code === "string");
3838
+ }
3839
+ function apiErrorBodyFromUnknown(value) {
3840
+ if (hasApiErrorBody(value)) {
3841
+ return value;
3842
+ }
3843
+ if (value instanceof Error) {
3844
+ const cause = value.cause;
3845
+ if (hasApiErrorBody(cause)) {
3846
+ return cause;
3847
+ }
3848
+ }
3849
+ if (value && typeof value === "object") {
3850
+ const cause = value.cause;
3851
+ if (hasApiErrorBody(cause)) {
3852
+ return cause;
3853
+ }
3854
+ const fiberFailureCause = value[/* @__PURE__ */ Symbol.for("effect/Runtime/FiberFailure/Cause")];
3855
+ if (fiberFailureCause && typeof fiberFailureCause === "object") {
3856
+ const error = fiberFailureCause.error;
3857
+ if (hasApiErrorBody(error)) {
3858
+ return error;
3859
+ }
3860
+ }
3861
+ }
3862
+ return null;
3863
+ }
3864
+ function errorMessageFromUnknown(value) {
3865
+ if (value instanceof Error && value.message.length > 0) {
3866
+ return value.message;
3867
+ }
3868
+ if (typeof value === "string" && value.length > 0) {
3869
+ return value;
3870
+ }
3871
+ return "Spoke Hub client request failed";
3872
+ }
3873
+ function classifyClientError(value) {
3874
+ const message = errorMessageFromUnknown(value).toLowerCase();
3875
+ if (value instanceof TypeError || message.includes("fetch failed") || message.includes("network") || message.includes("econnrefused") || message.includes("enotfound") || message.includes("etimedout")) {
3876
+ return "network";
3877
+ }
3878
+ if (message.includes("private key") || message.includes("asn1") || message.includes("key object") || message.includes("decoder")) {
3879
+ return "client";
3880
+ }
3881
+ return "unknown";
3882
+ }
3883
+ function clientErrorFromUnknown(value) {
3884
+ const body = apiErrorBodyFromUnknown(value);
3885
+ if (body) {
3886
+ return new SpokeHubApiError(body, value);
3887
+ }
3888
+ return new SpokeHubClientError({
3889
+ kind: classifyClientError(value),
3890
+ message: errorMessageFromUnknown(value),
3891
+ cause: value
3892
+ });
3893
+ }
3894
+ async function runPromise2(effect) {
3895
+ try {
3896
+ return [await Effect.runPromise(effect), null];
3897
+ } catch (error) {
3898
+ return [null, clientErrorFromUnknown(error)];
3899
+ }
3900
+ }
3901
+ async function runPromiseUnsafe(effect) {
3902
+ const [data, error] = await runPromise2(effect);
3903
+ if (error !== null) {
3904
+ throw error;
3905
+ }
3906
+ return data;
3907
+ }
3792
3908
  function resolveHubApiBaseUrl(baseUrl, apiBasePath = defaultApiBasePath) {
3793
3909
  const resolved = new URL(baseUrl.toString());
3794
3910
  resolved.pathname = joinUrlPath(resolved.pathname, apiBasePath);
@@ -3824,7 +3940,7 @@ function toPromiseClient(value) {
3824
3940
  if (typeof member === "function") {
3825
3941
  return [
3826
3942
  key,
3827
- (...args) => Effect.runPromise(
3943
+ (...args) => runPromise2(
3828
3944
  member(
3829
3945
  ...args
3830
3946
  )
@@ -3838,23 +3954,29 @@ function toPromiseClient(value) {
3838
3954
  });
3839
3955
  return Object.fromEntries(entries);
3840
3956
  }
3841
- var makePromiseHubApiClient = async (options) => toPromiseClient(await Effect.runPromise(makeHubApiClient(options)));
3957
+ var makePromiseHubApiClient = async (options) => toPromiseClient(await runPromiseUnsafe(makeHubApiClient(options)));
3842
3958
  var makePromiseServiceHubApiClient = async (options) => {
3843
- const { client } = await Effect.runPromise(makeServiceHubApiClient(options));
3959
+ const { client } = await runPromiseUnsafe(makeServiceHubApiClient(options));
3844
3960
  return {
3845
3961
  client: toPromiseClient(client)
3846
3962
  };
3847
3963
  };
3848
3964
 
3849
3965
  // src/onsite-resource-client.ts
3966
+ function unwrapMaybeResult(result) {
3967
+ return Array.isArray(result) && result.length === 2 ? result[0] : result;
3968
+ }
3850
3969
  function makeOnsiteResourceClient(client) {
3851
3970
  const call = async (input) => {
3852
3971
  const { organizationId, ...payload } = input;
3853
- const result = await client.onsiteCalls.callOnsiteResource({
3972
+ const [result, error] = await client.onsiteCalls.callOnsiteResource({
3854
3973
  path: { organizationId },
3855
3974
  payload
3856
3975
  });
3857
- return Array.isArray(result) ? result[0] : result;
3976
+ if (error !== null) {
3977
+ return [null, error];
3978
+ }
3979
+ return [unwrapMaybeResult(result), null];
3858
3980
  };
3859
3981
  return {
3860
3982
  call,
@@ -3916,9 +4038,15 @@ function cleanParams(value) {
3916
4038
  ])
3917
4039
  );
3918
4040
  }
4041
+ function unwrapMaybeResult2(result) {
4042
+ return Array.isArray(result) && result.length === 2 ? result[0] : result;
4043
+ }
3919
4044
  async function unwrap(value) {
3920
- const result = await value;
3921
- return Array.isArray(result) ? result[0] : result;
4045
+ const [result, error] = await value;
4046
+ if (error !== null) {
4047
+ return [null, error];
4048
+ }
4049
+ return [unwrapMaybeResult2(result), null];
3922
4050
  }
3923
4051
  function makeSpokeClient(hubClient) {
3924
4052
  const onsite = makeOnsiteResourceClient(hubClient);
@@ -4451,6 +4579,8 @@ export {
4451
4579
  SourceManagedMemberLookupCommandPayloadSchema,
4452
4580
  SourceManagedMemberRenewalCommandPayloadSchema,
4453
4581
  SourceManagedMemberUpdateCommandPayloadSchema,
4582
+ SpokeHubApiError,
4583
+ SpokeHubClientError,
4454
4584
  TimestampSchema,
4455
4585
  TriggerOrganizationMemberSyncSchema,
4456
4586
  UnauthorizedErrorSchema,