-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
types(groq-builder): implemented correct types for root and filterByType
- Loading branch information
scottrippey
committed
Oct 1, 2023
1 parent
9666711
commit 5e81c85
Showing
6 changed files
with
93 additions
and
16 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/nextjs/sanity-studio/builder/commands/filter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createGroqBuilder } from "../groq-builder"; | ||
import { SanitySchemaTypes } from "../../sanity-types"; | ||
import { expectType } from "../test-utils/expectType"; | ||
import { ExtractScope } from "../common-types"; | ||
import { SanitySchemaRaw } from "../../sanity.config"; | ||
import { Simplify, SimplifyDeep } from "../type-utils"; | ||
|
||
const q = createGroqBuilder<SanitySchemaTypes>(); | ||
|
||
describe("filter", () => { | ||
it("", () => { | ||
const res = q.filterByType("flavour"); | ||
expectType<ExtractScope<typeof res>>().toStrictEqual<SanitySchemaRaw["flavour"]>(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/nextjs/sanity-studio/builder/commands/grab.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { createGroqBuilder } from "../groq-builder"; | ||
import { SanitySchemaTypes } from "../../sanity-types"; | ||
import { expectType } from "../test-utils/expectType"; | ||
import { ExtractScope } from "../common-types"; | ||
|
||
const q = createGroqBuilder<SanitySchemaTypes>(); | ||
|
||
describe("grab", () => { | ||
it("grab one property", () => { | ||
const res = q.filterByType("variant").grab({ | ||
name: true, | ||
}); | ||
|
||
expect(res).toMatchObject({ | ||
query: "*[_type == 'variant']{name}", | ||
}); | ||
|
||
expectType<ExtractScope<typeof res>>().toStrictEqual<{ | ||
name: string; | ||
}>(); | ||
}); | ||
|
||
it("grab multiple properties", () => { | ||
const res = q.filterByType("variant").grab({ | ||
id: true, | ||
name: true, | ||
price: true, | ||
msrp: true, | ||
}); | ||
|
||
expect(res).toMatchObject({ | ||
query: "*[_type == 'variant']{name,price,id,msrp}", | ||
}); | ||
|
||
expectType<ExtractScope<typeof res>>().toStrictEqual<{ | ||
id: string | undefined; | ||
name: string; | ||
price: number; | ||
msrp: number; | ||
}>(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
export type Simplify<T> = { | ||
[KeyType in keyof T]: T[KeyType]; | ||
} & {}; | ||
|
||
export type SimplifyDeep<T> = T extends object | ||
? T extends infer O | ||
? { [K in keyof O]: SimplifyDeep<O[K]> } | ||
: never | ||
: T; | ||
|
||
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; | ||
|
||
export type Override<T, TOverrides> = Omit<T, keyof TOverrides> & TOverrides; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { SanitySchemaRaw } from "./sanity.config"; | ||
// import { SimplifyDeep } from "./builder/type-utils"; | ||
|
||
/** Typescript type of all types! */ | ||
export type SanitySchemaTypes = SimplifyDeep<SanitySchemaRaw>; | ||
type SimplifyDeep<T> = T extends object ? (T extends infer O ? { [K in keyof O]: SimplifyDeep<O[K]> } : never) : T; | ||
// export type SanitySchemaTypes = SimplifyDeep<SanitySchemaRaw>; | ||
export type SanitySchemaTypes = SanitySchemaRaw; |