npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@freighter-studio/sanity-typed-queries

v0.8.3

Published

A collection of helper functions to improve the typing of Sanity resources.

Downloads

5

Readme

A zero-dependency schema generator and query builder that is fully-typed and works in JavaScript and TypeScript.

Features

  • 📚 Documentation: Sanity documentation appears as you type.
  • 💪 TypeScript: Written in TypeScript.

Progress

  • [x] Fully typed schema builder
  • [x] Query builder (working with string, boolean, number), ordering, projections
  • [x] Inferred type of arrays
  • [x] Support for object types with subfields
  • [x] Custom mappings ("prop": my.prop)
  • [x] Resolving image and file types
  • [x] Resolving custom object/document types
  • [ ] Distinguish object/document types within valid field types
  • [ ] Additional query filters
  • [ ] Querying multiple types of document

Help and contributions are welcome.

Quick Start

First install sanity-typed-queries:

yarn add sanity-typed-queries

# or npm

npm install sanity-typed-queries --save

Schema definition

Now you will need to generate your Sanity schema documents using the schema builder. You will get documentation as you type, and enforced compliance with Sanity's schema builder, such as being able to see validation rules applicable to the type of field you are creating, and so on.

schema/author.js:

import { defineDocument } from 'sanity-typed-queries'

const { document } = defineDocument('author', {
  name: {
    type: 'string',
    validation: Rule => Rule.required(),
  },
  biography: {
    type: 'text',
    rows: 4,
  },
  yearOfBirth: {
    type: 'number',
  },
})

export default document

This is equivalent to defining the following schema:

export default {
  name: 'author',
  title: 'Author',
  type: 'document',
  fields: [
    {
      name: 'name',
      title: 'Name',
      type: 'string',
      validation: Rule => Rule.required(),
    },
    {
      name: 'biography',
      title: 'Biography',
      type: 'text',
      rows: 4,
    },
    {
      name: 'yearOfBirth',
      title: 'Year Of Birth',
      type: 'number',
    },
  ],
}

Query builder

For more documentation, see this GROQ/query builder cheat sheet.

You can also export a query builder from the same file.

import { defineDocument } from 'sanity-typed-queries'

const { document, builder } = defineDocument('author', {
  ...
})

// Export your query builder for use elsewhere
export { builder }
export default document

You can use this builder elsewhere to generate the appropriate types and GROQ queries. For example:

import { builder as authorBuilder } from './cms/schema/author.js'

const [query, type] = authorBuilder.pick('name').first().use()

// *[_type == 'author'][0].name
const queryString = query

// string
type AuthorName = typeof type

If you're using the Sanity client, you might use it like this:

import sanityClient from '@sanity/client'
import { author } from './cms/schema/author.js'

const [query, type] = author.pick('name').first().use()

const client = sanityClient(config)
// Promise<string>
const result = client.fetch<typeof type>(query)

Custom types

You can export utility objects or documents for reference within other schemas.

schema/tag.js:

import { defineObject } from 'sanity-typed-queries'

const { tag, object } = defineObject('tag', {
  ...
})

export { tag }
export default object

Then you can pass that when defining documents that reference it.

schema/author.js:

import { defineObject } from 'sanity-typed-queries'
import { tag } from './tag'

const { builder, document } = defineDocument(
  'author',
  {
    tag: {
      type: 'tag',
    },
  },
  [tag]
)

export default document

Inspirations

Projects I've found helpful are:

Contributors

This has been developed to suit my needs but additional use cases and contributions are very welcome.

License

MIT License - Copyright © Daniel Roe