Skip to content

Commit

Permalink
feat: add stegaClean method, deprecate vercelStegaCleanAll (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored May 2, 2024
1 parent aea84ce commit 2749586
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,11 @@ const debugClient = client.withConfig({
Removing stega from part of the result, available on [`@sanity/client/stega`]:

```ts
import {vercelStegaCleanAll} from '@sanity/client/stega'
import {stegaClean} from '@sanity/client/stega'
const result = await client.fetch('*[_type == "video"][0]')
// Remove stega from the payload sent to a third party library
const videoAsset = vercelStegaCleanAll(result.videoAsset)
const videoAsset = stegaClean(result.videoAsset)
```

#### Creating Studio edit intent links
Expand Down
2 changes: 1 addition & 1 deletion src/stega/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
export {encodeIntoResult} from './encodeIntoResult'
export {stegaEncodeSourceMap} from './stegaEncodeSourceMap'
export * from './types'
export {vercelStegaCleanAll} from './vercelStegaCleanAll'
export {stegaClean, vercelStegaCleanAll} from './vercelStegaCleanAll'

/**
* @deprecated -- Use `import {SanityClient} from '@sanity/client'` instead
Expand Down
21 changes: 12 additions & 9 deletions src/stega/vercelStegaCleanAll.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import {vercelStegaSplit} from '@vercel/stega'
import {vercelStegaClean} from '@vercel/stega'

/**
* Can take a `result` JSON from a `const {result} = client.fetch(query, params, {filterResponse: false})`
* and remove all stega-encoded data from it.
* @alpha
* @public
*/
export function vercelStegaCleanAll<Result = unknown>(result: Result): Result {
export function stegaClean<Result = unknown>(result: Result): Result {
try {
return JSON.parse(
JSON.stringify(result, (key, value) => {
if (typeof value !== 'string') return value
return vercelStegaSplit(value).cleaned
}),
)
return vercelStegaClean<Result>(result)
} catch {
return result
}
}

/**
* Can take a `result` JSON from a `const {result} = client.fetch(query, params, {filterResponse: false})`
* and remove all stega-encoded data from it.
* @alpha
* @deprecated Use `stegaClean` instead
*/
export const vercelStegaCleanAll = stegaClean
22 changes: 10 additions & 12 deletions test/stega/vercelStegaCleanAll.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {stegaClean} from '@sanity/client/stega'
import {vercelStegaCombine} from '@vercel/stega'
import {expect, test} from 'vitest'

import {vercelStegaCleanAll} from '../../src/stega/vercelStegaCleanAll'

test('it removes everything', () => {
const payload = {
foo: ['bar', 'baz'],
Expand All @@ -18,34 +17,33 @@ test('it removes everything', () => {
vercelStegaCombine('embedded', editInfo),
].join(' ')
expect(encoded).not.toEqual(payload)
expect(vercelStegaCleanAll(encoded)).toEqual(payload)
expect(stegaClean(encoded)).toEqual(payload)
})

test('it handles strings', () => {
const payload = 'foo'
const editInfo = JSON.stringify({origin: 'sanity.io', href: '/studio'})
const encoded = vercelStegaCombine(payload, editInfo)
expect(encoded).not.toEqual(payload)
expect(vercelStegaCleanAll(encoded)).toEqual(payload)
expect(stegaClean(encoded)).toEqual(payload)
})

test('it handles values that are not supported by JSON', () => {
expect(vercelStegaCleanAll(undefined)).toMatchInlineSnapshot(`undefined`)
expect(vercelStegaCleanAll(null)).toMatchInlineSnapshot(`null`)
expect(vercelStegaCleanAll(Symbol('foo'))).toMatchInlineSnapshot(`Symbol(foo)`)
expect(vercelStegaCleanAll(new Set([1, 2, 3]))).toMatchInlineSnapshot(`{}`)
expect(stegaClean(undefined)).toMatchInlineSnapshot(`undefined`)
expect(stegaClean(null)).toMatchInlineSnapshot(`null`)
expect(stegaClean(Symbol('foo'))).toMatchInlineSnapshot(`Symbol(foo)`)
expect(stegaClean(new Set([1, 2, 3]))).toMatchInlineSnapshot(`{}`)
expect(
vercelStegaCleanAll(
stegaClean(
new Map([
[0, 0],
[1, 1],
[2, 2],
]),
),
).toMatchInlineSnapshot(`{}`)
expect(
vercelStegaCleanAll([{foo: undefined, bar: null, baz: new Date('1995-12-17T02:24:00.000Z')}]),
).toMatchInlineSnapshot(`
expect(stegaClean([{foo: undefined, bar: null, baz: new Date('1995-12-17T02:24:00.000Z')}]))
.toMatchInlineSnapshot(`
[
{
"bar": null,
Expand Down

0 comments on commit 2749586

Please sign in to comment.