JSPM

  • Created
  • Published
  • Downloads 2595
  • Score
    100M100P100Q118013F

Typed Sanity Cllient Results, all inferred, no client changes!

Package Exports

  • @sanity-typed/client
  • @sanity-typed/client/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@sanity-typed/client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@sanity-typed/client

NPM Downloads GitHub commit activity (branch) GitHub Repo stars GitHub contributors Minified Size License

GitHub Sponsors

Typed Sanity Cllient Results, all inferred, no client changes!

Page Contents

Install

npm install @sanity-typed/client

Usage

Use createClient from this library like you would from sanity's own exports with a minor change for proper type inference.

your-super-cool-application.ts:

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

// See the API for this in https://github.com/saiichihashimoto/sanity-typed/tree/main/packages/types
import type { SanityValues } from "./sanity.schema";

/** Small change using createClient https://www.sanity.io/docs/config-api-reference#dd1dc18716de */
// const client = createClient({
const client = createClient<SanityValues>()({
  projectId: "your-project-id",
  dataset: "your-dataset-name",
  useCdn: true,
  apiVersion: "2023-05-23",
});

/** Typescript type from GROQ queries! */
const data = await client.fetch('*[_type=="product"]');
/**
 *  typeof data === {
 *    _createdAt: string;
 *    _id: string;
 *    _rev: string;
 *    _type: "product";
 *    _updatedAt: string;
 *    productName?: string;
 *    tags?: {
 *      _key: string;
 *      label?: string;
 *      value?: string;
 *    }[];
 *  }[]
 */

The createClient<SanityValues>()(config) syntax is due to having to infer one generic (the config shape) while explicitly providing the Sanity Values' type, which can't be done in the same generics.