@spoketech/hub-client 0.1.6 → 0.1.9

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,67 @@ 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
+ status;
3719
+ method;
3720
+ url;
3721
+ reason;
3722
+ cause;
3723
+ constructor(input) {
3724
+ super(input.message);
3725
+ this.name = "SpokeHubClientError";
3726
+ this.kind = input.kind;
3727
+ this.code = input.code;
3728
+ this.body = input.body;
3729
+ this.status = input.status;
3730
+ this.method = input.method;
3731
+ this.url = input.url;
3732
+ this.reason = input.reason;
3733
+ this.cause = input.cause;
3734
+ Object.defineProperty(this, "cause", {
3735
+ value: input.cause,
3736
+ enumerable: false,
3737
+ configurable: true
3738
+ });
3739
+ }
3740
+ toJSON() {
3741
+ return Object.fromEntries(
3742
+ Object.entries({
3743
+ name: this.name,
3744
+ kind: this.kind,
3745
+ code: this.code,
3746
+ message: this.message,
3747
+ status: this.status,
3748
+ method: this.method,
3749
+ url: this.url,
3750
+ reason: this.reason,
3751
+ body: this.body
3752
+ }).filter(([, value]) => value !== void 0)
3753
+ );
3754
+ }
3755
+ [/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
3756
+ return this.toJSON();
3757
+ }
3758
+ };
3759
+ var SpokeHubApiError = class extends SpokeHubClientError {
3760
+ constructor(body, cause, context = {}) {
3761
+ super({
3762
+ kind: "api",
3763
+ message: body.message ?? body.code ?? "Spoke Hub API request failed",
3764
+ code: body.code,
3765
+ body,
3766
+ status: context.status,
3767
+ method: context.method,
3768
+ url: context.url,
3769
+ reason: context.reason,
3770
+ cause
3771
+ });
3772
+ this.name = "SpokeHubApiError";
3773
+ }
3774
+ };
3714
3775
  var defaultApiBasePath = "/api";
3715
3776
  function trimSlashes(value) {
3716
3777
  return value.replace(/^\/+|\/+$/g, "");
@@ -3803,6 +3864,153 @@ function configureSignedHttpClient(client, options) {
3803
3864
  })
3804
3865
  );
3805
3866
  }
