Skip to content

Commit 752aea2

Browse files
committed
Release 0.13.22
1 parent deae554 commit 752aea2

13 files changed

+112
-135
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pave",
33
"type": "module",
4-
"version": "0.13.21",
4+
"version": "0.13.22",
55
"author": "Casey Foster <c@sey.me>",
66
"license": "MIT",
77
"repository": {

src/execute.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ const { Promise } = globalThis;
1010
const { isArray } = Array;
1111

1212
/**
13-
* @template {string} [TypeName=string] Default is `string`
14-
* @template {{ [K: string]: any }} [Extensions={ [K: string]: any }] Default is
15-
* `{ [K: string]: any }`
16-
* @template [Context=any] Default is `any`
13+
* @template {Schema<any, any, any>} S
1714
* @param {{
1815
* context?: any;
1916
* object?: any;
2017
* path?: string[];
2118
* query: Query;
22-
* schema: Schema<TypeName, Extensions, Context>;
23-
* type: Type<TypeName, Extensions, Context>;
19+
* schema: S;
20+
* type: Type<S>;
2421
* value?: any;
2522
* }} options
2623
*/

src/get-query-cost.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
/** @import {Query, Schema, Type} from '#src/index.js'; */
1+
/** @import {Query, Schema, SchemaContext, Type} from '#src/index.js'; */
22

33
import { isObject } from '#src/is-object.js';
44

55
const { isArray } = Array;
66

77
/**
8-
* @template {string} [TypeName=string] Default is `string`
9-
* @template {{ [K: string]: any }} [Extensions={ [K: string]: any }] Default is
10-
* `{ [K: string]: any }`
11-
* @template [Context=any] Default is `any`
8+
* @template {Schema<any, any, any>} S
129
* @param {{
13-
* context?: Context;
10+
* context?: SchemaContext<S>;
1411
* path?: string[];
1512
* query: Query;
16-
* schema: Schema<TypeName, Extensions, Context>;
17-
* type: Type<TypeName, Extensions, Context>;
13+
* schema: S;
14+
* type: Type<S>;
1815
* }} options
1916
*/
2017
export const getQueryCost = ({ context, path = [], query, schema, type }) => {
2118
let cost = 0;
2219
while (true) {
2320
if (!type) return cost;
2421

25-
if (isArray(type)) type = { object: type };
22+
if (isArray(type)) type = /** @type {Type<S>} */ ({ object: type });
2623

2724
let nextType;
2825
if (!isObject(type)) nextType = schema[type];

src/index.js

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,67 @@
1515
*/
1616

1717
/**
18-
* @template {string} [TypeName=never] Default is `never`
19-
* @template {{ [K: string]: any }} [Extensions={}] Default is `{}`
20-
* @template [Context=unknown] Default is `unknown`
18+
* @template {string} TypeName
19+
* @template {{ [K: string]: any }} Extensions
20+
* @template Context
21+
* @typedef {{
22+
* [K in TypeName]: Type<
23+
* Schema<TypeName, Extensions, Context>,
24+
* any,
25+
* any,
26+
* any,
27+
* any
28+
* >;
29+
* }} Schema
30+
*/
31+
32+
/**
33+
* @template {Schema<any, any, any>} S
34+
* @typedef {S extends Schema<infer TypeName, any, any> ? TypeName : never} SchemaTypeName
35+
*/
36+
37+
/**
38+
* @template {Schema<any, any, any>} S
39+
* @typedef {S extends Schema<any, infer Extensions, any> ? Extensions : never} SchemaExtensions
40+
*/
41+
42+
/**
43+
* @template {Schema<any, any, any>} S
44+
* @typedef {S extends Schema<any, any, infer Context> ? Context : never} SchemaContext
45+
*/
46+
47+
/**
48+
* @template {Schema<any, any, any>} [S=Schema<never, {}, unknown>] Default is
49+
* `Schema<never, {}, unknown>`
2150
* @template [Input=unknown] Default is `unknown`
2251
* @template [Object=unknown] Default is `unknown`
2352
* @template [Value=unknown] Default is. Default is `unknown`
2453
* @template [ResolvedValue=NonNullable<unknown>] Default is
2554
* `NonNullable<unknown>`
2655
* @typedef {Recursive<
27-
* | TypeName
56+
* | SchemaTypeName<S>
2857
* | ((
29-
* | { optional: Type<TypeName, Extensions, Context> }
30-
* | { nullable: Type<TypeName, Extensions, Context> }
31-
* | {
32-
* arrayOf: Type<TypeName, Extensions, Context>;
33-
* minLength?: number;
34-
* maxLength?: number;
35-
* }
58+
* | { optional: Type<S> }
59+
* | { nullable: Type<S> }
60+
* | { arrayOf: Type<S>; minLength?: number; maxLength?: number }
3661
* | {
37-
* oneOf: { [K: string]: Type<TypeName, Extensions, Context> };
62+
* oneOf: { [K: string]: Type<S> };
3863
* resolveType: (value: NonNullable<unknown>) => string;
3964
* }
65+
* | { object: { [K: string]: Type<S> }; defaultType?: Type<S> }
4066
* | {
41-
* object: { [K: string]: Type<TypeName, Extensions, Context> };
42-
* defaultType?: Type<TypeName, Extensions, Context>;
43-
* }
44-
* | {
45-
* input?: Type<TypeName, Extensions, Context>;
46-
* type?: Type<TypeName, Extensions, Context>;
67+
* input?: Type<S>;
68+
* type?: Type<S>;
4769
* typeInput?: any;
4870
* resolve?:
4971
* | ((options: {
50-
* context: Context;
72+
* context: SchemaContext<S>;
5173
* input: Input;
5274
* object: Object;
5375
* path: string[];
5476
* query: Query;
55-
* schema: Schema<TypeName, Extensions, Context>;
56-
* type: Type<TypeName, Extensions, Context>;
77+
* schema: S;
78+
* type: Type<S>;
5779
* value: Value;
5880
* }) => any)
5981
* | {}
@@ -63,40 +85,31 @@
6385
* cost?:
6486
* | number
6587
* | ((options: {
66-
* context: Context;
88+
* context: SchemaContext<S>;
6789
* cost: number;
6890
* input: Input;
6991
* object: Object;
7092
* path: string[];
7193
* query: Query;
72-
* schema: Schema<TypeName, Extensions, Context>;
73-
* type: Type<TypeName, Extensions, Context>;
94+
* schema: S;
95+
* type: Type<S>;
7496
* value: Value;
7597
* }) => number);
7698
* defaultValue?: any;
7799
* validate?: (options: {
78-
* context: Context;
100+
* context: SchemaContext<S>;
79101
* input: Input;
80102
* object: Object;
81103
* path: string[];
82104
* query: Query;
83-
* schema: Schema<TypeName, Extensions, Context>;
84-
* type: Type<TypeName, Extensions, Context>;
105+
* schema: S;
106+
* type: Type<S>;
85107
* value: ResolvedValue;
86108
* }) => any;
87-
* } & Extensions)
109+
* } & SchemaExtensions<S>)
88110
* >} Type
89111
*/
90112

91-
/**
92-
* @template {string} TypeName
93-
* @template {{ [K: string]: any }} Extensions
94-
* @template Context
95-
* @typedef {{
96-
* [K in TypeName]: Type<TypeName, Extensions, Context, any, any, any, any>;
97-
* }} Schema
98-
*/
99-
100113
export { Context } from '#src/context.js';
101114
export { createClient } from '#src/create-client.js';
102115
export { execute } from '#src/execute.js';

src/validate-query.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@ const { isArray } = Array;
99
const skipInput = {};
1010

1111
/**
12-
* @template {string} [TypeName=string] Default is `string`
13-
* @template {{ [K: string]: any }} [Extensions={ [K: string]: any }] Default is
14-
* `{ [K: string]: any }`
15-
* @template [Context=any] Default is `any`
12+
* @template {Schema<any, any, any>} S
1613
* @param {{
1714
* context?: any;
1815
* path?: string[];
1916
* query: Query;
20-
* schema: Schema<TypeName, Extensions, Context>;
21-
* type: Type<TypeName, Extensions, Context>;
17+
* schema: S;
18+
* type: Type<S>;
2219
* }} options
2320
* @returns {Query}
2421
*/

src/validate-schema.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@ const { Set } = globalThis;
88
const { isArray } = Array;
99

1010
/**
11-
* @template {string} [TypeName=string] Default is `string`
12-
* @template {{ [K: string]: any }} [Extensions={ [K: string]: any }] Default is
13-
* `{ [K: string]: any }`
14-
* @template [Context=any] Default is `any`
15-
* @param {{
16-
* extensions?: { [K: string]: any };
17-
* schema: Schema<TypeName, Extensions, Context>;
18-
* }} options
11+
* @template {Schema<any, any, any>} S
12+
* @param {{ extensions?: { [K: string]: any }; schema: S }} options
1913
*/
2014
export const validateSchema = ({ extensions, schema }) => {
2115
const positiveNumber = {

src/validate-value.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ import { throwPaveError } from '#src/throw-pave-error.js';
77
const { isArray } = Array;
88

99
/**
10-
* @template {string} [TypeName=string] Default is `string`
11-
* @template {{ [K: string]: any }} [Extensions={ [K: string]: any }] Default is
12-
* `{ [K: string]: any }`
13-
* @template [Context=any] Default is `any`
10+
* @template {Schema<any, any, any>} S
1411
* @param {{
1512
* context?: any;
1613
* object?: any;
1714
* path?: string[];
1815
* query?: Query;
19-
* schema: Schema<TypeName, Extensions, Context>;
20-
* type: Type<TypeName, Extensions, Context>;
16+
* schema: S;
17+
* type: Type<S>;
2118
* value?: any;
2219
* }} options
2320
*/

types/execute.d.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
export function execute<TypeName extends string = string, Extensions extends {
2-
[K: string]: any;
3-
} = {
4-
[K: string]: any;
5-
}, Context = any>({ context, object, path, query, schema, type, value }: {
1+
export function execute<S extends Schema<any, any, any>>({ context, object, path, query, schema, type, value }: {
62
context?: any;
73
object?: any;
84
path?: string[];
95
query: Query;
10-
schema: Schema<TypeName, Extensions, Context>;
11-
type: Type<TypeName, Extensions, Context>;
6+
schema: S;
7+
type: Type<S>;
128
value?: any;
139
}): Promise<any>;
14-
import type { Query } from '#types/index.js';
1510
import type { Schema } from '#types/index.js';
11+
import type { Query } from '#types/index.js';
1612
import type { Type } from '#types/index.js';

types/get-query-cost.d.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
export function getQueryCost<TypeName extends string = string, Extensions extends {
2-
[K: string]: any;
3-
} = {
4-
[K: string]: any;
5-
}, Context = any>({ context, path, query, schema, type }: {
6-
context?: Context;
1+
export function getQueryCost<S extends Schema<any, any, any>>({ context, path, query, schema, type }: {
2+
context?: SchemaContext<S>;
73
path?: string[];
84
query: Query;
9-
schema: Schema<TypeName, Extensions, Context>;
10-
type: Type<TypeName, Extensions, Context>;
5+
schema: S;
6+
type: Type<S>;
117
}): any;
12-
import type { Query } from '#types/index.js';
138
import type { Schema } from '#types/index.js';
9+
import type { SchemaContext } from '#types/index.js';
10+
import type { Query } from '#types/index.js';
1411
import type { Type } from '#types/index.js';

types/index.d.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,64 +18,65 @@ export type Query<T = any> = {
1818
};
1919
export type Recursive<T> = T | RecursiveArray<T>;
2020
export type RecursiveArray<T> = Recursive<T>[];
21-
export type Type<TypeName extends string = never, Extensions extends {
21+
export type Schema<TypeName extends string, Extensions extends {
2222
[K: string]: any;
23-
} = {}, Context = unknown, Input = unknown, Object = unknown, Value = unknown, ResolvedValue = {}> = Recursive<TypeName | (({
24-
optional: Type<TypeName, Extensions, Context>;
23+
}, Context> = { [K in TypeName]: Type<Schema<TypeName, Extensions, Context>, any, any, any, any>; };
24+
export type SchemaTypeName<S extends Schema<any, any, any>> = S extends Schema<infer TypeName, any, any> ? TypeName : never;
25+
export type SchemaExtensions<S extends Schema<any, any, any>> = S extends Schema<any, infer Extensions, any> ? Extensions : never;
26+
export type SchemaContext<S extends Schema<any, any, any>> = S extends Schema<any, any, infer Context> ? Context : never;
27+
export type Type<S extends Schema<any, any, any> = Schema<never, {}, unknown>, Input = unknown, Object = unknown, Value = unknown, ResolvedValue = {}> = Recursive<SchemaTypeName<S> | (({
28+
optional: Type<S>;
2529
} | {
26-
nullable: Type<TypeName, Extensions, Context>;
30+
nullable: Type<S>;
2731
} | {
28-
arrayOf: Type<TypeName, Extensions, Context>;
32+
arrayOf: Type<S>;
2933
minLength?: number;
3034
maxLength?: number;
3135
} | {
3236
oneOf: {
33-
[K: string]: Type<TypeName, Extensions, Context>;
37+
[K: string]: Type<S>;
3438
};
3539
resolveType: (value: NonNullable<unknown>) => string;
3640
} | {
3741
object: {
38-
[K: string]: Type<TypeName, Extensions, Context>;
42+
[K: string]: Type<S>;
3943
};
40-
defaultType?: Type<TypeName, Extensions, Context>;
44+
defaultType?: Type<S>;
4145
} | {
42-
input?: Type<TypeName, Extensions, Context>;
43-
type?: Type<TypeName, Extensions, Context>;
46+
input?: Type<S>;
47+
type?: Type<S>;
4448
typeInput?: any;
4549
resolve?: ((options: {
46-
context: Context;
50+
context: SchemaContext<S>;
4751
input: Input;
4852
object: Object;
4953
path: string[];
5054
query: Query;
51-
schema: Schema<TypeName, Extensions, Context>;
52-
type: Type<TypeName, Extensions, Context>;
55+
schema: S;
56+
type: Type<S>;
5357
value: Value;
5458
}) => any) | {} | null;
5559
}) & {
5660
cost?: number | ((options: {
57-
context: Context;
61+
context: SchemaContext<S>;
5862
cost: number;
5963
input: Input;
6064
object: Object;
6165
path: string[];
6266
query: Query;
63-
schema: Schema<TypeName, Extensions, Context>;
64-
type: Type<TypeName, Extensions, Context>;
67+
schema: S;
68+
type: Type<S>;
6569
value: Value;
6670
}) => number);
6771
defaultValue?: any;
6872
validate?: (options: {
69-
context: Context;
73+
context: SchemaContext<S>;
7074
input: Input;
7175
object: Object;
7276
path: string[];
7377
query: Query;
74-
schema: Schema<TypeName, Extensions, Context>;
75-
type: Type<TypeName, Extensions, Context>;
78+
schema: S;
79+
type: Type<S>;
7680
value: ResolvedValue;
7781
}) => any;
78-
} & Extensions)>;
79-
export type Schema<TypeName extends string, Extensions extends {
80-
[K: string]: any;
81-
}, Context> = { [K in TypeName]: Type<TypeName, Extensions, Context, any, any, any, any>; };
82+
} & SchemaExtensions<S>)>;

0 commit comments

Comments
 (0)