yedra 0.13.2 → 0.13.4

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/lib.d.ts CHANGED
@@ -9,6 +9,7 @@ export { _enum as enum } from './validation/enum.js';
9
9
  export { ValidationError } from './validation/error.js';
10
10
  export { intersection } from './validation/intersection.js';
11
11
  export { number } from './validation/number.js';
12
+ export { integer } from './validation/integer.js';
12
13
  export { object } from './validation/object.js';
13
14
  export { record } from './validation/record.js';
14
15
  export { Schema } from './validation/schema.js';
package/dist/lib.js CHANGED
@@ -14,6 +14,7 @@ export { _enum as enum } from './validation/enum.js';
14
14
  export { ValidationError } from './validation/error.js';
15
15
  export { intersection } from './validation/intersection.js';
16
16
  export { number } from './validation/number.js';
17
+ export { integer } from './validation/integer.js';
17
18
  export { object } from './validation/object.js';
18
19
  export { record } from './validation/record.js';
19
20
  export { Schema } from './validation/schema.js';
@@ -1,4 +1,5 @@
1
1
  import { type Server } from 'node:http';
2
+ import { URL } from 'node:url';
2
3
  import { RestEndpoint } from './rest.js';
3
4
  import { WsEndpoint } from './websocket.js';
4
5
  declare class Context {
@@ -36,7 +37,12 @@ export declare class Yedra {
36
37
  url: string;
37
38
  }[];
38
39
  }): object;
39
- listen(port: number): Context;
40
+ listen(port: number, options?: {
41
+ tls?: {
42
+ key: string;
43
+ cert: string;
44
+ };
45
+ }): Context;
40
46
  private static errorResponse;
41
47
  private matchRestRoute;
42
48
  private matchWsRoute;
@@ -1,6 +1,8 @@
1
1
  import { readFile, readdir, stat } from 'node:fs/promises';
2
- import { createServer } from 'node:http';
2
+ import { createServer as createHttpServer } from 'node:http';
3
+ import { createServer as createHttpsServer } from 'node:https';
3
4
  import { extname, join } from 'node:path';
5
+ import { URL } from 'node:url';
4
6
  import mime from 'mime';
5
7
  import { WebSocketServer } from 'ws';
6
8
  import { HttpError } from './errors.js';
@@ -129,8 +131,13 @@ export class Yedra {
129
131
  paths,
130
132
  };
131
133
  }