3867
+ var fiberFailureCauseSymbol = /* @__PURE__ */ Symbol.for("effect/Runtime/FiberFailure/Cause");
3868
+ function isRecord(value) {
3869
+ return !!value && typeof value === "object";
3870
+ }
3871
+ function stringFromUnknown(value) {
3872
+ if (typeof value === "string" && value.length > 0) {
3873
+ return value;
3874
+ }
3875
+ if (value instanceof URL) {
3876
+ return value.toString();
3877
+ }
3878
+ return void 0;
3879
+ }
3880
+ function numberFromUnknown(value) {
3881
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
3882
+ }
3883
+ function failureErrorFromUnknown(value) {
3884
+ if (!isRecord(value)) {
3885
+ return void 0;
3886
+ }
3887
+ const fiberFailureCause = value[fiberFailureCauseSymbol];
3888
+ if (isRecord(fiberFailureCause) && "error" in fiberFailureCause) {
3889
+ return fiberFailureCause.error;
3890
+ }
3891
+ return void 0;
3892
+ }
3893
+ function errorCandidatesFromUnknown(value) {
3894
+ const candidates = [value];
3895
+ const failureError = failureErrorFromUnknown(value);
3896
+ if (failureError) {
3897
+ candidates.push(failureError);
3898
+ }
3899
+ for (const candidate of [...candidates]) {
3900
+ if (candidate instanceof Error && candidate.cause) {
3901
+ candidates.push(candidate.cause);
3902
+ } else if (isRecord(candidate) && candidate.cause) {
3903
+ candidates.push(candidate.cause);
3904
+ }
3905
+ }
3906
+ return candidates;
3907
+ }
3908
+ function httpContextFromMessage(message) {
3909
+ const match = message.match(/\((\d{3})\s+([A-Z]+)\s+([^)]+)\)/);
3910
+ if (!match) {
3911
+ return {};
3912
+ }
3913
+ return {
3914
+ status: Number(match[1]),
3915
+ method: match[2],
3916
+ url: match[3]
3917
+ };
3918
+ }
3919
+ function httpContextFromUnknown(value) {
3920
+ const context = {};
3921
+ for (const candidate of errorCandidatesFromUnknown(value)) {
3922
+ if (candidate instanceof Error) {
3923
+ Object.assign(context, httpContextFromMessage(candidate.message));
3924
+ }
3925
+ if (!isRecord(candidate)) {
3926
+ continue;
3927
+ }
3928
+ const request = candidate.request;
3929
+ const response = candidate.response;
3930
+ const requestRecord = isRecord(request) ? request : void 0;
3931
+ const responseRecord = isRecord(response) ? response : void 0;
3932
+ context.status ??= numberFromUnknown(responseRecord?.status);
3933
+ context.method ??= stringFromUnknown(requestRecord?.method);
3934
+ context.url ??= stringFromUnknown(requestRecord?.url) ?? stringFromUnknown(responseRecord?.url);
3935
+ context.reason ??= stringFromUnknown(candidate.reason);
3936
+ if (context.status !== void 0 && context.method !== void 0 && context.url !== void 0 && context.reason !== void 0) {
3937
+ break;
3938
+ }
3939
+ }
3940
+ return context;
3941
+ }
3942
+ function hasApiErrorBody(value) {
3943
+ if (value instanceof Error) {
3944
+ return false;
3945
+ }
3946
+ const body = value;
3947
+ return !!value && typeof value === "object" && (typeof body.message === "string" || typeof body.code === "string");
3948
+ }
3949
+ function apiErrorBodyFromUnknown(value) {
3950
+ for (const candidate of errorCandidatesFromUnknown(value)) {
3951
+ if (hasApiErrorBody(candidate)) {
3952
+ return candidate;
3953
+ }
3954
+ }
3955
+ return null;
3956
+ }
3957
+ function errorMessageFromUnknown(value) {
3958
+ for (const candidate of errorCandidatesFromUnknown(value)) {
3959
+ if (candidate instanceof Error && candidate.message.length > 0) {
3960
+ return candidate.message;
3961
+ }
3962
+ if (typeof candidate === "string" && candidate.length > 0) {
3963
+ return candidate;
3964
+ }
3965
+ }
3966
+ return "Spoke Hub client request failed";
3967
+ }
3968
+ function classifyClientError(value) {
3969
+ const context = httpContextFromUnknown(value);
3970
+ if (context.status !== void 0) {
3971
+ return "api";
3972
+ }
3973
+ const message = errorMessageFromUnknown(value).toLowerCase();
3974
+ if (value instanceof TypeError || message.includes("fetch failed") || message.includes("network") || message.includes("econnrefused") || message.includes("enotfound") || message.includes("etimedout")) {
3975
+ return "network";
3976
+ }
3977
+ if (message.includes("private key") || message.includes("asn1") || message.includes("key object") || message.includes("decoder")) {
3978
+ return "client";
3979
+ }
3980
+ return "unknown";
3981
+ }
3982
+ function clientErrorFromUnknown(value) {
3983
+ const body = apiErrorBodyFromUnknown(value);
3984
+ const context = httpContextFromUnknown(value);
3985
+ if (body) {
3986
+ return new SpokeHubApiError(body, value, context);
3987
+ }
3988
+ const statusCode = context.status ? `http_${context.status}` : void 0;
3989
+ return new SpokeHubClientError({
3990
+ kind: classifyClientError(value),
3991
+ code: statusCode,
3992
+ message: errorMessageFromUnknown(value),
3993
+ status: context.status,
3994
+ method: context.method,
3995
+ url: context.url,
3996
+ reason: context.reason,
3997
+ cause: value
3998
+ });
3999
+ }
4000
+ async function runPromise2(effect) {
4001
+ try {
4002
+ return [await Effect.runPromise(effect), null];
4003
+ } catch (error) {
4004
+ return [null, clientErrorFromUnknown(error)];
4005
+ }
4006
+ }
4007
+ async function runPromiseUnsafe(effect) {
4008
+ const [data, error] = await runPromise2(effect);
4009
+ if (error !== null) {
4010
+ throw error;
4011
+ }
4012
+ return data;
4013
+ }
3806
4014
  function resolveHubApiBaseUrl(baseUrl, apiBasePath = defaultApiBasePath) {
3807
4015
  const resolved = new URL(baseUrl.toString());
3808
4016
  resolved.pathname = joinUrlPath(resolved.pathname, apiBasePath);
@@ -3838,7 +4046,7 @@ function toPromiseClient(value) {
3838
4046
  if (typeof member === "function") {
3839
4047
  return [
3840
4048
  key,
3841
- (...args) => Effect.runPromise(
4049
+ (...args) => runPromise2(
3842
4050
  member(
3843
4051
  ...args
3844
4052
  )
@@ -3852,23 +4060,29 @@ function toPromiseClient(value) {
3852
4060
  });
3853
4061
  return Object.fromEntries(entries);
3854
4062
  }
3855
- var makePromiseHubApiClient = async (options) => toPromiseClient(await Effect.runPromise(makeHubApiClient(options)));
4063
+ var makePromiseHubApiClient = async (options) => toPromiseClient(await runPromiseUnsafe(makeHubApiClient(options)));
3856
4064
  var makePromiseServiceHubApiClient = async (options) => {
3857
- const { client } = await Effect.runPromise(makeServiceHubApiClient(options));
4065
+ const { client } = await runPromiseUnsafe(makeServiceHubApiClient(options));
3858
4066
  return {
3859
4067
  client: toPromiseClient(client)
3860
4068
  };
3861
4069
  };
3862
4070
 
3863
4071
  // src/onsite-resource-client.ts
4072
+ function unwrapMaybeResult(result) {
4073
+ return Array.isArray(result) && result.length === 2 ? result[0] : result;
4074
+ }
3864
4075
  function makeOnsiteResourceClient(client) {
3865
4076
  const call = async (input) => {
3866
4077
  const { organizationId, ...payload } = input;
3867
- const result = await client.onsiteCalls.callOnsiteResource({
4078
+ const [result, error] = await client.onsiteCalls.callOnsiteResource({
3868
4079
  path: { organizationId },
3869
4080
  payload
3870
4081
  });
3871
- return Array.isArray(result) ? result[0] : result;
4082
+ if (error !== null) {
4083
+ return [null, error];
4084
+ }
4085
+ return [unwrapMaybeResult(result), null];
3872
4086
  };
3873
4087
  return {
3874
4088
  call,
@@ -3930,9 +4144,15 @@ function cleanParams(value) {
3930
4144
  ])
3931
4145
  );
3932
4146
  }
4147
+ function unwrapMaybeResult2(result) {
4148
+ return Array.isArray(result) && result.length === 2 ? result[0] : result;
4149
+ }
3933
4150
  async function unwrap(value) {
3934
- const result = await value;
3935
- return Array.isArray(result) ? result[0] : result;
4151
+ const [result, error] = await value;
4152
+ if (error !== null) {
4153
+ return [null, error];
4154
+ }
4155
+ return [unwrapMaybeResult2(result), null];
3936
4156
  }
3937
4157
  function makeSpokeClient(hubClient) {
3938
4158
  const onsite = makeOnsiteResourceClient(hubClient);
@@ -4465,6 +4685,8 @@ export {
4465
4685
  SourceManagedMemberLookupCommandPayloadSchema,
4466
4686
  SourceManagedMemberRenewalCommandPayloadSchema,
4467
4687
  SourceManagedMemberUpdateCommandPayloadSchema,
4688
+ SpokeHubApiError,
4689
+ SpokeHubClientError,
4468
4690
  TimestampSchema,
4469
4691
  TriggerOrganizationMemberSyncSchema,
4470
4692
  UnauthorizedErrorSchema,