@open-apime/sdk 0.3.0 → 0.4.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.
package/dist/index.cjs CHANGED
@@ -29,6 +29,7 @@ __export(index_exports, {
29
29
  ConfigurationError: () => ConfigurationError,
30
30
  ConflictError: () => ConflictError,
31
31
  ConnectionError: () => ConnectionError,
32
+ DEFAULT_HEALTH_TIMEOUT_MS: () => DEFAULT_HEALTH_TIMEOUT_MS,
32
33
  InvalidRequestError: () => InvalidRequestError,
33
34
  InvalidSignatureError: () => InvalidSignatureError,
34
35
  NotFoundError: () => NotFoundError,
@@ -37,6 +38,7 @@ __export(index_exports, {
37
38
  SessionUnavailableError: () => SessionUnavailableError,
38
39
  TimeoutError: () => TimeoutError,
39
40
  UnprocessableError: () => UnprocessableError,
41
+ checkHealth: () => checkHealth,
40
42
  constructEvent: () => constructEvent,
41
43
  hasSentry: () => hasSentry,
42
44
  verifyWebhookSignature: () => verifyWebhookSignature
@@ -494,7 +496,7 @@ var MessagesResource = class {
494
496
  return this.http.request({ method: "POST", path: `${this.base}/audio`, body: form, ...options ? { options } : {} });
495
497
  }
496
498
  sendDocument(input, options) {
497
- const form = toForm(input, ["file"]);
499
+ const form = toForm(input, ["file", "filename"]);
498
500
  form.set("file", input.file, input.filename ?? fileName(input.file, "document"));
499
501
  return this.http.request({ method: "POST", path: `${this.base}/document`, body: form, ...options ? { options } : {} });
500
502
  }
@@ -516,7 +518,13 @@ function toForm(input, skip) {
516
518
  const form = new FormData();
517
519
  for (const [key, value] of Object.entries(input)) {
518
520
  if (skip.includes(key) || value === void 0 || value === null) continue;
519
- form.set(key, typeof value === "string" ? value : String(value));
521
+ if (typeof value === "string") {
522
+ form.set(key, value);
523
+ } else if (Array.isArray(value) || typeof value === "object" && !(value instanceof Blob)) {
524
+ form.set(key, JSON.stringify(value));
525
+ } else {
526
+ form.set(key, String(value));
527
+ }
520
528
  }
521
529
  return form;
522
530
  }
@@ -779,6 +787,31 @@ var Apime = {
779
787
  }
780
788
  };
781
789
 
790
+ // src/health.ts
791
+ var DEFAULT_HEALTH_TIMEOUT_MS = 1e4;
792
+ async function checkHealth(baseUrl, options = {}) {
793
+ const url = buildUrl(assertUsableBaseUrl(baseUrl), "/healthz");
794
+ const impl = options.fetch ?? globalThis.fetch;
795
+ const timeoutMs = options.timeoutMs ?? DEFAULT_HEALTH_TIMEOUT_MS;
796
+ const controller = new AbortController();
797
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
798
+ try {
799
+ const response = await impl(url, { method: "GET", signal: controller.signal });
800
+ if (!response.ok) {
801
+ throw new ApimeError(`apime respondeu ${response.status} no healthz`, { status: response.status });
802
+ }
803
+ return await response.json();
804
+ } catch (cause) {
805
+ if (cause instanceof ApimeError) throw cause;
806
+ if (controller.signal.aborted) {
807
+ throw new TimeoutError(`o healthz passou de ${timeoutMs}ms`, { cause });
808
+ }
809
+ throw new ConnectionError("falha de rede ao falar com o apime", { cause });
810
+ } finally {
811
+ clearTimeout(timer);
812
+ }
813
+ }
814
+
782
815
  // src/webhook/index.ts
783
816
  var import_node_crypto = require("crypto");
784
817
  var InvalidSignatureError = class extends ApimeError {
@@ -816,6 +849,7 @@ function constructEvent(rawBody, signature, secret) {
816
849
  ConfigurationError,
817
850
  ConflictError,
818
851
  ConnectionError,
852
+ DEFAULT_HEALTH_TIMEOUT_MS,
819
853
  InvalidRequestError,
820
854
  InvalidSignatureError,
821
855
  NotFoundError,
@@ -824,6 +858,7 @@ function constructEvent(rawBody, signature, secret) {
824
858
  SessionUnavailableError,
825
859
  TimeoutError,
826
860
  UnprocessableError,
861
+ checkHealth,
827
862
  constructEvent,
828
863
  hasSentry,
829
864
  verifyWebhookSignature
package/dist/index.d.cts CHANGED
@@ -523,4 +523,16 @@ declare const Apime: {
523
523
  }, options: ClientOptions): ApimeInstanceClient;
524
524
  };
525
525
 
526
- export { type ApiToken, type ApiTokenAuth, Apime, ApimeError, ApimeInstanceClient, ApimeUserClient, type Auth, type AuthKind, type CheckNumberResult, type ClientOptions, type ContactEntry, type CreateInstanceInput, EventLogEntry, type FailureInfo, type GroupInfo, type GroupParticipant, Instance, InstanceInfo, type InstanceTokenAuth, type IpFamily, type JoinRequestAction, type MarkReadInput, type Observer, type ParticipantAction, type PresenceState, Profile, QrCode, type QuoteInput, type RequestInfo, type RequestOptions, type SendAudioInput, type SendContactInput, type SendDocumentInput, type SendLocationInput, type SendMediaInput, type SendTextInput, SentMessage, type SuccessInfo, type UpdateInstanceInput, type User, type UserAuth, type UserJwtAuth, hasSentry };
526
+ interface HealthOptions {
527
+ /** Kept short on purpose: a health check that hangs is worse than one that fails. */
528
+ timeoutMs?: number;
529
+ fetch?: typeof globalThis.fetch;
530
+ }
531
+ interface HealthResult {
532
+ status?: string;
533
+ [key: string]: unknown;
534
+ }
535
+ declare const DEFAULT_HEALTH_TIMEOUT_MS = 10000;
536
+ declare function checkHealth(baseUrl: string, options?: HealthOptions): Promise<HealthResult>;
537
+
538
+ export { type ApiToken, type ApiTokenAuth, Apime, ApimeError, ApimeInstanceClient, ApimeUserClient, type Auth, type AuthKind, type CheckNumberResult, type ClientOptions, type ContactEntry, type CreateInstanceInput, DEFAULT_HEALTH_TIMEOUT_MS, EventLogEntry, type FailureInfo, type GroupInfo, type GroupParticipant, type HealthOptions, type HealthResult, Instance, InstanceInfo, type InstanceTokenAuth, type IpFamily, type JoinRequestAction, type MarkReadInput, type Observer, type ParticipantAction, type PresenceState, Profile, QrCode, type QuoteInput, type RequestInfo, type RequestOptions, type SendAudioInput, type SendContactInput, type SendDocumentInput, type SendLocationInput, type SendMediaInput, type SendTextInput, SentMessage, type SuccessInfo, type UpdateInstanceInput, type User, type UserAuth, type UserJwtAuth, checkHealth, hasSentry };
package/dist/index.d.ts CHANGED
@@ -523,4 +523,16 @@ declare const Apime: {
523
523
  }, options: ClientOptions): ApimeInstanceClient;
524
524
  };
525
525
 
526
- export { type ApiToken, type ApiTokenAuth, Apime, ApimeError, ApimeInstanceClient, ApimeUserClient, type Auth, type AuthKind, type CheckNumberResult, type ClientOptions, type ContactEntry, type CreateInstanceInput, EventLogEntry, type FailureInfo, type GroupInfo, type GroupParticipant, Instance, InstanceInfo, type InstanceTokenAuth, type IpFamily, type JoinRequestAction, type MarkReadInput, type Observer, type ParticipantAction, type PresenceState, Profile, QrCode, type QuoteInput, type RequestInfo, type RequestOptions, type SendAudioInput, type SendContactInput, type SendDocumentInput, type SendLocationInput, type SendMediaInput, type SendTextInput, SentMessage, type SuccessInfo, type UpdateInstanceInput, type User, type UserAuth, type UserJwtAuth, hasSentry };
526
+ interface HealthOptions {
527
+ /** Kept short on purpose: a health check that hangs is worse than one that fails. */
528
+ timeoutMs?: number;
529
+ fetch?: typeof globalThis.fetch;
530
+ }
531
+ interface HealthResult {
532
+ status?: string;
533
+ [key: string]: unknown;
534
+ }
535
+ declare const DEFAULT_HEALTH_TIMEOUT_MS = 10000;
536
+ declare function checkHealth(baseUrl: string, options?: HealthOptions): Promise<HealthResult>;
537
+
538
+ export { type ApiToken, type ApiTokenAuth, Apime, ApimeError, ApimeInstanceClient, ApimeUserClient, type Auth, type AuthKind, type CheckNumberResult, type ClientOptions, type ContactEntry, type CreateInstanceInput, DEFAULT_HEALTH_TIMEOUT_MS, EventLogEntry, type FailureInfo, type GroupInfo, type GroupParticipant, type HealthOptions, type HealthResult, Instance, InstanceInfo, type InstanceTokenAuth, type IpFamily, type JoinRequestAction, type MarkReadInput, type Observer, type ParticipantAction, type PresenceState, Profile, QrCode, type QuoteInput, type RequestInfo, type RequestOptions, type SendAudioInput, type SendContactInput, type SendDocumentInput, type SendLocationInput, type SendMediaInput, type SendTextInput, SentMessage, type SuccessInfo, type UpdateInstanceInput, type User, type UserAuth, type UserJwtAuth, checkHealth, hasSentry };
package/dist/index.js CHANGED
@@ -425,7 +425,7 @@ var MessagesResource = class {
425
425
  return this.http.request({ method: "POST", path: `${this.base}/audio`, body: form, ...options ? { options } : {} });
426
426
  }
427
427
  sendDocument(input, options) {
428
- const form = toForm(input, ["file"]);
428
+ const form = toForm(input, ["file", "filename"]);
429
429
  form.set("file", input.file, input.filename ?? fileName(input.file, "document"));
430
430
  return this.http.request({ method: "POST", path: `${this.base}/document`, body: form, ...options ? { options } : {} });
431
431
  }
@@ -447,7 +447,13 @@ function toForm(input, skip) {
447
447
  const form = new FormData();
448
448
  for (const [key, value] of Object.entries(input)) {
449
449
  if (skip.includes(key) || value === void 0 || value === null) continue;
450
- form.set(key, typeof value === "string" ? value : String(value));
450
+ if (typeof value === "string") {
451
+ form.set(key, value);
452
+ } else if (Array.isArray(value) || typeof value === "object" && !(value instanceof Blob)) {
453
+ form.set(key, JSON.stringify(value));
454
+ } else {
455
+ form.set(key, String(value));
456
+ }
451
457
  }
452
458
  return form;
453
459
  }
@@ -709,6 +715,31 @@ var Apime = {
709
715
  );
710
716
  }
711
717
  };
