@niledatabase/server 5.0.0-alpha.0 → 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/index.d.ts CHANGED
@@ -1,5 +1,55 @@
1
1
  import pg, { PoolConfig, PoolClient } from 'pg';
2
2
 
3
+ declare class Config {
4
+ routes: Routes;
5
+ handlers: {
6
+ GET: (req: Request) => Promise<void | Response>;
7
+ POST: (req: Request) => Promise<void | Response>;
8
+ DELETE: (req: Request) => Promise<void | Response>;
9
+ PUT: (req: Request) => Promise<void | Response>;
10
+ };
11
+ paths: {
12
+ get: string[];
13
+ post: string[];
14
+ delete: string[];
15
+ put: string[];
16
+ };
17
+ logger?: LoggerType;
18
+ /**
19
+ * Stores the set tenant id from Server for use in sub classes
20
+ */
21
+ tenantId: string | null | undefined;
22
+ /**
23
+ * Stores the set user id from Server for use in sub classes
24
+ */
25
+ userId: string | null | undefined;
26
+ /**
27
+ * Stores the headers to be used in `fetch` calls
28
+ */
29
+ headers: Headers;
30
+ /**
31
+ * The nile-auth url
32
+ */
33
+ apiUrl: string;
34
+ origin?: string | undefined | null;
35
+ /**
36
+ * important for separating the `origin` config value from a default in order to make requests
37
+ */
38
+ serverOrigin: string;
39
+ debug?: boolean;
40
+ /**
41
+ * To use secure cookies or not in the fetch
42
+ */
43
+ secureCookies?: boolean;
44
+ callbackUrl?: string;
45
+ /**
46
+ * change the starting route
47
+ */
48
+ routePrefix: string;
49
+ db: NilePoolConfig;
50
+ constructor(config?: NileConfig, logger?: string);
51
+ }
52
+
3
53
  type Routes = {
4
54
  SIGNIN: string;
5
55
  SESSION: string;
@@ -9,6 +59,7 @@ type Routes = {
9
59
  SIGNOUT: string;
10
60
  ERROR: string;
11
61
  ME: string;
62
+ USER_TENANTS: string;
12
63
  USERS: string;
13
64
  TENANTS: string;
14
65
  TENANT: string;
@@ -18,52 +69,9 @@ type Routes = {
18
69
  VERIFY_REQUEST: string;
19
70
  PASSWORD_RESET: string;
20
71
  LOG: string;
72
+ VERIFY_EMAIL: string;
21
73
  };
22
74
 
23
- type ApiParams = {
24
- basePath?: string | undefined;
25
- cookieKey?: string;
26
- token?: string | undefined;
27
- callbackUrl?: string | undefined;
28
- routes?: Partial<Routes>;
29
- routePrefix?: string | undefined;
30
- secureCookies?: boolean;
31
- origin?: null | undefined | string;
32
- };
33
- declare class ApiConfig {
34
- #private;
35
- cookieKey?: string;
36
- basePath?: string | undefined;
37
- routes?: Partial<Routes>;
38
- routePrefix?: string;
39
- secureCookies?: boolean;
40
- origin?: string | null;
41
- /**
42
- * The client side callback url. Defaults to nothing (so nile.origin will be it), but in the cases of x-origin, you want to set this explicitly to be sure nile-auth does the right thing
43
- * If this is set, any `callbackUrl` from the client will be ignored.
44
- */
45
- callbackUrl?: string;
46
- constructor(config?: ServerConfig, logger?: string);
47
- get token(): string | undefined;
48
- set token(value: string | undefined);
49
- }
50
- declare class Config {
51
- #private;
52
- user: string;
53
- password: string;
54
- databaseId: string;
55
- databaseName: string;
56
- logger?: LoggerType;
57
- debug: boolean;
58
- db: NilePoolConfig;
59
- api: ApiConfig;
60
- get tenantId(): string | undefined | null;
61
- set tenantId(value: string | undefined | null);
62
- get userId(): string | undefined | null;
63
- set userId(value: string | undefined | null);
64
- constructor(config?: ServerConfig, logger?: string);
65
- }
66
-
67
75
  type Opts = {
68
76
  basePath?: string;
69
77
  fetch?: typeof fetch;
@@ -77,23 +85,177 @@ type LoggerType = {
77
85
  error?: (args: unknown | unknown[]) => void;
78
86
  debug?: (args: unknown | unknown[]) => void;
79
87
  };
80
- type ServerConfig = {
88
+ type NileConfig = {
89
+ /**
90
+ * The specific database id. Either passed in or figured out by NILEDB_API_URL
91
+ * process.env.NILEDB_ID
92
+ */
81
93
  databaseId?: string;
94
+ /**
95
+ * The user UUID to the database
96
+ * process.env.NILEDB_USER
97
+ */
82
98
  user?: string;
99
+ /**
100
+ * The password UUID to the database
101
+ * process.env.NILEDB_PASSWORD
102
+ */
83
103
  password?: string;
104
+ /**
105
+ * The name of the database. Automatically obtained from NILEDB_POSTGRES_URL
106
+ * process.env.NILEDB_NAME
107
+ */
84
108
  databaseName?: string;
109
+ /**
110
+ * A tenant id. Scopes requests to a specific tenant, both API and DB
111
+ * process.env.NILEDB_TENANT
112
+ */
85
113
  tenantId?: string | null | undefined;
114
+ /**
115
+ * A user id. Possibly not the logged in user, used for setting database context (nile.user_id)
116
+ * Generally speaking, this wouldn't be used for authentication, and in some cases simply won't do anything on some endpoints
117
+ */
86
118
  userId?: string | null | undefined;
119
+ /**
120
+ * Shows a bunch of logging on the server side to see what's being done between the sdk and nile-auth
121
+ */
87
122
  debug?: boolean;
88
- configureUrl?: string;
123
+ /**
124
+ * DB configuration overrides. Environment variables are the way to go, but maybe you need something more
125
+ */
89
126
  db?: NilePoolConfig;
90
- api?: ApiParams;
127
+ /**
128
+ * Some kind of logger if you want to send to an external service
129
+ */
91
130
  logger?: LoggerType;
131
+ /**
132
+ * The configuration value that maps to `NILEDB_API_URL` - its going to be nile-auth (or similar service)
133
+ */
134
+ apiUrl?: string | undefined;
135
+ /**
136
+ * Ignore client callbackUrls by setting this.
137
+ * You can force the callback URL server side to be sure nile-auth redirects to whatever location.
138
+ */
139
+ callbackUrl?: string | undefined;
140
+ /**
141
+ * Need to override some routes? Change it here
142
+ */
143
+ routes?: Partial<Routes>;
144
+ /**
145
+ * don't like the default `/api`? change it here
146
+ */
147
+ routePrefix?: string | undefined;
148
+ /**
149
+ * In some cases, you may want to force secure cookies.
150
+ * The SDK handles this for you, but might be necessary in some firewall / internal cases
151
+ * Defaults to true if you're in production
152
+ */
153
+ secureCookies?: boolean;
154
+ /**
155
+ * The origin for the requests.
156
+ * Allows the setting of the callback origin to a random FE
157
+ * eg FE localhost:3001 -> BE: localhost:5432 would set to localhost:3001 to be sure nile-auth uses that.
158
+ * In full stack cases, will just be the `host` header of the incoming request, which is used by default
159
+ * It is also important to set this when dealing with secure cookies. Calling via server side needs to know if TLS is being used so that nile-auth knows which cookies to be sent.
160
+ */
161
+ origin?: null | undefined | string;
162
+ /**
163
+ * Set the headers to use in API requests.
164
+ * The `cookie` would be expected if you are setting this, else most calls will be unauthorized
165
+ */
166
+ headers?: null | Headers | Record<string, string>;
92
167
  };
93
168
  type NileDb = NilePoolConfig & {
94
169
  tenantId?: string;
95
170
  };
96
171
  type AfterCreate = (conn: PoolClient, done: (err: null | Error, conn: PoolClient) => void) => void;
172
+ interface NileBody<R, B> {
173
+ readonly body: ReadableStream<Uint8Array> | null | B;
174
+ readonly bodyUsed: boolean;
175
+ arrayBuffer(): Promise<ArrayBuffer>;
176
+ blob(): Promise<Blob>;
177
+ formData(): Promise<FormData>;
178
+ json(): Promise<R>;
179
+ text(): Promise<string>;
180
+ }
181
+ interface NResponse<T> extends NileBody<T, any> {
182
+ readonly headers: Headers;
183
+ readonly ok: boolean;
184
+ readonly redirected: boolean;
185
+ readonly status: number;
186
+ readonly statusText: string;
187
+ readonly type: ResponseType;
188
+ readonly url: string;
189
+ clone(): Response;
190
+ }
191
+ interface NRequest<T> extends NileBody<any, T> {
192
+ /** Returns the cache mode associated with request, which is a string indicating how the request will interact with the browser's cache when fetching. */
193
+ readonly cache: RequestCache;
194
+ /** Returns the credentials mode associated with request, which is a string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. */
195
+ readonly credentials: RequestCredentials;
196
+ /** Returns the kind of resource requested by request, e.g., "document" or "script". */
197
+ readonly destination: RequestDestination;
198
+ /** Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header. */
199
+ readonly headers: Headers;
200
+ /** Returns request's subresource integrity metadata, which is a cryptographic hash of the resource being fetched. Its value consists of multiple hashes separated by whitespace. [SRI] */
201
+ readonly integrity: string;
202
+ /** Returns a boolean indicating whether or not request can outlive the global in which it was created. */
203
+ readonly keepalive: boolean;
204
+ /** Returns request's HTTP method, which is "GET" by default. */
205
+ readonly method: string;
206
+ /** Returns the mode associated with request, which is a string indicating whether the request will use CORS, or will be restricted to same-origin URLs. */
207
+ readonly mode: RequestMode;
208
+ /** Returns the redirect mode associated with request, which is a string indicating how redirects for the request will be handled during fetching. A request will follow redirects by default. */
209
+ readonly redirect: RequestRedirect;
210
+ /** Returns the referrer of request. Its value can be a same-origin URL if explicitly set in init, the empty string to indicate no referrer, and "about:client" when defaulting to the global's default. This is used during fetching to determine the value of the `Referer` header of the request being made. */
211
+ readonly referrer: string;
212
+ /** Returns the referrer policy associated with request. This is used during fetching to compute the value of the request's referrer. */
213
+ readonly referrerPolicy: ReferrerPolicy;
214
+ /** Returns the signal associated with request, which is an AbortSignal object indicating whether or not request has been aborted, and its abort event handler. */
215
+ readonly signal: AbortSignal;
216
+ /** Returns the URL of request as a string. */
217
+ readonly url: string;
218
+ clone(): Request;
219
+ }
220
+ type NileRequest<T> = NRequest<T> | T;
221
+ declare const APIErrorErrorCodeEnum: {
222
+ readonly InternalError: "internal_error";
223
+ readonly BadRequest: "bad_request";
224
+ readonly EntityNotFound: "entity_not_found";
225
+ readonly DuplicateEntity: "duplicate_entity";
226
+ readonly InvalidCredentials: "invalid_credentials";
227
+ readonly UnknownOidcProvider: "unknown_oidc_provider";
228
+ readonly ProviderAlreadyExists: "provider_already_exists";
229
+ readonly ProviderConfigError: "provider_config_error";
230
+ readonly ProviderMismatch: "provider_mismatch";
231
+ readonly ProviderUpdateError: "provider_update_error";
232
+ readonly SessionStateMissing: "session_state_missing";
233
+ readonly SessionStateMismatch: "session_state_mismatch";
234
+ readonly OidcCodeMissing: "oidc_code_missing";
235
+ };
236
+ type APIErrorErrorCodeEnum = (typeof APIErrorErrorCodeEnum)[keyof typeof APIErrorErrorCodeEnum];
237
+ interface APIError {
238
+ [key: string]: any | any;
239
+ /**
240
+ *
241
+ * @type {string}
242
+ * @memberof APIError
243
+ */
244
+ errorCode: APIErrorErrorCodeEnum;
245
+ /**
246
+ *
247
+ * @type {string}
248
+ * @memberof APIError
249
+ */
250
+ message: string;
251
+ /**
252
+ *
253
+ * @type {number}
254
+ * @memberof APIError
255
+ */
256
+ statusCode: number;
257
+ }
258
+ type NileResponse<T> = Promise<T | NResponse<T & APIError>>;
97
259
 
98
260
  interface CreateBasicUserRequest {
99
261
  email: string;
@@ -139,9 +301,7 @@ interface User {
139
301
  created: string;
140
302
  updated?: string;
141
303
  emailVerified?: string | null;
142
- tenants: {
143
- id: string;
144
- }[];
304
+ tenants: string[];
145
305
  }
146
306
 
147
307
  type Tenant = {
@@ -149,12 +309,16 @@ type Tenant = {
149
309
  name: string;
150
310
  };
151
311
 
312
+ type ProviderName = 'discord' | 'github' | 'google' | 'hubspot' | 'linkedin' | 'slack' | 'twitter' | 'email' | 'credentials' | 'azure';
313
+ type Providers = {
314
+ [providerName in ProviderName]: Provider;
315
+ };
152
316
  type Provider = {
153
317
  id: string;
154
318
  name: string;
155
319
  type: string;
156
320
  signinUrl: string;
157
- callbackUr: string;
321
+ callbackUrl: string;
158
322
  };
159
323
  type JWT = {
160
324
  email: string;
@@ -177,171 +341,168 @@ type ActiveSession = {
177
341
  };
178
342
  };
179
343
 
180
- interface NileBody<R, B> {
181
- readonly body: ReadableStream<Uint8Array> | null | B;
182
- readonly bodyUsed: boolean;
183
- arrayBuffer(): Promise<ArrayBuffer>;
184
- blob(): Promise<Blob>;
185
- formData(): Promise<FormData>;
186
- json(): Promise<R>;
187
- text(): Promise<string>;
188
- }
189
- interface NRequest<T> extends NileBody<any, T> {
190
- /** Returns the cache mode associated with request, which is a string indicating how the request will interact with the browser's cache when fetching. */
191
- readonly cache: RequestCache;
192
- /** Returns the credentials mode associated with request, which is a string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. */
193
- readonly credentials: RequestCredentials;
194
- /** Returns the kind of resource requested by request, e.g., "document" or "script". */
195
- readonly destination: RequestDestination;
196
- /** Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header. */
197
- readonly headers: Headers;
198
- /** Returns request's subresource integrity metadata, which is a cryptographic hash of the resource being fetched. Its value consists of multiple hashes separated by whitespace. [SRI] */
199
- readonly integrity: string;
200
- /** Returns a boolean indicating whether or not request can outlive the global in which it was created. */
201
- readonly keepalive: boolean;
202
- /** Returns request's HTTP method, which is "GET" by default. */
203
- readonly method: string;
204
- /** Returns the mode associated with request, which is a string indicating whether the request will use CORS, or will be restricted to same-origin URLs. */
205
- readonly mode: RequestMode;
206
- /** Returns the redirect mode associated with request, which is a string indicating how redirects for the request will be handled during fetching. A request will follow redirects by default. */
207
- readonly redirect: RequestRedirect;
208
- /** Returns the referrer of request. Its value can be a same-origin URL if explicitly set in init, the empty string to indicate no referrer, and "about:client" when defaulting to the global's default. This is used during fetching to determine the value of the `Referer` header of the request being made. */
209
- readonly referrer: string;
210
- /** Returns the referrer policy associated with request. This is used during fetching to compute the value of the request's referrer. */
211
- readonly referrerPolicy: ReferrerPolicy;
212
- /** Returns the signal associated with request, which is an AbortSignal object indicating whether or not request has been aborted, and its abort event handler. */
213
- readonly signal: AbortSignal;
214
- /** Returns the URL of request as a string. */
215
- readonly url: string;
216
- clone(): Request;
217
- }
218
- type NileRequest<T> = NRequest<T> | T;
219
-
220
- declare class Auth extends Config {
221
- headers?: Headers;
222
- resetHeaders?: (headers?: Headers) => void;
223
- constructor(config: Config, headers?: Headers, params?: {
224
- resetHeaders: () => void;
225
- });
226
- handleHeaders(init?: RequestInit): RequestInit | undefined;
227
- get sessionUrl(): string;
228
- getSession: <T = JWT | ActiveSession | Response | undefined>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
229
- get getCsrfUrl(): string;
230
- getCsrf<T = Response | JSON>(req: NileRequest<void> | Headers, init?: RequestInit, raw?: boolean): Promise<T>;
231
- get listProvidersUrl(): string;
232
- listProviders: <T = Response | {
233
- [key: string]: Provider;
234
- }>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
235
- get signOutUrl(): string;
236
- signOut: <T = Response | {
237
- url: string;
238
- }>(req: NileRequest<void | {
239
- callbackUrl?: string;
240
- }> | Headers, init?: RequestInit) => Promise<T>;
344
+ declare class Users {
345
+ #private;
346
+ constructor(config: Config);
347
+ updateSelf<T = User[] | Response>(req: Partial<Omit<User, 'email' | 'tenants' | 'created' | 'updated' | 'emailVerified'> & {
348
+ emailVerified: boolean;
349
+ }>, rawResponse?: boolean): Promise<T>;
350
+ removeSelf(): Promise<Response>;
351
+ getSelf(rawResponse?: true): Promise<Response>;
352
+ getSelf<T = User | Response>(): Promise<T>;
353
+ verifySelf(rawResponse?: true): Promise<Response>;
354
+ verifySelf<T = Response | void>(): Promise<T>;
241
355
  }
242
356
 
243
- declare class Tenants extends Config {
244
- headers?: Headers;
245
- constructor(config: Config, headers?: Headers);
246
- handleHeaders(init?: RequestInit): RequestInit | undefined;
247
- get tenantsUrl(): string;
248
- get tenantUrl(): string;
249
- createTenant: <T = Tenant | Response>(req: NileRequest<{
357
+ type ReqContext = {
358
+ userId?: string;
359
+ tenantId?: string;
360
+ };
361
+ type JoinTenantRequest = string | ReqContext | {
362
+ id: string;
363
+ };
364
+ declare class Tenants {
365
+ #private;
366
+ constructor(config: Config);
367
+ create<T = Tenant | Response>(name: string, rawResponse?: boolean): Promise<T>;
368
+ create<T = Tenant | Response>(payload: {
250
369
  name: string;
251
370
  id?: string;
252
- }> | Headers | string, init?: RequestInit) => Promise<T>;
253
- getTenant: <T = Tenant | Response>(req: NileRequest<{
371
+ }, rawResponse?: boolean): Promise<T>;
372
+ delete<T = Response>(id?: string): Promise<T>;
373
+ delete<T = Response>(payload: {
254
374
  id: string;
255
- }> | Headers | string | void, init?: RequestInit) => Promise<T>;
256
- get tenantListUrl(): string;
257
- listTenants: <T = Tenant[] | Response>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
258
- deleteTenant: <T = Response>(req: NileRequest<void> | Headers | string, init?: RequestInit) => Promise<T>;
259
- updateTenant: <T = Tenant | Response>(req: NileRequest<void> | Headers | {
260
- name: string;
261
- }, init?: RequestInit) => Promise<T>;
262
- }
263
-
264
- declare class Users extends Config {
265
- headers?: Headers;
266
- constructor(config: Config, headers?: Headers);
267
- usersUrl(user: CreateBasicUserRequest): string;
268
- get tenantUsersUrl(): string;
269
- get linkUsersUrl(): string;
270
- get tenantUserUrl(): string;
271
- handleHeaders(init?: RequestInit): RequestInit | undefined;
272
- createUser: <T = User | Response>(user: CreateBasicUserRequest, init?: RequestInit) => Promise<T>;
273
- createTenantUser: <T = User | Response>(req: NileRequest<CreateTenantUserRequest>, init?: RequestInit) => Promise<T>;
274
- updateUser: <T = User[] | Response>(req: NileRequest<Partial<Omit<User, "email" | "tenants" | "created" | "updated">>>, init?: RequestInit) => Promise<T>;
275
- listUsers: <T = User[] | Response>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
276
- linkUser: <T = User | Response>(req: NileRequest<{
277
- id: string;
278
- tenantId?: string;
279
- }> | Headers | string, init?: RequestInit) => Promise<T>;
280
- unlinkUser: <T = Response>(req: NileRequest<{
375
+ }): Promise<T>;
376
+ get(rawResponse: true): Promise<Response>;
377
+ get<T = Tenant | Response>(id: string, rawResponse?: boolean): Promise<T>;
378
+ get<T = Tenant | Response>(payload: {
281
379
  id: string;
380
+ }, rawResponse?: boolean): Promise<T>;
381
+ update(req: Partial<Tenant>, rawResponse: true): Promise<Response>;
382
+ update<T = Tenant | Response | undefined>(req: Partial<Tenant>, rawResponse?: boolean): Promise<T>;
383
+ list(rawResponse: true): Promise<Response>;
384
+ list<T = Tenant[] | Response>(): Promise<T>;
385
+ leaveTenant<T = Response>(req?: string | {
386
+ tenantId: string;
387
+ }): Promise<T>;
388
+ addMember(req: JoinTenantRequest, rawResponse: true): Promise<Response>;
389
+ addMember<T = User | Response>(req: JoinTenantRequest, rawResponse?: boolean): Promise<T>;
390
+ removeMember(req: JoinTenantRequest, rawResponse?: boolean): Promise<Response>;
391
+ users(req: boolean): Promise<Response>;
392
+ users<T = User[] | Response>(req?: boolean | {
282
393
  tenantId?: string;
283
- }> | Headers | string, init?: RequestInit) => Promise<T>;
284
- get meUrl(): string;
285
- me: <T = User | Response>(req: NileRequest<void> | Headers, init?: RequestInit) => Promise<T>;
286
- updateMe: <T = User | Response>(req: NileRequest<Partial<Omit<User, "email" | "id" | "tenants" | "created" | "updated">>> | Headers, init?: RequestInit) => Promise<T>;
394
+ }, rawResponse?: boolean): Promise<T>;
287
395
  }
288
396
 
289
- declare class Api {
397
+ type SignUpPayload = {
398
+ email: string;
399
+ password: string;
400
+ tenantId?: string;
401
+ newTenantName?: string;
402
+ };
403
+ declare class Auth {
404
+ #private;
405
+ constructor(config: Config);
406
+ getSession(rawResponse: true): Promise<Response>;
407
+ getSession<T = JWT | ActiveSession | undefined>(rawResponse?: false): Promise<T>;
408
+ getCsrf(rawResponse: true): Promise<Response>;
409
+ getCsrf<T = Response | JSON>(rawResponse?: false): Promise<T>;
410
+ listProviders(rawResponse: true): Promise<Response>;
411
+ listProviders<T = {
412
+ [key: string]: Provider;
413
+ }>(rawResponse?: false): Promise<T>;
414
+ signOut(): Promise<Response>;
415
+ /**
416
+ * signUp only works with email + password
417
+ * @param payload
418
+ * @param rawResponse
419
+ */
420
+ signUp(payload: SignUpPayload, rawResponse: true): Promise<Response>;
421
+ signUp<T = User | Response>(payload: SignUpPayload): Promise<T>;
422
+ resetPassword(req: Request | {
423
+ email: string;
424
+ password: string;
425
+ callbackUrl?: string;
426
+ redirectUrl?: string;
427
+ }): Promise<Response>;
428
+ callback(provider: ProviderName, body?: string | Request): Promise<Response>;
429
+ /**
430
+ * The return value from this will be a redirect for the client
431
+ * In most cases, you should forward the response directly to the client
432
+ * @param payload
433
+ * @param rawResponse
434
+ */
435
+ signIn<T = Response>(provider: ProviderName, payload?: Request | {
436
+ email: string;
437
+ password: string;
438
+ }, rawResponse?: true): Promise<T>;
439
+ }
440
+ declare function parseCSRF(headers?: Headers): string | undefined;
441
+ declare function parseCallback(headers?: Headers): string | undefined;
442
+ declare function parseToken(headers?: Headers): string | undefined;
443
+
444
+ type CTXHandlerType = {
445
+ GET: (req: Request) => Promise<{
446
+ response: void | Response;
447
+ nile: Server;
448
+ }>;
449
+ POST: (req: Request) => Promise<{
450
+ response: void | Response;
451
+ nile: Server;
452
+ }>;
453
+ DELETE: (req: Request) => Promise<{
454
+ response: void | Response;
455
+ nile: Server;
456
+ }>;
457
+ PUT: (req: Request) => Promise<{
458
+ response: void | Response;
459
+ nile: Server;
460
+ }>;
461
+ };
462
+
463
+ declare class Server {
290
464
  #private;
291
- config: Config;
292
465
  users: Users;
293
- auth: Auth;
294
466
  tenants: Tenants;
295
- routes: Routes;
296
- handlers: {
297
- GET: (req: Request) => Promise<void | Response>;
298
- POST: (req: Request) => Promise<void | Response>;
299
- DELETE: (req: Request) => Promise<void | Response>;
300
- PUT: (req: Request) => Promise<void | Response>;
467
+ auth: Auth;
468
+ constructor(config?: NileConfig);
469
+ get db(): pg.Pool & {
470
+ clearConnections: () => void;
301
471
  };
302
- paths: {
472
+ /**
473
+ * A convenience function that applies a config and ensures whatever was passed is set properly
474
+ */
475
+ getInstance<T = Request | Headers | Record<string, string>>(config: NileConfig, req?: T): Server;
476
+ getPaths(): {
303
477
  get: string[];
304
478
  post: string[];
305
479
  delete: string[];
306
480
  put: string[];
307
481
  };
308
- constructor(config: Config);
309
- reset: () => void;
310
- updateConfig: (config: Config) => void;
311
- resetHeaders: (headers?: Headers) => void;
312
- set headers(headers: Headers | Record<string, string>);
313
- get headers(): Headers | undefined;
314
- getCookie(req?: Request | Headers): string | null | undefined;
315
- login: (payload: {
316
- email: string;
317
- password: string;
318
- }, config?: {
319
- returnResponse?: boolean;
320
- }) => Promise<Response | undefined>;
321
- session: (req?: Request | Headers | null | undefined) => Promise<Response | JWT | ActiveSession | null | undefined>;
322
- setContext: (req: Request | Headers | Record<string, string>) => void;
323
- }
324
-
325
- declare class Server {
326
- config: Config;
327
- api: Api;
328
- private manager;
329
- constructor(config?: ServerConfig);
330
- setConfig(cfg: Config): void;
331
- set databaseId(val: string | void);
332
- get userId(): string | undefined | null;
333
- set userId(userId: string | undefined | null);
334
- get tenantId(): string | undefined | null;
335
- set tenantId(tenantId: string | undefined | null);
336
- get token(): string | undefined | null;
337
- set token(token: string | undefined | null);
338
- get db(): pg.Pool;
339
- clearConnections(): void;
482
+ get handlers(): {
483
+ GET: (req: Request) => Promise<void | Response>;
484
+ POST: (req: Request) => Promise<void | Response>;
485
+ DELETE: (req: Request) => Promise<void | Response>;
486
+ PUT: (req: Request) => Promise<void | Response>;
487
+ withContext: CTXHandlerType;
488
+ };
340
489
  /**
341
- * A convenience function that applies a config and ensures whatever was passed is set properly
490
+ * Allow the setting of headers from a req or header object.
491
+ * Makes it possible to handle REST requests easily
492
+ * Also makes it easy to set user + tenant in some way
493
+ * @param req
494
+ * @returns undefined
342
495
  */
343
- getInstance(config: ServerConfig): Server;
496
+ setContext(req: Request | Headers | Record<string, string> | unknown | {
497
+ tenantId?: string;
498
+ userId?: string;
499
+ }): void;
500
+ getContext(): {
501
+ headers: Headers | undefined;
502
+ userId: string | null | undefined;
503
+ tenantId: string | null | undefined;
504
+ };
344
505
  }
345
- declare function create(config?: ServerConfig): Server;
506
+ declare function create(config?: NileConfig): Server;
346
507
 
347
- export { type ActiveSession, type AfterCreate, type CreateBasicUserRequest, type CreateTenantUserRequest, type JWT, type LoggerType, type LoginUserResponse, type LoginUserResponseToken, LoginUserResponseTokenTypeEnum, create as Nile, type NileDb, type NilePoolConfig, type Opts, Server, type ServerConfig, type Tenant, type User };
508
+ export { type APIError, APIErrorErrorCodeEnum, type ActiveSession, type AfterCreate, type CreateBasicUserRequest, type CreateTenantUserRequest, type JWT, type LoggerType, type LoginUserResponse, type LoginUserResponseToken, LoginUserResponseTokenTypeEnum, create as Nile, type NileConfig, type NileDb, type NilePoolConfig, type NileRequest, type NileResponse, type Opts, type Providers, Server, type Tenant, type User, parseCSRF, parseCallback, parseToken };