@niledatabase/server 5.0.0-alpha.25 → 5.0.0-alpha.27

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
@@ -6,6 +6,7 @@ type Loggable = {
6
6
  debug: LogFunction;
7
7
  warn: LogFunction;
8
8
  error: LogFunction;
9
+ silly: LogFunction;
9
10
  };
10
11
  type LogReturn = (prefixes?: string | string[]) => Loggable;
11
12
 
@@ -17,7 +18,7 @@ type ConfigurablePaths = {
17
18
  };
18
19
  type ExtensionReturns = void | Response | Request | ExtensionState;
19
20
  type ExtensionCtx = {
20
- runExtensions: <T = ExtensionReturns>(toRun: ExtensionState, config: Config, params: any, _init?: RequestInit & {
21
+ runExtensions: <T = ExtensionReturns>(toRun: ExtensionState, config: Config, params?: any, _init?: RequestInit & {
21
22
  request: Request;
22
23
  }) => Promise<T>;
23
24
  };
@@ -31,18 +32,7 @@ declare class Config {
31
32
  extensionCtx: ExtensionCtx;
32
33
  extensions?: Extension[];
33
34
  logger: LogReturn;
34
- /**
35
- * Stores the set tenant id from Server for use in sub classes
36
- */
37
- tenantId: string | null | undefined;
38
- /**
39
- * Stores the set user id from Server for use in sub classes
40
- */
41
- userId: string | null | undefined;
42
- /**
43
- * Stores the headers to be used in `fetch` calls
44
- */
45
- headers: Headers;
35
+ context: PartialContext;
46
36
  /**
47
37
  * The nile-auth url
48
38
  */
@@ -522,6 +512,10 @@ declare function parseToken(headers?: Headers): string | undefined;
522
512
  * Internal helper for the password reset flow.
523
513
  */
524
514
  declare function parseResetToken(headers: Headers | void): string | void;
515
+ /**
516
+ * Extract the tenantId cookie from a set of headers.
517
+ */
518
+ declare function parseTenantId(headers?: Headers): string | null | undefined;
525
519
 
526
520
  declare class Server {
527
521
  #private;
@@ -534,28 +528,23 @@ declare class Server {
534
528
  };
535
529
  get logger(): LogReturn;
536
530
  get extensions(): {
537
- remove: (id: string) => Promise<ExtensionResult[] | undefined>;
531
+ remove: (id: string) => Promise<ExtensionResult<any>[] | undefined>;
538
532
  add: (extension: Extension) => void;
539
533
  };
540
- /**
541
- * A convenience function that applies a config and ensures whatever was passed is set properly
542
- */
543
- getInstance<T = Request | Headers | Record<string, string>>(config: NileConfig, req?: T): this;
544
534
  get handlers(): NileHandlers;
545
535
  get paths(): ConfigurablePaths;
546
536
  set paths(paths: ConfigurablePaths);
537
+ /** Allows setting of context outside of the request lifecycle
538
+ * Basically means you want to disregard cookies and do everything manually
539
+ * If we elect to DDL, we don't want to use tenant id or user id, so remove those.
540
+ */
541
+ withContext(context?: ContextParams): Promise<this>;
542
+ withContext<T>(context: ContextParams, fn: (sdk: this) => Promise<T>): Promise<T>;
547
543
  /**
548
- * Allow the setting of headers from a req or header object.
549
- * Makes it possible to handle REST requests easily
550
- * Also makes it easy to set user + tenant in some way
551
- * @param req
552
- * @returns undefined
544
+ *
545
+ * @returns the last used (basically global) context object, useful for debugging or making your own context
553
546
  */
554
- setContext: (req: Request | Headers | Record<string, string> | unknown | {
555
- tenantId?: string;
556
- userId?: string;
557
- }, ...remaining: unknown[]) => void;
558
- getContext(): any;
547
+ getContext(): Context;
559
548
  }
560
549
  declare function create<T = Server>(config?: NileConfig): T;
561
550
 
@@ -563,15 +552,35 @@ type Opts = {
563
552
  basePath?: string;
564
553
  fetch?: typeof fetch;
565
554
  };
555
+ type Context = {
556
+ headers: Headers;
557
+ tenantId: string | undefined | null;
558
+ userId: string | undefined | null;
559
+ preserveHeaders: boolean;
560
+ };
561
+ type PartialContext = {
562
+ headers?: null | Headers;
563
+ tenantId?: string | undefined | null;
564
+ userId?: string | undefined | null;
565
+ preserveHeaders?: boolean;
566
+ };
567
+ type ContextParams = PartialContext & {
568
+ ddl?: boolean;
569
+ };
570
+ type CTX = {
571
+ run: <T>(ctx: Partial<Context>, fn: () => T) => T;
572
+ get: () => Context;
573
+ set: (partial: Partial<PartialContext>) => void;
574
+ getLastUsed: () => Context;
575
+ };
566
576
  type Any = any;
567
- type ExtensionResult = {
577
+ type ExtensionResult<TParams> = {
568
578
  id: string;
569
- onRequest?: (...params: Any) => void | Promise<void | RequestInit>;
570
- onResponse?: (...params: Any) => void | Promise<void>;
571
- onHandleRequest?: (...params: Any) => RouteReturn | Promise<RouteReturn>;
572
- onConfigure?: (...params: Any) => void;
573
- onSetContext?: (...params: Any) => void;
574
- onGetContext?: () => Any;
579
+ withContext?: (ctx: CTX) => Promise<void>;
580
+ onRequest?: (params: TParams, ctx: CTX) => void | Promise<void | RequestInit>;
581
+ onResponse?: (params: TParams, ctx: CTX) => void | Promise<void>;
582
+ onHandleRequest?: (params?: TParams) => RouteReturn | Promise<RouteReturn>;
583
+ onConfigure?: (params?: TParams) => void;
575
584
  replace?: {
576
585
  handlers: (handlers: NileHandlers) => Any;
577
586
  };
@@ -579,11 +588,12 @@ type ExtensionResult = {
579
588
  type NileHandlers = RouteFunctions & {
580
589
  withContext: CTXHandlerType;
581
590
  };
582
- type Extension = (instance: Server) => ExtensionResult;
591
+ type Extension<TParams = Any> = (instance: Server) => ExtensionResult<TParams>;
583
592
  declare enum ExtensionState {
584
593
  onHandleRequest = "onHandleRequest",
585
594
  onRequest = "onRequest",
586
- onResponse = "onResponse"
595
+ onResponse = "onResponse",
596
+ withContext = "withContext"
587
597
  }
588
598
  type NilePoolConfig = PoolConfig & {
589
599
  afterCreate?: AfterCreate;
@@ -802,4 +812,4 @@ declare const USER_COOKIE = "nile.user-id";
802
812
  declare const HEADER_ORIGIN = "nile-origin";
803
813
  declare const HEADER_SECURE_COOKIES = "nile-secure-cookies";
804
814
 
805
- export { type APIError, APIErrorErrorCodeEnum, type ActiveSession, type AfterCreate, type CTXHandlerType, type ContextReturn, type CreateBasicUserRequest, type CreateTenantUserRequest, type Extension, type ExtensionResult, ExtensionState, HEADER_ORIGIN, HEADER_SECURE_COOKIES, type Invite, type JWT, type LoggerType, type LoginUserResponse, type LoginUserResponseToken, LoginUserResponseTokenTypeEnum, create as Nile, type NileConfig, type NileDb, type NileHandlers, type NilePoolConfig, type NileRequest, type NileResponse, type Opts, type Providers, type RouteFunctions, type RouteReturn, Server, TENANT_COOKIE, type Tenant, USER_COOKIE, type User, parseCSRF, parseCallback, parseResetToken, parseToken };
815
+ export { type APIError, APIErrorErrorCodeEnum, type ActiveSession, type AfterCreate, type CTX, type CTXHandlerType, type Context, type ContextParams, type ContextReturn, type CreateBasicUserRequest, type CreateTenantUserRequest, type Extension, type ExtensionResult, ExtensionState, HEADER_ORIGIN, HEADER_SECURE_COOKIES, type Invite, type JWT, type LoggerType, type LoginUserResponse, type LoginUserResponseToken, LoginUserResponseTokenTypeEnum, create as Nile, type NileConfig, type NileDb, type NileHandlers, type NilePoolConfig, type NileRequest, type NileResponse, type Opts, type PartialContext, type Providers, type RouteFunctions, type RouteReturn, Server, TENANT_COOKIE, type Tenant, USER_COOKIE, type User, parseCSRF, parseCallback, parseResetToken, parseTenantId, parseToken };
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ type Loggable = {
6
6
  debug: LogFunction;
7
7
  warn: LogFunction;
8
8
  error: LogFunction;
9
+ silly: LogFunction;
9
10
  };
10
11
  type LogReturn = (prefixes?: string | string[]) => Loggable;
11
12
 
@@ -17,7 +18,7 @@ type ConfigurablePaths = {
17
18
  };
18
19
  type ExtensionReturns = void | Response | Request | ExtensionState;
19
20
  type ExtensionCtx = {
20
- runExtensions: <T = ExtensionReturns>(toRun: ExtensionState, config: Config, params: any, _init?: RequestInit & {
21
+ runExtensions: <T = ExtensionReturns>(toRun: ExtensionState, config: Config, params?: any, _init?: RequestInit & {
21
22
  request: Request;
22
23
  }) => Promise<T>;
23
24
  };
@@ -31,18 +32,7 @@ declare class Config {
31
32
  extensionCtx: ExtensionCtx;
32
33
  extensions?: Extension[];
33
34
  logger: LogReturn;
34
- /**
35
- * Stores the set tenant id from Server for use in sub classes
36
- */
37
- tenantId: string | null | undefined;
38
- /**
39
- * Stores the set user id from Server for use in sub classes
40
- */
41
- userId: string | null | undefined;
42
- /**
43
- * Stores the headers to be used in `fetch` calls
44
- */
45
- headers: Headers;
35
+ context: PartialContext;
46
36
  /**
47
37
  * The nile-auth url
48
38
  */
@@ -522,6 +512,10 @@ declare function parseToken(headers?: Headers): string | undefined;
522
512
  * Internal helper for the password reset flow.
523
513
  */
524
514
  declare function parseResetToken(headers: Headers | void): string | void;
515
+ /**
516
+ * Extract the tenantId cookie from a set of headers.
517
+ */
518
+ declare function parseTenantId(headers?: Headers): string | null | undefined;
525
519
 
526
520
  declare class Server {
527
521
  #private;
@@ -534,28 +528,23 @@ declare class Server {
534
528
  };
535
529
  get logger(): LogReturn;
536
530
  get extensions(): {
537
- remove: (id: string) => Promise<ExtensionResult[] | undefined>;
531
+ remove: (id: string) => Promise<ExtensionResult<any>[] | undefined>;
538
532
  add: (extension: Extension) => void;
539
533
  };
540
- /**
541
- * A convenience function that applies a config and ensures whatever was passed is set properly
542
- */
543
- getInstance<T = Request | Headers | Record<string, string>>(config: NileConfig, req?: T): this;
544
534
  get handlers(): NileHandlers;
545
535
  get paths(): ConfigurablePaths;
546
536
  set paths(paths: ConfigurablePaths);
537
+ /** Allows setting of context outside of the request lifecycle
538
+ * Basically means you want to disregard cookies and do everything manually
539
+ * If we elect to DDL, we don't want to use tenant id or user id, so remove those.
540
+ */
541
+ withContext(context?: ContextParams): Promise<this>;
542
+ withContext<T>(context: ContextParams, fn: (sdk: this) => Promise<T>): Promise<T>;
547
543
  /**
548
- * Allow the setting of headers from a req or header object.
549
- * Makes it possible to handle REST requests easily
550
- * Also makes it easy to set user + tenant in some way
551
- * @param req
552
- * @returns undefined
544
+ *
545
+ * @returns the last used (basically global) context object, useful for debugging or making your own context
553
546
  */
554
- setContext: (req: Request | Headers | Record<string, string> | unknown | {
555
- tenantId?: string;
556
- userId?: string;
557
- }, ...remaining: unknown[]) => void;
558
- getContext(): any;
547
+ getContext(): Context;
559
548
  }
560
549
  declare function create<T = Server>(config?: NileConfig): T;
561
550
 
@@ -563,15 +552,35 @@ type Opts = {
563
552
  basePath?: string;
564
553
  fetch?: typeof fetch;
565
554
  };
555
+ type Context = {
556
+ headers: Headers;
557
+ tenantId: string | undefined | null;
558
+ userId: string | undefined | null;
559
+ preserveHeaders: boolean;
560
+ };
561
+ type PartialContext = {
562
+ headers?: null | Headers;
563
+ tenantId?: string | undefined | null;
564
+ userId?: string | undefined | null;
565
+ preserveHeaders?: boolean;
566
+ };
567
+ type ContextParams = PartialContext & {
568
+ ddl?: boolean;
569
+ };
570
+ type CTX = {
571
+ run: <T>(ctx: Partial<Context>, fn: () => T) => T;
572
+ get: () => Context;
573
+ set: (partial: Partial<PartialContext>) => void;
574
+ getLastUsed: () => Context;
575
+ };
566
576
  type Any = any;
567
- type ExtensionResult = {
577
+ type ExtensionResult<TParams> = {
568
578
  id: string;
569
- onRequest?: (...params: Any) => void | Promise<void | RequestInit>;
570
- onResponse?: (...params: Any) => void | Promise<void>;
571
- onHandleRequest?: (...params: Any) => RouteReturn | Promise<RouteReturn>;
572
- onConfigure?: (...params: Any) => void;
573
- onSetContext?: (...params: Any) => void;
574
- onGetContext?: () => Any;
579
+ withContext?: (ctx: CTX) => Promise<void>;
580
+ onRequest?: (params: TParams, ctx: CTX) => void | Promise<void | RequestInit>;
581
+ onResponse?: (params: TParams, ctx: CTX) => void | Promise<void>;
582
+ onHandleRequest?: (params?: TParams) => RouteReturn | Promise<RouteReturn>;
583
+ onConfigure?: (params?: TParams) => void;
575
584
  replace?: {
576
585
  handlers: (handlers: NileHandlers) => Any;
577
586
  };
@@ -579,11 +588,12 @@ type ExtensionResult = {
579
588
  type NileHandlers = RouteFunctions & {
580
589
  withContext: CTXHandlerType;
581
590
  };
582
- type Extension = (instance: Server) => ExtensionResult;
591
+ type Extension<TParams = Any> = (instance: Server) => ExtensionResult<TParams>;
583
592
  declare enum ExtensionState {
584
593
  onHandleRequest = "onHandleRequest",
585
594
  onRequest = "onRequest",
586
- onResponse = "onResponse"
595
+ onResponse = "onResponse",
596
+ withContext = "withContext"
587
597
  }
588
598
  type NilePoolConfig = PoolConfig & {
589
599
  afterCreate?: AfterCreate;
@@ -802,4 +812,4 @@ declare const USER_COOKIE = "nile.user-id";
802
812
  declare const HEADER_ORIGIN = "nile-origin";
803
813
  declare const HEADER_SECURE_COOKIES = "nile-secure-cookies";
804
814
 
805
- export { type APIError, APIErrorErrorCodeEnum, type ActiveSession, type AfterCreate, type CTXHandlerType, type ContextReturn, type CreateBasicUserRequest, type CreateTenantUserRequest, type Extension, type ExtensionResult, ExtensionState, HEADER_ORIGIN, HEADER_SECURE_COOKIES, type Invite, type JWT, type LoggerType, type LoginUserResponse, type LoginUserResponseToken, LoginUserResponseTokenTypeEnum, create as Nile, type NileConfig, type NileDb, type NileHandlers, type NilePoolConfig, type NileRequest, type NileResponse, type Opts, type Providers, type RouteFunctions, type RouteReturn, Server, TENANT_COOKIE, type Tenant, USER_COOKIE, type User, parseCSRF, parseCallback, parseResetToken, parseToken };
815
+ export { type APIError, APIErrorErrorCodeEnum, type ActiveSession, type AfterCreate, type CTX, type CTXHandlerType, type Context, type ContextParams, type ContextReturn, type CreateBasicUserRequest, type CreateTenantUserRequest, type Extension, type ExtensionResult, ExtensionState, HEADER_ORIGIN, HEADER_SECURE_COOKIES, type Invite, type JWT, type LoggerType, type LoginUserResponse, type LoginUserResponseToken, LoginUserResponseTokenTypeEnum, create as Nile, type NileConfig, type NileDb, type NileHandlers, type NilePoolConfig, type NileRequest, type NileResponse, type Opts, type PartialContext, type Providers, type RouteFunctions, type RouteReturn, Server, TENANT_COOKIE, type Tenant, USER_COOKIE, type User, parseCSRF, parseCallback, parseResetToken, parseTenantId, parseToken };