Skip to content

Commit

Permalink
groq-builder: feature(compat): moved all exports to the main index, r…
Browse files Browse the repository at this point in the history
…emoving the need for the `/validation` entrypoint
  • Loading branch information
scottrippey committed Dec 16, 2023
1 parent e61fcc1 commit 0517e0e
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/groq-builder/docs/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const productsQuery = q("*")
#### After, with `groq-builder`

```ts
import { createGroqBuilderWithValidation } from "groq-builder/validation";
import { createGroqBuilderWithValidation } from "groq-builder";
const q = createGroqBuilderWithValidation<any>(); // Using 'any' makes the query schema-unaware

const productsQuery = q.star
Expand Down
10 changes: 1 addition & 9 deletions packages/groq-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,10 @@
},
"./dist/index.js"
],
"./validation": [
{
"types": "./dist/validation/index.d.ts",
"default": "./dist/validation/index.js"
},
"./dist/validation/index.js"
],
"./package.json": "./package.json"
},
"files": [
"dist",
"validation"
"dist"
],
"scripts": {
"test:watch": "vitest",
Expand Down
16 changes: 13 additions & 3 deletions packages/groq-builder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import type { RootConfig } from "./types/schema-types";
// Be sure to keep these 2 imports in the correct order:
import { GroqBuilder, GroqBuilderOptions } from "./groq-builder";
import { ButFirst } from "./types/utils";

import "./commands";

import type { RootConfig } from "./types/schema-types";
import type { ButFirst } from "./types/utils";

// Export all our public types:
export * from "./types/public-types";
export * from "./types/schema-types";
export { GroqBuilder, GroqBuilderOptions } from "./groq-builder";
export { validate, createGroqBuilderWithValidation } from "./validation";

type RootResult = never;

/**
* Creates the root `q` query builder.
*
* The TRootConfig type argument is used to bind the query builder to the Sanity schema config.
* If you specify `any`, then your schema will be loosely-typed, but the output types will still be strongly typed.
*
* @param options - Allows you to specify if you want indentation added to the final query. Useful for debugging. Defaults to none.
*/
export function createGroqBuilder<TRootConfig extends RootConfig>(
options: GroqBuilderOptions = { indent: "" }
) {
Expand Down
24 changes: 8 additions & 16 deletions packages/groq-builder/src/validation/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { primitives } from "./primitives";
import {
createGroqBuilder as _createGroqBuilder,
RootConfig,
GroqBuilderOptions,
} from "../index";
import { sanityValidators } from "./content-block";
import { primitiveValidators } from "./primitives";
import { createGroqBuilder, RootConfig, GroqBuilderOptions } from "../index";
import { sanityValidators } from "./sanity";

export * from "../index";
export { primitives } from "./primitives";
export { sanityValidators } from "./content-block";
export const validate = {
...primitiveValidators,
...sanityValidators,
};

export function createGroqBuilderWithValidation<TRootConfig extends RootConfig>(
options?: GroqBuilderOptions
) {
return Object.assign(_createGroqBuilder<TRootConfig>(options), validate);
return Object.assign(createGroqBuilder<TRootConfig>(options), validate);
}

export const validate = {
...primitives,
...sanityValidators,
};
2 changes: 1 addition & 1 deletion packages/groq-builder/src/validation/primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expectType } from "../tests/expectType";
import { InferParserInput, InferParserOutput } from "../types/public-types";
import { ValidationErrors } from "./validation-errors";

describe("primitives", () => {
describe("primitiveValidators", () => {
it("string", () => {
const str = validate.string();

Expand Down
2 changes: 1 addition & 1 deletion packages/groq-builder/src/validation/primitives.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ParserFunction } from "../types/public-types";

export const primitives = {
export const primitiveValidators = {
string: memo(() => createTypeValidator("string")),
boolean: memo(() => createTypeValidator("boolean")),
number: memo(() => createTypeValidator("number")),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { memo, primitives } from "./primitives";
import { memo, primitiveValidators } from "./primitives";

export const sanityValidators = {
contentBlock: memo(<
TConfig extends ContentBlockConfig = ContentBlockConfig
>() => primitives.object<ContentBlock<TConfig>>()),
>() => primitiveValidators.object<ContentBlock<TConfig>>()),

contentBlocks: memo(<
TConfig extends ContentBlockConfig = ContentBlockConfig
>() => primitives.array<Array<ContentBlock<TConfig>>>()),
>() => primitiveValidators.array<Array<ContentBlock<TConfig>>>()),
};

export type ContentBlocks<
Expand Down
3 changes: 0 additions & 3 deletions packages/groq-builder/validation/package.json

This file was deleted.

0 comments on commit 0517e0e

Please sign in to comment.