Skip to content

Commit ea25ca3

Browse files
committed
Improve type for sortFields
1 parent f26d5f3 commit ea25ca3

File tree

9 files changed

+49
-15
lines changed

9 files changed

+49
-15
lines changed

rollup.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ function replaceCryptoImportWithRequire() {
2828
return updated;
2929
}
3030
},
31-
buildEnd() {
32-
if (!found) {
31+
buildEnd(error) {
32+
if (!error && !found) {
3333
throw new Error('Crypto import not found');
3434
}
3535
},

src/cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export function buildCursor<TNode extends Record<string, unknown>>({
146146
}: {
147147
queryName: string;
148148
node: TNode;
149-
sortFields: readonly FieldWithOrder[];
149+
sortFields: readonly FieldWithOrder<TNode>[];
150150
}): Cursor {
151151
const fields: Cursor['fields'] = {};
152152

src/field-name.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { FieldName as FieldNameZod } from './zod-models/field-name';
2-
import { FieldNameWithAlias as FieldNameWithAliasZod } from './zod-models/field-name-with-alias';
1+
import { FieldName as FieldNameType } from './zod-models/field-name';
2+
import { FieldNameWithAlias as FieldNameWithAliasType } from './zod-models/field-name-with-alias';
33

44
const regex = /[^.]*$/;
55

6-
export type ParsedFieldName = {
7-
alias: string;
6+
export type ParsedFieldName<TNode extends Record<string, unknown>> = {
7+
alias: string & keyof TNode;
88
name: string;
99
};
1010

11-
export function parseFieldName(
12-
fieldName: FieldNameZod | FieldNameWithAliasZod,
13-
): ParsedFieldName {
11+
export function parseFieldName<TNode extends Record<string, unknown>>(
12+
fieldName: FieldNameType<TNode> | FieldNameWithAliasType<TNode>,
13+
): ParsedFieldName<TNode> {
1414
const name = typeof fieldName === 'string' ? fieldName : fieldName.name;
1515
const alias =
1616
typeof fieldName === 'string'

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
export * from './sql-cursor-pagination';
33
export { decryptCursor, rawCursor, RawCursor } from './cursor';
44
export { buildCursorSecret } from './cursor-secret';
5-
export type { Order } from './zod-models/order';
65
export { Asc, Desc } from './zod-models/order';
76
export type * from './query-builder';
87
export * from './errors';
8+
export type { Cursor } from './zod-models/cursor';
9+
export type { FieldNameWithAlias } from './zod-models/field-name-with-alias';
10+
export type { FieldName } from './zod-models/field-name';
11+
export type { FieldValue } from './zod-models/field-value';
12+
export type { FieldWithOrder } from './zod-models/field-with-order';
13+
export type { Order } from './zod-models/order';

src/sql-cursor-pagination.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type _WithPaginationInputSetup<
9393
* There must be at least one entry and you must include an entry that maps to a unique key,
9494
* otherwise it's possible for there to be cursor collisions, which will result in an exception.
9595
*/
96-
sortFields: readonly FieldWithOrder[];
96+
sortFields: readonly FieldWithOrder<TNode>[];
9797
/**
9898
* A name for this query.
9999
*

src/subtype.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* c8 ignore start */
2+
// eslint-disable-next-line no-use-before-define
3+
export type Subtype<Sub extends Super, Super> = Sub;
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { z } from 'zod';
2+
import { Subtype } from '../subtype';
23
import { FieldName } from './field-name';
34

45
export const FieldNameWithAlias = z
56
.object({ alias: z.string().min(1), name: FieldName })
67
.readonly();
7-
export type FieldNameWithAlias = z.TypeOf<typeof FieldNameWithAlias>;
8+
export type FieldNameWithAlias<TNode extends Record<string, unknown>> = Subtype<
9+
{
10+
readonly alias: string & keyof TNode;
11+
readonly name: string;
12+
},
13+
z.TypeOf<typeof FieldNameWithAlias>
14+
>;

src/zod-models/field-name.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import { z } from 'zod';
2+
import { Subtype } from '../subtype';
23

34
const fieldRegex = /^[a-zA-Z0-9_]*(\.[a-zA-Z0-9_]*)*$/;
5+
6+
export type FullyQualifiedName<TName extends string> = `${string}.${TName}`;
7+
export type MaybeFullyQualifiedName<TName extends string> =
8+
| TName
9+
| FullyQualifiedName<TName>;
10+
411
export const FieldName = z
512
.string()
613
.min(1)
714
.regex(fieldRegex, 'Invalid field name');
8-
export type FieldName = z.TypeOf<typeof FieldName>;
15+
16+
export type FieldName<TNode> = Subtype<
17+
MaybeFullyQualifiedName<string & keyof TNode>,
18+
z.TypeOf<typeof FieldName>
19+
>;

src/zod-models/field-with-order.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import z from 'zod';
2+
import { Subtype } from '../subtype';
23
import { FieldName } from './field-name';
34
import { FieldNameWithAlias } from './field-name-with-alias';
45
import { Order } from './order';
@@ -9,4 +10,11 @@ export const FieldWithOrder = z
910
order: Order,
1011
})
1112
.readonly();
12-
export type FieldWithOrder = z.TypeOf<typeof FieldWithOrder>;
13+
14+
export type FieldWithOrder<TNode extends Record<string, unknown>> = Subtype<
15+
{
16+
readonly field: FieldName<TNode> | FieldNameWithAlias<TNode>;
17+
readonly order: Order;
18+
},
19+
z.TypeOf<typeof FieldWithOrder>
20+
>;

0 commit comments

Comments
 (0)