yedra 0.17.6 → 0.17.7

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.
@@ -21,9 +21,9 @@ export class Path {
21
21
  .filter((segment) => segment !== '');
22
22
  const invalidSegment = this.expected.find((part) => part.match(/^((:?[A-Za-z0-9\-.]+\??)|\*)$/) === null);
23
23
  if (invalidSegment) {
24
- throw new Error(`API path ${path} is invalid: Segment ${invalidSegment} does not match regex /^((:?[A-Za-z0-9-\.]+\\??)|\\*)$/.`);
24
+ throw new Error(`API path ${path} is invalid: Segment ${invalidSegment} does not match regex /^((:?[A-Za-z0-9-.]+\\??)|\\*)$/.`);
25
25
  }
26
- const wildcard = this.expected.findIndex((part) => part === '*');
26
+ const wildcard = this.expected.indexOf('*');
27
27
  if (wildcard !== -1 && wildcard !== this.expected.length - 1) {
28
28
  throw new Error(`API path ${path} is invalid: * must be the last path segment.`);
29
29
  }
@@ -1,6 +1,6 @@
1
1
  import type { Readable } from 'node:stream';
2
2
  import type { SecurityScheme } from '../util/security.js';
3
- import type { BodyType, Typeof } from '../validation/body.js';
3
+ import type { BodyType, Typeof, TypeofAccepts } from '../validation/body.js';
4
4
  import { NoneBody } from '../validation/none.js';
5
5
  import { type ObjectSchema } from '../validation/object.js';
6
6
  import type { Schema } from '../validation/schema.js';
@@ -20,7 +20,7 @@ type ResObject<Body> = Promise<{
20
20
  body: Body;
21
21
  headers?: Record<string, string>;
22
22
  };
23
- type EndpointOptions<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Req extends BodyType<unknown>, Res extends BodyType<unknown>> = {
23
+ type EndpointOptions<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Req extends BodyType<unknown, unknown>, Res extends BodyType<unknown, unknown>> = {
24
24
  category: string;
25
25
  summary: string;
26
26
  description?: string;
@@ -38,7 +38,7 @@ type EndpointOptions<Params extends Record<string, Schema<unknown>>, Query exten
38
38
  headers: Headers;
39
39
  req: Req;
40
40
  res: Res;
41
- do: (req: ReqObject<Typeof<ObjectSchema<Params>>, Typeof<ObjectSchema<Query>>, Typeof<ObjectSchema<Headers>>, Typeof<Req>>) => ResObject<Typeof<Res>>;
41
+ do: (req: ReqObject<Typeof<ObjectSchema<Params>>, Typeof<ObjectSchema<Query>>, Typeof<ObjectSchema<Headers>>, Typeof<Req>>) => ResObject<TypeofAccepts<Res>>;
42
42
  };
43
43
  export declare abstract class RestEndpoint {
44
44
  abstract get method(): 'GET' | 'POST' | 'PUT' | 'DELETE';
@@ -61,7 +61,7 @@ export declare abstract class RestEndpoint {
61
61
  * `RestEndpoint`, is not abstract because there are multiple implementations,
62
62
  * but so that we can hide all the generic parameters of this class.
63
63
  */
64
- declare class ConcreteRestEndpoint<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Req extends BodyType<unknown>, Res extends BodyType<unknown>> extends RestEndpoint {
64
+ declare class ConcreteRestEndpoint<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Req extends BodyType<unknown, unknown>, Res extends BodyType<unknown, unknown>> extends RestEndpoint {
65
65
  private _method;
66
66
  private options;
67
67
  private paramsSchema;
@@ -83,16 +83,16 @@ declare class ConcreteRestEndpoint<Params extends Record<string, Schema<unknown>
83
83
  isHidden(): boolean;
84
84
  documentation(path: string, securitySchemes: Set<SecurityScheme>): object;
85
85
  }
86
- export declare class Get<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Res extends BodyType<unknown>> extends ConcreteRestEndpoint<Params, Query, Headers, NoneBody, Res> {
86
+ export declare class Get<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Res extends BodyType<unknown, unknown>> extends ConcreteRestEndpoint<Params, Query, Headers, NoneBody, Res> {
87
87
  constructor(options: Omit<EndpointOptions<Params, Query, Headers, NoneBody, Res>, 'req'>);
88
88
  }
89
- export declare class Post<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Req extends BodyType<unknown>, Res extends BodyType<unknown>> extends ConcreteRestEndpoint<Params, Query, Headers, Req, Res> {
89
+ export declare class Post<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Req extends BodyType<unknown, unknown>, Res extends BodyType<unknown, unknown>> extends ConcreteRestEndpoint<Params, Query, Headers, Req, Res> {
90
90
  constructor(options: EndpointOptions<Params, Query, Headers, Req, Res>);
91
91
  }
92
- export declare class Put<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Req extends BodyType<unknown>, Res extends BodyType<unknown>> extends ConcreteRestEndpoint<Params, Query, Headers, Req, Res> {
92
+ export declare class Put<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Req extends BodyType<unknown, unknown>, Res extends BodyType<unknown, unknown>> extends ConcreteRestEndpoint<Params, Query, Headers, Req, Res> {
93
93
  constructor(options: EndpointOptions<Params, Query, Headers, Req, Res>);
94
94
  }
95
- export declare class Delete<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Res extends BodyType<unknown>> extends ConcreteRestEndpoint<Params, Query, Headers, NoneBody, Res> {
95
+ export declare class Delete<Params extends Record<string, Schema<unknown>>, Query extends Record<string, Schema<unknown>>, Headers extends Record<string, Schema<unknown>>, Res extends BodyType<unknown, unknown>> extends ConcreteRestEndpoint<Params, Query, Headers, NoneBody, Res> {
96
96
  constructor(options: Omit<EndpointOptions<Params, Query, Headers, NoneBody, Res>, 'req'>);
97
97
  }
98
98
  export {};
@@ -1,27 +1,46 @@
1
1
  import type { Readable } from 'node:stream';
2
2
  /**
3
3
  * The base class for all body types.
4
+ * @typeParam Provides - The type that is provided by this body type.
5
+ * When a parse is successful, this is the type of object you get back.
6
+ * @typeParam Accepts - The type that is accepted by this body type.
7
+ * When you want a parse to be successful, this is the type of object
8
+ * you have to pass in. This is always weaker than `Provides`, since you
9
+ * can always pass in a value of `Provides` and get a success.
4
10
  */
5
- export declare abstract class BodyType<T> {
6
- _typeof: T;
11
+ export declare abstract class BodyType<Provides, Accepts> {
12
+ _provides: Provides;
13
+ _accepts: Accepts;
7
14
  constructor();
8
15
  /**
9
16
  * Deserialize a raw stream.
10
17
  * @param stream - The raw stream.
11
18
  * @param contentType - The content type.
12
19
  */
13
- abstract deserialize(stream: Readable, contentType: string): Promise<T>;
20
+ abstract deserialize(stream: Readable, contentType: string): Promise<Provides>;
14
21
  /**
15
22
  * Generate OpenAPI docs for this body.
16
23
  */
17
24
  abstract bodyDocs(): object;
18
25
  }
19
26
  /**
20
- * Get the type that is parsed by a schema.
27
+ * Get the type that is provided by a schema. When you parse
28
+ * something with a schema, this is the type you get back.
29
+ */
30
+ export type TypeofProvides<T extends BodyType<unknown, unknown>> = T['_provides'];
31
+ /**
32
+ * Get the type that is accepted by a schema. When you parse
33
+ * something with a schema, this is the type you have to pass
34
+ * in to get a successful result.
35
+ */
36
+ export type TypeofAccepts<T extends BodyType<unknown, unknown>> = T['_accepts'];
37
+ /**
38
+ * Get the type that is parsed by a schema. This is the same
39
+ * as `TypeofProvides`.
21
40
  *
22
41
  * ```typescript
23
42
  * const schema = y.string();
24
43
  * type SchemaType = y.Typeof<typeof schema>; // string
25
44
  * ```
26
45
  */
27
- export type Typeof<T extends BodyType<unknown>> = T['_typeof'];
46
+ export type Typeof<T extends BodyType<unknown, unknown>> = TypeofProvides<T>;
@@ -1,8 +1,15 @@
1
1
  /**
2
2
  * The base class for all body types.
3
+ * @typeParam Provides - The type that is provided by this body type.
4
+ * When a parse is successful, this is the type of object you get back.
5
+ * @typeParam Accepts - The type that is accepted by this body type.
6
+ * When you want a parse to be successful, this is the type of object
7
+ * you have to pass in. This is always weaker than `Provides`, since you
8
+ * can always pass in a value of `Provides` and get a success.
3
9
  */
4
10
  export class BodyType {
5
11
  constructor() {
6
- this._typeof = undefined;
12
+ this._provides = undefined;
13
+ this._accepts = undefined;
7
14
  }
8
15
  }
@@ -1,6 +1,6 @@
1
1
  import type { Readable } from 'node:stream';
2
- import { BodyType, type Typeof } from './body.js';
3
- declare class EitherBody<T extends [...BodyType<unknown>[]]> extends BodyType<Typeof<T[number]>> {
2
+ import { BodyType, type Typeof, type TypeofAccepts, type TypeofProvides } from './body.js';
3
+ declare class EitherBody<T extends [...BodyType<unknown, unknown>[]]> extends BodyType<TypeofProvides<T[number]>, TypeofAccepts<T[number]>> {
4
4
  private options;
5
5
  constructor(options: T);
6
6
  deserialize(stream: Readable, contentType: string): Promise<Typeof<T[number]>>;
@@ -11,5 +11,5 @@ declare class EitherBody<T extends [...BodyType<unknown>[]]> extends BodyType<Ty
11
11
  * @param options
12
12
  * @returns
13
13
  */
14
- export declare const either: <T extends [...BodyType<unknown>[]]>(...options: T) => EitherBody<T>;
14
+ export declare const either: <T extends [...BodyType<unknown, unknown>[]]>(...options: T) => EitherBody<T>;
15
15
  export {};
@@ -1,4 +1,4 @@
1
- import { BodyType } from './body.js';
1
+ import { BodyType, } from './body.js';
2
2
  import { ValidationError } from './error.js';
3
3
  class EitherBody extends BodyType {
4
4
  constructor(options) {
@@ -15,7 +15,7 @@ class EnumSchema extends ModifiableSchema {
15
15
  }
16
16
  // compare only the stringified (normalized) values
17
17
  const normalizedObj = obj.toString();
18
- const index = this.normalized.findIndex((value) => value === normalizedObj);
18
+ const index = this.normalized.indexOf(normalizedObj);
19
19
  if (index === -1) {
20
20
  // invalid value
21
21
  throw new ValidationError([
@@ -1,6 +1,6 @@
1
1
  import type { Readable } from 'node:stream';
2
2
  import { BodyType } from './body.js';
3
- export declare class NoneBody extends BodyType<undefined> {
3
+ export declare class NoneBody extends BodyType<undefined, undefined> {
4
4
  deserialize(_stream: Readable, _contentType: string): Promise<undefined>;
5
5
  bodyDocs(): object;
6
6
  }
@@ -1,6 +1,6 @@
1
1
  import type { Readable } from 'node:stream';
2
2
  import { BodyType } from './body.js';
3
- declare class RawBody extends BodyType<Buffer<ArrayBuffer>> {
3
+ declare class RawBody extends BodyType<Buffer<ArrayBuffer>, Buffer<ArrayBufferLike>> {
4
4
  private contentType;
5
5
  constructor(contentType: string);
6
6
  deserialize(stream: Readable, _contentType: string): Promise<Buffer<ArrayBuffer>>;
@@ -3,7 +3,7 @@ import { BodyType } from './body.js';
3
3
  /**
4
4
  * The base class for all schemas.
5
5
  */
6
- export declare abstract class Schema<T> extends BodyType<T> {
6
+ export declare abstract class Schema<T> extends BodyType<T, T> {
7
7
  deserialize(stream: Readable, contentType: string): Promise<T>;
8
8
  bodyDocs(): object;
9
9
  /**
@@ -1,6 +1,6 @@
1
1
  import type { Readable } from 'node:stream';
2
2
  import { BodyType } from './body.js';
3
- declare class StreamBody extends BodyType<ReadableStream> {
3
+ declare class StreamBody extends BodyType<ReadableStream, ReadableStream> {
4
4
  private readonly contentType;
5
5
  constructor(contentType: string);
6
6
  deserialize(stream: Readable, _contentType: string): Promise<ReadableStream>;
@@ -1,9 +1,10 @@
1
+ import type { Typeof } from './body.js';
1
2
  import { ModifiableSchema } from './modifiable.js';
2
3
  import type { Schema } from './schema.js';
3
- declare class UnionSchema<T extends [...Schema<unknown>[]]> extends ModifiableSchema<T[number]['_typeof']> {
4
+ declare class UnionSchema<T extends [...Schema<unknown>[]]> extends ModifiableSchema<Typeof<T[number]>> {
4
5
  private readonly options;
5
6
  constructor(options: T);
6
- parse(obj: unknown): T[number]['_typeof'];
7
+ parse(obj: unknown): Typeof<T[number]>;
7
8
  documentation(): object;
8
9
  }
9
10
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yedra",
3
- "version": "0.17.6",
3
+ "version": "0.17.7",
4
4
  "repository": "github:0codekit/yedra",
5
5
  "main": "dist/index.js",
6
6
  "devDependencies": {