Reusable utility functions for dealing with image and file assets in Sanity
$ npm install @sanity/asset-utils
// ESM / TypeScript
import {someUtilityFunction} from '@sanity/asset-utils'
// CommonJS
const {someUtilityFunction} = require('@sanity/asset-utils')
MIT-licensed. See LICENSE.
Default hotspot (equals to horizontal/vertical center, full size of image)
See getExtension
See getFile
See getFileAsset
See getIdFromString
See getImage
See getImageAsset
See getProject
See getUrlFilename
See getUrlPath
Builds the base file path from the minimal set of parts required to assemble it
An asset-like shape defining ID, dimensions and extension
Project ID and dataset the file belongs to, along with other options
string
Builds the base file URL from the minimal set of parts required to assemble it
An asset-like shape defining ID and extension
string
Builds the base image path from the minimal set of parts required to assemble it
An asset-like shape defining ID, dimensions and extension
Project ID and dataset the image belongs to, along with other options
string
Builds the base image URL from the minimal set of parts required to assemble it
An asset-like shape defining ID, dimensions and extension
Project ID and dataset the image belongs to
string
Tries to resolve the asset document ID from any inferrable structure
Input source (image/file object, asset, reference, id, url, path)
The asset document ID
Returns cloned version of the default crop (prevents accidental mutations)
Default image crop object
Returns cloned version of the default hotspot (prevents accidental mutations)
Default image hotspot object
Returns the file extension for a given asset
Input source (file/image object, asset, reference, id, url, path)
The file extension, if resolvable (no .
included)
Tries to resolve an file object with as much information as possible, from any inferrable structure (id, url, path, file object etc)
Input source (file object, asset, reference, id, url, path)
Project ID and dataset the file belongs to
File object
Tries to resolve a (partial) file asset document with as much information as possible, from any inferrable structure (id, url, path, file object etc)
Input source (file object, asset, reference, id, url, path)
Project ID and dataset the file belongs to, along with other options
File asset document
Tries to cooerce a string (ID, URL or path) to an image asset ID
Input string (ID, URL or path)
string
Tries to resolve an image object with as much information as possible, from any inferrable structure (id, url, path, image object etc)
Input source (image object, asset, reference, id, url, path)
Project ID and dataset the image belongs to
Image object
Tries to resolve a (partial) image asset document with as much information as possible, from any inferrable structure (id, url, path, image object etc)
Input source (image object, asset, reference, id, url, path)
Project ID and dataset the image belongs to
Image asset document
Returns the width, height and aspect ratio of a passed image asset, from any inferrable structure (id, url, path, asset document, image object etc)
Input source (image object, asset, reference, id, url, path)
Object with width, height and aspect ratio properties
Resolves project ID and dataset the image belongs to, based on full URL or path
object | undefined
Strips the CDN URL, path and query params from a URL, eg:
https://cdn.sanity.io/images/project/dataset/filename-200x200.jpg?foo=bar
=>
filename-200x200.jpg
URL to get filename from
The filename of an URL, if URL matches the CDN URL
Strips the CDN URL and query params from a URL, eg:
https://cdn.sanity.io/images/project/dataset/filename-200x200.jpg?foo=bar
=>
images/project/dataset/filename-200x200.jpg
URL to get path name from
The path of a CDN URL
Get the "path stub" at the end of the path, if the user hasn't explicitly opted out of this behavior
Checks whether or not the given URL contains an asset path
Whether or not it contained an asset path
Converts from a full asset URL to just the asset document ID
A full asset URL to convert
string
Returns whether or not the passed filename is a valid file or image asset filename
Filename to validate
Whether or not the filename is an asset filename
Checks whether or not the given document ID is a valid Sanity asset document ID (file or image)
Document ID to check
Whether or not the given document ID is a Sanity asset document ID (file or image)
Checks whether or not the given source is an asset ID stub
(an object containing an _id
property)
Possible asset id stub
Whether or not the passed object is an object id stub
Checks whether or not the given source is an asset object stub
Possible asset object stub
Whether or not the passed object is an object stub
Checks whether or not the given source is an asset path stub
(an object containing a path
property)
Possible asset path stub
Whether or not the passed object is an object path stub
Checks whether or not the given source is an asset URL stub
(an object containing a url
property)
Possible asset url stub
Whether or not the passed object is an object url stub
Returns whether or not the passed crop has the default values for a crop region
The crop to return whether or not is the default crop
True if passed crop matches default, false otherwise
Returns whether or not the passed hotspot has the default values for a hotspot region
The hotspot to return whether or not is the default hotspot
True if passed hotspot matches default, false otherwise
Returns whether or not the passed filename is a valid file asset filename
Filename to validate
Whether or not the filename is a file asset filename
Checks whether or not the given document ID is a valid Sanity file asset document ID
Document ID to check
Whether or not the given document ID is a Sanity file asset document ID
Return whether or not the passed source is a file source
Source to check
Whether or not the given source is a file source
Returns whether or not the passed filename is a valid image asset filename
Filename to validate
Whether or not the filename is an image asset filename
Checks whether or not the given document ID is a valid Sanity image asset document ID
Document ID to check
Whether or not the given document ID is a Sanity image asset document ID
Return whether or not the passed source is an image source
Source to check
Whether or not the given source is an image source
Checks whether or not the given source is a Sanity reference (an object containing _ref string key)
Possible reference
Whether or not the passed object is a reference
Checks whether or not a given URL is a valid Sanity asset URL
URL to test
True if url is a valid Sanity asset URL, false otherwise
Checks whether or not the given source is a (partial) sanity file asset document.
Only checks the _type
property, all other properties may be missing
Source to check
Whether or not the given source is a file asset
Checks whether or not a given URL is a valid Sanity file asset URL
URL to test
True if url is a valid Sanity file asset URL, false otherwise
Checks whether or not the given source is a (partial) sanity image asset document.
Only checks the _type
property, all other properties may be missing
Source to check
Whether or not the given source is a file asset
Checks whether or not a given URL is a valid Sanity image asset URL
URL to test
True if url is a valid Sanity image asset URL, false otherwise
Checks whether or not an error instance is of type UnresolvableError
Error to check for unresolvable error type
True if the passed error instance appears to be an unresolveable error
Checks whether or not a given filename matches the expected Sanity asset filename pattern
Filename to check for validity
Whether or not the specified filename is valid
Parses a Sanity asset filename into individual parts (type, id, extension, width, height)
Filename to parse into named parts
Object of named properties
Parses a Sanity asset document ID into individual parts (type, id, extension, width/height etc)
Document ID to parse into named parts
Object of named properties
Parses a full Sanity asset URL into individual parts (type, project ID, dataset, id, extension, width, height)
Full URL to parse into named parts
Object of named properties
Parses a Sanity file asset document ID into individual parts (type, id, extension)
File asset document ID to parse into named parts
Object of named properties
Parses a full Sanity file asset URL into individual parts (type, project ID, dataset, id, extension, width, height)
Full URL to parse into named parts
Object of named properties
Parses a Sanity image asset document ID into individual parts (type, id, extension, width, height)
Image asset document ID to parse into named parts
Object of named properties
Parses a full Sanity image asset URL into individual parts (type, project ID, dataset, id, extension, width, height)
Full URL to parse into named parts
Object of named properties
Tries to get the asset path from a given asset source
The source image to infer an asset path from
A path if resolvable, undefined otherwise
Default crop (equals to "whole image")