@niledatabase/server 5.0.0-alpha.0 → 5.0.0-alpha.10
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 +447 -204
- package/dist/index.d.ts +447 -204
- package/dist/index.js +2538 -2023
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2534 -2027
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -16
- package/dist/express.d.mts +0 -32
- package/dist/express.d.ts +0 -32
- package/dist/express.js +0 -167
- package/dist/express.js.map +0 -1
- package/dist/express.mjs +0 -163
- package/dist/express.mjs.map +0 -1
- package/dist/nitro.d.mts +0 -7
- package/dist/nitro.d.ts +0 -7
- package/dist/nitro.js +0 -36
- package/dist/nitro.js.map +0 -1
- package/dist/nitro.mjs +0 -34
- package/dist/nitro.mjs.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,74 @@
|
|
|
1
1
|
import pg, { PoolConfig, PoolClient } from 'pg';
|
|
2
2
|
|
|
3
|
+
type LogFunction = (message: string | unknown, meta?: Record<string, unknown>) => void;
|
|
4
|
+
type Loggable = {
|
|
5
|
+
info: LogFunction;
|
|
6
|
+
debug: LogFunction;
|
|
7
|
+
warn: LogFunction;
|
|
8
|
+
error: LogFunction;
|
|
9
|
+
};
|
|
10
|
+
type LogReturn = (prefixes?: string | string[]) => Loggable;
|
|
11
|
+
|
|
12
|
+
type ExtensionCtx = {
|
|
13
|
+
handleOnRequest: (config: Config, _init: RequestInit & {
|
|
14
|
+
request: Request;
|
|
15
|
+
}, params: RequestInit) => Promise<void>;
|
|
16
|
+
};
|
|
17
|
+
type ConfigConstructor = NileConfig & {
|
|
18
|
+
extensionCtx?: ExtensionCtx;
|
|
19
|
+
};
|
|
20
|
+
declare class Config {
|
|
21
|
+
routes: Routes;
|
|
22
|
+
handlers: {
|
|
23
|
+
GET: (req: Request) => Promise<void | Response>;
|
|
24
|
+
POST: (req: Request) => Promise<void | Response>;
|
|
25
|
+
DELETE: (req: Request) => Promise<void | Response>;
|
|
26
|
+
PUT: (req: Request) => Promise<void | Response>;
|
|
27
|
+
};
|
|
28
|
+
paths: {
|
|
29
|
+
get: string[];
|
|
30
|
+
post: string[];
|
|
31
|
+
delete: string[];
|
|
32
|
+
put: string[];
|
|
33
|
+
};
|
|
34
|
+
extensionCtx: ExtensionCtx;
|
|
35
|
+
extensions?: Extension[];
|
|
36
|
+
logger: LogReturn;
|
|
37
|
+
/**
|
|
38
|
+
* Stores the set tenant id from Server for use in sub classes
|
|
39
|
+
*/
|
|
40
|
+
tenantId: string | null | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Stores the set user id from Server for use in sub classes
|
|
43
|
+
*/
|
|
44
|
+
userId: string | null | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Stores the headers to be used in `fetch` calls
|
|
47
|
+
*/
|
|
48
|
+
headers: Headers;
|
|
49
|
+
/**
|
|
50
|
+
* The nile-auth url
|
|
51
|
+
*/
|
|
52
|
+
apiUrl: string;
|
|
53
|
+
origin?: string | undefined | null;
|
|
54
|
+
/**
|
|
55
|
+
* important for separating the `origin` config value from a default in order to make requests
|
|
56
|
+
*/
|
|
57
|
+
serverOrigin: string;
|
|
58
|
+
debug?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* To use secure cookies or not in the fetch
|
|
61
|
+
*/
|
|
62
|
+
secureCookies?: boolean;
|
|
63
|
+
callbackUrl?: string;
|
|
64
|
+
/**
|
|
65
|
+
* change the starting route
|
|
66
|
+
*/
|
|
67
|
+
routePrefix: string;
|
|
68
|
+
db: NilePoolConfig;
|
|
69
|
+
constructor(config?: ConfigConstructor);
|
|
70
|
+
}
|
|
71
|
+
|
|
3
72
|
type Routes = {
|
|
4
73
|
SIGNIN: string;
|
|
5
74
|
SESSION: string;
|
|
@@ -9,6 +78,7 @@ type Routes = {
|
|
|
9
78
|
SIGNOUT: string;
|
|
10
79
|
ERROR: string;
|
|
11
80
|
ME: string;
|
|
81
|
+
USER_TENANTS: string;
|
|
12
82
|
USERS: string;
|
|
13
83
|
TENANTS: string;
|
|
14
84
|
TENANT: string;
|
|
@@ -18,83 +88,11 @@ type Routes = {
|
|
|
18
88
|
VERIFY_REQUEST: string;
|
|
19
89
|
PASSWORD_RESET: string;
|
|
20
90
|
LOG: string;
|
|
91
|
+
VERIFY_EMAIL: string;
|
|
92
|
+
INVITES: string;
|
|
93
|
+
INVITE: string;
|
|
21
94
|
};
|
|
22
95
|
|
|
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
|
-
type Opts = {
|
|
68
|
-
basePath?: string;
|
|
69
|
-
fetch?: typeof fetch;
|
|
70
|
-
};
|
|
71
|
-
type NilePoolConfig = PoolConfig & {
|
|
72
|
-
afterCreate?: AfterCreate;
|
|
73
|
-
};
|
|
74
|
-
type LoggerType = {
|
|
75
|
-
info?: (args: unknown | unknown[]) => void;
|
|
76
|
-
warn?: (args: unknown | unknown[]) => void;
|
|
77
|
-
error?: (args: unknown | unknown[]) => void;
|
|
78
|
-
debug?: (args: unknown | unknown[]) => void;
|
|
79
|
-
};
|
|
80
|
-
type ServerConfig = {
|
|
81
|
-
databaseId?: string;
|
|
82
|
-
user?: string;
|
|
83
|
-
password?: string;
|
|
84
|
-
databaseName?: string;
|
|
85
|
-
tenantId?: string | null | undefined;
|
|
86
|
-
userId?: string | null | undefined;
|
|
87
|
-
debug?: boolean;
|
|
88
|
-
configureUrl?: string;
|
|
89
|
-
db?: NilePoolConfig;
|
|
90
|
-
api?: ApiParams;
|
|
91
|
-
logger?: LoggerType;
|
|
92
|
-
};
|
|
93
|
-
type NileDb = NilePoolConfig & {
|
|
94
|
-
tenantId?: string;
|
|
95
|
-
};
|
|
96
|
-
type AfterCreate = (conn: PoolClient, done: (err: null | Error, conn: PoolClient) => void) => void;
|
|
97
|
-
|
|
98
96
|
interface CreateBasicUserRequest {
|
|
99
97
|
email: string;
|
|
100
98
|
password: string;
|
|
@@ -139,22 +137,111 @@ interface User {
|
|
|
139
137
|
created: string;
|
|
140
138
|
updated?: string;
|
|
141
139
|
emailVerified?: string | null;
|
|
142
|
-
tenants:
|
|
143
|
-
|
|
144
|
-
|
|
140
|
+
tenants: string[];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
declare class Users {
|
|
144
|
+
#private;
|
|
145
|
+
constructor(config: Config);
|
|
146
|
+
updateSelf<T = User[] | Response>(req: Partial<Omit<User, 'email' | 'tenants' | 'created' | 'updated' | 'emailVerified'> & {
|
|
147
|
+
emailVerified: boolean;
|
|
148
|
+
}>, rawResponse?: boolean): Promise<T>;
|
|
149
|
+
removeSelf(): Promise<Response>;
|
|
150
|
+
getSelf<T = User | Response>(): Promise<T>;
|
|
151
|
+
getSelf(rawResponse?: true): Promise<Response>;
|
|
152
|
+
verifySelf<T = void>(): Promise<T>;
|
|
153
|
+
verifySelf(rawResponse: true): Promise<Response>;
|
|
154
|
+
verifySelf<T = Response | User>(options: {
|
|
155
|
+
bypassEmail?: boolean;
|
|
156
|
+
callbackUrl?: string;
|
|
157
|
+
}, rawResponse?: true): Promise<T>;
|
|
145
158
|
}
|
|
146
159
|
|
|
147
160
|
type Tenant = {
|
|
148
161
|
id: string;
|
|
149
162
|
name: string;
|
|
150
163
|
};
|
|
164
|
+
type Invite = {
|
|
165
|
+
id: string;
|
|
166
|
+
tenant_id: string;
|
|
167
|
+
token: string;
|
|
168
|
+
identifier: string;
|
|
169
|
+
roles: null | string;
|
|
170
|
+
created_by: string;
|
|
171
|
+
expires: Date;
|
|
172
|
+
};
|
|
151
173
|
|
|
174
|
+
type ReqContext = {
|
|
175
|
+
userId?: string;
|
|
176
|
+
tenantId?: string;
|
|
177
|
+
};
|
|
178
|
+
type JoinTenantRequest = string | ReqContext | {
|
|
179
|
+
id: string;
|
|
180
|
+
};
|
|
181
|
+
declare class Tenants {
|
|
182
|
+
#private;
|
|
183
|
+
constructor(config: Config);
|
|
184
|
+
create(name: string, rawResponse: true): Promise<Response>;
|
|
185
|
+
create<T = Tenant | Response>(name: string, rawResponse?: boolean): Promise<T>;
|
|
186
|
+
create<T = Tenant | Response>(payload: {
|
|
187
|
+
name: string;
|
|
188
|
+
id?: string;
|
|
189
|
+
}, rawResponse?: boolean): Promise<T>;
|
|
190
|
+
delete<T = Response>(id?: string): Promise<T>;
|
|
191
|
+
delete<T = Response>(payload: {
|
|
192
|
+
id: string;
|
|
193
|
+
}): Promise<T>;
|
|
194
|
+
get<T = Tenant | Response>(): Promise<T>;
|
|
195
|
+
get<T = Tenant | Response>(id: string, rawResponse?: boolean): Promise<T>;
|
|
196
|
+
get(rawResponse: true): Promise<Response>;
|
|
197
|
+
get<T = Tenant | Response>(payload: {
|
|
198
|
+
id: string;
|
|
199
|
+
}, rawResponse?: boolean): Promise<T>;
|
|
200
|
+
update(req: Partial<Tenant>, rawResponse: true): Promise<Response>;
|
|
201
|
+
update<T = Tenant | Response | undefined>(req: Partial<Tenant>, rawResponse?: boolean): Promise<T>;
|
|
202
|
+
list<T = Tenant[] | Response>(): Promise<T>;
|
|
203
|
+
list(rawResponse: true): Promise<Response>;
|
|
204
|
+
leaveTenant<T = Response>(req?: string | {
|
|
205
|
+
tenantId: string;
|
|
206
|
+
}): Promise<T>;
|
|
207
|
+
addMember(req: JoinTenantRequest, rawResponse: true): Promise<Response>;
|
|
208
|
+
addMember<T = User | Response>(req: JoinTenantRequest, rawResponse?: boolean): Promise<T>;
|
|
209
|
+
removeMember(req: JoinTenantRequest, rawResponse?: boolean): Promise<Response>;
|
|
210
|
+
users<T = User[] | Response>(req?: boolean | {
|
|
211
|
+
tenantId?: string;
|
|
212
|
+
}, rawResponse?: boolean): Promise<T>;
|
|
213
|
+
users(req: true): Promise<Response>;
|
|
214
|
+
invites<T = Invite[] | Response>(): Promise<T>;
|
|
215
|
+
invite<T = Response | Invite>(req: string | {
|
|
216
|
+
email: string;
|
|
217
|
+
callbackUrl?: string;
|
|
218
|
+
redirectUrl?: string;
|
|
219
|
+
}, rawResponse?: boolean): Promise<T>;
|
|
220
|
+
invite(req: string | {
|
|
221
|
+
email: string;
|
|
222
|
+
callbackUrl?: string;
|
|
223
|
+
redirectUrl?: string;
|
|
224
|
+
}, rawResponse: true): Promise<Response>;
|
|
225
|
+
acceptInvite<T = Response>(req?: {
|
|
226
|
+
identifier: string;
|
|
227
|
+
token: string;
|
|
228
|
+
redirectUrl?: string;
|
|
229
|
+
}, rawResponse?: boolean): Promise<T>;
|
|
230
|
+
deleteInvite<T = Response>(req: string | {
|
|
231
|
+
id: string;
|
|
232
|
+
}): Promise<T>;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
type ProviderName = 'discord' | 'github' | 'google' | 'hubspot' | 'linkedin' | 'slack' | 'twitter' | 'email' | 'credentials' | 'azure';
|
|
236
|
+
type Providers = {
|
|
237
|
+
[providerName in ProviderName]: Provider;
|
|
238
|
+
};
|
|
152
239
|
type Provider = {
|
|
153
240
|
id: string;
|
|
154
241
|
name: string;
|
|
155
242
|
type: string;
|
|
156
243
|
signinUrl: string;
|
|
157
|
-
|
|
244
|
+
callbackUrl: string;
|
|
158
245
|
};
|
|
159
246
|
type JWT = {
|
|
160
247
|
email: string;
|
|
@@ -177,6 +264,236 @@ type ActiveSession = {
|
|
|
177
264
|
};
|
|
178
265
|
};
|
|
179
266
|
|
|
267
|
+
type SignUpPayload = {
|
|
268
|
+
email: string;
|
|
269
|
+
password: string;
|
|
270
|
+
tenantId?: string;
|
|
271
|
+
newTenantName?: string;
|
|
272
|
+
};
|
|
273
|
+
declare class Auth {
|
|
274
|
+
#private;
|
|
275
|
+
constructor(config: Config);
|
|
276
|
+
getSession<T = JWT | ActiveSession | undefined>(rawResponse?: false): Promise<T>;
|
|
277
|
+
getSession(rawResponse: true): Promise<Response>;
|
|
278
|
+
getCsrf<T = Response | JSON>(rawResponse?: false): Promise<T>;
|
|
279
|
+
getCsrf(rawResponse: true): Promise<Response>;
|
|
280
|
+
listProviders(rawResponse: true): Promise<Response>;
|
|
281
|
+
listProviders<T = {
|
|
282
|
+
[key: string]: Provider;
|
|
283
|
+
}>(rawResponse?: false): Promise<T>;
|
|
284
|
+
signOut(): Promise<Response>;
|
|
285
|
+
/**
|
|
286
|
+
* signUp only works with email + password
|
|
287
|
+
* @param payload
|
|
288
|
+
* @param rawResponse
|
|
289
|
+
*/
|
|
290
|
+
signUp(payload: SignUpPayload, rawResponse: true): Promise<Response>;
|
|
291
|
+
signUp<T = User | Response>(payload: SignUpPayload): Promise<T>;
|
|
292
|
+
forgotPassword(req: {
|
|
293
|
+
email: string;
|
|
294
|
+
callbackUrl?: string;
|
|
295
|
+
redirectUrl?: string;
|
|
296
|
+
}): Promise<Response>;
|
|
297
|
+
resetPassword(req: Request | {
|
|
298
|
+
email: string;
|
|
299
|
+
password: string;
|
|
300
|
+
callbackUrl?: string;
|
|
301
|
+
redirectUrl?: string;
|
|
302
|
+
}): Promise<Response>;
|
|
303
|
+
callback(provider: ProviderName, body?: string | Request): Promise<Response>;
|
|
304
|
+
/**
|
|
305
|
+
* The return value from this will be a redirect for the client
|
|
306
|
+
* In most cases, you should forward the response directly to the client
|
|
307
|
+
* @param payload
|
|
308
|
+
* @param rawResponse
|
|
309
|
+
*/
|
|
310
|
+
signIn<T = Response>(provider: ProviderName, payload?: Request | {
|
|
311
|
+
email: string;
|
|
312
|
+
password: string;
|
|
313
|
+
}, rawResponse?: true): Promise<T>;
|
|
314
|
+
}
|
|
315
|
+
declare function parseCSRF(headers?: Headers): string | undefined;
|
|
316
|
+
declare function parseCallback(headers?: Headers): string | undefined;
|
|
317
|
+
declare function parseToken(headers?: Headers): string | undefined;
|
|
318
|
+
|
|
319
|
+
type CTXHandlerType = {
|
|
320
|
+
GET: (req: Request) => Promise<{
|
|
321
|
+
response: void | Response;
|
|
322
|
+
nile: Server;
|
|
323
|
+
}>;
|
|
324
|
+
POST: (req: Request) => Promise<{
|
|
325
|
+
response: void | Response;
|
|
326
|
+
nile: Server;
|
|
327
|
+
}>;
|
|
328
|
+
DELETE: (req: Request) => Promise<{
|
|
329
|
+
response: void | Response;
|
|
330
|
+
nile: Server;
|
|
331
|
+
}>;
|
|
332
|
+
PUT: (req: Request) => Promise<{
|
|
333
|
+
response: void | Response;
|
|
334
|
+
nile: Server;
|
|
335
|
+
}>;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
declare class Server {
|
|
339
|
+
#private;
|
|
340
|
+
users: Users;
|
|
341
|
+
tenants: Tenants;
|
|
342
|
+
auth: Auth;
|
|
343
|
+
constructor(config?: NileConfig);
|
|
344
|
+
get db(): pg.Pool & {
|
|
345
|
+
clearConnections: () => void;
|
|
346
|
+
};
|
|
347
|
+
/**
|
|
348
|
+
* A convenience function that applies a config and ensures whatever was passed is set properly
|
|
349
|
+
*/
|
|
350
|
+
getInstance<T = Request | Headers | Record<string, string>>(config: NileConfig, req?: T): Server;
|
|
351
|
+
getPaths(): {
|
|
352
|
+
get: string[];
|
|
353
|
+
post: string[];
|
|
354
|
+
delete: string[];
|
|
355
|
+
put: string[];
|
|
356
|
+
};
|
|
357
|
+
get handlers(): {
|
|
358
|
+
GET: (req: Request) => Promise<void | Response>;
|
|
359
|
+
POST: (req: Request) => Promise<void | Response>;
|
|
360
|
+
DELETE: (req: Request) => Promise<void | Response>;
|
|
361
|
+
PUT: (req: Request) => Promise<void | Response>;
|
|
362
|
+
withContext: CTXHandlerType;
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
365
|
+
* Allow the setting of headers from a req or header object.
|
|
366
|
+
* Makes it possible to handle REST requests easily
|
|
367
|
+
* Also makes it easy to set user + tenant in some way
|
|
368
|
+
* @param req
|
|
369
|
+
* @returns undefined
|
|
370
|
+
*/
|
|
371
|
+
setContext(req: Request | Headers | Record<string, string> | unknown | {
|
|
372
|
+
tenantId?: string;
|
|
373
|
+
userId?: string;
|
|
374
|
+
}): void;
|
|
375
|
+
getContext(): {
|
|
376
|
+
headers: Headers | undefined;
|
|
377
|
+
userId: string | null | undefined;
|
|
378
|
+
tenantId: string | null | undefined;
|
|
379
|
+
preserveHeaders: boolean;
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
declare function create(config?: NileConfig): Server;
|
|
383
|
+
|
|
384
|
+
type Opts = {
|
|
385
|
+
basePath?: string;
|
|
386
|
+
fetch?: typeof fetch;
|
|
387
|
+
};
|
|
388
|
+
interface ExtensionResult {
|
|
389
|
+
id?: string;
|
|
390
|
+
[key: string]: unknown;
|
|
391
|
+
onRequest?: (req: Request) => void | Promise<void | RequestInit>;
|
|
392
|
+
onResponse?: (res: Response) => void | Promise<void>;
|
|
393
|
+
}
|
|
394
|
+
type Extension = (instance: Server) => ExtensionResult | Promise<ExtensionResult>;
|
|
395
|
+
type NilePoolConfig = PoolConfig & {
|
|
396
|
+
afterCreate?: AfterCreate;
|
|
397
|
+
};
|
|
398
|
+
type LoggerType = {
|
|
399
|
+
info: (args: unknown | unknown[]) => void;
|
|
400
|
+
warn: (args: unknown | unknown[]) => void;
|
|
401
|
+
error: (args: unknown | unknown[]) => void;
|
|
402
|
+
debug: (args: unknown | unknown[]) => void;
|
|
403
|
+
};
|
|
404
|
+
type NileConfig = {
|
|
405
|
+
/**
|
|
406
|
+
* The specific database id. Either passed in or figured out by NILEDB_API_URL
|
|
407
|
+
* process.env.NILEDB_ID
|
|
408
|
+
*/
|
|
409
|
+
databaseId?: string;
|
|
410
|
+
/**
|
|
411
|
+
* The user UUID to the database
|
|
412
|
+
* process.env.NILEDB_USER
|
|
413
|
+
*/
|
|
414
|
+
user?: string;
|
|
415
|
+
/**
|
|
416
|
+
* The password UUID to the database
|
|
417
|
+
* process.env.NILEDB_PASSWORD
|
|
418
|
+
*/
|
|
419
|
+
password?: string;
|
|
420
|
+
/**
|
|
421
|
+
* The name of the database. Automatically obtained from NILEDB_POSTGRES_URL
|
|
422
|
+
* process.env.NILEDB_NAME
|
|
423
|
+
*/
|
|
424
|
+
databaseName?: string;
|
|
425
|
+
/**
|
|
426
|
+
* A tenant id. Scopes requests to a specific tenant, both API and DB
|
|
427
|
+
* process.env.NILEDB_TENANT
|
|
428
|
+
*/
|
|
429
|
+
tenantId?: string | null | undefined;
|
|
430
|
+
/**
|
|
431
|
+
* A user id. Possibly not the logged in user, used for setting database context (nile.user_id)
|
|
432
|
+
* Generally speaking, this wouldn't be used for authentication, and in some cases simply won't do anything on some endpoints
|
|
433
|
+
*/
|
|
434
|
+
userId?: string | null | undefined;
|
|
435
|
+
/**
|
|
436
|
+
* Shows a bunch of logging on the server side to see what's being done between the sdk and nile-auth
|
|
437
|
+
*/
|
|
438
|
+
debug?: boolean;
|
|
439
|
+
/**
|
|
440
|
+
* DB configuration overrides. Environment variables are the way to go, but maybe you need something more
|
|
441
|
+
*/
|
|
442
|
+
db?: NilePoolConfig;
|
|
443
|
+
/**
|
|
444
|
+
* Some kind of logger if you want to send to an external service
|
|
445
|
+
*/
|
|
446
|
+
logger?: LogReturn;
|
|
447
|
+
/**
|
|
448
|
+
* The configuration value that maps to `NILEDB_API_URL` - its going to be nile-auth (or similar service)
|
|
449
|
+
*/
|
|
450
|
+
apiUrl?: string | undefined;
|
|
451
|
+
/**
|
|
452
|
+
* Ignore client callbackUrls by setting this.
|
|
453
|
+
* You can force the callback URL server side to be sure nile-auth redirects to whatever location.
|
|
454
|
+
*/
|
|
455
|
+
callbackUrl?: string | undefined;
|
|
456
|
+
/**
|
|
457
|
+
* Need to override some routes? Change it here
|
|
458
|
+
*/
|
|
459
|
+
routes?: Partial<Routes>;
|
|
460
|
+
/**
|
|
461
|
+
* don't like the default `/api`? change it here
|
|
462
|
+
*/
|
|
463
|
+
routePrefix?: string | undefined;
|
|
464
|
+
/**
|
|
465
|
+
* In some cases, you may want to force secure cookies.
|
|
466
|
+
* The SDK handles this for you, but might be necessary in some firewall / internal cases
|
|
467
|
+
* Defaults to true if you're in production
|
|
468
|
+
*/
|
|
469
|
+
secureCookies?: boolean;
|
|
470
|
+
/**
|
|
471
|
+
* The origin for the requests.
|
|
472
|
+
* Allows the setting of the callback origin to a random FE
|
|
473
|
+
* eg FE localhost:3001 -> BE: localhost:5432 would set to localhost:3001 to be sure nile-auth uses that.
|
|
474
|
+
* In full stack cases, will just be the `host` header of the incoming request, which is used by default
|
|
475
|
+
* 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.
|
|
476
|
+
*/
|
|
477
|
+
origin?: null | undefined | string;
|
|
478
|
+
/**
|
|
479
|
+
* Set the headers to use in API requests.
|
|
480
|
+
* The `cookie` would be expected if you are setting this, else most calls will be unauthorized
|
|
481
|
+
*/
|
|
482
|
+
headers?: null | Headers | Record<string, string>;
|
|
483
|
+
/**
|
|
484
|
+
* Functions to run at various points to make life easier
|
|
485
|
+
*/
|
|
486
|
+
extensions?: Extension[];
|
|
487
|
+
/**
|
|
488
|
+
* In some cases, like when using REST context, we want to preserve the headers from the request,
|
|
489
|
+
* regardless of what an extension might do
|
|
490
|
+
*/
|
|
491
|
+
preserveHeaders?: boolean;
|
|
492
|
+
};
|
|
493
|
+
type NileDb = NilePoolConfig & {
|
|
494
|
+
tenantId?: string;
|
|
495
|
+
};
|
|
496
|
+
type AfterCreate = (conn: PoolClient, done: (err: null | Error, conn: PoolClient) => void) => void;
|
|
180
497
|
interface NileBody<R, B> {
|
|
181
498
|
readonly body: ReadableStream<Uint8Array> | null | B;
|
|
182
499
|
readonly bodyUsed: boolean;
|
|
@@ -186,6 +503,16 @@ interface NileBody<R, B> {
|
|
|
186
503
|
json(): Promise<R>;
|
|
187
504
|
text(): Promise<string>;
|
|
188
505
|
}
|
|
506
|
+
interface NResponse<T> extends NileBody<T, any> {
|
|
507
|
+
readonly headers: Headers;
|
|
508
|
+
readonly ok: boolean;
|
|
509
|
+
readonly redirected: boolean;
|
|
510
|
+
readonly status: number;
|
|
511
|
+
readonly statusText: string;
|
|
512
|
+
readonly type: ResponseType;
|
|
513
|
+
readonly url: string;
|
|
514
|
+
clone(): Response;
|
|
515
|
+
}
|
|
189
516
|
interface NRequest<T> extends NileBody<any, T> {
|
|
190
517
|
/** 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
518
|
readonly cache: RequestCache;
|
|
@@ -216,132 +543,48 @@ interface NRequest<T> extends NileBody<any, T> {
|
|
|
216
543
|
clone(): Request;
|
|
217
544
|
}
|
|
218
545
|
type NileRequest<T> = NRequest<T> | T;
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
url: string;
|
|
238
|
-
}>(req: NileRequest<void | {
|
|
239
|
-
callbackUrl?: string;
|
|
240
|
-
}> | Headers, init?: RequestInit) => Promise<T>;
|
|
241
|
-
}
|
|
242
|
-
|
|
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<{
|
|
250
|
-
name: string;
|
|
251
|
-
id?: string;
|
|
252
|
-
}> | Headers | string, init?: RequestInit) => Promise<T>;
|
|
253
|
-
getTenant: <T = Tenant | Response>(req: NileRequest<{
|
|
254
|
-
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<{
|
|
281
|
-
id: string;
|
|
282
|
-
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>;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
declare class Api {
|
|
290
|
-
#private;
|
|
291
|
-
config: Config;
|
|
292
|
-
users: Users;
|
|
293
|
-
auth: Auth;
|
|
294
|
-
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>;
|
|
301
|
-
};
|
|
302
|
-
paths: {
|
|
303
|
-
get: string[];
|
|
304
|
-
post: string[];
|
|
305
|
-
delete: string[];
|
|
306
|
-
put: string[];
|
|
307
|
-
};
|
|
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;
|
|
546
|
+
declare const APIErrorErrorCodeEnum: {
|
|
547
|
+
readonly InternalError: "internal_error";
|
|
548
|
+
readonly BadRequest: "bad_request";
|
|
549
|
+
readonly EntityNotFound: "entity_not_found";
|
|
550
|
+
readonly DuplicateEntity: "duplicate_entity";
|
|
551
|
+
readonly InvalidCredentials: "invalid_credentials";
|
|
552
|
+
readonly UnknownOidcProvider: "unknown_oidc_provider";
|
|
553
|
+
readonly ProviderAlreadyExists: "provider_already_exists";
|
|
554
|
+
readonly ProviderConfigError: "provider_config_error";
|
|
555
|
+
readonly ProviderMismatch: "provider_mismatch";
|
|
556
|
+
readonly ProviderUpdateError: "provider_update_error";
|
|
557
|
+
readonly SessionStateMissing: "session_state_missing";
|
|
558
|
+
readonly SessionStateMismatch: "session_state_mismatch";
|
|
559
|
+
readonly OidcCodeMissing: "oidc_code_missing";
|
|
560
|
+
};
|
|
561
|
+
type APIErrorErrorCodeEnum = (typeof APIErrorErrorCodeEnum)[keyof typeof APIErrorErrorCodeEnum];
|
|
562
|
+
interface APIError {
|
|
563
|
+
[key: string]: any | any;
|
|
340
564
|
/**
|
|
341
|
-
*
|
|
565
|
+
*
|
|
566
|
+
* @type {string}
|
|
567
|
+
* @memberof APIError
|
|
568
|
+
*/
|
|
569
|
+
errorCode: APIErrorErrorCodeEnum;
|
|
570
|
+
/**
|
|
571
|
+
*
|
|
572
|
+
* @type {string}
|
|
573
|
+
* @memberof APIError
|
|
574
|
+
*/
|
|
575
|
+
message: string;
|
|
576
|
+
/**
|
|
577
|
+
*
|
|
578
|
+
* @type {number}
|
|
579
|
+
* @memberof APIError
|
|
342
580
|
*/
|
|
343
|
-
|
|
581
|
+
statusCode: number;
|
|
344
582
|
}
|
|
345
|
-
|
|
583
|
+
type NileResponse<T> = Promise<T | NResponse<T & APIError>>;
|
|
584
|
+
|
|
585
|
+
declare const TENANT_COOKIE = "nile.tenant-id";
|
|
586
|
+
declare const USER_COOKIE = "nile.user-id";
|
|
587
|
+
declare const HEADER_ORIGIN = "nile-origin";
|
|
588
|
+
declare const HEADER_SECURE_COOKIES = "nile-secure-cookies";
|
|
346
589
|
|
|
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
|
|
590
|
+
export { type APIError, APIErrorErrorCodeEnum, type ActiveSession, type AfterCreate, type CreateBasicUserRequest, type CreateTenantUserRequest, type Extension, type ExtensionResult, HEADER_ORIGIN, HEADER_SECURE_COOKIES, type Invite, 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, TENANT_COOKIE, type Tenant, USER_COOKIE, type User, parseCSRF, parseCallback, parseToken };
|