Skip to content

Commit

Permalink
fix(csm): remove unused intent resolve parameters (#397)
Browse files Browse the repository at this point in the history
fix(csm): remove optional intent resolve parameters
  • Loading branch information
stipsan committed Nov 13, 2023
1 parent 3b5e94d commit 00b5ffa
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 79 deletions.
10 changes: 0 additions & 10 deletions src/csm/createEditUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import type {CreateEditUrlOptions, EditIntentUrl, StudioBaseUrl} from './types'
export function createEditUrl(options: CreateEditUrlOptions): `${StudioBaseUrl}${EditIntentUrl}` {
const {
baseUrl,
projectId,
dataset,
workspace: _workspace = 'default',
tool: _tool = 'default',
id: _id,
Expand All @@ -19,12 +17,6 @@ export function createEditUrl(options: CreateEditUrlOptions): `${StudioBaseUrl}$
if (!baseUrl) {
throw new Error('baseUrl is required')
}
if (!projectId) {
throw new Error('projectId is required')
}
if (!dataset) {
throw new Error('dataset is required')
}
if (!path) {
throw new Error('path is required')
}
Expand All @@ -46,8 +38,6 @@ export function createEditUrl(options: CreateEditUrlOptions): `${StudioBaseUrl}$
// @TODO Using searchParams as a temporary workaround until `@sanity/overlays` can decode state from the path reliably
const searchParams = new URLSearchParams({
baseUrl,
projectId,
dataset,
id,
type,
path: stringifiedPath,
Expand Down
4 changes: 1 addition & 3 deletions src/csm/resolveEditInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ export function resolveEditInfo(options: ResolveEditInfoOptions): CreateEditUrlO
typeof options.studioUrl === 'function' ? options.studioUrl(sourceDoc) : options.studioUrl,
)
if (!baseUrl) return undefined
const {_id, _type, _dataset = options.projectId, _projectId = options.dataset} = sourceDoc
const {_id, _type} = sourceDoc
return {
baseUrl,
projectId: _projectId,
dataset: _dataset,
workspace,
tool,
id: _id,
Expand Down
4 changes: 1 addition & 3 deletions src/csm/resolveEditUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import type {ResolveEditUrlOptions} from './types'
export function resolveEditUrl(
options: ResolveEditUrlOptions,
): ReturnType<typeof createEditUrl> | undefined {
const {projectId, dataset, resultSourceMap, studioUrl} = options
const {resultSourceMap, studioUrl} = options
const resultPath = studioPathToJsonPath(options.resultPath)

const editInfo = resolveEditInfo({
projectId,
dataset,
resultPath,
resultSourceMap,
studioUrl,
Expand Down
5 changes: 1 addition & 4 deletions src/csm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ export type EditIntentUrl = `/intent/edit/id=${string};type=${string};path=${str
/** @internal */
export interface CreateEditUrlOptions {
baseUrl: string
projectId: string
dataset: string
workspace?: string
tool?: string
id: string
Expand All @@ -73,8 +71,7 @@ export interface CreateEditUrlOptions {
}

/** @alpha */
export interface ResolveEditInfoOptions
extends Pick<CreateEditUrlOptions, 'projectId' | 'dataset'> {
export interface ResolveEditInfoOptions {
studioUrl: StudioUrl | ResolveStudioUrl
resultSourceMap: ContentSourceMap
resultPath: ContentSourceMapParsedPath
Expand Down
12 changes: 2 additions & 10 deletions src/stega/SanityStegaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,7 @@ export class ObservableSanityStegaClient extends ObservableSanityClient {
.pipe(
map((res: Any) => {
const {result: _result, resultSourceMap} = res as RawQueryResponse<R>
const {projectId, dataset} = this.config()
const result = stegaEncodeSourceMap(_result, resultSourceMap, this.stegaConfig, {
projectId,
dataset,
})
const result = stegaEncodeSourceMap(_result, resultSourceMap, this.stegaConfig)
return originalFilterResponse ? result : {...res, result}
}),
)
Expand Down Expand Up @@ -255,11 +251,7 @@ export class SanityStegaClient extends SanityClient {
)
.then((res: Any) => {
const {result: _result, resultSourceMap} = res as RawQueryResponse<R>
const {projectId, dataset} = this.config()
const result = stegaEncodeSourceMap(_result, resultSourceMap, this.stegaConfig, {
projectId,
dataset,
})
const result = stegaEncodeSourceMap(_result, resultSourceMap, this.stegaConfig)
return originalFilterResponse ? result : {...res, result}
})
}
Expand Down
18 changes: 1 addition & 17 deletions src/stega/stegaEncodeSourceMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export function stegaEncodeSourceMap<Result = unknown>(
result: Result,
resultSourceMap: ContentSourceMap | undefined,
config: InitializedStegaConfig,
clientConfig: {projectId: string | undefined; dataset: string | undefined},
): Result {
const {filter, vercelStegaCombineSkip, logger, enabled} = config
if (!enabled) {
Expand Down Expand Up @@ -49,14 +48,6 @@ export function stegaEncodeSourceMap<Result = unknown>(
skipped: [],
}

const {projectId, dataset} = clientConfig
if (!projectId) {
throw new Error('Missing projectId')
}
if (!dataset) {
throw new Error('Missing dataset')
}

const resultWithStega = encodeIntoResult(
result,
resultSourceMap,
Expand Down Expand Up @@ -93,21 +84,14 @@ export function stegaEncodeSourceMap<Result = unknown>(
: config.studioUrl!,
)
if (!baseUrl) return value
const {
_id: id,
_type: type,
_dataset = clientConfig.projectId,
_projectId = clientConfig.dataset,
} = sourceDocument
const {_id: id, _type: type} = sourceDocument

return vercelStegaCombine(
value,
{
origin: 'sanity.io',
href: createEditUrl({
baseUrl,
projectId: _projectId!,
dataset: _dataset!,
workspace,
tool,
id,
Expand Down
12 changes: 4 additions & 8 deletions test/csm/createEditUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {createEditUrl} from '../../src/csm/createEditUrl'
import {parseJsonPath} from '../../src/csm/jsonPath'

const baseUrl = 'https://test.sanity.studio'
const projectId = 'abc123'
const dataset = 'production'
const workspace = 'staging'
const tool = 'content'
const id = 'drafts.homepage'
Expand All @@ -16,24 +14,22 @@ const cases = [
context: {baseUrl, workspace, tool, id, type},
path: parseJsonPath("$['foo'][?(@._key=='section-1')][0]['baz'][?(@._key=='section-2')]"),
expected:
'https://test.sanity.studio/staging/content/intent/edit/id=homepage;type=page;path=foo[_key=="section-1"][0].baz[_key=="section-2"]?baseUrl=https://test.sanity.studio&projectId=abc123&dataset=production&id=homepage&type=page&path=foo[_key=="section-1"][0].baz[_key=="section-2"]&workspace=staging&tool=content',
'https://test.sanity.studio/staging/content/intent/edit/id=homepage;type=page;path=foo[_key=="section-1"][0].baz[_key=="section-2"]?baseUrl=https://test.sanity.studio&id=homepage&type=page&path=foo[_key=="section-1"][0].baz[_key=="section-2"]&workspace=staging&tool=content',
},
{
context: {baseUrl: '/', id, type},
path: parseJsonPath("$['foo'][?(@._key=='section-1')][0]"),
expected:
'/intent/edit/id=homepage;type=page;path=foo[_key=="section-1"][0]?baseUrl=/&projectId=abc123&dataset=production&id=homepage&type=page&path=foo[_key=="section-1"][0]',
'/intent/edit/id=homepage;type=page;path=foo[_key=="section-1"][0]?baseUrl=/&id=homepage&type=page&path=foo[_key=="section-1"][0]',
},
{
context: {baseUrl: '/', workspace, tool, id, type},
path: parseJsonPath("$['foo'][?(@._key=='section-1')][0]"),
expected:
'/staging/content/intent/edit/id=homepage;type=page;path=foo[_key=="section-1"][0]?baseUrl=/&projectId=abc123&dataset=production&id=homepage&type=page&path=foo[_key=="section-1"][0]&workspace=staging&tool=content',
'/staging/content/intent/edit/id=homepage;type=page;path=foo[_key=="section-1"][0]?baseUrl=/&id=homepage&type=page&path=foo[_key=="section-1"][0]&workspace=staging&tool=content',
},
]

test.each(cases)('$expected', ({context, path, expected}) => {
expect(decodeURIComponent(createEditUrl({projectId, dataset, ...context, path}))).toEqual(
expected,
)
expect(decodeURIComponent(createEditUrl({...context, path}))).toEqual(expected)
})
10 changes: 3 additions & 7 deletions test/csm/resolveEditUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {type ContentSourceMap, resolveEditUrl} from '@sanity/client/csm'
import {expect, test} from 'vitest'

const projectId = 'abc123'
const dataset = 'production'
const mock = {
query:
'{\n "products": *[_type == "product" && defined(slug.current)]{\n _id,\n title,\n description,\n slug,\n "media": media[0]\n },\n "siteSettings": *[_id == "siteSettings"][0]{\n title,\n copyrightText\n }\n}',
Expand Down Expand Up @@ -503,19 +501,19 @@ const cases = [
path: 'products[0].title',
studioUrl: 'https://test.sanity.studio',
expected:
'https://test.sanity.studio/intent/edit/id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=title?baseUrl=https://test.sanity.studio&projectId=production&dataset=abc123&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=title',
'https://test.sanity.studio/intent/edit/id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=title?baseUrl=https://test.sanity.studio&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=title',
},
{
path: 'products[0].media.alt',
studioUrl: '/',
expected:
'/intent/edit/id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=media[_key=="cee5fbb69da2"].alt?baseUrl=/&projectId=production&dataset=abc123&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=media[_key=="cee5fbb69da2"].alt',
'/intent/edit/id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=media[_key=="cee5fbb69da2"].alt?baseUrl=/&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=media[_key=="cee5fbb69da2"].alt',
},
{
path: 'products[0].description[0].children[0].text',
studioUrl: '/',
expected:
'/intent/edit/id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=description[0].children[0].text?baseUrl=/&projectId=production&dataset=abc123&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=description[0].children[0].text',
'/intent/edit/id=462efcc6-3c8b-47c6-8474-5544e1a4acde;type=product;path=description[0].children[0].text?baseUrl=/&id=462efcc6-3c8b-47c6-8474-5544e1a4acde&type=product&path=description[0].children[0].text',
},
]

Expand All @@ -524,8 +522,6 @@ test.each(cases)('resolveEditUrl $path', ({path, studioUrl, expected}) => {
expect(
decodeURIComponent(
resolveEditUrl({
projectId,
dataset,
studioUrl,
resultSourceMap,
resultPath: path,
Expand Down
6 changes: 3 additions & 3 deletions test/stega/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ describe('@sanity/client/stega', async () => {
expect(vercelStegaSplit(res[0].title).cleaned).toBe(result[0].title)
expect(vercelStegaDecode(res[0].title)).toMatchInlineSnapshot(`
{
"href": "/studio/intent/edit/id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fstudio&projectId=foo&dataset=bf1942&id=njgNkngskjg&type=beer&path=title",
"href": "/studio/intent/edit/id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=title",
"origin": "sanity.io",
}
`)
expect(vercelStegaDecodeAll(JSON.stringify(res))).toMatchInlineSnapshot(`
[
{
"href": "/studio/intent/edit/id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fstudio&projectId=foo&dataset=bf1942&id=njgNkngskjg&type=beer&path=title",
"href": "/studio/intent/edit/id=njgNkngskjg;type=beer;path=title?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=title",
"origin": "sanity.io",
},
{
"href": "/studio/intent/edit/id=njgNkngskjg;type=beer;path=country?baseUrl=%2Fstudio&projectId=foo&dataset=bf1942&id=njgNkngskjg&type=beer&path=country",
"href": "/studio/intent/edit/id=njgNkngskjg;type=beer;path=country?baseUrl=%2Fstudio&id=njgNkngskjg&type=beer&path=country",
"origin": "sanity.io",
},
]
Expand Down
21 changes: 7 additions & 14 deletions test/stega/stegaEncodeSourceMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {defaultStegaConfig} from '../../src/stega/config'
import {stegaEncodeSourceMap} from '../../src/stega/stegaEncodeSourceMap'
import type {ContentSourceMap, Logger} from '../../src/stega/types'

const projectId = 'abc123'
const dataset = 'production'
const mock = {
query:
'{\n "products": *[_type == "product" && defined(slug.current)]{\n _id,\n title,\n description,\n slug,\n "media": media[0]\n },\n "siteSettings": *[_id == "siteSettings"][0]{\n title,\n copyrightText\n }\n}',
Expand Down Expand Up @@ -517,18 +515,13 @@ test.each(cases)('resolveEditUrl $studioUrl', ({studioUrl}) => {
log: vi.fn(),
table: vi.fn(),
} satisfies Logger
const encoded = stegaEncodeSourceMap(
mock.result,
mock.resultSourceMap,
{
enabled: true,
studioUrl,
filter: defaultStegaConfig.filter!,
vercelStegaCombineSkip: defaultStegaConfig.vercelStegaCombineSkip!,
logger,
},
{projectId, dataset},
)
const encoded = stegaEncodeSourceMap(mock.result, mock.resultSourceMap, {
enabled: true,
studioUrl,
filter: defaultStegaConfig.filter!,
vercelStegaCombineSkip: defaultStegaConfig.vercelStegaCombineSkip!,
logger,
})
expect(
vercelStegaDecodeAll(JSON.stringify(encoded)).map(
({href}: any) => decodeURIComponent(href).split('?')[0],
Expand Down

0 comments on commit 00b5ffa

Please sign in to comment.