Skip to content

Latest commit

 

History

History
159 lines (105 loc) · 5.48 KB

_README.md

File metadata and controls

159 lines (105 loc) · 5.48 KB

@sanity-typed/client

NPM Downloads GitHub commit activity (branch) GitHub Repo stars GitHub contributors GitHub issues by-label Minified Size License

GitHub Sponsors

@sanity/client with typed GROQ Results

Using.Sanity.Typed.mp4

@[:page_toc](## Page Contents)

Install

npm install sanity @sanity-typed/client

Usage

Use createClient exactly as you would from @sanity/client.

@typescript @typescript @typescript

@:markdown

Typing an untyped client (and vice versa)

Sometimes, you'll have a preconfigured client from a separate library that you will still want typed results from. A castToTyped function is provided to do just that.

import { createClient } from "some-other-create-client";

import { castToTyped } from "@sanity-typed/client";

import type { SanityValues } from "./sanity.config";

const client = createClient({
  // ...
});

const typedClient = castToTyped<SanityValues>()(client);

// Also, if you need the config in the client (eg. for queries using $param),
// you can provide the same config again to include it in the types.

// const typedClient = castToTyped<SanityValues>()(client, {
//   ...same contents from createClient
// });

const data = await typedClient.fetch("*");
/**
 *  typeof data === {
 *    _createdAt: string;
 *    _id: string;
 *    _rev: string;
 *    _type: "product";
 *    _updatedAt: string;
 *    productName?: string;
 *    tags?: {
 *      _key: string;
 *      label?: string;
 *      value?: string;
 *    }[];
 *  }[]
 */

This function (nor the createClient function) have any runtime implications; it passes through the initial client unaltered.

Similarly, if you have a typed client that you want to untype (presumably to export from a library for general consumption), you can always cast it:

import type { SanityClient as SanityClientNative } from "@sanity/client";

import { createClient } from "@sanity-typed/client";

import type { SanityValues } from "./sanity.config";

const client = createClient<SanityValues>({
  // ...
});

export const typedClient = client;

export const untypedClient = client as SanityClientNative;

export default untypedClient;

Considerations

@:markdown @:markdown @:markdown @:markdown

Breaking Changes

3 to 4

Typescript version from 5.4.2 <= x <= 5.6.3

The supported Typescript version is now 5.4.2 <= x <= 5.6.3. Older versions are no longer supported and newer versions will be added as we validate it.

2 to 3

No more createClient<SanityValues>()(config)

Removing the double function signature from createClient:

- const client = createClient<SanityValues>()({
+ const client = createClient<SanityValues>({
  // ...
});

We no longer derive types from your config values. Most of the types weren't significant, but the main loss will be _originalId when the perspective was "previewDrafts".

1 to 2

Removal of castFromTyped

Casting from typed to untyped is now just a simple cast:

+ import type { SanityClient as SanityClientNative } from "@sanity/client";

- import { castFromTyped, createClient } from "@sanity-typed/client";
+ import { createClient } from "@sanity-typed/client";

import type { SanityValues } from "./sanity.config";

const client = createClient<SanityValues>()({
  // ...
});

export const typedClient = client;

- export const untypedClient = castFromTyped(client);
+ export const untypedClient = client as SanityClientNative;

export default untypedClient;

castToTyped still exists.

Alternatives