718
+
719
+ // src/health.ts
720
+ var DEFAULT_HEALTH_TIMEOUT_MS = 1e4;
721
+ async function checkHealth(baseUrl, options = {}) {
722
+ const url = buildUrl(assertUsableBaseUrl(baseUrl), "/healthz");
723
+ const impl = options.fetch ?? globalThis.fetch;
724
+ const timeoutMs = options.timeoutMs ?? DEFAULT_HEALTH_TIMEOUT_MS;
725
+ const controller = new AbortController();
726
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
727
+ try {
728
+ const response = await impl(url, { method: "GET", signal: controller.signal });
729
+ if (!response.ok) {
730
+ throw new ApimeError(`apime respondeu ${response.status} no healthz`, { status: response.status });
731
+ }
732
+ return await response.json();
733
+ } catch (cause) {
734
+ if (cause instanceof ApimeError) throw cause;
735
+ if (controller.signal.aborted) {
736
+ throw new TimeoutError(`o healthz passou de ${timeoutMs}ms`, { cause });
737
+ }
738
+ throw new ConnectionError("falha de rede ao falar com o apime", { cause });
739
+ } finally {
740
+ clearTimeout(timer);
741
+ }
742
+ }
712
743
  export {
713
744
  ApiError,
714
745
  Apime,
@@ -719,6 +750,7 @@ export {
719
750
  ConfigurationError,
720
751
  ConflictError,
721
752
  ConnectionError,
753
+ DEFAULT_HEALTH_TIMEOUT_MS,
722
754
  InvalidRequestError,
723
755
  InvalidSignatureError,
724
756
  NotFoundError,
@@ -727,6 +759,7 @@ export {
727
759
  SessionUnavailableError,
728
760
  TimeoutError,
729
761
  UnprocessableError,
762
+ checkHealth,
730
763
  constructEvent,
731
764
  hasSentry,
732
765
  verifyWebhookSignature
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-apime/sdk",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Cliente TypeScript da API do apime: WhatsApp, instâncias e webhooks",
5
5
  "license": "MIT",
6
6
  "repository": {