@niledatabase/server 5.0.0-alpha.1 → 5.0.0-alpha.3

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/express.js CHANGED
@@ -4,7 +4,8 @@ require('dotenv/config');
4
4
 
5
5
  // src/api/utils/routes/index.ts
6
6
  var NILEDB_API_URL = process.env.NILEDB_API_URL;
7
- var appRoutes = (prefix = "/api") => ({
7
+ var DEFAULT_PREFIX = "/api";
8
+ var appRoutes = (prefix = DEFAULT_PREFIX) => ({
8
9
  SIGNIN: `${prefix}${"/auth/signin" /* SIGNIN */}`,
9
10
  PROVIDERS: `${prefix}${"/auth/providers" /* PROVIDERS */}`,
10
11
  SESSION: `${prefix}${"/auth/session" /* SESSION */}`,
@@ -13,7 +14,8 @@ var appRoutes = (prefix = "/api") => ({
13
14
  SIGNOUT: `${prefix}${"/auth/signout" /* SIGNOUT */}`,
14
15
  ERROR: `${prefix}/auth/error`,
15
16
  VERIFY_REQUEST: `${prefix}/auth/verify-request`,
16
- PASSWORD_RESET: `${prefix}/auth/reset-password`,
17
+ VERIFY_EMAIL: `${prefix}${"/auth/verify-email" /* VERIFY_EMAIL */}`,
18
+ PASSWORD_RESET: `${prefix}${"/auth/reset-password" /* PASSWORD_RESET */}`,
17
19
  ME: `${prefix}${"/me" /* ME */}`,
18
20
  USERS: `${prefix}${"/users" /* USERS */}`,
19
21
  USER_TENANTS: `${prefix}${"/users/{userId}/tenants" /* USER_TENANTS */}`,
@@ -47,7 +49,8 @@ var proxyRoutes = (config) => ({
47
49
  SIGNOUT: makeRestUrl(config, "/auth/signout" /* SIGNOUT */),
48
50
  ERROR: makeRestUrl(config, "/auth/error"),
49
51
  VERIFY_REQUEST: makeRestUrl(config, "/auth/verify-request"),
50
- PASSWORD_RESET: makeRestUrl(config, "/auth/reset-password")
52
+ PASSWORD_RESET: makeRestUrl(config, "/auth/reset-password" /* PASSWORD_RESET */),
53
+ VERIFY_EMAIL: makeRestUrl(config, "/auth/verify-email" /* VERIFY_EMAIL */)
51
54
  });
52
55
  function filterNullUndefined(obj) {
53
56
  if (!obj) {
@@ -72,9 +75,9 @@ function makeRestUrl(config, path, qp) {
72
75
  const strParams = params.toString();
73
76
  return `${[url, path.substring(1, path.length)].join("/")}${strParams ? `?${strParams}` : ""}`;
74
77
  }
75
- function urlMatches(requestUrl, route16) {
78
+ function urlMatches(requestUrl, route17) {
76
79
  const url = new URL(requestUrl);
77
- return url.pathname.startsWith(route16);
80
+ return url.pathname.startsWith(route17);
78
81
  }
79
82
  function isUUID(value) {
80
83
  if (!value) {
@@ -144,9 +147,9 @@ function matchesLog(configRoutes, request2) {
144
147
  }
145
148
 
146
149
  // src/utils/constants.ts
147
- var X_NILE_TENANT = "nile.tenant_id";
148
- var X_NILE_ORIGIN = "nile.origin";
149
- var X_NILE_SECURECOOKIES = "nile.secure_cookies";
150
+ var X_NILE_TENANT = "nile-tenant-id";
151
+ var X_NILE_ORIGIN = "nile-origin";
152
+ var X_NILE_SECURECOOKIES = "nile-secure-cookies";
150
153
 
151
154
  // src/api/utils/request.ts
152
155
  async function request(url, _init, config) {
@@ -165,15 +168,29 @@ async function request(url, _init, config) {
165
168
  }
166
169
  if (config.secureCookies != null) {
167
170
  updatedHeaders.set(X_NILE_SECURECOOKIES, String(config.secureCookies));
171
+ } else {
172
+ updatedHeaders.set(
173
+ X_NILE_SECURECOOKIES,
174
+ process.env.NODE_ENV === "production" ? "true" : "false"
175
+ );
168
176
  }
169
177
  updatedHeaders.set("host", requestUrl.host);
170
178
  if (config.callbackUrl) {
171
179
  const cbUrl = new URL(config.callbackUrl);
172
180
  debug(`Obtained origin from config.callbackUrl ${config.callbackUrl}`);
173
181
  updatedHeaders.set(X_NILE_ORIGIN, cbUrl.origin);
182
+ } else if (config.origin) {
183
+ debug(`Obtained origin from config.origin ${config.origin}`);
184
+ updatedHeaders.set(X_NILE_ORIGIN, config.origin);
174
185
  } else {
175
- updatedHeaders.set(X_NILE_ORIGIN, requestUrl.origin);
176
- debug(`Obtained origin from request ${requestUrl.origin}`);
186
+ const passedOrigin = request2.headers.get(X_NILE_ORIGIN);
187
+ if (passedOrigin) {
188
+ updatedHeaders.set(X_NILE_ORIGIN, passedOrigin);
189
+ } else {
190
+ const reqOrigin = config.routePrefix !== DEFAULT_PREFIX ? `${requestUrl.origin}${config.routePrefix}` : requestUrl.origin;
191
+ updatedHeaders.set(X_NILE_ORIGIN, reqOrigin);
192
+ debug(`Obtained origin from request ${reqOrigin}`);
193
+ }
177
194
  }
178
195
  const params = { ...init };
179
196
  if (params.method?.toLowerCase() === "post" || params.method?.toLowerCase() === "put") {
@@ -427,11 +444,11 @@ async function route3(request2, config) {
427
444
  function matches3(configRoutes, request2) {
428
445
  const url = new URL(request2.url);
429
446
  const [userId, possibleTenantId, tenantId] = url.pathname.split("/").reverse();
430
- let route16 = configRoutes[key3].replace("{tenantId}", tenantId).replace("{userId}", userId);
447
+ let route17 = configRoutes[key3].replace("{tenantId}", tenantId).replace("{userId}", userId);
431
448
  if (userId === "users") {
432
- route16 = configRoutes[key3].replace("{tenantId}", possibleTenantId);
449
+ route17 = configRoutes[key3].replace("{tenantId}", possibleTenantId);
433
450
  }
434
- return urlMatches(request2.url, route16);
451
+ return urlMatches(request2.url, route17);
435
452
  }
436
453
 
437
454
  // src/api/routes/tenants/GET.ts
@@ -711,6 +728,34 @@ function matches13(configRoutes, request2) {
711
728
  return urlMatches(request2.url, configRoutes.PASSWORD_RESET);
712
729
  }
713
730
 
731
+ // src/api/routes/auth/verify-email.ts
732
+ var key11 = "VERIFY_EMAIL";
733
+ async function route14(req, config) {
734
+ const url = proxyRoutes(config)[key11];
735
+ const res = await request(
736
+ url,
737
+ {
738
+ method: req.method,
739
+ request: req
740
+ },
741
+ config
742
+ );
743
+ const location = res?.headers.get("location");
744
+ if (location) {
745
+ return new Response(res?.body, {
746
+ status: 302,
747
+ headers: res?.headers
748
+ });
749
+ }
750
+ return new Response(res?.body, {
751
+ status: res?.status,
752
+ headers: res?.headers
753
+ });
754
+ }
755
+ function matches14(configRoutes, request2) {
756
+ return urlMatches(request2.url, configRoutes[key11]);
757
+ }
758
+
714
759
  // src/api/handlers/GET.ts
715
760
  function GETTER(configRoutes, config) {
716
761
  const { info, warn } = Logger(config, "[GET MATCHER]");
@@ -763,6 +808,10 @@ function GETTER(configRoutes, config) {
763
808
  info("matches verify-request");
764
809
  return route12(req, config);
765
810
  }
811
+ if (matches14(configRoutes, req)) {
812
+ info("matches verify-email");
813
+ return route14(req, config);
814
+ }
766
815
  if (matches11(configRoutes, req)) {
767
816
  info("matches error");
768
817
  return route11(req, config);
@@ -781,8 +830,8 @@ async function POST4(config, init) {
781
830
  }
782
831
 
783
832
  // src/api/routes/signup/index.tsx
784
- var key11 = "SIGNUP";
785
- async function route14(request2, config) {
833
+ var key12 = "SIGNUP";
834
+ async function route15(request2, config) {
786
835
  switch (request2.method) {
787
836
  case "POST":
788
837
  return await POST4(config, { request: request2 });
@@ -790,8 +839,8 @@ async function route14(request2, config) {
790
839
  return new Response("method not allowed", { status: 405 });
791
840
  }
792
841
  }
793
- function matches14(configRoutes, request2) {
794
- return urlMatches(request2.url, configRoutes[key11]);
842
+ function matches15(configRoutes, request2) {
843
+ return urlMatches(request2.url, configRoutes[key12]);
795
844
  }
796
845
 
797
846
  // src/api/handlers/POST.ts
@@ -799,24 +848,21 @@ function POSTER(configRoutes, config) {
799
848
  const { info, warn, error } = Logger(config, "[POST MATCHER]");
800
849
  return async function POST5(req) {
801
850
  if (matchesLog(configRoutes, req)) {
802
- if (req.body) {
803
- try {
804
- const text = await req.text();
805
- error(text);
806
- return new Response(null, {
807
- status: 200
808
- });
809
- } catch (e) {
810
- }
851
+ try {
852
+ const json = await req.clone().json();
853
+ error(req.body && json);
854
+ } catch {
855
+ error(await req.text());
811
856
  }
857
+ return new Response(null, { status: 200 });
812
858
  }
813
859
  if (matches3(configRoutes, req)) {
814
860
  info("matches tenant users");
815
861
  return route3(req, config);
816
862
  }
817
- if (matches14(configRoutes, req)) {
863
+ if (matches15(configRoutes, req)) {
818
864
  info("matches signup");
819
- return route14(req, config);
865
+ return route15(req, config);
820
866
  }
821
867
  if (matches2(configRoutes, req)) {
822
868
  info("matches users");
@@ -854,6 +900,10 @@ function POSTER(configRoutes, config) {
854
900
  info("matches signout");
855
901
  return route10(req, config);
856
902
  }
903
+ if (matches14(configRoutes, req)) {
904
+ info("matches verify-email");
905
+ return route14(req, config);
906
+ }
857
907
  warn(`No POST routes matched ${req.url}`);
858
908
  return new Response(null, { status: 404 });
859
909
  };
@@ -882,11 +932,11 @@ async function PUT4(config, init) {
882
932
  }
883
933
 
884
934
  // src/api/routes/tenants/[tenantId]/users/[userId]/index.ts
885
- var key12 = "TENANT_USER";
886
- async function route15(request2, config) {
935
+ var key13 = "TENANT_USER";
936
+ async function route16(request2, config) {
887
937
  const { info } = Logger(
888
938
  { ...config, debug: config.debug },
889
- `[ROUTES][${key12}]`
939
+ `[ROUTES][${key13}]`
890
940
  );
891
941
  const session = await auth(request2, config);
892
942
  if (!session) {
@@ -908,23 +958,23 @@ async function route15(request2, config) {
908
958
  return new Response("method not allowed", { status: 405 });
909
959
  }
910
960
  }
911
- function matches15(configRoutes, request2) {
961
+ function matches16(configRoutes, request2) {
912
962
  const url = new URL(request2.url);
913
963
  const [, userId, possibleTenantId, tenantId] = url.pathname.split("/").reverse();
914
- let route16 = configRoutes[key12].replace("{tenantId}", tenantId).replace("{userId}", userId);
964
+ let route17 = configRoutes[key13].replace("{tenantId}", tenantId).replace("{userId}", userId);
915
965
  if (userId === "users") {
916
- route16 = configRoutes[key12].replace("{tenantId}", possibleTenantId);
966
+ route17 = configRoutes[key13].replace("{tenantId}", possibleTenantId);
917
967
  }
918
- return urlMatches(request2.url, route16);
968
+ return urlMatches(request2.url, route17);
919
969
  }
920
970
 
921
971
  // src/api/handlers/DELETE.ts
922
972
  function DELETER(configRoutes, config) {
923
973
  const { info, warn } = Logger(config, "[DELETE MATCHER]");
924
974
  return async function DELETE4(req) {
925
- if (matches15(configRoutes, req)) {
975
+ if (matches16(configRoutes, req)) {
926
976
  info("matches tenant user");
927
- return route15(req, config);
977
+ return route16(req, config);
928
978
  }
929
979
  if (matches3(configRoutes, req)) {
930
980
  info("matches tenant users");
@@ -947,9 +997,9 @@ function DELETER(configRoutes, config) {
947
997
  function PUTER(configRoutes, config) {
948
998
  const { info, warn } = Logger(config, "[PUT MATCHER]");
949
999
  return async function PUT5(req) {
950
- if (matches15(configRoutes, req)) {
1000
+ if (matches16(configRoutes, req)) {
951
1001
  info("matches tenant user");
952
- return route15(req, config);
1002
+ return route16(req, config);
953
1003
  }
954
1004
  if (matches3(configRoutes, req)) {
955
1005
  info("matches tenant users");
@@ -1154,7 +1204,6 @@ var stringCheck = (str) => {
1154
1204
  // src/utils/Config/index.ts
1155
1205
  var Config = class {
1156
1206
  routes;
1157
- // handlersWithContext;
1158
1207
  handlers;
1159
1208
  paths;
1160
1209
  logger;
@@ -1175,6 +1224,10 @@ var Config = class {
1175
1224
  */
1176
1225
  apiUrl;
1177
1226
  origin;
1227
+ /**
1228
+ * important for separating the `origin` config value from a default in order to make requests
1229
+ */
1230
+ serverOrigin;
1178
1231
  debug;
1179
1232
  /**
1180
1233
  * To use secure cookies or not in the fetch
@@ -1193,7 +1246,8 @@ var Config = class {
1193
1246
  this.secureCookies = getSecureCookies(envVarConfig);
1194
1247
  this.callbackUrl = getCallbackUrl(envVarConfig);
1195
1248
  this.debug = config?.debug;
1196
- this.origin = config?.origin ?? "http://localhost:3000";
1249
+ this.origin = config?.origin;
1250
+ this.serverOrigin = config?.origin ?? "http://localhost:3000";
1197
1251
  this.apiUrl = getApiUrl(envVarConfig);
1198
1252
  const user = getUsername(envVarConfig);
1199
1253
  const password = getPassword(envVarConfig);
@@ -1332,17 +1386,17 @@ async function NileExpressHandler(nile, config) {
1332
1386
  body = await response.text();
1333
1387
  }
1334
1388
  const newHeaders = {};
1335
- response.headers.forEach((value, key13) => {
1336
- if (!["content-length", "transfer-encoding"].includes(key13.toLowerCase())) {
1337
- if (newHeaders[key13]) {
1338
- const prev = newHeaders[key13];
1389
+ response.headers.forEach((value, key14) => {
1390
+ if (!["content-length", "transfer-encoding"].includes(key14.toLowerCase())) {
1391
+ if (newHeaders[key14]) {
1392
+ const prev = newHeaders[key14];
1339
1393
  if (Array.isArray(prev)) {
1340
- newHeaders[key13] = [...prev, value];
1394
+ newHeaders[key14] = [...prev, value];
1341
1395
  } else {
1342
- newHeaders[key13] = [prev, value];
1396
+ newHeaders[key14] = [prev, value];
1343
1397
  }
1344
1398
  } else {
1345
- newHeaders[key13] = value;
1399
+ newHeaders[key14] = value;
1346
1400
  }
1347
1401
  }
1348
1402
  });