@niledatabase/server 4.0.0-alpha.13 → 4.0.0-alpha.14

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.d.mts CHANGED
@@ -215,20 +215,25 @@ type NileRequest<T> = NRequest<T> | T;
215
215
 
216
216
  declare class Auth extends Config {
217
217
  headers?: Headers;
218
- constructor(config: Config, headers?: Headers);
218
+ resetHeaders?: () => void;
219
+ constructor(config: Config, headers?: Headers, params?: {
220
+ resetHeaders: () => void;
221
+ });
219
222
  handleHeaders(init?: RequestInit): RequestInit | undefined;
220
223
  get sessionUrl(): string;
221
- getSession: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<JWT | ActiveSession | Response | undefined>;
224
+ getSession: <T = JWT | ActiveSession | Response | undefined>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
222
225
  get getCsrfUrl(): string;
223
- getCsrf(req: NileRequest<void> | Headers, init?: RequestInit): Promise<JSON | Response>;
226
+ getCsrf<T = Response | JSON>(req: NileRequest<void> | Headers, init?: RequestInit, raw?: boolean): Promise<T>;
224
227
  get listProvidersUrl(): string;
225
- listProviders: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<Response | {
228
+ listProviders: <T = Response | {
226
229
  [key: string]: Provider;
227
- }>;
230
+ }>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
228
231
  get signOutUrl(): string;
229
- signOut: (req: NileRequest<void | {
232
+ signOut: <T = Response | {
233
+ url: string;
234
+ }>(req: NileRequest<void | {
230
235
  callbackUrl?: string;
231
- }> | Headers, init?: RequestInit) => Promise<Response | JSON>;
236
+ }> | Headers, init?: RequestInit) => Promise<T>;
232
237
  }
233
238
 