132
- listen(port) {
133
- const server = createServer();
134
+ listen(port, options) {
135
+ const server = options?.tls === undefined
136
+ ? createHttpServer()
137
+ : createHttpsServer({
138
+ key: options.tls.key,
139
+ cert: options.tls.cert,
140
+ });
134
141
  server.on('request', (req, res) => {
135
142
  const url = new URL(req.url, 'http://localhost');
136
143
  const begin = Date.now();
@@ -1,3 +1,4 @@
1
+ import type { URL } from 'node:url';
1
2
  import type { WebSocket as NodeWebSocket } from 'ws';
2
3
  import type { Typeof } from '../validation/body.js';
3
4
  import { type ObjectSchema } from '../validation/object.js';
@@ -0,0 +1,23 @@
1
+ import { ModifiableSchema } from './modifiable.js';
2
+ declare class IntegerSchema extends ModifiableSchema<number> {
3
+ private readonly minValue?;
4
+ private readonly maxValue?;
5
+ constructor(min?: number, max?: number);
6
+ /**
7
+ * Set the minimum value the number is allowed to be.
8
+ * @param value - The minimum value.
9
+ */
10
+ min(value: number): IntegerSchema;
11
+ /**
12
+ * Set the maximum value the number is allowed to be.
13
+ * @param value - The maximum value.
14
+ */
15
+ max(value: number): IntegerSchema;
16
+ parse(obj: unknown): number;
17
+ documentation(): object;
18
+ }
19
+ /**
20
+ * A schema that matches an integer.
21
+ */
22
+ export declare const integer: () => IntegerSchema;
23
+ export {};
@@ -0,0 +1,64 @@
1
+ import { Issue, ValidationError } from './error.js';
2
+ import { ModifiableSchema } from './modifiable.js';
3
+ class IntegerSchema extends ModifiableSchema {
4
+ constructor(min, max) {
5
+ super();
6
+ this.minValue = min;
7
+ this.maxValue = max;
8
+ }
9
+ /**
10
+ * Set the minimum value the number is allowed to be.
11
+ * @param value - The minimum value.
12
+ */
13
+ min(value) {
14
+ if (!Number.isInteger(value)) {
15
+ throw new Error('minimum value has to be an integer');
16
+ }
17
+ return new IntegerSchema(value, this.maxValue);
18
+ }
19
+ /**
20
+ * Set the maximum value the number is allowed to be.
21
+ * @param value - The maximum value.
22
+ */
23
+ max(value) {
24
+ if (!Number.isInteger(value)) {
25
+ throw new Error('maximum value has to be an integer');
26
+ }
27
+ return new IntegerSchema(this.minValue, value);
28
+ }
29
+ parse(obj) {
30
+ if (typeof obj !== 'number' && typeof obj !== 'string') {
31
+ throw new ValidationError([
32
+ new Issue('invalidType', [], 'number', typeof obj),
33
+ ]);
34
+ }
35
+ const num = typeof obj === 'number' ? obj : Number.parseFloat(obj);
36
+ if (Number.isNaN(num) || !Number.isInteger(num)) {
37
+ throw new ValidationError([
38
+ new Issue('invalidType', [], 'integer', typeof obj),
39
+ ]);
40
+ }
41
+ if (this.minValue !== undefined && num < this.minValue) {
42
+ throw new ValidationError([
43
+ new Issue('tooSmall', [], this.minValue.toString(), num.toString()),
44
+ ]);
45
+ }
46
+ if (this.maxValue !== undefined && num > this.maxValue) {
47
+ throw new ValidationError([
48
+ new Issue('tooBig', [], this.maxValue.toString(), num.toString()),
49
+ ]);
50
+ }
51
+ return num;
52
+ }
53
+ documentation() {
54
+ return {
55
+ type: 'integer',
56
+ minimum: this.minValue,
57
+ maximum: this.maxValue,
58
+ };
59
+ }
60
+ }
61
+ /**
62
+ * A schema that matches an integer.
63
+ */
64
+ export const integer = () => new IntegerSchema();
@@ -2,7 +2,7 @@ import { BodyType } from './body.js';
2
2
  declare class RawBody extends BodyType<Uint8Array> {
3
3
  private contentType;
4
4
  constructor(contentType: string | undefined);
5
- deserialize(buffer: Uint8Array, _contentType: string): Uint8Array;
5
+ deserialize(buffer: Uint8Array, _contentType: string): Uint8Array<ArrayBufferLike>;
6
6
  bodyDocs(): object;
7
7
  }
8
8
  /**
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "yedra",
3
- "version": "0.13.2",
3
+ "version": "0.13.4",
4
4
  "repository": "github:0codekit/yedra",
5
5
  "main": "dist/index.js",
6
6
  "devDependencies": {
7
7
  "@biomejs/biome": "^1.9.4",
8
- "@types/node": "^22.8.2",
8
+ "@types/bun": "^1.1.14",
9
+ "@types/node": "^22.10.2",
9
10
  "@types/uuid": "^10.0.0",
10
- "@types/ws": "^8.5.12",
11
- "typescript": "^5.6.3"
11
+ "@types/ws": "^8.5.13",
12
+ "typescript": "^5.7.2"
12
13
  },
13
14
  "bugs": "https://github.com/0codekit/yedra/issues",
14
15
  "contributors": ["Justus Zorn <jzorn@wemakefuture.com>"],
@@ -23,7 +24,7 @@
23
24
  "type": "module",
24
25
  "dependencies": {
25
26
  "mime": "^4.0.4",
26
- "uuid": "^11.0.2",
27
+ "uuid": "^11.0.3",
27
28
  "ws": "^8.18.0"
28
29
  }
29
30
  }