0.9.8 • Published 12 months ago

sanity-codegen v0.9.8

Weekly downloads
1,356
License
MIT
Repository
github
Last release
12 months ago

Sanity Codegen ✨ · codecov github status checks bundlephobia

Generate TypeScript types from your Sanity schemas

Demos

CLI — generate types in seconds

CLI handles a babel setup and shims out the Sanity part system to generate TypeScript types with ease.

CLI Demo

Client — for optimized Sanity DX

Sanity Codegen ships with a simple and tiny client that hooks up with your types.

Client Demo

Installation

npm i --save-dev sanity-codegen prettier

or

yarn add --dev sanity-codegen prettier

Note: Prettier is a peer dependency

CLI Usage

Create a sanity-codegen.config.ts or sanity-codegen.config.js at the root of your project.

import { SanityCodegenConfig } from 'sanity-codegen';

const config: SanityCodegenConfig = {
  schemaPath: './path/to/your/schema',
  outputPath: './schema.ts',
};

export default config;

See here for the rest of the available options.

Then run the CLI with npx at the root of your sanity project.

npx sanity-codegen

Running with npx runs the CLI in the context of your project's node_modules.

Client

The Sanity Codegen client is a very simple Sanity client that utilizes the generated types for great DX.

Create sanity-client.ts file and configure and export the client.

Client Installation

// sanity-client.ts
import { createClient } from 'sanity-codegen';
import { Documents } from './your-generated-schema-types.ts';

// This type parameter enables the client to be aware of your generated types
//                           👇👇👇
export default createClient<Documents>({
  // Note: these are useful to pull from environment variables
  // (required) your sanity project id
  projectId: '...',
  // (required) your sanity dataset
  dataset: '...',
  // (required) the fetch implementation to use
  fetch: window.fetch,
  //
  // (optional) if true, the client will prefer drafts over the published versions
  previewMode: true,
  // (optional) only required if your dataset is private or if you want to use preview mode
  token: '...',
});

Schema Codegen Options

If you want your type to be marked as required instead of optional, add codegen: { required: true } to your schema fields:

export default {
  name: 'myDocument',
  type: 'document',
  fields: [
    {
      name: 'aRequiredField',
      type: 'string',
      // 👇👇👇
      codegen: { required: true },
      validation: (Rule) => Rule.required(),
      // 👆👆👆
    },
  ],
};

This will tell the codegen to remove the optional ? modifier on the field.

NOTE: Drafts that are run through the document may have incorrect types. Be aware of this when using preview mode.

Client Usage

The client currently only contains 3 methods:

/**
 * Pass in a document type name and an ID and the client will return the full
 * document typed. Returns `null` if the document can't be found.
 */
function get<T>(type: string, id: string): Promise<T | null>;

/**
 * Pass in a document type and optionally a groq filter clause and the client
 * will return an array of those documents.
 */
function getAll<T>(type: string, filter?: string): Promise<T[]>;

/**
 * Given a Sanity reference, this will fetch that reference with types.
 * Calls the above `get` function internally
 */
function expand<T>(ref: SanityReference<T>): Promise<R>;

The design behind the client is to fetch full documents and handle projections and transforms in code.

The appeal of this approach is purely its simplicity, and in the context Jamstack apps the extra weight of the request doesn't matter since it'll get compiled to static data anyway.

If you're using next.js you can write your projections/transforms in getStaticProps and use the return type to infer incoming props. The types will flow down nicely 😎.

import sanity from './sanity-client';

export const getStaticProps = async (context) => {
  const slug = context.params?.slug as string;
  const [blogPost] = sanity.getAll('blogPost', `seo.slug.current == "${slug}"`);
  const { title, content } = blogPost;

  return { props: { title, content } };
};

type Props = ReturnType<typeof getStaticProps>['props'];

function BlogPost({ title, content }: Props) {
  return (
    <>
      <h1>{title}</h1>
      <p>{content}</p>
    </>
  );
}

export default BlogPost;

API Usage

Better docs coming soon. For now the gist is:

import generateTypes from 'sanity-codegen/generate-types';

generateTypes({
  // see here:
  // https://github.com/ricokahler/sanity-codegen/blob/13250d60892bfc95b73d88b28e88b574a31935a7/src/generate-types.ts#L85-L109
}).then((generatedTypes) => {
  // `generatedTypes` is a string with the typescript code
});

However you may run into challenges with executing the code if your schema imports from the sanity parts system. The CLI tries to help you with this.

1.0.0-alpha.29

1 year ago

1.0.0-alpha.32

1 year ago

1.0.0-alpha.31

1 year ago

1.0.0-alpha.38

1 year ago

1.0.0-alpha.37

1 year ago

1.0.0-alpha.39

1 year ago

1.0.0-alpha.34

1 year ago

1.0.0-alpha.36

1 year ago

1.0.0-alpha.35

1 year ago

1.0.0-alpha.41

1 year ago

1.0.0-alpha.40

1 year ago

1.0.0-alpha.43

1 year ago

1.0.0-alpha.42

1 year ago

1.0.0-alpha.45

12 months ago

1.0.0-alpha.44

12 months ago

1.0.0-alpha.26

2 years ago

1.0.0-alpha.19

2 years ago

1.0.0-alpha.18

2 years ago

1.0.0-alpha.14

2 years ago

1.0.0-alpha.13

2 years ago

1.0.0-alpha.25

2 years ago

1.0.0-alpha.24

2 years ago

0.9.8

2 years ago

0.9.7

2 years ago

1.0.0-alpha.12

3 years ago

0.9.6

3 years ago

1.0.0-alpha.11

3 years ago

1.0.0-alpha.10

3 years ago

1.0.0-alpha.9

3 years ago

0.9.5

3 years ago

1.0.0-alpha.8

3 years ago

1.0.0-alpha.7

3 years ago

1.0.0-alpha.6

3 years ago

1.0.0-alpha.5

3 years ago

1.0.0-alpha.4

3 years ago

0.9.4

3 years ago

1.0.0-alpha.3

3 years ago

1.0.0-alpha.2

3 years ago

1.0.0-alpha.1

3 years ago

1.0.0-alpha.0

3 years ago

0.9.2

3 years ago

0.9.3

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.8.2-alpha.3

3 years ago

0.8.2-alpha.2

3 years ago

0.8.2-alpha.1

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.8.0-alpha.1

3 years ago

0.7.0

3 years ago

0.7.0-alpha.1

3 years ago

0.6.4

3 years ago

0.6.4-alpha.1

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.1-alpha.1

3 years ago

0.6.0

3 years ago

0.6.0-alpha.1

3 years ago

0.5.0

3 years ago

0.5.0-alpha.3

3 years ago

0.5.0-alpha.2

3 years ago

0.5.0-alpha.1

3 years ago

0.4.0

3 years ago

0.4.0-alpha.5

3 years ago

0.4.0-alpha.4

3 years ago

0.4.0-alpha.3

3 years ago

0.4.0-alpha.2

3 years ago

0.4.0-alpha.1

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.3.0-alpha.2

3 years ago

0.3.0-alpha.1

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.1.2

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.0-alpha.7

3 years ago

0.0.0-alpha.6

3 years ago

0.0.0-alpha.4

3 years ago

0.0.0-alpha.3

3 years ago

0.0.0-alpha.2

3 years ago

0.0.0-alpha.1

3 years ago

0.0.0-alpha.0

3 years ago