glitchgrab 1.63.1 → 1.65.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/server.mjs CHANGED
@@ -256,13 +256,14 @@ async function callSdkApi(path, token, options, init) {
256
256
  const controller = typeof AbortController === "function" ? new AbortController() : void 0;
257
257
  const timer = controller ? setTimeout(() => controller.abort(), (_d = options.timeoutMs) != null ? _d : SDK_API_TIMEOUT_MS) : void 0;
258
258
  try {
259
+ const multipart = typeof FormData !== "undefined" && init.body instanceof FormData;
259
260
  const request = {
260
261
  method: (_e = init.method) != null ? _e : "GET",
261
262
  headers: {
262
263
  Authorization: `Bearer ${token}`,
263
- ...init.body === void 0 ? {} : { "Content-Type": "application/json" }
264
+ ...init.body === void 0 || multipart ? {} : { "Content-Type": "application/json" }
264
265
  },
265
- ...init.body === void 0 ? {} : { body: JSON.stringify(init.body) },
266
+ ...init.body === void 0 ? {} : { body: multipart ? init.body : JSON.stringify(init.body) },
266
267
  ...init.cache === "no-store" ? { cache: "no-store" } : { next: { revalidate: init.cache.revalidate } },
267
268
  ...controller ? { signal: controller.signal } : {}
268
269
  };
@@ -831,12 +832,19 @@ function dialFollowUp(externalId, call, options = {}) {
831
832
  ringNumbers: Array.isArray(d.ringNumbers) ? d.ringNumbers.filter((n) => typeof n === "string") : []
832
833
  }));
833
834
  }
