@niledatabase/server 5.0.0-alpha.30 → 5.0.0-alpha.32
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 +16 -10
- package/dist/index.d.ts +16 -10
- package/dist/index.js +39 -38
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +39 -38
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -8,6 +8,8 @@ var ExtensionState = /* @__PURE__ */ ((ExtensionState2) => {
|
|
|
8
8
|
ExtensionState2["onRequest"] = "onRequest";
|
|
9
9
|
ExtensionState2["onResponse"] = "onResponse";
|
|
10
10
|
ExtensionState2["withContext"] = "withContext";
|
|
11
|
+
ExtensionState2["withTenantId"] = "withTenantId";
|
|
12
|
+
ExtensionState2["withUserId"] = "withUserId";
|
|
11
13
|
return ExtensionState2;
|
|
12
14
|
})(ExtensionState || {});
|
|
13
15
|
var APIErrorErrorCodeEnum = {
|
|
@@ -220,6 +222,14 @@ function bindRunExtensions(instance) {
|
|
|
220
222
|
if (extensionConfig.disableExtensions?.includes(ext.id)) {
|
|
221
223
|
continue;
|
|
222
224
|
}
|
|
225
|
+
if (ext.withTenantId && toRun === "withTenantId" /* withTenantId */) {
|
|
226
|
+
ctx.set({
|
|
227
|
+
tenantId: await ext.withTenantId()
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
if (ext.withUserId && toRun === "withUserId" /* withUserId */) {
|
|
231
|
+
ctx.set({ userId: await ext.withUserId() });
|
|
232
|
+
}
|
|
223
233
|
if (ext.withContext && toRun === "withContext" /* withContext */) {
|
|
224
234
|
await ext.withContext(ctx);
|
|
225
235
|
}
|
|
@@ -235,10 +245,6 @@ function bindRunExtensions(instance) {
|
|
|
235
245
|
const [param] = Array.isArray(params) ? params : [params];
|
|
236
246
|
if (ext.onRequest && toRun === "onRequest" /* onRequest */) {
|
|
237
247
|
const { ...previousContext } = ctx.get();
|
|
238
|
-
const preserveHeaders = previousContext.preserveHeaders;
|
|
239
|
-
if (preserveHeaders) {
|
|
240
|
-
ctx.set({ preserveHeaders: false });
|
|
241
|
-
}
|
|
242
248
|
if (!_init) {
|
|
243
249
|
continue;
|
|
244
250
|
}
|
|
@@ -249,7 +255,7 @@ function bindRunExtensions(instance) {
|
|
|
249
255
|
const cookie = updatedContext.headers.get("cookie");
|
|
250
256
|
if (cookie && param.headers) {
|
|
251
257
|
const updatedCookies = mergeCookies(
|
|
252
|
-
|
|
258
|
+
previousHeaders?.get("cookie"),
|
|
253
259
|
updatedContext.headers.get("cookie")
|
|
254
260
|
);
|
|
255
261
|
param.headers.set("cookie", updatedCookies);
|
|
@@ -294,6 +300,11 @@ function mergeCookies(...cookieStrings) {
|
|
|
294
300
|
}
|
|
295
301
|
async function runExtensionContext(config) {
|
|
296
302
|
await config?.extensionCtx?.runExtensions("withContext" /* withContext */, config);
|
|
303
|
+
await config?.extensionCtx?.runExtensions(
|
|
304
|
+
"withTenantId" /* withTenantId */,
|
|
305
|
+
config
|
|
306
|
+
);
|
|
307
|
+
await config?.extensionCtx?.runExtensions("withUserId" /* withUserId */, config);
|
|
297
308
|
}
|
|
298
309
|
|
|
299
310
|
// src/api/utils/request-context.ts
|
|
@@ -302,8 +313,7 @@ var storage = new AsyncLocalStorage();
|
|
|
302
313
|
var defaultContext = {
|
|
303
314
|
headers: new Headers(),
|
|
304
315
|
tenantId: void 0,
|
|
305
|
-
userId: void 0
|
|
306
|
-
preserveHeaders: false
|
|
316
|
+
userId: void 0
|
|
307
317
|
};
|
|
308
318
|
var lastUsedContext = defaultContext;
|
|
309
319
|
var ctx = {
|
|
@@ -353,8 +363,6 @@ var ctx = {
|
|
|
353
363
|
}
|
|
354
364
|
if ("tenantId" in partial) store.tenantId = partial.tenantId;
|
|
355
365
|
if ("userId" in partial) store.userId = partial.userId;
|
|
356
|
-
if ("preserveHeaders" in partial)
|
|
357
|
-
store.preserveHeaders = Boolean(partial.preserveHeaders);
|
|
358
366
|
silly(`[SET] ${serializeContext(store)}`);
|
|
359
367
|
lastUsedContext = { ...store };
|
|
360
368
|
},
|
|
@@ -372,8 +380,7 @@ function withNileContext(config, fn, name = "unknown") {
|
|
|
372
380
|
const context2 = {
|
|
373
381
|
headers: mergedHeaders,
|
|
374
382
|
tenantId: existing.tenantId,
|
|
375
|
-
userId: existing.userId
|
|
376
|
-
preserveHeaders: existing.preserveHeaders ?? false
|
|
383
|
+
userId: existing.userId
|
|
377
384
|
};
|
|
378
385
|
silly(`${name} [INITIAL - Request] ${serializeContext(context2)}`);
|
|
379
386
|
return ctx.run(context2, fn);
|
|
@@ -386,12 +393,10 @@ function withNileContext(config, fn, name = "unknown") {
|
|
|
386
393
|
}
|
|
387
394
|
const hasTenantId = "tenantId" in initialContext;
|
|
388
395
|
const hasUserId = "userId" in initialContext;
|
|
389
|
-
const hasPreserveHeaders = "preserveHeaders" in initialContext;
|
|
390
396
|
const context = {
|
|
391
397
|
headers: mergedHeaders,
|
|
392
398
|
tenantId: hasTenantId ? initialContext.tenantId : existing.tenantId,
|
|
393
|
-
userId: hasUserId ? initialContext.userId : existing.userId
|
|
394
|
-
preserveHeaders: hasPreserveHeaders ? Boolean(initialContext.preserveHeaders) : existing.preserveHeaders ?? false
|
|
399
|
+
userId: hasUserId ? initialContext.userId : existing.userId
|
|
395
400
|
};
|
|
396
401
|
silly(`${name} [INITIAL - Partial<Context>] ${serializeContext(context)}`);
|
|
397
402
|
return ctx.run(context, async () => {
|
|
@@ -408,8 +413,7 @@ function serializeContext(context) {
|
|
|
408
413
|
return JSON.stringify({
|
|
409
414
|
headers,
|
|
410
415
|
tenantId: context.tenantId,
|
|
411
|
-
userId: context.userId
|
|
412
|
-
preserveHeaders: context.preserveHeaders
|
|
416
|
+
userId: context.userId
|
|
413
417
|
});
|
|
414
418
|
}
|
|
415
419
|
function parseCookieHeader(header) {
|
|
@@ -2014,8 +2018,7 @@ var Config = class {
|
|
|
2014
2018
|
this.context = {
|
|
2015
2019
|
tenantId: config?.tenantId,
|
|
2016
2020
|
userId: config?.userId,
|
|
2017
|
-
headers: config?.headers ? new Headers(config.headers) : new Headers()
|
|
2018
|
-
preserveHeaders: false
|
|
2021
|
+
headers: config?.headers ? new Headers(config.headers) : new Headers()
|
|
2019
2022
|
};
|
|
2020
2023
|
this.routes = {
|
|
2021
2024
|
...appRoutes(config?.routePrefix),
|
|
@@ -2727,7 +2730,7 @@ var Auth = class {
|
|
|
2727
2730
|
].filter(Boolean).join("; ");
|
|
2728
2731
|
const uHeaders = new Headers({ cookie });
|
|
2729
2732
|
updateHeaders(uHeaders);
|
|
2730
|
-
ctx.set({ headers: uHeaders
|
|
2733
|
+
ctx.set({ headers: uHeaders });
|
|
2731
2734
|
} else {
|
|
2732
2735
|
error("Unable to set context after sign in", {
|
|
2733
2736
|
headers: signInRes.headers
|
|
@@ -2849,7 +2852,7 @@ async function obtainCsrf(config, rawResponse = false) {
|
|
|
2849
2852
|
parseToken(res.headers)
|
|
2850
2853
|
].filter(Boolean).join("; ");
|
|
2851
2854
|
headers.set("cookie", cookie);
|
|
2852
|
-
ctx.set({ headers
|
|
2855
|
+
ctx.set({ headers });
|
|
2853
2856
|
updateHeaders(headers);
|
|
2854
2857
|
}
|
|
2855
2858
|
if (!rawResponse) {
|
|
@@ -2868,7 +2871,7 @@ async function obtainCsrf(config, rawResponse = false) {
|
|
|
2868
2871
|
}
|
|
2869
2872
|
const cookie = cookieParts.filter(Boolean).join("; ");
|
|
2870
2873
|
headers.set("cookie", cookie);
|
|
2871
|
-
ctx.set({ headers
|
|
2874
|
+
ctx.set({ headers });
|
|
2872
2875
|
updateHeaders(new Headers({ cookie }));
|
|
2873
2876
|
}
|
|
2874
2877
|
if (rawResponse) {
|
|
@@ -3158,12 +3161,12 @@ var Tenants = class {
|
|
|
3158
3161
|
try {
|
|
3159
3162
|
const json = await me.json();
|
|
3160
3163
|
if ("id" in json) {
|
|
3161
|
-
ctx.set({ userId: json.id
|
|
3164
|
+
ctx.set({ userId: json.id });
|
|
3162
3165
|
}
|
|
3163
3166
|
} catch {
|
|
3164
3167
|
}
|
|
3165
3168
|
if (typeof req === "string") {
|
|
3166
|
-
ctx.set({ tenantId: req
|
|
3169
|
+
ctx.set({ tenantId: req });
|
|
3167
3170
|
} else {
|
|
3168
3171
|
this.#handleContext(req);
|
|
3169
3172
|
}
|
|
@@ -3173,7 +3176,7 @@ var Tenants = class {
|
|
|
3173
3176
|
async addMember(req, rawResponse) {
|
|
3174
3177
|
return withNileContext(this.#config, async () => {
|
|
3175
3178
|
if (typeof req === "string") {
|
|
3176
|
-
ctx.set({ userId: req
|
|
3179
|
+
ctx.set({ userId: req });
|
|
3177
3180
|
} else {
|
|
3178
3181
|
this.#handleContext(req);
|
|
3179
3182
|
}
|
|
@@ -3191,7 +3194,7 @@ var Tenants = class {
|
|
|
3191
3194
|
return withNileContext(this.#config, async () => {
|
|
3192
3195
|
this.#handleContext(req);
|
|
3193
3196
|
if (typeof req === "string") {
|
|
3194
|
-
ctx.set({ userId: req
|
|
3197
|
+
ctx.set({ userId: req });
|
|
3195
3198
|
}
|
|
3196
3199
|
const res = await fetchTenantUser(this.#config, "DELETE");
|
|
3197
3200
|
return responseHandler(res, rawResponse);
|
|
@@ -3314,10 +3317,10 @@ var Tenants = class {
|
|
|
3314
3317
|
#handleContext(req) {
|
|
3315
3318
|
if (typeof req === "object") {
|
|
3316
3319
|
if ("tenantId" in req) {
|
|
3317
|
-
ctx.set({ tenantId: req.tenantId
|
|
3320
|
+
ctx.set({ tenantId: req.tenantId });
|
|
3318
3321
|
}
|
|
3319
3322
|
if ("userId" in req) {
|
|
3320
|
-
ctx.set({ userId: req.userId
|
|
3323
|
+
ctx.set({ userId: req.userId });
|
|
3321
3324
|
}
|
|
3322
3325
|
}
|
|
3323
3326
|
}
|
|
@@ -3404,7 +3407,7 @@ function updateConfig(response, config) {
|
|
|
3404
3407
|
...config,
|
|
3405
3408
|
origin,
|
|
3406
3409
|
headers: headers ?? void 0,
|
|
3407
|
-
|
|
3410
|
+
useLastContext: true
|
|
3408
3411
|
};
|
|
3409
3412
|
}
|
|
3410
3413
|
|
|
@@ -3436,7 +3439,6 @@ var Server = class {
|
|
|
3436
3439
|
watchHeaders((headers) => {
|
|
3437
3440
|
if (headers) {
|
|
3438
3441
|
this.#config.context.headers = new Headers(headers);
|
|
3439
|
-
this.#config.context.preserveHeaders = true;
|
|
3440
3442
|
this.#reset();
|
|
3441
3443
|
}
|
|
3442
3444
|
});
|
|
@@ -3444,7 +3446,6 @@ var Server = class {
|
|
|
3444
3446
|
...this.#config.handlers,
|
|
3445
3447
|
withContext: handlersWithContext(this.#config)
|
|
3446
3448
|
};
|
|
3447
|
-
this.#config.context.preserveHeaders = config?.preserveHeaders ?? false;
|
|
3448
3449
|
this.#config.context.tenantId = getTenantId({ config: this.#config });
|
|
3449
3450
|
this.#manager = new DBManager(this.#config);
|
|
3450
3451
|
this.#handleHeaders(config);
|
|
@@ -3510,18 +3511,18 @@ var Server = class {
|
|
|
3510
3511
|
set paths(paths) {
|
|
3511
3512
|
this.#config.paths = paths;
|
|
3512
3513
|
}
|
|
3513
|
-
async withContext(
|
|
3514
|
-
const
|
|
3515
|
-
|
|
3516
|
-
const
|
|
3514
|
+
async withContext(contextOrFn, maybeFn) {
|
|
3515
|
+
const isFn = typeof contextOrFn === "function";
|
|
3516
|
+
const context = isFn ? {} : contextOrFn ?? {};
|
|
3517
|
+
const fn = isFn ? contextOrFn : maybeFn;
|
|
3518
|
+
const preserve = "useLastContext" in context ? context.useLastContext : true;
|
|
3517
3519
|
if (preserve) {
|
|
3518
3520
|
this.#config.context = { ...this.getContext(), ...context };
|
|
3521
|
+
} else {
|
|
3522
|
+
this.#config.context = { ...defaultContext, ...context };
|
|
3519
3523
|
}
|
|
3520
3524
|
return withNileContext(this.#config, async () => {
|
|
3521
|
-
|
|
3522
|
-
return fn(this);
|
|
3523
|
-
}
|
|
3524
|
-
return this;
|
|
3525
|
+
return fn ? fn(this) : this;
|
|
3525
3526
|
});
|
|
3526
3527
|
}
|
|
3527
3528
|
async noContext(fn) {
|