elysia 1.4.29 → 1.4.31

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.js CHANGED
@@ -65,6 +65,9 @@ __export(index_exports, {
65
65
  });
66
66
  module.exports = __toCommonJS(index_exports);
67
67
  var import_memoirist = require("memoirist"), import_typebox = require("@sinclair/typebox"), import_fast_decode_uri_component = __toESM(require("fast-decode-uri-component")), import_type_system = require('./type-system/index.js'), import_sucrose = require('./sucrose.js'), import_bun = require('./adapter/bun/index.js'), import_web_standard = require('./adapter/web-standard/index.js'), import_env = require('./universal/env.js'), import_utils = require('./utils.js'), import_schema = require('./schema.js'), import_compose = require('./compose.js'), import_trace = require('./trace.js'), import_utils2 = require('./utils.js'), import_dynamic_handle = require('./dynamic-handle.js'), import_error = require('./error.js'), import_replace_schema = require('./replace-schema.js'), import_type_system2 = require('./type-system/index.js'), import_utils3 = require('./type-system/utils.js'), import_cookies = require('./cookies.js'), import_trace2 = require('./trace.js'), import_schema2 = require('./schema.js'), import_replace_schema2 = require('./replace-schema.js'), import_utils4 = require('./utils.js'), import_error2 = require('./error.js'), import_env2 = require('./universal/env.js'), import_file = require('./universal/file.js'), import_system = require("@sinclair/typebox/system"), _a;
68
+ const decodePathParameter = (value) => (0, import_fast_decode_uri_component.default)(
69
+ value.replace(/%2f/gi, (separator) => `%25${separator.slice(1)}`)
70
+ );
68
71
  _a = Symbol.dispose;