835
+ function ownerQuery(options) {
836
+ return options.ownerId ? `?ownerId=${encodeURIComponent(options.ownerId)}` : "";
837
+ }
838
+ function operatorQuery(operator, options) {
839
+ const query = new URLSearchParams("email" in operator ? { email: operator.email } : { phone: operator.phone });
840
+ if (options.ownerId) query.set("ownerId", options.ownerId);
841
+ return query.toString();
842
+ }
834
843
  async function getOperatorRingNumbers(operator, options = {}) {
835
844
  try {
836
845
  const key = callsKey(options);
837
846
  if (!key) return { ok: false, error: NO_KEY_ERROR };
838
- const query = new URLSearchParams("email" in operator ? { email: operator.email } : { phone: operator.phone });
839
- const { data, error, code } = await callSdkApi(`/api/v1/sdk/phone-lines/operator?${query.toString()}`, key, options, {
847
+ const { data, error, code } = await callSdkApi(`/api/v1/sdk/phone-lines/operator?${operatorQuery(operator, options)}`, key, options, {
840
848
  cache: "no-store"
841
849
  });
842
850
  if (error !== null || !isRecord(data)) return { ok: false, error: error != null ? error : "Unexpected response", ...code ? { code } : {} };
@@ -875,9 +883,143 @@ async function addLineCaller(caller, options = {}) {
875
883
  return { ok: false, error: "Could not reach Glitchgrab" };
876
884
  }
877
885
  }
886
+ async function placeCall(call, options = {}) {
887
+ try {
888
+ const key = callsKey(options);
889
+ if (!key) return { ok: false, error: NO_KEY_ERROR };
890
+ const { data, error, code } = await callSdkApi("/api/v1/sdk/phone-lines/call", key, options, {
891
+ method: "POST",
892
+ body: call,
893
+ cache: "no-store"
894
+ });
895
+ if (error !== null || !isRecord(data) || typeof data.callId !== "string") {
896
+ return { ok: false, error: error != null ? error : "Unexpected response", ...code ? { code } : {} };
897
+ }
898
+ return {
899
+ ok: true,
900
+ callId: data.callId,
901
+ ringing: typeof data.ringing === "string" ? data.ringing : "",
902
+ ringNumbers: Array.isArray(data.ringNumbers) ? data.ringNumbers.filter((n) => typeof n === "string") : [],
903
+ from: typeof data.from === "string" ? data.from : ""
904
+ };
905
+ } catch (e) {
906
+ return { ok: false, error: "Could not reach Glitchgrab" };
907
+ }
908
+ }
909
+ function lineRequestOf(value) {
910
+ if (!isRecord(value) || typeof value.kycStatus !== "string") return null;
911
+ return {
912
+ kycStatus: value.kycStatus,
913
+ kycReason: stringOrNull(value.kycReason),
914
+ enabled: value.enabled === true,
915
+ buyState: typeof value.buyState === "string" ? value.buyState : "NONE",
916
+ buyError: stringOrNull(value.buyError),
917
+ number: stringOrNull(value.number)
918
+ };
919
+ }
920
+ async function getPhoneLine(params = {}, options = {}) {
921
+ var _a;
922
+ try {
923
+ const key = callsKey(options);
924
+ if (!key) return null;
925
+ const query = params.ownerId ? `?ownerId=${encodeURIComponent(params.ownerId)}` : "";
926
+ const { data, error } = await callSdkApi(`/api/v1/sdk/phone-lines${query}`, key, options, { cache: "no-store" });
927
+ if (error !== null || !isRecord(data)) return null;
928
+ const line = isRecord(data.line) && typeof data.line.number === "string" ? data.line : null;
929
+ return {
930
+ line: line ? {
931
+ number: line.number,
932
+ label: stringOrNull(line.label),
933
+ active: line.active === true,
934
+ callers: (_a = numberOrNull(line.callers)) != null ? _a : 0
935
+ } : null,
936
+ request: lineRequestOf(data.request)
937
+ };
938
+ } catch (e) {
939
+ return null;
940
+ }
941
+ }
942
+ async function lineRequestCall(path, body, options) {
943
+ try {
944
+ const key = callsKey(options);
945
+ if (!key) return { ok: false, error: NO_KEY_ERROR };
946
+ const { data, error, code } = await callSdkApi(path, key, options, { method: "POST", body, cache: "no-store" });
947
+ const request = isRecord(data) ? lineRequestOf(data.request) : null;
948
+ if (error !== null || !request) return { ok: false, error: error != null ? error : "Unexpected response", ...code ? { code } : {} };
949
+ return { ok: true, request };
950
+ } catch (e) {
951
+ return { ok: false, error: "Could not reach Glitchgrab" };
952
+ }
953
+ }
954
+ function submitPhoneKyc(kyc, options = {}) {
955
+ const form = new FormData();
956
+ for (const k of ["ownerId", "businessName", "email", "addressLine1", "city", "state", "postalCode"]) {
957
+ form.set(k, kyc[k]);
958
+ }
959
+ form.set("registrationCertificate", kyc.registrationCertificate, "registration-certificate");
960
+ form.set("gstCertificate", kyc.gstCertificate, "gst-certificate");
961
+ return lineRequestCall("/api/v1/sdk/phone-lines/kyc", form, options);
962
+ }
963
+ function enableCalling(params, options = {}) {
964
+ return lineRequestCall("/api/v1/sdk/phone-lines/enable", { ...params, agreedToCharge: true }, options);
965
+ }
966
+ function disableCalling(params, options = {}) {
967
+ return lineRequestCall("/api/v1/sdk/phone-lines/disable", params, options);
968
+ }
969
+ async function registerWebhook(webhook, options = {}) {
970
+ try {
971
+ const key = callsKey(options);
972
+ if (!key) return { ok: false, error: NO_KEY_ERROR };
973
+ const { data, error } = await callSdkApi("/api/v1/sdk/webhooks", key, options, {
974
+ method: "POST",
975
+ body: webhook,
976
+ cache: "no-store"
977
+ });
978
+ if (error !== null || !isRecord(data) || typeof data.id !== "string" || typeof data.secret !== "string") {
979
+ return { ok: false, error: error != null ? error : "Unexpected response" };
980
+ }
981
+ return {
982
+ ok: true,
983
+ secret: data.secret,
984
+ webhook: {
985
+ id: data.id,
986
+ url: typeof data.url === "string" ? data.url : webhook.url,
987
+ events: Array.isArray(data.events) ? data.events.filter((e) => typeof e === "string") : []
988
+ }
989
+ };
990
+ } catch (e) {
991
+ return { ok: false, error: "Could not reach Glitchgrab" };
992
+ }
993
+ }
994
+ async function listWebhooks(options = {}) {
995
+ try {
996
+ const key = callsKey(options);
997
+ if (!key) return [];
998
+ const { data, error } = await callSdkApi("/api/v1/sdk/webhooks", key, options, { cache: "no-store" });
999
+ if (error !== null || !isRecord(data) || !Array.isArray(data.webhooks)) return [];
1000
+ return data.webhooks.filter(isRecord).flatMap(
1001
+ (w) => typeof w.id === "string" && typeof w.url === "string" ? [{ id: w.id, url: w.url, events: Array.isArray(w.events) ? w.events.filter((e) => typeof e === "string") : [] }] : []
1002
+ );
1003
+ } catch (e) {
1004
+ return [];
1005
+ }
1006
+ }
1007
+ async function removeWebhook(id, options = {}) {
1008
+ try {
1009
+ const key = callsKey(options);
1010
+ if (!key) return { ok: false, error: NO_KEY_ERROR };
1011
+ const { error } = await callSdkApi(`/api/v1/sdk/webhooks/${encodeURIComponent(id)}`, key, options, {
1012
+ method: "DELETE",
1013
+ cache: "no-store"
1014
+ });
1015
+ return error === null ? { ok: true } : { ok: false, error };
1016
+ } catch (e) {
1017
+ return { ok: false, error: "Could not reach Glitchgrab" };
1018
+ }
1019
+ }
878
1020
  function removeLineCaller(email, options = {}) {
879
1021
  return ringNumberWrite(
880
- `/api/v1/sdk/phone-lines/callers/${encodeURIComponent(email)}`,
1022
+ `/api/v1/sdk/phone-lines/callers/${encodeURIComponent(email)}${ownerQuery(options)}`,
881
1023
  "DELETE",
882
1024
  void 0,
883
1025
  options,
@@ -902,8 +1044,7 @@ async function listRingNumbers(operator, options = {}) {
902
1044
  try {
903
1045
  const key = callsKey(options);
904
1046
  if (!key) return { ok: false, error: NO_KEY_ERROR };
905
- const query = new URLSearchParams("email" in operator ? { email: operator.email } : { phone: operator.phone });
906
- const { data, error, code } = await callSdkApi(ringNumbersPath(`?${query.toString()}`), key, options, {
1047
+ const { data, error, code } = await callSdkApi(ringNumbersPath(`?${operatorQuery(operator, options)}`), key, options, {
907
1048
  cache: "no-store"
908
1049
  });
909
1050
  if (error !== null || !isRecord(data)) return { ok: false, error: error != null ? error : "Unexpected response", ...code ? { code } : {} };
@@ -931,20 +1072,27 @@ async function listRingNumbers(operator, options = {}) {
931
1072
  }
932
1073
  }
933
1074
  function addRingNumber(operator, number, options = {}) {
934
- return ringNumberWrite(ringNumbersPath(), "POST", { operator, ...number }, options, (d) => ({
1075
+ const body = { operator, ...number, ...options.ownerId ? { ownerId: options.ownerId } : {} };
1076
+ return ringNumberWrite(ringNumbersPath(), "POST", body, options, (d) => ({
935
1077
  id: typeof d.id === "string" ? d.id : "",
936
1078
  testRing: d.testRing === "failed" ? "failed" : "ringing",
937
1079
  verifyError: stringOrNull(d.verifyError)
938
1080
  }));
939
1081
  }
940
1082
  function updateRingNumber(id, change, options = {}) {
941
- return ringNumberWrite(ringNumbersPath(`/${encodeURIComponent(id)}`), "PATCH", change, options, () => ({}));
1083
+ return ringNumberWrite(ringNumbersPath(`/${encodeURIComponent(id)}${ownerQuery(options)}`), "PATCH", change, options, () => ({}));
942
1084
  }
943
1085
  function removeRingNumber(id, options = {}) {
944
- return ringNumberWrite(ringNumbersPath(`/${encodeURIComponent(id)}`), "DELETE", void 0, options, () => ({}));
1086
+ return ringNumberWrite(
1087
+ ringNumbersPath(`/${encodeURIComponent(id)}${ownerQuery(options)}`),
1088
+ "DELETE",
1089
+ void 0,
1090
+ options,
1091
+ () => ({})
1092
+ );
945
1093
  }
946
1094
  function testRingNumber(id, options = {}) {
947
- return ringNumberWrite(ringNumbersPath(`/${encodeURIComponent(id)}/verify`), "POST", void 0, options, (d) => ({
1095
+ return ringNumberWrite(ringNumbersPath(`/${encodeURIComponent(id)}/verify${ownerQuery(options)}`), "POST", void 0, options, (d) => ({
948
1096
  verified: d.verified === true
949
1097
  }));
950
1098
  }
@@ -957,28 +1105,36 @@ export {
957
1105
  configureServerReporter,
958
1106
  deleteCall,
959
1107
  dialFollowUp,
1108
+ disableCalling,
960
1109
  discardBookingQuestionsDraft,
961
1110
  editFollowUpNote,
1111
+ enableCalling,
962
1112
  escapeJsonForScript,
963
1113
  getBookingSettings,
964
1114
  getCall,
965
1115
  getFollowUp,
966
1116
  getGuide,
967
1117
  getOperatorRingNumbers,
1118
+ getPhoneLine,
968
1119
  listBookings,
969
1120
  listCalls,
970
1121
  listFollowUps,
971
1122
  listGuides,
972
1123
  listLeads,
973
1124
  listRingNumbers,
1125
+ listWebhooks,
1126
+ placeCall,
974
1127
  publishBookingQuestions,
1128
+ registerWebhook,
975
1129
  removeLineCaller,
976
1130
  removeRingNumber,
1131
+ removeWebhook,
977
1132
  reportServerError,
978
1133
  rescheduleBooking,
979
1134
  resetServerReporter,
980
1135
  setFollowUpBooking,
981
1136
  signSession,
1137
+ submitPhoneKyc,
982
1138
  testRingNumber,
983
1139
  updateBookingSettings,
984
1140
  updateRingNumber