UNPKG

@sanity/client

Version:

Client for retrieving, creating and patching data from Sanity.io

1 lines 6.95 kB
{"version":3,"file":"stega.cjs","sources":["../src/stega/stegaBrand.ts","../src/stega/index.ts"],"sourcesContent":["import type {Any, ClientReturn} from '@sanity/client'\n\n/**\n * A string that might contain stega-encoded data, and is unsafe to compare against string literals\n * until it's been cleaned with `stegaClean()`.\n *\n * The type intersects a template literal that has a human readable suffix, with a brand property\n * that holds the original string type:\n * - The suffix makes TypeScript report \"This comparison appears to be unintentional\" (TS2367) when\n * the string is compared to a literal (`===`, `switch` cases), and makes it unassignable to\n * literal unions like `'left' | 'right'`, while keeping it assignable to `string` so rendering,\n * interpolation and string methods keep working. The suffix is a type-level fiction, the runtime\n * value never ends with that text.\n * - The brand property preserves the original type so `stegaClean()` can recover it exactly. It's\n * a string-keyed property rather than a `unique symbol` so that branded types stay structurally\n * compatible across duplicated copies of `@sanity/client` in `node_modules`. It only exists in\n * the type system and is never present at runtime.\n *\n * @beta\n */\nexport type StegaString<T extends string = string> =\n `${T} (may contain hidden stega characters)` & {\n readonly ' stegaBrand': T\n }\n\n/**\n * Deeply brands the string properties of a query result as `StegaString`, marking them as\n * potentially containing stega-encoded data.\n *\n * String properties that the stega encoder is guaranteed to never encode keep their plain type:\n * - keys starting with `_` (`_id`, `_type`, `_createdAt`, `_updatedAt`, `_key`, `_ref`, `_rev` and\n * so on), which also preserves discriminated union narrowing on `_type`\n * - `slug.current` patterns: a `current` key directly under a `slug` key, as well as string valued\n * `slug` keys (covering `\"slug\": slug.current` projections)\n * - Portable Text internals: the encoder only visits `children` of `_type: 'block'` objects and\n * `text` of `_type: 'span'` objects, so properties like `style`, `listItem` and `marks` stay\n * plain\n * - symbol-keyed properties (like TypeGen's `internalGroqTypeReferenceTo` reference marker): JSON\n * can't contain symbol keys, so they only exist in the type system and are never encoded\n *\n * Every other string is assumed to be \"poisoned\", even if the runtime `filter` happens to skip it\n * (URLs, dates, keys ending in `Id`, denylisted keys). Being conservative is safe: the worst case\n * is a redundant `stegaClean()` call, whereas under-branding would hide real bugs.\n *\n * The `Key` and `ParentKey` type parameters track the key the current value is found under, and\n * the key of the object containing it, they're only used internally during recursion.\n *\n * @beta\n */\nexport type StegaBranded<\n T,\n Key extends PropertyKey = number,\n ParentKey extends PropertyKey = number,\n> = 0 extends 1 & T\n ? T\n : T extends string\n ? T extends {readonly ' stegaBrand': string}\n ? T\n : Key extends `_${string}` | 'slug' | symbol\n ? T\n : Key extends 'current'\n ? ParentKey extends 'slug'\n ? T\n : StegaString<T>\n : StegaString<T>\n : T extends number | bigint | boolean | null | undefined\n ? T\n : T extends Date | RegExp | ((...args: never[]) => unknown)\n ? T\n : T extends readonly unknown[]\n ? {[Index in keyof T]: StegaBranded<T[Index], number, number>}\n : T extends {_type: 'block'}\n ? {[K in keyof T]: K extends 'children' ? StegaBranded<T[K], K, Key> : T[K]}\n : T extends {_type: 'span'}\n ? {[K in keyof T]: K extends 'text' ? StegaBranded<T[K], K, Key> : T[K]}\n : T extends object\n ? {[K in keyof T]: StegaBranded<T[K], K, Key>}\n : T\n\n/**\n * Drop-in replacement for `ClientReturn` that brands the result strings as `StegaString`, for use\n * as the first generic of `client.fetch` when stega is enabled:\n * ```ts\n * import {createClient} from '@sanity/client'\n * import type {ClientReturnStega} from '@sanity/client/stega'\n *\n * const query = '*[_type == \"post\"][0]'\n * const post = await client.fetch<ClientReturnStega<typeof query>>(query)\n * ```\n * When the query is registered in the `SanityQueries` interface (for example by `sanity typegen`),\n * the result type is looked up and deeply branded with `StegaBranded`. Otherwise it falls back to\n * `Fallback`, which defaults to `any`, just like `ClientReturn`.\n *\n * @beta\n */\nexport type ClientReturnStega<GroqString extends string, Fallback = Any> = StegaBranded<\n ClientReturn<GroqString, Fallback>\n>\n\n/**\n * Marks strings in an already fetched query result as potentially containing stega-encoded data,\n * by re-typing them as `StegaString`. Comparing branded strings to string literals is a type error\n * until they're cleaned with `stegaClean()`, which recovers the original type.\n *\n * This is an identity function, it only changes the type of the input, not its value.\n * Prefer passing `ClientReturnStega` as the first generic to `client.fetch` when possible, and use\n * this function for data that has already been fetched.\n *\n * @beta\n */\nexport function stegaBrand<Result>(result: Result): StegaBranded<Result> {\n return result as StegaBranded<Result>\n}\n","export * from '@sanity/client'\nimport {\n createClient as originalCreateClient,\n ObservableSanityClient,\n requester as originalRequester,\n SanityClient,\n} from '@sanity/client'\n\nexport {encodeIntoResult} from './encodeIntoResult'\nexport {type ClientReturnStega, stegaBrand, type StegaBranded, type StegaString} from './stegaBrand'\nexport {stegaClean, type StegaCleaned, vercelStegaCleanAll} from './stegaClean'\nexport {stegaEncodeSourceMap} from './stegaEncodeSourceMap'\nexport * from './types'\n\n/**\n * @deprecated -- Use `import {SanityClient} from '@sanity/client'` instead\n * @public\n */\nexport class SanityStegaClient extends SanityClient {}\n\n/**\n * @deprecated -- Use `import {ObservableSanityClient} from '@sanity/client'` instead\n * @public\n */\nexport class ObservableSanityStegaClient extends ObservableSanityClient {}\n\n/**\n * @deprecated -- Use `import {requester} from '@sanity/client'` instead\n * @public\n */\nexport const requester = originalRequester\n\n/**\n * @deprecated -- Use `import {createClient} from '@sanity/client'` instead\n * @public\n */\nexport const createClient = originalCreateClient\n"],"names":["SanityClient","ObservableSanityClient","originalRequester","originalCreateClient"],"mappings":";;;AA8GO,SAAS,WAAmB,QAAsC;AACvE,SAAO;AACT;AC9FO,MAAM,0BAA0BA,OAAAA,aAAa;AAAC;AAM9C,MAAM,oCAAoCC,OAAAA,uBAAuB;AAAC;AAMlE,MAAM,YAAYC,OAAAA,WAMZ,eAAeC,OAAAA;;;;;;;;;;;;;;;;;;"}