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')
An HTML version is also available, which also includes interface definitions, constants and more.### Functions
- buildFilePath
- buildFileUrl
- buildImagePath
- buildImageUrl
- getAssetDocumentId
- getDefaultCrop
- getDefaultHotspot
- getExtension
- getFile
- getFileAsset
- getIdFromString
- getImage
- getImageAsset
- getImageDimensions
- getProject
- getUrlFilename
- getUrlPath
- hasPath
- idFromUrl
- isAssetFilename
- isAssetIdStub
- isAssetObjectStub
- isAssetPathStub
- isAssetUrlStub
- isFileAssetFilename
- isFileSource
- isImageAssetFilename
- isImageSource
- isReference
- isSanityFileAsset
- isUnresolvableError
- isValidFilename
- parseAssetFilename
- parseAssetId
- parseFileAssetId
- parseImageAssetId
- tryGetAssetDocumentId
- tryGetAssetPath
- tryGetExtension
- tryGetFile
- tryGetFileAsset
- tryGetIdFromString
- tryGetImage
- tryGetImageAsset
- tryGetImageDimensions
- tryGetProject
- tryGetUrlFilename
- tryGetUrlPath
▸ buildFilePath(asset
: FileUrlBuilderOptions, project
: SanityProjectDetails): string
Builds the base file path from the minimal set of parts required to assemble it
Name | Type | Description |
---|---|---|
asset |
FileUrlBuilderOptions | An asset-like shape defining ID, dimensions and extension |
project |
SanityProjectDetails | Project ID and dataset the file belongs to |
Returns: string
Defined in src/paths.ts:57
▸ buildFileUrl(asset
: FileUrlBuilderOptions, project
: SanityProjectDetails): string
Builds the base file URL from the minimal set of parts required to assemble it
Name | Type | Description |
---|---|---|
asset |
FileUrlBuilderOptions | An asset-like shape defining ID and extension |
project |
SanityProjectDetails | Project ID and dataset the file belongs to |
Returns: string
Defined in src/paths.ts:78
▸ buildImagePath(asset
: ImageUrlBuilderOptions, project
: SanityProjectDetails): string
Builds the base image path from the minimal set of parts required to assemble it
Name | Type | Description |
---|---|---|
asset |
ImageUrlBuilderOptions | An asset-like shape defining ID, dimensions and extension |
project |
SanityProjectDetails | Project ID and dataset the image belongs to |
Returns: string
Defined in src/paths.ts:21
▸ buildImageUrl(asset
: ImageUrlBuilderOptions, project
: SanityProjectDetails): string
Builds the base image URL from the minimal set of parts required to assemble it
Name | Type | Description |
---|---|---|
asset |
ImageUrlBuilderOptions | An asset-like shape defining ID, dimensions and extension |
project |
SanityProjectDetails | Project ID and dataset the image belongs to |
Returns: string
Defined in src/paths.ts:43
▸ getAssetDocumentId(src
: SanityAssetSource): string
Tries to resolve the asset document ID from any inferrable structure
Name | Type | Description |
---|---|---|
src |
SanityAssetSource | Input source (image/file object, asset, reference, id, url, path) |
Returns: string
Defined in src/resolve.ts:291
▸ getDefaultCrop(): SanityImageCrop
Returns cloned version of the default crop (prevents accidental mutations)
Name | Type |
---|
Returns: SanityImageCrop
Defined in src/resolve.ts:65
▸ getDefaultHotspot(): SanityImageHotspot
Returns cloned version of the default hotspot (prevents accidental mutations)
Name | Type |
---|
Returns: SanityImageHotspot
Defined in src/resolve.ts:72
▸ getExtension(src
: SanityAssetSource): string
Returns the file extension for a given asset
Name | Type | Description |
---|---|---|
src |
SanityAssetSource | Input source (file/image object, asset, reference, id, url, path) |
Returns: string
Defined in src/resolve.ts:108
▸ getFile(src
: SanityFileSource, project
: SanityProjectDetails): ResolvedSanityFile
Tries to resolve an file object with as much information as possible, from any inferrable structure (id, url, path, file object etc)
Name | Type | Description |
---|---|---|
src |
SanityFileSource | Input source (file object, asset, reference, id, url, path) |
project |
SanityProjectDetails | Project ID and dataset the file belongs to |
Returns: ResolvedSanityFile
Defined in src/resolve.ts:220
▸ getFileAsset(src
: SanityFileSource, project
: SanityProjectDetails): SanityFileAsset
Tries to resolve a (partial) file asset document with as much information as possible, from any inferrable structure (id, url, path, file object etc)
Name | Type | Description |
---|---|---|
src |
SanityFileSource | Input source (file object, asset, reference, id, url, path) |
project |
SanityProjectDetails | Project ID and dataset the file belongs to |
Returns: SanityFileAsset
Defined in src/resolve.ts:245
▸ getIdFromString(str
: string): string
Tries to cooerce a string (ID, URL or path) to an image asset ID
Name | Type | Description |
---|---|---|
str |
string | Input string (ID, URL or path) |
Returns: string
Defined in src/resolve.ts:333
▸ getImage(src
: SanityImageSource, project
: SanityProjectDetails): ResolvedSanityImage
Tries to resolve an image object with as much information as possible, from any inferrable structure (id, url, path, image object etc)
Name | Type | Description |
---|---|---|
src |
SanityImageSource | Input source (image object, asset, reference, id, url, path) |
project |
SanityProjectDetails | Project ID and dataset the image belongs to |
Returns: ResolvedSanityImage
Defined in src/resolve.ts:133
▸ getImageAsset(src
: SanityImageSource, project
: SanityProjectDetails): SanityImageAsset
Tries to resolve a (partial) image asset document with as much information as possible, from any inferrable structure (id, url, path, image object etc)
Name | Type | Description |
---|---|---|
src |
SanityImageSource | Input source (image object, asset, reference, id, url, path) |
project |
SanityProjectDetails | Project ID and dataset the image belongs to |
Returns: SanityImageAsset
Defined in src/resolve.ts:167
▸ getImageDimensions(src
: SanityImageSource): SanityImageDimensions
Returns the width, height and aspect ratio of a passed image asset, from any inferrable structure (id, url, path, asset document, image object etc)
Name | Type | Description |
---|---|---|
src |
SanityImageSource | Input source (image object, asset, reference, id, url, path) |
Returns: SanityImageDimensions
Defined in src/resolve.ts:84
▸ getProject(src
: SanityImageSource): SanityProjectDetails
Resolves project ID and dataset the image belongs to, based on full URL or path
Name | Type |
---|---|
src |
SanityImageSource |
Returns: SanityProjectDetails
Defined in src/resolve.ts:391
▸ getUrlFilename(url
: string): string
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
Name | Type | Description |
---|---|---|
url |
string | URL to get filename from |
Returns: string
Defined in src/paths.ts:163
▸ getUrlPath(url
: string): string
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
Name | Type | Description |
---|---|---|
url |
string | URL to get path name from |
Returns: string
Defined in src/paths.ts:131
▸ hasPath(urlOrPath
: string): boolean
Checks whether or not the given URL contains an asset path
Name | Type |
---|---|
urlOrPath |
string |
Returns: boolean
Defined in src/paths.ts:88
▸ idFromUrl(url
: string): string
Converts from a full asset URL to just the asset document ID
Name | Type | Description |
---|---|---|
url |
string | A full asset URL to convert |
Returns: string
Defined in src/resolve.ts:376
▸ isAssetFilename(filename
: string): boolean
Returns whether or not the passed filename is a valid file or image asset filename
Name | Type | Description |
---|---|---|
filename |
string | Filename to validate |
Returns: boolean
Defined in src/resolve.ts:439
▸ isAssetIdStub(stub
: unknown): stub is SanityAssetIdStub
Checks whether or not the given source is an asset ID stub
(an object containing an _id
property)
Name | Type | Description |
---|---|---|
stub |
unknown | Possible asset id stub |
Returns: stub is SanityAssetIdStub
Defined in src/types.ts:108
▸ isAssetObjectStub(stub
: unknown): stub is SanityAssetObjectStub
Checks whether or not the given source is an asset object stub
Name | Type | Description |
---|---|---|
stub |
unknown | Possible asset object stub |
Returns: stub is SanityAssetObjectStub
Defined in src/types.ts:251
▸ isAssetPathStub(stub
: unknown): stub is SanityAssetPathStub
Checks whether or not the given source is an asset path stub
(an object containing a path
property)
Name | Type | Description |
---|---|---|
stub |
unknown | Possible asset path stub |
Returns: stub is SanityAssetPathStub
Defined in src/types.ts:123
▸ isAssetUrlStub(stub
: unknown): stub is SanityAssetUrlStub
Checks whether or not the given source is an asset URL stub
(an object containing a url
property)
Name | Type | Description |
---|---|---|
stub |
unknown | Possible asset url stub |
Returns: stub is SanityAssetUrlStub
Defined in src/types.ts:138
▸ isFileAssetFilename(filename
: string): boolean
Returns whether or not the passed filename is a valid file asset filename
Name | Type | Description |
---|---|---|
filename |
string | Filename to validate |
Returns: boolean
Defined in src/resolve.ts:429
▸ isFileSource(src
: SanityAssetSource): src is SanityFileSource
Name | Type |
---|---|
src |
SanityAssetSource |
Returns: src is SanityFileSource
Defined in src/resolve.ts:443
▸ isImageAssetFilename(filename
: string): boolean
Returns whether or not the passed filename is a valid image asset filename
Name | Type | Description |
---|---|---|
filename |
string | Filename to validate |
Returns: boolean
Defined in src/resolve.ts:419
▸ isImageSource(src
: SanityAssetSource): src is SanityImageSource
Name | Type |
---|---|
src |
SanityAssetSource |
Returns: src is SanityImageSource
Defined in src/resolve.ts:448
▸ isReference(ref
: unknown): ref is SanityReference
Checks whether or not the given source is a Sanity reference (an object containing _ref string key)
Name | Type | Description |
---|---|---|
ref |
unknown | Possible reference |
Returns: ref is SanityReference
Defined in src/types.ts:93
▸ isSanityFileAsset(src
: unknown): src is SanityFileAsset
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
Name | Type | Description |
---|---|---|
src |
unknown | Source to check |
Returns: src is SanityFileAsset
Defined in src/types.ts:169
▸ isUnresolvableError(err
: Error): err is UnresolvableError
Checks whether or not an error instance is of type UnresolvableError
Name | Type | Description |
---|---|---|
err |
Error | Error to check for unresolvable error type |
Returns: err is UnresolvableError
Defined in src/utils.ts:36
▸ isValidFilename(filename
: string): boolean
Checks whether or not a given filename matches the expected Sanity asset filename pattern
Name | Type | Description |
---|---|---|
filename |
string | Filename to check for validity |
Returns: boolean
Defined in src/paths.ts:187
▸ parseAssetFilename(filename
: string): SanityAssetIdParts
Parses a Sanity asset filename into individual parts (type, id, extension, width, height)
Name | Type | Description |
---|---|---|
filename |
string | Filename to parse into named parts |
Returns: SanityAssetIdParts
Defined in src/parse.ts:77
▸ parseAssetId(documentId
: string): SanityAssetIdParts
Parses a Sanity asset document ID into individual parts (type, id, extension, width/height etc)
Name | Type | Description |
---|---|---|
documentId |
string | Document ID to parse into named parts |
Returns: SanityAssetIdParts
Defined in src/parse.ts:22
▸ parseFileAssetId(documentId
: string): SanityFileAssetIdParts
Parses a Sanity file asset document ID into individual parts (type, id, extension)
Name | Type | Description |
---|---|---|
documentId |
string | File asset document ID to parse into named parts |
Returns: SanityFileAssetIdParts
Defined in src/parse.ts:41
▸ parseImageAssetId(documentId
: string): SanityImageAssetIdParts
Parses a Sanity image asset document ID into individual parts (type, id, extension, width, height)
Name | Type | Description |
---|---|---|
documentId |
string | Image asset document ID to parse into named parts |
Returns: SanityImageAssetIdParts
Defined in src/parse.ts:59
▸ tryGetAssetDocumentId(src
: SanityAssetSource): string
Tries to resolve the asset document ID from any inferrable structure
Name | Type | Description |
---|---|---|
src |
SanityAssetSource | Input source (image/file object, asset, reference, id, url, path) |
Returns: string
Defined in src/resolve.ts:321
▸ tryGetAssetPath(src
: SanityAssetSource): string | undefined
Tries to get the asset path from a given asset source
Name | Type | Description |
---|---|---|
src |
SanityAssetSource | The source image to infer an asset path from |
Returns: string | undefined
Defined in src/paths.ts:98
▸ tryGetExtension(src
: SanityAssetSource): string
Returns the file extension for a given asset
Name | Type | Description |
---|---|---|
src |
SanityAssetSource | Input source (file/image object, asset, reference, id, url, path) |
Returns: string
Defined in src/resolve.ts:120
▸ tryGetFile(src
: SanityFileSource, project
: SanityProjectDetails): ResolvedSanityFile
Tries to resolve an file object with as much information as possible, from any inferrable structure (id, url, path, file object etc)
Name | Type | Description |
---|---|---|
src |
SanityFileSource | Input source (file object, asset, reference, id, url, path) |
project |
SanityProjectDetails | Project ID and dataset the file belongs to |
Returns: ResolvedSanityFile
Defined in src/resolve.ts:232
▸ tryGetFileAsset(src
: SanityFileSource, project
: SanityProjectDetails): SanityFileAsset
Tries to resolve a (partial) file asset document with as much information as possible, from any inferrable structure (id, url, path, file object etc)
Name | Type | Description |
---|---|---|
src |
SanityFileSource | Input source (file object, asset, reference, id, url, path) |
project |
SanityProjectDetails | Project ID and dataset the file belongs to |
Returns: SanityFileAsset
Defined in src/resolve.ts:280
▸ tryGetIdFromString(str
: string): string
Tries to cooerce a string (ID, URL or path) to an image asset ID
Name | Type | Description |
---|---|---|
str |
string | Input string (ID, URL or path) |
Returns: string
Defined in src/resolve.ts:368
▸ tryGetImage(src
: SanityImageSource, project
: SanityProjectDetails): ResolvedSanityImage
Tries to resolve an image object with as much information as possible, from any inferrable structure (id, url, path, image object etc)
Name | Type | Description |
---|---|---|
src |
SanityImageSource | Input source (image object, asset, reference, id, url, path) |
project |
SanityProjectDetails | Project ID and dataset the image belongs to |
Returns: ResolvedSanityImage
Defined in src/resolve.ts:154
▸ tryGetImageAsset(src
: SanityImageSource, project
: SanityProjectDetails): SanityImageAsset
Tries to resolve a (partial) image asset document with as much information as possible, from any inferrable structure (id, url, path, image object etc)
Name | Type | Description |
---|---|---|
src |
SanityImageSource | Input source (image object, asset, reference, id, url, path) |
project |
SanityProjectDetails | Project ID and dataset the image belongs to |
Returns: SanityImageAsset
Defined in src/resolve.ts:207
▸ tryGetImageDimensions(src
: SanityImageSource): SanityImageDimensions
Returns the width, height and aspect ratio of a passed image asset, from any inferrable structure (id, url, path, asset document, image object etc)
Name | Type | Description |
---|---|---|
src |
SanityImageSource | Input source (image object, asset, reference, id, url, path) |
Returns: SanityImageDimensions
Defined in src/resolve.ts:97
▸ tryGetProject(src
: SanityImageSource): SanityProjectDetails
Resolves project ID and dataset the image belongs to, based on full URL or path
Name | Type |
---|---|
src |
SanityImageSource |
Returns: SanityProjectDetails
Defined in src/resolve.ts:411
▸ tryGetUrlFilename(url
: string): string
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
Name | Type | Description |
---|---|---|
url |
string | URL to get filename from |
Returns: string
Defined in src/paths.ts:179
▸ tryGetUrlPath(url
: string): string
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
Name | Type | Description |
---|---|---|
url |
string | URL to get path name from |
Returns: string
Defined in src/paths.ts:152
MIT-licensed. See LICENSE.