elysia 1.4.28 → 1.4.30

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.mjs CHANGED
@@ -23,7 +23,6 @@ import {
23
23
  encodePath,
24
24
  lifeCycleToArray,
25
25
  supportPerMethodInlineHandler,
26
- redirect,
27
26
  emptySchema,
28
27
  insertStandaloneValidator
29
28
  } from "./utils.mjs";
@@ -53,7 +52,6 @@ import {
53
52
  createDynamicHandler
54
53
  } from "./dynamic-handle.mjs";
55
54
  import {
56
- status,
57
55
  ERROR_CODE,
58
56
  ValidationError
59
57
  } from "./error.mjs";
@@ -546,37 +544,12 @@ const _Elysia = class _Elysia {
546
544
  return;
547
545
  }
548
546
  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
547
  const fn = adapter.createNativeStaticHandler?.(
577
548
  handle,
578
549
  hooks,
579
- context.set
550
+ {
551
+ headers: Object.assign({}, this.setHeaders)
552
+ }
580
553
  );
581
554
  return fn instanceof Promise ? fn.then((fn2) => {
582
555
  if (fn2) return fn2;
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): {
package/dist/schema.js CHANGED
@@ -268,13 +268,20 @@ const isOptional = (schema) => schema ? schema?.[import_typebox.Kind] === "Impor
268
268
  if ("~standard" in schema || validators?.length && validators.some(
269
269
  (x) => x && typeof x != "string" && "~standard" in x
270
270
  )) {
271
- let Check2 = function(value, validated = !1) {
272
- let v = validated ? value : mainCheck.validate(value);
271
+ let Validate2 = function(value) {
272
+ const v = mainCheck.validate(value);
273
273
  if (v instanceof Promise)
274
- return v.then((v2) => Check2(v2, !0));
274
+ return v.then((resolved) => {
275
+ if (resolved.issues) return resolved;
276
+ const values2 = [];
277
+ return resolved && typeof resolved == "object" && values2.push(resolved.value), runCheckers2(value, 0, values2, resolved);
278
+ });
275
279
  if (v.issues) return v;
276
280
  const values = [];
277
281
  return v && typeof v == "object" && values.push(v.value), runCheckers2(value, 0, values, v);
282
+ }, Check2 = function(value) {
283
+ const v = Validate2(value);
284
+ return v instanceof Promise ? !1 : !v.issues;
278
285
  }, runCheckers2 = function(value, startIndex, values, lastV) {
279
286
  for (let i = startIndex; i < checkers.length; i++) {
280
287
  let v = checkers[i].validate(value);
@@ -298,7 +305,7 @@ const isOptional = (schema) => schema ? schema?.[import_typebox.Kind] === "Impor
298
305
  newValue = (0, import_utils.mergeDeep)(newValue, values[i]);
299
306
  return { value: newValue };
300
307
  };
301
- var Check = Check2, runCheckers = runCheckers2, mergeValues = mergeValues2;
308
+ var Validate = Validate2, Check = Check2, runCheckers = runCheckers2, mergeValues = mergeValues2;
302
309
  const typeboxSubValidator = (schema2) => {
303
310
  let mirror;
304
311
  if (normalize === !0 || normalize === "exactMirror")
@@ -353,13 +360,13 @@ const isOptional = (schema) => schema ? schema?.[import_typebox.Kind] === "Impor
353
360
  checkFunc: () => {
354
361
  },
355
362
  code: "",
356
- // @ts-ignore
357
363
  Check: Check2,
364
+ Validate: Validate2,
358
365
  // @ts-ignore
359
- Errors: (value) => Check2(value)?.then?.((x) => x?.issues),
366
+ Errors: (value) => Validate2(value)?.then?.((x) => x?.issues),
360
367
  Code: () => "",
361
368
  // @ts-ignore
362
- Decode: Check2,
369
+ Decode: Validate2,
363
370
  // @ts-ignore
364
371
  Encode: (value) => value,
365
372
  hasAdditionalProperties: !1,
package/dist/schema.mjs CHANGED
@@ -250,13 +250,20 @@ const isOptional = (schema) => schema ? schema?.[Kind] === "Import" && schema.Re
250
250
  if ("~standard" in schema || validators?.length && validators.some(
251
251
  (x) => x && typeof x != "string" && "~standard" in x
252
252
  )) {
253
- let Check2 = function(value, validated = !1) {
254
- let v = validated ? value : mainCheck.validate(value);
253
+ let Validate2 = function(value) {
254
+ const v = mainCheck.validate(value);
255
255
  if (v instanceof Promise)
256
- return v.then((v2) => Check2(v2, !0));
256
+ return v.then((resolved) => {
257
+ if (resolved.issues) return resolved;
258
+ const values2 = [];
259
+ return resolved && typeof resolved == "object" && values2.push(resolved.value), runCheckers2(value, 0, values2, resolved);
260
+ });
257
261
  if (v.issues) return v;
258
262
  const values = [];
259
263
  return v && typeof v == "object" && values.push(v.value), runCheckers2(value, 0, values, v);
264
+ }, Check2 = function(value) {
265
+ const v = Validate2(value);
266
+ return v instanceof Promise ? !1 : !v.issues;
260
267
  }, runCheckers2 = function(value, startIndex, values, lastV) {
261
268
  for (let i = startIndex; i < checkers.length; i++) {
262
269
  let v = checkers[i].validate(value);
@@ -280,7 +287,7 @@ const isOptional = (schema) => schema ? schema?.[Kind] === "Import" && schema.Re
280
287
  newValue = mergeDeep(newValue, values[i]);
281
288
  return { value: newValue };
282
289
  };
283
- var Check = Check2, runCheckers = runCheckers2, mergeValues = mergeValues2;
290
+ var Validate = Validate2, Check = Check2, runCheckers = runCheckers2, mergeValues = mergeValues2;
284
291
  const typeboxSubValidator = (schema2) => {
285
292
  let mirror;
286
293
  if (normalize === !0 || normalize === "exactMirror")
@@ -335,13 +342,13 @@ const isOptional = (schema) => schema ? schema?.[Kind] === "Import" && schema.Re
335
342
  checkFunc: () => {
336
343
  },
337
344
  code: "",
338
- // @ts-ignore
339
345
  Check: Check2,
346
+ Validate: Validate2,
340
347
  // @ts-ignore
341
- Errors: (value) => Check2(value)?.then?.((x) => x?.issues),
348
+ Errors: (value) => Validate2(value)?.then?.((x) => x?.issues),
342
349
  Code: () => "",
343
350
  // @ts-ignore
344
- Decode: Check2,
351
+ Decode: Validate2,
345
352
  // @ts-ignore
346
353
  Encode: (value) => value,
347
354
  hasAdditionalProperties: !1,
package/dist/ws/index.js CHANGED
@@ -22,6 +22,9 @@ __export(ws_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(ws_exports);
24
24
  var import_utils = require('../utils.js'), import_error = require('../error.js');
25
+ function isInvalidResponse(validator, data) {
26
+ return validator ? validator.provider === "standard" ? validator.schema["~standard"].validate(data).issues : validator.Check(data) === !1 : !1;
27
+ }
25
28
  const websocket = {
26
29
  open(ws) {
27
30
  ws.data.open?.(ws);
@@ -60,7 +63,7 @@ class ElysiaWS {
60
63
  * ws.send(new Uint8Array([1, 2, 3, 4]));
61
64
  */
62
65
  send(data, compress) {
63
- return Buffer.isBuffer(data) ? this.raw.send(data, compress) : this.validator?.Check(data) === !1 ? this.raw.send(
66
+ return Buffer.isBuffer(data) ? this.raw.send(data, compress) : isInvalidResponse(this.validator, data) ? this.raw.send(
64
67
  new import_error.ValidationError("message", this.validator, data).message
65
68
  ) : (typeof data == "object" && (data = JSON.stringify(data)), this.raw.send(data, compress));
66
69
  }
@@ -70,7 +73,7 @@ class ElysiaWS {
70
73
  * @param data The data to send
71
74
  */
72
75
  ping(data) {
73
- return Buffer.isBuffer(data) ? this.raw.ping(data) : this.validator?.Check(data) === !1 ? this.raw.send(
76
+ return Buffer.isBuffer(data) ? this.raw.ping(data) : isInvalidResponse(this.validator, data) ? this.raw.send(
74
77
  new import_error.ValidationError("message", this.validator, data).message
75
78
  ) : (typeof data == "object" && (data = JSON.stringify(data)), this.raw.ping(data));
76
79
  }
@@ -80,7 +83,7 @@ class ElysiaWS {
80
83
  * @param data The data to send
81
84
  */
82
85
  pong(data) {
83
- return Buffer.isBuffer(data) ? this.raw.pong(data) : this.validator?.Check(data) === !1 ? this.raw.send(
86
+ return Buffer.isBuffer(data) ? this.raw.pong(data) : isInvalidResponse(this.validator, data) ? this.raw.send(
84
87
  new import_error.ValidationError("message", this.validator, data).message
85
88
  ) : (typeof data == "object" && (data = JSON.stringify(data)), this.raw.pong(data));
86
89
  }
@@ -100,7 +103,7 @@ class ElysiaWS {
100
103
  topic,
101
104
  data,
102
105
  compress
103
- ) : this.validator?.Check(data) === !1 ? this.raw.send(
106
+ ) : isInvalidResponse(this.validator, data) ? this.raw.send(
104
107
  new import_error.ValidationError("message", this.validator, data).message
105
108
  ) : (typeof data == "object" && (data = JSON.stringify(data)), this.raw.publish(topic, data, compress));
106
109
  }
@@ -136,19 +139,15 @@ const createWSMessageParser = (parse) => {
136
139
  return data.then((data2) => handleWSResponse(ws, data2));
137
140
  if (Buffer.isBuffer(data)) return ws.send(data.toString());
138
141
  if (data === void 0) return;
139
- const validateResponse = responseValidator ? (
140
- // @ts-ignore
141
- responseValidator.provider === "standard" ? (data2) => (
142
- // @ts-ignore
143
- responseValidator.schema["~standard"].validate(data2).issues
144
- ) : (data2) => responseValidator.Check(data2) === !1
145
- ) : void 0, send = (datum) => {
146
- if (validateResponse && validateResponse(datum) === !1)
147
- return ws.send(
148
- new import_error.ValidationError("message", responseValidator, datum).message
149
- );
150
- if (typeof datum == "object") return ws.send(JSON.stringify(datum));
151
- ws.send(datum);
142
+ const send = (datum) => {
143
+ if (datum !== void 0) {
144
+ if (isInvalidResponse(responseValidator, datum))
145
+ return ws.send(
146
+ new import_error.ValidationError("message", responseValidator, datum).message
147
+ );
148
+ if (typeof datum == "object") return ws.send(JSON.stringify(datum));
149
+ ws.send(datum);
150
+ }
152
151
  };
153
152
  if (typeof data?.next != "function")
154
153
  return void send(data);
@@ -156,14 +155,6 @@ const createWSMessageParser = (parse) => {
156
155
  if (init instanceof Promise)
157
156
  return (async () => {
158
157
  const first = await init;
159
- if (validateResponse && validateResponse(first))
160
- return ws.send(
161
- new import_error.ValidationError(
162
- "message",
163
- responseValidator,
164
- first
165
- ).message
166
- );
167
158
  if (send(first.value), !first.done)
168
159
  for await (const datum of data) send(datum);
169
160
  })();
package/dist/ws/index.mjs CHANGED
@@ -1,5 +1,8 @@
1
1
  import { isNumericString } from "../utils.mjs";
2
2
  import { ValidationError } from "../error.mjs";
3
+ function isInvalidResponse(validator, data) {
4
+ return validator ? validator.provider === "standard" ? validator.schema["~standard"].validate(data).issues : validator.Check(data) === !1 : !1;
5
+ }
3
6
  const websocket = {
4
7
  open(ws) {
5
8
  ws.data.open?.(ws);
@@ -38,7 +41,7 @@ class ElysiaWS {
38
41
  * ws.send(new Uint8Array([1, 2, 3, 4]));
39
42
  */
40
43
  send(data, compress) {
41
- return Buffer.isBuffer(data) ? this.raw.send(data, compress) : this.validator?.Check(data) === !1 ? this.raw.send(
44
+ return Buffer.isBuffer(data) ? this.raw.send(data, compress) : isInvalidResponse(this.validator, data) ? this.raw.send(
42
45
  new ValidationError("message", this.validator, data).message
43
46
  ) : (typeof data == "object" && (data = JSON.stringify(data)), this.raw.send(data, compress));
44
47
  }
@@ -48,7 +51,7 @@ class ElysiaWS {
48
51
  * @param data The data to send
49
52
  */
50
53
  ping(data) {
51
- return Buffer.isBuffer(data) ? this.raw.ping(data) : this.validator?.Check(data) === !1 ? this.raw.send(
54
+ return Buffer.isBuffer(data) ? this.raw.ping(data) : isInvalidResponse(this.validator, data) ? this.raw.send(
52
55
  new ValidationError("message", this.validator, data).message
53
56
  ) : (typeof data == "object" && (data = JSON.stringify(data)), this.raw.ping(data));
54
57
  }
@@ -58,7 +61,7 @@ class ElysiaWS {
58
61
  * @param data The data to send
59
62
  */
60
63
  pong(data) {
61
- return Buffer.isBuffer(data) ? this.raw.pong(data) : this.validator?.Check(data) === !1 ? this.raw.send(
64
+ return Buffer.isBuffer(data) ? this.raw.pong(data) : isInvalidResponse(this.validator, data) ? this.raw.send(
62
65
  new ValidationError("message", this.validator, data).message
63
66
  ) : (typeof data == "object" && (data = JSON.stringify(data)), this.raw.pong(data));
64
67
  }
@@ -78,7 +81,7 @@ class ElysiaWS {
78
81
  topic,
79
82
  data,
80
83
  compress
81
- ) : this.validator?.Check(data) === !1 ? this.raw.send(
84
+ ) : isInvalidResponse(this.validator, data) ? this.raw.send(
82
85
  new ValidationError("message", this.validator, data).message
83
86
  ) : (typeof data == "object" && (data = JSON.stringify(data)), this.raw.publish(topic, data, compress));
84
87
  }
@@ -114,19 +117,15 @@ const createWSMessageParser = (parse) => {
114
117
  return data.then((data2) => handleWSResponse(ws, data2));
115
118
  if (Buffer.isBuffer(data)) return ws.send(data.toString());
116
119
  if (data === void 0) return;
117
- const validateResponse = responseValidator ? (
118
- // @ts-ignore
119
- responseValidator.provider === "standard" ? (data2) => (
120
- // @ts-ignore
121
- responseValidator.schema["~standard"].validate(data2).issues
122
- ) : (data2) => responseValidator.Check(data2) === !1
123
- ) : void 0, send = (datum) => {
124
- if (validateResponse && validateResponse(datum) === !1)
125
- return ws.send(
126
- new ValidationError("message", responseValidator, datum).message
127
- );
128
- if (typeof datum == "object") return ws.send(JSON.stringify(datum));
129
- ws.send(datum);
120
+ const send = (datum) => {
121
+ if (datum !== void 0) {
122
+ if (isInvalidResponse(responseValidator, datum))
123
+ return ws.send(
124
+ new ValidationError("message", responseValidator, datum).message
125
+ );
126
+ if (typeof datum == "object") return ws.send(JSON.stringify(datum));
127
+ ws.send(datum);
128
+ }
130
129
  };
131
130
  if (typeof data?.next != "function")
132
131
  return void send(data);
@@ -134,14 +133,6 @@ const createWSMessageParser = (parse) => {
134
133
  if (init instanceof Promise)
135
134
  return (async () => {
136
135
  const first = await init;
137
- if (validateResponse && validateResponse(first))
138
- return ws.send(
139
- new ValidationError(
140
- "message",
141
- responseValidator,
142
- first
143
- ).message
144
- );
145
136
  if (send(first.value), !first.done)
146
137
  for await (const datum of data) send(datum);
147
138
  })();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "elysia",
3
3
  "description": "Ergonomic Framework for Human",
4
- "version": "1.4.28",
4
+ "version": "1.4.30",
5
5
  "author": {
6
6
  "name": "saltyAom",
7
7
  "url": "https://github.com/SaltyAom",