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

@sanity/id-utils

v1.0.0

Published

Utilities for working with Sanity document IDs

Downloads

101

Readme

@sanity/id-utils

Utilities for working with Sanity document IDs

Why?

Quite often you'll see an APIs that takes a Sanity document ID as a string, but in many cases there’s an implicit expectation in the API about this document ID being either a published ID or a draft ID. Passing the wrong type of ID can lead to subtle errors, and at the same time guarding against this often leads to lots of redundant checks in places we a certain document ID variant is required.

With the help from this library, and it’s use of branded types functions and components can now declare which ID variant they need as part of their signature, which provides developers with immediate feedback if attempting to pass the wrong variant.

For example, imagine that this component has an implicit assumption that props.id will always be the id of the published document:

function SomeComponent(props: {id: string}) {
//... Things will break in spectacular ways if `props.id` is a draft id
}

This can now be written this way:

function SomeComponent(props: {id: PublishedId}) {
//...
}

If you try to pass this component a draft id now, you’ll immediately see an error in your IDE

<SomeComponent id={someDraftId} />
// TS2322: Type string is not assignable to type PublishedId
// Or, if it's of the DraftId type:
// TS2322: Type DraftId is not assignable to type PublishedId

You will have to make sure to turn it into a published id before passing it:

<SomeComponent id={getPublishedId(someDraftId)} />

Features

  • Added type safety through branded types via ts-brand.
  • Runtime validation of IDs. Tells you if you accidentally use an invalid ID.
  • Easily convert between the IDs the published documents, draft document and any version of a document.
  • Generate safe document ids from strings, useful for importing from external systems that may have incompatible ids

Usage example

import {
  DocumentId,
  getDraftId,
  getPublishedId,
  getVersionId,
  getVersionNameFromId,
} from '@sanity/id-utils'

// Make the document id "foo". This would have thrown the provided id was not valid
const id = DocumentId('foo')

// get the draft id of foo
const draftId = getDraftId(id)

console.log(draftId)
// => drafts.foo

// get the id of the document in version "someversion"
const someVersionId = getVersionId(draftId, 'some-version')
console.log(someVersionId)
// => versions.some-version.foo

// get the id of the document in version "other-version"
const otherVersionId = getVersionId(draftId, 'other-version')
console.log(otherVersionId)
// => versions.other-version.foo

// get the published id of the version
console.log(getPublishedId(otherVersionId))
// => foo

// get the published id of the other version
console.log(getVersionNameFromId(otherVersionId))
// => other-version

// get the version name from the draft id
// @ts-expect-error - this is a type error because draft ids does not contain version names
console.log(getVersionNameFromId(draftId))

API Docs

Find the latest autogenerated API docs here