@palbase/backend 10.0.0 → 10.2.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
@@ -852,6 +852,9 @@ function camelToUpperSnake(s) {
852
852
  var TEST_USERS_CONFIG_KIND = "test-users";
853
853
  var TEST_USER_NAME_RE = /^[a-zA-Z][a-zA-Z0-9_]*$/;
854
854
  var EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
855
+ var E164_RE = /^\+[1-9]\d{1,14}$/;
856
+ var OTP_LENGTH = 6;
857
+ var OTP_RE = /^\d{6}$/;
855
858
  var MIN_PASSWORD_LENGTH = 8;
856
859
  function isPlainObject(value) {
857
860
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -886,7 +889,7 @@ function normalizeSeed(seed, userName) {
886
889
  }
887
890
  function testUser(opts = {}) {
888
891
  if (!isPlainObject(opts)) {
889
- throw new Error("testUser() expects { email?, password?, seed? }");
892
+ throw new Error("testUser() expects { email?, password?, phone?, otp?, seed? }");
890
893
  }
891
894
  const hasEmail = opts.email !== void 0;
892
895
  const hasPassword = opts.password !== void 0;
@@ -909,7 +912,30 @@ function testUser(opts = {}) {
909
912
  email = opts.email;
910
913
  password = opts.password;
911
914
  }
912
- return { email, password, seed: normalizeSeed(opts.seed, email ?? "<template>") };
915
+ const hasPhone = opts.phone !== void 0;
916
+ const hasOTP = opts.otp !== void 0;
917
+ if (hasPhone !== hasOTP) {
918
+ throw new Error(
919
+ "testUser() needs phone AND otp together \u2014 nothing can receive an SMS at a fabricated number, so a phone fixture without its fixed code can never be signed in as"
920
+ );
921
+ }
922
+ let phone = null;
923
+ let otp = null;
924
+ if (hasPhone) {
925
+ if (typeof opts.phone !== "string" || !E164_RE.test(opts.phone)) {
926
+ throw new Error(
927
+ `testUser phone ${JSON.stringify(opts.phone)} must be E.164, e.g. "+905551112233"`
928
+ );
929
+ }
930
+ if (typeof opts.otp !== "string" || !OTP_RE.test(opts.otp)) {
931
+ throw new Error(
932
+ `testUser otp ${JSON.stringify(opts.otp)} must be ${OTP_LENGTH} digits`
933
+ );
934
+ }
935
+ phone = opts.phone;
936
+ otp = opts.otp;
937
+ }
938
+ return { email, password, phone, otp, seed: normalizeSeed(opts.seed, email ?? "<template>") };
913
939
  }
914
940
  function defineTestUsers(input) {
915
941
  if (!isPlainObject(input) || !isPlainObject(input.users)) {
@@ -951,6 +977,8 @@ function valueMatchesType(type, value) {
951
977
  return typeof value === "number" && Number.isFinite(value);
952
978
  case "string":
953
979
  return typeof value === "string";
980
+ case "json":
981
+ return typeof value === "object" && value !== null && !Array.isArray(value);
954
982
  }
955
983
  }
956
984
  function flag(opts) {
@@ -958,8 +986,8 @@ function flag(opts) {
958
986
  throw new Error("flag() expects { type, default, variants?, description? }");
959
987
  }
960
988
  const { type } = opts;
961
- if (type !== "boolean" && type !== "number" && type !== "string") {
962
- throw new Error(`flag type must be "boolean", "number", or "string", got ${JSON.stringify(type)}`);
989
+ if (type !== "boolean" && type !== "number" && type !== "string" && type !== "json") {
990
+ throw new Error(`flag type must be "boolean", "number", "string", or "json", got ${JSON.stringify(type)}`);
963
991
  }
964
992
  if (!valueMatchesType(type, opts.default)) {
965
993
  throw new Error(
@@ -968,7 +996,7 @@ function flag(opts) {
968
996
  }
969
997
  let variants = null;
970
998
  if (opts.variants !== void 0) {
971
- if (type !== "string") {
999
+ if (opts.type !== "string") {
972
1000
  throw new Error(`flag variants are only valid for type "string" (got type "${type}")`);
973
1001
  }
974
1002
  if (!Array.isArray(opts.variants)) {