yedra 0.13.3 → 0.13.5

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 {
@@ -31,6 +32,14 @@ export declare class Yedra {
31
32
  description: string;
32
33
  version: string;
33
34
  };
35
+ security?: Record<string, {
36
+ type: 'http';
37
+ scheme: 'basic' | 'bearer';
38
+ } | {
39
+ type: 'apiKey';
40
+ in: 'header' | 'query' | 'cookie';
41
+ name: string;
42
+ }>;
34
43
  servers: {
35
44
  description: string;
36
45
  url: string;
@@ -2,6 +2,7 @@ import { readFile, readdir, stat } from 'node:fs/promises';
2
2
  import { createServer as createHttpServer } from 'node:http';
3
3
  import { createServer as createHttpsServer } from 'node:https';
4
4
  import { extname, join } from 'node:path';
5
+ import { URL } from 'node:url';
5
6
  import mime from 'mime';
6
7
  import { WebSocketServer } from 'ws';
7
8
  import { HttpError } from './errors.js';
@@ -126,6 +127,9 @@ export class Yedra {
126
127
  return {
127
128
  openapi: '3.0.2',
128
129
  info: options.info,
130
+ components: {
131
+ securitySchemes: options.security,
132
+ },
129
133
  servers: options.servers,
130
134
  paths,
131
135
  };
@@ -22,6 +22,7 @@ type EndpointOptions<Params extends Record<string, Schema<unknown>>, Query exten
22
22
  category: string;
23
23
  summary: string;
24
24
  description?: string;
25
+ security?: string[];
25
26
  params: Params;
26
27
  query: Query;
27
28
  headers: Headers;
@@ -106,6 +106,7 @@ class ConcreteRestEndpoint extends RestEndpoint {
106
106
  tags: [this.options.category],
107
107
  summary: this.options.summary,
108
108
  description: this.options.description,
109
+ security: Object.fromEntries((this.options.security ?? []).map((security) => [security, []])),
109
110
  parameters,
110
111
  requestBody: this.options.req instanceof NoneBody
111
112
  ? undefined
@@ -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.3",
3
+ "version": "0.13.5",
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
  }