@niledatabase/server 5.0.0-alpha.16 → 5.0.0-alpha.18

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
@@ -179,8 +179,8 @@ declare class Users {
179
179
  *
180
180
  * @param [rawResponse] - When `true` return the raw {@link Response}.
181
181
  */
182
- getSelf<T = User | Response>(): Promise<T>;
183
- getSelf(rawResponse?: true): Promise<Response>;
182
+ getSelf<T = User | Response>(rawResponse?: false): Promise<T>;
183
+ getSelf(rawResponse: true): Promise<Response>;
184
184
  /**
185
185
  * Initiate an email verification flow.
186
186
  *
@@ -557,12 +557,7 @@ declare class Server {
557
557
  tenantId?: string;
558
558
  userId?: string;
559
559
  }, ...remaining: unknown[]) => void;
560
- getContext(): {
561
- headers: Headers | undefined;
562
- userId: string | null | undefined;
563
- tenantId: string | null | undefined;
564
- preserveHeaders: boolean;
565
- };
560
+ getContext(): any;
566
561
  }
567
562
  declare function create(config?: NileConfig): Server;
568
563
 
@@ -578,12 +573,13 @@ type ExtensionResult = {
578
573
  onHandleRequest?: (...params: Any) => RouteReturn | Promise<RouteReturn>;
579
574
  onConfigure?: (...params: Any) => void;
580
575
  onSetContext?: (...params: Any) => void;
576
+ onGetContext?: () => Any;
581
577
  };
582
578
  type Extension = (instance: Server) => ExtensionResult;
583
579
  declare enum ExtensionState {
584
580
  onHandleRequest = "onHandleRequest",
585
581
  onRequest = "onRequest",
586
- onResponse = "onResposne"
582
+ onResponse = "onResponse"
587
583
  }
588
584
  type NilePoolConfig = PoolConfig & {
589
585
  afterCreate?: AfterCreate;
package/dist/index.d.ts CHANGED
@@ -179,8 +179,8 @@ declare class Users {
179
179
  *
180
180
  * @param [rawResponse] - When `true` return the raw {@link Response}.
181
181
  */
182
- getSelf<T = User | Response>(): Promise<T>;
183
- getSelf(rawResponse?: true): Promise<Response>;
182
+ getSelf<T = User | Response>(rawResponse?: false): Promise<T>;
183
+ getSelf(rawResponse: true): Promise<Response>;
184
184
  /**
185
185
  * Initiate an email verification flow.
186
186
  *
@@ -557,12 +557,7 @@ declare class Server {
557
557
  tenantId?: string;
558
558
  userId?: string;
559
559
  }, ...remaining: unknown[]) => void;
560
- getContext(): {
561
- headers: Headers | undefined;
562
- userId: string | null | undefined;
563
- tenantId: string | null | undefined;
564
- preserveHeaders: boolean;
565
- };
560
+ getContext(): any;
566
561
  }
567
562
  declare function create(config?: NileConfig): Server;
568
563
 
@@ -578,12 +573,13 @@ type ExtensionResult = {
578
573
  onHandleRequest?: (...params: Any) => RouteReturn | Promise<RouteReturn>;
579
574
  onConfigure?: (...params: Any) => void;
580
575
  onSetContext?: (...params: Any) => void;
576
+ onGetContext?: () => Any;
581
577
  };
582
578
  type Extension = (instance: Server) => ExtensionResult;
583
579
  declare enum ExtensionState {
584
580
  onHandleRequest = "onHandleRequest",
585
581
  onRequest = "onRequest",
586
- onResponse = "onResposne"
582
+ onResponse = "onResponse"
587
583
  }
588
584
  type NilePoolConfig = PoolConfig & {
589
585
  afterCreate?: AfterCreate;
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ var pg__default = /*#__PURE__*/_interopDefault(pg);
11
11
  var ExtensionState = /* @__PURE__ */ ((ExtensionState2) => {
12
12
  ExtensionState2["onHandleRequest"] = "onHandleRequest";
13
13
  ExtensionState2["onRequest"] = "onRequest";
14
- ExtensionState2["onResponse"] = "onResposne";
14
+ ExtensionState2["onResponse"] = "onResponse";
15
15
  return ExtensionState2;
16
16
  })(ExtensionState || {});
17
17
  var APIErrorErrorCodeEnum = {
@@ -221,7 +221,7 @@ async function request(url, _init, config) {
221
221
  text: await loggingRes?.text()
222
222
  });
223
223
  const updatedRes = await config.extensionCtx?.runExtensions(
224
- "onResposne" /* onResponse */,
224
+ "onResponse" /* onResponse */,
225
225
  config,
226
226
  params
227
227
  );
@@ -3084,7 +3084,7 @@ function bindRunExtensions(instance) {
3084
3084
  debug(`${ext.id ?? create2.name} ran onRequest`);
3085
3085
  continue;
3086
3086
  }
3087
- if (ext.onResponse && toRun === "onResposne" /* onResponse */) {
3087
+ if (ext.onResponse && toRun === "onResponse" /* onResponse */) {
3088
3088
  const result = await ext.onResponse(param);
3089
3089
  if (result != null) {
3090
3090
  return result;
@@ -3229,6 +3229,19 @@ var Server = class {
3229
3229
  * @returns undefined
3230
3230
  */
3231
3231
  setContext = (req, ...remaining) => {
3232
+ let ok = false;
3233
+ if (req && typeof req === "object" && "tenantId" in req) {
3234
+ ok = true;
3235
+ this.#config.tenantId = req.tenantId;
3236
+ }
3237
+ if (req && typeof req === "object" && "userId" in req) {
3238
+ ok = true;
3239
+ this.#config.userId = req.userId;
3240
+ }
3241
+ if (req && typeof req === "object" && "preserveHeaders" in req) {
3242
+ ok = true;
3243
+ this.#preserveHeaders = Boolean(req.preserveHeaders);
3244
+ }
3232
3245
  let atLeastOne = false;
3233
3246
  if (this.#config?.extensions) {
3234
3247
  for (const create2 of this.#config.extensions) {
@@ -3261,19 +3274,6 @@ var Server = class {
3261
3274
  }
3262
3275
  } catch {
3263
3276
  }
3264
- let ok = false;
3265
- if (req && typeof req === "object" && "tenantId" in req) {
3266
- ok = true;
3267
- this.#config.tenantId = req.tenantId;
3268
- }
3269
- if (req && typeof req === "object" && "userId" in req) {
3270
- ok = true;
3271
- this.#config.userId = req.userId;
3272
- }
3273
- if (req && typeof req === "object" && "preserveHeaders" in req) {
3274
- ok = true;
3275
- this.#preserveHeaders = Boolean(req.preserveHeaders);
3276
- }
3277
3277
  if (ok) {
3278
3278
  return;
3279
3279
  }
@@ -3296,6 +3296,17 @@ var Server = class {
3296
3296
  }
3297
3297
  };
3298
3298
  getContext() {
3299
+ if (this.#config?.extensions) {
3300
+ for (const create2 of this.#config.extensions) {
3301
+ if (typeof create2 !== "function") {
3302
+ continue;
3303
+ }
3304
+ const ext = create2(this);
3305
+ if (typeof ext.onGetContext === "function") {
3306
+ return ext.onGetContext();
3307
+ }
3308
+ }
3309
+ }
3299
3310
  return {
3300
3311
  headers: this.#headers,
3301
3312
  userId: this.#config?.userId,