234
239
  declare class Tenants extends Config {
@@ -237,18 +242,18 @@ declare class Tenants extends Config {
237
242
  handleHeaders(init?: RequestInit): RequestInit | undefined;
238
243
  get tenantsUrl(): string;
239
244
  get tenantUrl(): string;
240
- createTenant: (req: NileRequest<{
245
+ createTenant: <T = Tenant | Response>(req: NileRequest<{
241
246
  name: string;
242
- }> | Headers | string, init?: RequestInit) => Promise<Tenant | Response>;
243
- getTenant: (req: NileRequest<{
247
+ }> | Headers | string, init?: RequestInit) => Promise<T>;
248
+ getTenant: <T = Tenant | Response>(req: NileRequest<{
244
249
  id: string;
245
- }> | Headers | string | void, init?: RequestInit) => Promise<Tenant | Response>;
250
+ }> | Headers | string | void, init?: RequestInit) => Promise<T>;
246
251
  get tenantListUrl(): string;
247
- listTenants: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<Tenant[] | Response>;
248
- deleteTenant: (req: NileRequest<void> | Headers | string, init?: RequestInit) => Promise<Response>;
249
- updateTenant: (req: NileRequest<void> | Headers | {
252
+ listTenants: <T = Tenant[] | Response>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
253
+ deleteTenant: <T = Response>(req: NileRequest<void> | Headers | string, init?: RequestInit) => Promise<T>;
254
+ updateTenant: <T = Tenant | Response>(req: NileRequest<void> | Headers | {
250
255
  name: string;
251
- }, init?: RequestInit) => Promise<Tenant | Response>;
256
+ }, init?: RequestInit) => Promise<T>;
252
257
  }
253
258
 
254
259
  declare class Users extends Config {
@@ -259,21 +264,21 @@ declare class Users extends Config {
259
264
  get linkUsersUrl(): string;
260
265
  get tenantUserUrl(): string;
261
266
  handleHeaders(init?: RequestInit): RequestInit | undefined;
262
- createUser: (req: NileRequest<CreateBasicUserRequest>, init?: RequestInit) => Promise<User | Response>;
263
- createTenantUser: (req: NileRequest<CreateBasicUserRequest>, init?: RequestInit) => Promise<User | Response>;
264
- updateUser: (req: NileRequest<Partial<Omit<User, "email" | "tenants" | "created" | "updated">>>, init?: RequestInit) => Promise<User | Response>;
265
- listUsers: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<User[] | Response>;
266
- linkUser: (req: NileRequest<{
267
+ createUser: <T = User | Response>(req: NileRequest<CreateBasicUserRequest>, init?: RequestInit) => Promise<T>;
268
+ createTenantUser: <T = User | Response>(req: NileRequest<CreateBasicUserRequest>, init?: RequestInit) => Promise<T>;
269
+ updateUser: <T = User[] | Response>(req: NileRequest<Partial<Omit<User, "email" | "tenants" | "created" | "updated">>>, init?: RequestInit) => Promise<T>;
270
+ listUsers: <T = User[] | Response>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
271
+ linkUser: <T = User | Response>(req: NileRequest<{
267
272
  id: string;
268
273
  tenantId?: string;
269
- }> | Headers | string, init?: RequestInit) => Promise<User | Response>;
270
- unlinkUser: (req: NileRequest<{
274
+ }> | Headers | string, init?: RequestInit) => Promise<T>;
275
+ unlinkUser: <T = Response>(req: NileRequest<{
271
276
  id: string;
272
277
  tenantId?: string;
273
- }> | Headers | string, init?: RequestInit) => Promise<Response>;
278
+ }> | Headers | string, init?: RequestInit) => Promise<T>;
274
279
  get meUrl(): string;
275
280
  me: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<User | Response>;
276
- updateMe: (req: NileRequest<Partial<Omit<User, "email" | "id" | "tenants" | "created" | "updated">>> | Headers, init?: RequestInit) => Promise<User | Response>;
281
+ updateMe: <T = User | Response>(req: NileRequest<Partial<Omit<User, "email" | "id" | "tenants" | "created" | "updated">>> | Headers, init?: RequestInit) => Promise<T>;
277
282
  }
278
283
 
279
284
  declare class Api {
@@ -296,13 +301,18 @@ declare class Api {
296
301
  put: string[];
297
302
  };
298
303
  constructor(config: Config);
299
- updateConfig(config: Config): void;
304
+ reset: () => void;
305
+ updateConfig: (config: Config) => void;
306
+ resetHeaders: () => void;
300
307
  set headers(headers: Headers);
301
- login(payload: {
308
+ get headers(): Headers | undefined;
309
+ login: (payload: {
302
310
  email: string;
303
311
  password: string;
304
- }): Promise<void>;
305
- session(req?: Request | Headers | null | undefined): Promise<Response | JWT | ActiveSession | null | undefined>;
312
+ }, config?: {
313
+ setCookie?: boolean;
314
+ }) => Promise<void>;
315
+ session: (req?: Request | Headers | null | undefined) => Promise<Response | JWT | ActiveSession | null | undefined>;
306
316
  }
307
317
 
308
318
  declare class Server {
package/dist/index.d.ts CHANGED
@@ -215,20 +215,25 @@ type NileRequest<T> = NRequest<T> | T;
215
215
 
216
216
  declare class Auth extends Config {
217
217
  headers?: Headers;
218
- constructor(config: Config, headers?: Headers);
218
+ resetHeaders?: () => void;
219
+ constructor(config: Config, headers?: Headers, params?: {
220
+ resetHeaders: () => void;
221
+ });
219
222
  handleHeaders(init?: RequestInit): RequestInit | undefined;
220
223
  get sessionUrl(): string;
221
- getSession: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<JWT | ActiveSession | Response | undefined>;
224
+ getSession: <T = JWT | ActiveSession | Response | undefined>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
222
225
  get getCsrfUrl(): string;
223
- getCsrf(req: NileRequest<void> | Headers, init?: RequestInit): Promise<JSON | Response>;
226
+ getCsrf<T = Response | JSON>(req: NileRequest<void> | Headers, init?: RequestInit, raw?: boolean): Promise<T>;
224
227
  get listProvidersUrl(): string;
225
- listProviders: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<Response | {
228
+ listProviders: <T = Response | {
226
229
  [key: string]: Provider;
227
- }>;
230
+ }>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
228
231
  get signOutUrl(): string;
229
- signOut: (req: NileRequest<void | {
232
+ signOut: <T = Response | {
233
+ url: string;
234
+ }>(req: NileRequest<void | {
230
235
  callbackUrl?: string;
231
- }> | Headers, init?: RequestInit) => Promise<Response | JSON>;
236
+ }> | Headers, init?: RequestInit) => Promise<T>;
232
237
  }
233
238
 
234
239
  declare class Tenants extends Config {
@@ -237,18 +242,18 @@ declare class Tenants extends Config {
237
242
  handleHeaders(init?: RequestInit): RequestInit | undefined;
238
243
  get tenantsUrl(): string;
239
244
  get tenantUrl(): string;
240
- createTenant: (req: NileRequest<{
245
+ createTenant: <T = Tenant | Response>(req: NileRequest<{
241
246
  name: string;
242
- }> | Headers | string, init?: RequestInit) => Promise<Tenant | Response>;
243
- getTenant: (req: NileRequest<{
247
+ }> | Headers | string, init?: RequestInit) => Promise<T>;
248
+ getTenant: <T = Tenant | Response>(req: NileRequest<{
244
249
  id: string;
245
- }> | Headers | string | void, init?: RequestInit) => Promise<Tenant | Response>;
250
+ }> | Headers | string | void, init?: RequestInit) => Promise<T>;
246
251
  get tenantListUrl(): string;
247
- listTenants: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<Tenant[] | Response>;
248
- deleteTenant: (req: NileRequest<void> | Headers | string, init?: RequestInit) => Promise<Response>;
249
- updateTenant: (req: NileRequest<void> | Headers | {
252
+ listTenants: <T = Tenant[] | Response>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
253
+ deleteTenant: <T = Response>(req: NileRequest<void> | Headers | string, init?: RequestInit) => Promise<T>;
254
+ updateTenant: <T = Tenant | Response>(req: NileRequest<void> | Headers | {
250
255
  name: string;
251
- }, init?: RequestInit) => Promise<Tenant | Response>;
256
+ }, init?: RequestInit) => Promise<T>;
252
257
  }
253
258
 
254
259
  declare class Users extends Config {
@@ -259,21 +264,21 @@ declare class Users extends Config {
259
264
  get linkUsersUrl(): string;
260
265
  get tenantUserUrl(): string;
261
266
  handleHeaders(init?: RequestInit): RequestInit | undefined;
262
- createUser: (req: NileRequest<CreateBasicUserRequest>, init?: RequestInit) => Promise<User | Response>;
263
- createTenantUser: (req: NileRequest<CreateBasicUserRequest>, init?: RequestInit) => Promise<User | Response>;
264
- updateUser: (req: NileRequest<Partial<Omit<User, "email" | "tenants" | "created" | "updated">>>, init?: RequestInit) => Promise<User | Response>;
265
- listUsers: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<User[] | Response>;
266
- linkUser: (req: NileRequest<{
267
+ createUser: <T = User | Response>(req: NileRequest<CreateBasicUserRequest>, init?: RequestInit) => Promise<T>;
268
+ createTenantUser: <T = User | Response>(req: NileRequest<CreateBasicUserRequest>, init?: RequestInit) => Promise<T>;
269
+ updateUser: <T = User[] | Response>(req: NileRequest<Partial<Omit<User, "email" | "tenants" | "created" | "updated">>>, init?: RequestInit) => Promise<T>;
270
+ listUsers: <T = User[] | Response>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
271
+ linkUser: <T = User | Response>(req: NileRequest<{
267
272
  id: string;
268
273
  tenantId?: string;
269
- }> | Headers | string, init?: RequestInit) => Promise<User | Response>;
270
- unlinkUser: (req: NileRequest<{
274
+ }> | Headers | string, init?: RequestInit) => Promise<T>;
275
+ unlinkUser: <T = Response>(req: NileRequest<{
271
276
  id: string;
272
277
  tenantId?: string;
273
- }> | Headers | string, init?: RequestInit) => Promise<Response>;
278
+ }> | Headers | string, init?: RequestInit) => Promise<T>;
274
279
  get meUrl(): string;
275
280
  me: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<User | Response>;
276
- updateMe: (req: NileRequest<Partial<Omit<User, "email" | "id" | "tenants" | "created" | "updated">>> | Headers, init?: RequestInit) => Promise<User | Response>;
281
+ updateMe: <T = User | Response>(req: NileRequest<Partial<Omit<User, "email" | "id" | "tenants" | "created" | "updated">>> | Headers, init?: RequestInit) => Promise<T>;
277
282
  }
278
283
 
279
284
  declare class Api {
@@ -296,13 +301,18 @@ declare class Api {
296
301
  put: string[];
297
302
  };
298
303
  constructor(config: Config);
299
- updateConfig(config: Config): void;
304
+ reset: () => void;
305
+ updateConfig: (config: Config) => void;
306
+ resetHeaders: () => void;
300
307
  set headers(headers: Headers);
301
- login(payload: {
308
+ get headers(): Headers | undefined;
309
+ login: (payload: {
302
310
  email: string;
303
311
  password: string;
304
- }): Promise<void>;
305
- session(req?: Request | Headers | null | undefined): Promise<Response | JWT | ActiveSession | null | undefined>;
312
+ }, config?: {
313
+ setCookie?: boolean;
314
+ }) => Promise<void>;
315
+ session: (req?: Request | Headers | null | undefined) => Promise<Response | JWT | ActiveSession | null | undefined>;
306
316
  }
307
317
 
308
318
  declare class Server {
package/dist/index.js CHANGED
@@ -1856,8 +1856,11 @@ var Requester = class extends Config {
1856
1856
  }
1857
1857
  return response;
1858
1858
  }
1859
- async get(req, url, init) {
1859
+ async get(req, url, init, raw = false) {
1860
1860
  const response = await this.request("GET", url, req, init);
1861
+ if (raw) {
1862
+ return response;
1863
+ }
1861
1864
  if (response && response.status >= 200 && response.status < 300) {
1862
1865
  const cloned = response.clone();
1863
1866
  try {
@@ -1892,7 +1895,7 @@ function serverLogin(config, handlers) {
1892
1895
  return async function login({
1893
1896
  email,
1894
1897
  password
1895
- }) {
1898
+ }, loginConfig) {
1896
1899
  if (!email || !password) {
1897
1900
  throw new Error("Server side login requires a user email and password.");
1898
1901
  }
@@ -1967,6 +1970,9 @@ function serverLogin(config, handlers) {
1967
1970
  if (!authCookie) {
1968
1971
  throw new Error("authentication failed");
1969
1972
  }
1973
+ if (loginConfig?.setCookie) {
1974
+ return loginRes;
1975
+ }
1970
1976
  const [, token] = /((__Secure-)?nile\.session-token=.+?);/.exec(authCookie) ?? [];
1971
1977
  if (!token) {
1972
1978
  error("Unable to obtain auth token", { authCookie });
@@ -1982,9 +1988,11 @@ function serverLogin(config, handlers) {
1982
1988
  }
1983
1989
  var Auth = class extends Config {
1984
1990
  headers;
1985
- constructor(config, headers) {
1991
+ resetHeaders;
1992
+ constructor(config, headers, params) {
1986
1993
  super(config);
1987
1994
  this.headers = headers;
1995
+ this.resetHeaders = params?.resetHeaders;
1988
1996
  }
1989
1997
  handleHeaders(init) {
1990
1998
  if (this.headers) {
@@ -2025,10 +2033,10 @@ var Auth = class extends Config {
2025
2033
  get getCsrfUrl() {
2026
2034
  return "/auth/csrf";
2027
2035
  }
2028
- async getCsrf(req, init) {
2036
+ async getCsrf(req, init, raw = false) {
2029
2037
  const _requester = new Requester(this);
2030
2038
  const _init = this.handleHeaders(init);
2031
- return await _requester.get(req, this.getCsrfUrl, _init);
2039
+ return await _requester.get(req, this.getCsrfUrl, _init, raw);
2032
2040
  }
2033
2041
  get listProvidersUrl() {
2034
2042
  return "/auth/providers";
@@ -2044,35 +2052,71 @@ var Auth = class extends Config {
2044
2052
  signOut = async (req, init) => {
2045
2053
  const _requester = new Requester(this);
2046
2054
  const _init = this.handleHeaders(init);
2047
- const csrf = await this.getCsrf(req);
2055
+ const csrf = await this.getCsrf(
2056
+ req,
2057
+ void 0,
2058
+ true
2059
+ );
2060
+ const csrfHeader = getCsrfToken(csrf.headers);
2048
2061
  const callbackUrl = req && "callbackUrl" in req ? String(req.callbackUrl) : "/";
2049
- if (csrf instanceof Request) {
2050
- return csrf;
2051
- }
2052
- const csrfToken = "csrfToken" in csrf ? String(csrf.csrfToken) : "";
2053
- if (!csrfToken) {
2062
+ if (!csrfHeader) {
2054
2063
  return new Response("Request blocked", { status: 400 });
2055
2064
  }
2056
- return await _requester.post(req, this.signOutUrl, {
2065
+ const headers = new Headers(_init?.headers);
2066
+ const { csrfToken } = await csrf.json() ?? {};
2067
+ const cooks = getCookies(headers);
2068
+ if (csrfHeader) {
2069
+ if (cooks["__Secure-nile.csrf-token"]) {
2070
+ cooks["__Secure-nile.csrf-token"] = encodeURIComponent(csrfHeader);
2071
+ }
2072
+ if (cooks["nile.csrf-token"]) {
2073
+ cooks["nile.csrf-token"] = encodeURIComponent(csrfHeader);
2074
+ }
2075
+ }
2076
+ headers.set(
2077
+ "cookie",
2078
+ Object.keys(cooks).map((key12) => `${key12}=${cooks[key12]}`).join("; ")
2079
+ );
2080
+ const res = await _requester.post(req, this.signOutUrl, {
2057
2081
  method: "post",
2058
2082
  body: JSON.stringify({
2059
2083
  csrfToken,
2060
2084
  callbackUrl,
2061
2085
  json: String(true)
2062
2086
  }),
2063
- ..._init
2087
+ ..._init,
2088
+ headers
2064
2089
  });
2090
+ this.resetHeaders && this.resetHeaders();
2091
+ return res;
2065
2092
  };
2066
2093
  };
2067
2094
  function getCallbackUrl2(headers) {
2068
2095
  if (headers) {
2069
- const cookieHeader = headers.get("cookie") || "";
2070
- const cookies = Object.fromEntries(
2071
- cookieHeader.split("; ").map((cookie) => cookie.split("=").map(decodeURIComponent))
2072
- );
2073
- return cookies["__Secure-nile.callback-url"] || cookies["nile.callback-url"];
2096
+ const cookies = getCookies(headers);
2097
+ if (cookies) {
2098
+ return cookies["__Secure-nile.callback-url"] || cookies["nile.callback-url"];
2099
+ }
2074
2100
  }
2075
2101
  }
2102
+ function getCsrfToken(headers) {
2103
+ if (headers) {
2104
+ const cookies = getCookies(headers);
2105
+ if (cookies) {
2106
+ return cookies["__Secure-nile.csrf-token"] || cookies["nile.csrf-token"];
2107
+ }
2108
+ }
2109
+ }
2110
+ var getCookies = (headers) => {
2111
+ if (!headers) return {};
2112
+ const cookieHeader = headers.get("cookie") || headers.get("set-cookie");
2113
+ if (!cookieHeader) return {};
2114
+ return Object.fromEntries(
2115
+ cookieHeader.split(/,\s*(?=[^;]+=[^;,]+)/).flatMap(
2116
+ (cookie) => cookie.split("; ")[0].split("=").length === 2 ? [cookie.split("; ")[0].split("=").map(decodeURIComponent)] : []
2117
+ )
2118
+ );
2119
+ };
2076
2120
 
2077
2121
  // src/tenants/index.ts
2078
2122
  var Tenants = class extends Config {
@@ -2278,7 +2322,9 @@ var Api = class {
2278
2322
  paths;
2279
2323
  constructor(config) {
2280
2324
  this.config = config;
2281
- this.auth = new Auth(config);
2325
+ this.auth = new Auth(config, void 0, {
2326
+ resetHeaders: this.resetHeaders
2327
+ });
2282
2328
  this.users = new Users(config);
2283
2329
  this.tenants = new Tenants(config);
2284
2330
  this.routes = {
@@ -2324,27 +2370,42 @@ var Api = class {
2324
2370
  delete: [this.routes.TENANT_USER, this.routes.TENANT]
2325
2371
  };
2326
2372
  }
2327
- updateConfig(config) {
2373
+ reset = () => {
2374
+ this.users = new Users(this.config, this._headers);
2375
+ this.tenants = new Tenants(this.config, this._headers);
2376
+ this.auth = new Auth(this.config, this._headers, {
2377
+ resetHeaders: this.resetHeaders
2378
+ });
2379
+ };
2380
+ updateConfig = (config) => {
2328
2381
  this.config = config;
2329
2382
  this.handlers = Handlers(this.routes, config);
2330
- }
2383
+ };
2384
+ resetHeaders = () => {
2385
+ this._headers = new Headers();
2386
+ this.reset();
2387
+ };
2331
2388
  set headers(headers) {
2332
- this.users = new Users(this.config, headers);
2333
- this.tenants = new Tenants(this.config, headers);
2334
- this.auth = new Auth(this.config, headers);
2335
2389
  this._headers = headers;
2390
+ this.reset();
2336
2391
  }
2337
- async login(payload) {
2338
- this.headers = await serverLogin(this.config, this.handlers)(payload);
2392
+ get headers() {
2393
+ return this._headers;
2339
2394
  }
2340
- async session(req) {
2395
+ login = async (payload, config) => {
2396
+ this.headers = await serverLogin(this.config, this.handlers)(
2397
+ payload,
2398
+ config
2399
+ );
2400
+ };
2401
+ session = async (req) => {
2341
2402
  if (req instanceof Headers) {
2342
2403
  return this.auth.getSession(req);
2343
2404
  } else if (req instanceof Request) {
2344
2405
  return auth(req, this.config);
2345
2406
  }
2346
2407
  return this.auth.getSession(this._headers);
2347
- }
2408
+ };
2348
2409
  };
2349
2410
 
2350
2411
  // src/Server.ts