69
72
  const _Elysia = class _Elysia {
70
73
  constructor(config = {}) {
@@ -121,14 +124,14 @@ const _Elysia = class _Elysia {
121
124
  get http() {
122
125
  return this["~http"] || (this["~http"] = new import_memoirist.Memoirist({
123
126
  lazy: !0,
124
- onParam: import_fast_decode_uri_component.default
127
+ onParam: decodePathParameter
125
128
  })), this["~http"];
126
129
  },
127
130
  "~dynamic": void 0,
128
131
  // Use in non-AOT mode
129
132
  get dynamic() {
130
133
  return this["~dynamic"] || (this["~dynamic"] = new import_memoirist.Memoirist({
131
- onParam: import_fast_decode_uri_component.default
134
+ onParam: decodePathParameter
132
135
  })), this["~dynamic"];
133
136
  },
134
137
  // Static Router
@@ -208,7 +211,7 @@ const _Elysia = class _Elysia {
208
211
  },
209
212
  experimental: config?.experimental ?? {},
210
213
  seed: config?.seed === void 0 ? "" : config?.seed
211
- }, this["~adapter"] = config.adapter ?? (typeof Bun < "u" ? import_bun.BunAdapter : import_web_standard.WebStandardAdapter), config?.analytic && (config?.name || config?.seed !== void 0) && (this.telemetry = {
214
+ }, this["~cookieConfig"] = config.cookie, this["~adapter"] = config.adapter ?? (typeof Bun < "u" ? import_bun.BunAdapter : import_web_standard.WebStandardAdapter), config?.analytic && (config?.name || config?.seed !== void 0) && (this.telemetry = {
212
215
  stack: new Error().stack
213
216
  });
214
217
  }
@@ -283,7 +286,9 @@ const _Elysia = class _Elysia {
283
286
  }
284
287
  add(method, path, handle, localHook, options) {
285
288
  const skipPrefix = options?.skipPrefix ?? !1, allowMeta = options?.allowMeta ?? !1;
286
- localHook ??= {}, this.applyMacro(localHook);
289
+ localHook ??= {};
290
+ const cookieConfig = localHook.config?.cookie ? (0, import_utils.mergeCookie)(this.config.cookie ?? {}, localHook.config.cookie) : this.config.cookie;
291
+ this.applyMacro(localHook);
287
292
  let standaloneValidators = [];
288
293
  if (localHook.standaloneValidator && (standaloneValidators = standaloneValidators.concat(
289
294
  localHook.standaloneValidator
@@ -326,7 +331,7 @@ const _Elysia = class _Elysia {
326
331
  return (0, import_schema.getCookieValidator)({
327
332
  modules,
328
333
  validator: cloned.cookie,
329
- defaultConfig: this.config.cookie,
334
+ defaultConfig: cookieConfig,
330
335
  normalize,
331
336
  config: cloned.cookie?.config ?? {},
332
337
  dynamic,
@@ -548,37 +553,12 @@ const _Elysia = class _Elysia {
548
553
  return;
549
554
  }
550
555
  const adapter = this["~adapter"].handler, nativeStaticHandler = typeof handle != "function" ? () => {
551
- const context = {
552
- redirect: import_utils.redirect,
553
- request: this["~adapter"].isWebStandard ? new Request(`http://ely.sia${path}`, {
554
- method
555
- }) : void 0,
556
- server: null,
557
- set: {
558
- headers: Object.assign({}, this.setHeaders)
559
- },
560
- status: import_error.status,
561
- store: this.store
562
- };
563
- try {
564
- this.event.request?.map((x) => {
565
- if (typeof x.fn == "function")
566
- return x.fn(context);
567
- if (typeof x == "function") return x(context);
568
- });
569
- } catch (error) {
570
- let res;
571
- context.error = error, this.event.error?.some((x) => {
572
- if (typeof x.fn == "function")
573
- return res = x.fn(context);
574
- if (typeof x == "function")
575
- return res = x(context);
576
- }), res !== void 0 && (handle = res);
577
- }
578
556
  const fn = adapter.createNativeStaticHandler?.(
579
557
  handle,
580
558
  hooks,
581
- context.set
559
+ {
560
+ headers: Object.assign({}, this.setHeaders)
561
+ }
582
562
  );
583
563
  return fn instanceof Promise ? fn.then((fn2) => {
584
564
  if (fn2) return fn2;
@@ -1278,7 +1258,16 @@ const _Elysia = class _Elysia {
1278
1258
  method,
1279
1259
  path,
1280
1260
  handler,
1281
- hooks,
1261
+ plugin2["~cookieConfig"] ? {
1262
+ ...hooks,
1263
+ config: {
1264
+ ...hooks.config,
1265
+ cookie: (0, import_utils.mergeCookie)(
1266
+ plugin2["~cookieConfig"],
1267
+ hooks.config?.cookie ?? {}
1268
+ )
1269
+ }
1270
+ } : hooks,
1282
1271
  void 0
1283
1272
  );
1284
1273
  return plugin2 === this ? void 0 : (this.propagatePromiseModules(plugin2), plugin2);
@@ -1334,7 +1323,21 @@ const _Elysia = class _Elysia {
1334
1323
  for (const { method, path, handler, hooks } of Object.values(
1335
1324
  plugin.router.history
1336
1325
  ))
1337
- this.add(method, path, handler, hooks);
1326
+ this.add(
1327
+ method,
1328
+ path,
1329
+ handler,
1330
+ plugin["~cookieConfig"] ? {
1331
+ ...hooks,
1332
+ config: {
1333
+ ...hooks.config,
1334
+ cookie: (0, import_utils.mergeCookie)(
1335
+ plugin["~cookieConfig"],
1336
+ hooks.config?.cookie ?? {}
1337
+ )
1338
+ }
1339
+ } : hooks
1340
+ );
1338
1341
  if (name) {
1339
1342
  name in this.dependencies || (this.dependencies[name] = []);
1340
1343
  const current = seed !== void 0 ? (0, import_utils2.checksum)(name + JSON.stringify(seed)) : 0;
package/dist/index.mjs CHANGED
@@ -23,9 +23,9 @@ import {
23
23
  encodePath,
24
24
  lifeCycleToArray,
25
25
  supportPerMethodInlineHandler,
26
- redirect,
27
26
  emptySchema,
28
- insertStandaloneValidator
27
+ insertStandaloneValidator,
28
+ mergeCookie
29
29
  } from "./utils.mjs";
30
30
  import {
31
31
  getSchemaValidator,
@@ -53,7 +53,6 @@ import {
53
53
  createDynamicHandler
54
54
  } from "./dynamic-handle.mjs";
55
55
  import {
56
- status,
57
56
  ERROR_CODE,
58
57
  ValidationError
59
58
  } from "./error.mjs";
@@ -63,6 +62,9 @@ import {
63
62
  queryCoercions,
64
63
  stringToStructureCoercions
65
64
  } from "./replace-schema.mjs";
65
+ const decodePathParameter = (value) => fastDecodeURIComponent(
66
+ value.replace(/%2f/gi, (separator) => `%25${separator.slice(1)}`)
67
+ );
66
68
  _a = Symbol.dispose;
67
69
  const _Elysia = class _Elysia {
68
70
  constructor(config = {}) {
@@ -119,14 +121,14 @@ const _Elysia = class _Elysia {
119
121
  get http() {
120
122
  return this["~http"] || (this["~http"] = new Memoirist({
121
123
  lazy: !0,
122
- onParam: fastDecodeURIComponent
124
+ onParam: decodePathParameter
123
125
  })), this["~http"];
124
126
  },
125
127
  "~dynamic": void 0,
126
128
  // Use in non-AOT mode
127
129
  get dynamic() {
128
130
  return this["~dynamic"] || (this["~dynamic"] = new Memoirist({
129
- onParam: fastDecodeURIComponent
131
+ onParam: decodePathParameter
130
132
  })), this["~dynamic"];
131
133
  },
132
134
  // Static Router
@@ -206,7 +208,7 @@ const _Elysia = class _Elysia {
206
208
  },
207
209
  experimental: config?.experimental ?? {},
208
210
  seed: config?.seed === void 0 ? "" : config?.seed
209
- }, this["~adapter"] = config.adapter ?? (typeof Bun < "u" ? BunAdapter : WebStandardAdapter), config?.analytic && (config?.name || config?.seed !== void 0) && (this.telemetry = {
211
+ }, this["~cookieConfig"] = config.cookie, this["~adapter"] = config.adapter ?? (typeof Bun < "u" ? BunAdapter : WebStandardAdapter), config?.analytic && (config?.name || config?.seed !== void 0) && (this.telemetry = {
210
212
  stack: new Error().stack
211
213
  });
212
214
  }
@@ -281,7 +283,9 @@ const _Elysia = class _Elysia {
281
283
  }
282
284
  add(method, path, handle, localHook, options) {
283
285
  const skipPrefix = options?.skipPrefix ?? !1, allowMeta = options?.allowMeta ?? !1;
284
- localHook ??= {}, this.applyMacro(localHook);
286
+ localHook ??= {};
287
+ const cookieConfig = localHook.config?.cookie ? mergeCookie(this.config.cookie ?? {}, localHook.config.cookie) : this.config.cookie;
288
+ this.applyMacro(localHook);
285
289
  let standaloneValidators = [];
286
290
  if (localHook.standaloneValidator && (standaloneValidators = standaloneValidators.concat(
287
291
  localHook.standaloneValidator
@@ -324,7 +328,7 @@ const _Elysia = class _Elysia {
324
328
  return getCookieValidator({
325
329
  modules,
326
330
  validator: cloned.cookie,
327
- defaultConfig: this.config.cookie,
331
+ defaultConfig: cookieConfig,
328
332
  normalize,
329
333
  config: cloned.cookie?.config ?? {},
330
334
  dynamic,
@@ -546,37 +550,12 @@ const _Elysia = class _Elysia {
546
550
  return;
547
551
  }
548
552
  const adapter = this["~adapter"].handler, nativeStaticHandler = typeof handle != "function" ? () => {
549
- const context = {
550
- redirect,
551
- request: this["~adapter"].isWebStandard ? new Request(`http://ely.sia${path}`, {
552
- method
553
- }) : void 0,
554
- server: null,
555
- set: {
556
- headers: Object.assign({}, this.setHeaders)
557
- },
558
- status,
559
- store: this.store
560
- };
561
- try {
562
- this.event.request?.map((x) => {
563
- if (typeof x.fn == "function")
564
- return x.fn(context);
565
- if (typeof x == "function") return x(context);
566
- });
567
- } catch (error) {
568
- let res;
569
- context.error = error, this.event.error?.some((x) => {
570
- if (typeof x.fn == "function")
571
- return res = x.fn(context);
572
- if (typeof x == "function")
573
- return res = x(context);
574
- }), res !== void 0 && (handle = res);
575
- }
576
553
  const fn = adapter.createNativeStaticHandler?.(
577
554
  handle,
578
555
  hooks,
579
- context.set
556
+ {
557
+ headers: Object.assign({}, this.setHeaders)
558
+ }
580
559
  );
581
560
  return fn instanceof Promise ? fn.then((fn2) => {
582
561
  if (fn2) return fn2;
@@ -1276,7 +1255,16 @@ const _Elysia = class _Elysia {
1276
1255
  method,
1277
1256
  path,
1278
1257
  handler,
1279
- hooks,
1258
+ plugin2["~cookieConfig"] ? {
1259
+ ...hooks,
1260
+ config: {
1261
+ ...hooks.config,
1262
+ cookie: mergeCookie(
1263
+ plugin2["~cookieConfig"],
1264
+ hooks.config?.cookie ?? {}
1265
+ )
1266
+ }
1267
+ } : hooks,
1280
1268
  void 0
1281
1269
  );
1282
1270
  return plugin2 === this ? void 0 : (this.propagatePromiseModules(plugin2), plugin2);
@@ -1332,7 +1320,21 @@ const _Elysia = class _Elysia {
1332
1320
  for (const { method, path, handler, hooks } of Object.values(
1333
1321
  plugin.router.history
1334
1322
  ))
1335
- this.add(method, path, handler, hooks);
1323
+ this.add(
1324
+ method,
1325
+ path,
1326
+ handler,
1327
+ plugin["~cookieConfig"] ? {
1328
+ ...hooks,
1329
+ config: {
1330
+ ...hooks.config,
1331
+ cookie: mergeCookie(
1332
+ plugin["~cookieConfig"],
1333
+ hooks.config?.cookie ?? {}
1334
+ )
1335
+ }
1336
+ } : hooks
1337
+ );
1336
1338
  if (name) {
1337
1339
  name in this.dependencies || (this.dependencies[name] = []);
1338
1340
  const current = seed !== void 0 ? checksum(name + JSON.stringify(seed)) : 0;
@@ -59,9 +59,14 @@ const replaceSchemaTypeFromManyOptions = (schema, options) => {
59
59
  )), s.not && (withTransformedChildren.not = walk({ ...newWalkInput, s: s.not })), s.properties) {
60
60
  withTransformedChildren.properties = {};
61
61
  for (const [k, v] of Object.entries(s.properties))
62
- withTransformedChildren.properties[k] = walk({
63
- ...newWalkInput,
64
- s: v
62
+ Object.defineProperty(withTransformedChildren.properties, k, {
63
+ value: walk({
64
+ ...newWalkInput,
65
+ s: v
66
+ }),
67
+ enumerable: !0,
68
+ configurable: !0,
69
+ writable: !0
65
70
  });
66
71
  }
67
72
  if (s.items) {
@@ -35,9 +35,14 @@ const replaceSchemaTypeFromManyOptions = (schema, options) => {
35
35
  )), s.not && (withTransformedChildren.not = walk({ ...newWalkInput, s: s.not })), s.properties) {
36
36
  withTransformedChildren.properties = {};
37
37
  for (const [k, v] of Object.entries(s.properties))
38
- withTransformedChildren.properties[k] = walk({
39
- ...newWalkInput,
40
- s: v
38
+ Object.defineProperty(withTransformedChildren.properties, k, {
39
+ value: walk({
40
+ ...newWalkInput,
41
+ s: v
42
+ }),
43
+ enumerable: !0,
44
+ configurable: !0,
45
+ writable: !0
41
46
  });
42
47
  }
43
48
  if (s.items) {
package/dist/schema.d.ts CHANGED
@@ -3,7 +3,7 @@ import { type Instruction as ExactMirrorInstruction } from 'exact-mirror';
3
3
  import { type TypeCheck } from './type-system';
4
4
  import { mapValueError } from './error';
5
5
  import type { CookieOptions } from './cookies';
6
- import type { AnySchema, ElysiaConfig, InputSchema, IsAny, MaybeArray, StandaloneInputSchema, UnwrapSchema } from './types';
6
+ import type { AnySchema, ElysiaConfig, InputSchema, IsAny, MaybeArray, MaybePromise, StandaloneInputSchema, UnwrapSchema } from './types';
7
7
  import type { StandardSchemaV1Like } from './types';
8
8
  import { type ReplaceSchemaTypeOptions } from './replace-schema';
9
9
  type MapValueError = ReturnType<typeof mapValueError>;
@@ -11,11 +11,14 @@ export interface ElysiaTypeCheck<T extends AnySchema> extends Omit<TypeCheck<TSc
11
11
  provider: 'typebox' | 'standard';
12
12
  schema: T;
13
13
  config: Object;
14
- Check(value: unknown): T extends TSchema ? boolean : {
14
+ Check(value: unknown): boolean;
15
+ Validate?(value: unknown): MaybePromise<{
15
16
  value: UnwrapSchema<T>;
17
+ issues?: never;
16
18
  } | {
19
+ value?: never;
17
20
  issues: unknown[];
18
- };
21
+ }>;
19
22
  Clean?(v: unknown): UnwrapSchema<T>;
20
23
  parse(v: unknown): UnwrapSchema<T>;
21
24
  safeParse(v: unknown): {
@@ -64,6 +67,8 @@ export declare const getSchemaValidator: <T extends AnySchema | string | undefin
64
67
  }) => IsAny<T> extends true ? ElysiaTypeCheck<TAnySchema> : undefined extends T ? undefined : ElysiaTypeCheck<NonNullable<Exclude<T, string>>>;
65
68
  export declare const isUnion: (schema: TSchema) => boolean;
66
69
  export declare const getSchemaProperties: (schema: TAnySchema | undefined) => Record<string, TAnySchema> | undefined;
70
+ export declare const cloneSchemaInput: (value: any) => any;
71
+ export declare const applySchemaDefaults: (checker: TypeCheck<any> | ElysiaTypeCheck<any>, value: unknown, nullish?: boolean) => any;
67
72
  export declare const mergeObjectSchemas: (schemas: TSchema[]) => {
68
73
  schema: TObject | undefined;
69
74
  notObjects: TSchema[];