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

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