Skip to content

Commit

Permalink
fix: check if @sanity/preview-kit/client is incorrectly setup
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jun 29, 2023
1 parent 7b477e6 commit 2507638
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ export const initConfig = (
throw new Error('Configuration must contain `projectId`')
}

if (
'encodeSourceMapAtPath' in newConfig ||
'encodeSourceMap' in newConfig ||
'studioUrl' in newConfig ||
'logger' in newConfig
) {
throw new Error(
`It looks like you're using options meant for '@sanity/preview-kit/client', such as 'encodeSourceMapAtPath', 'encodeSourceMap', 'studioUrl' and 'logger'. Make sure you're using the right import.`
)
}

const isBrowser = typeof window !== 'undefined' && window.location && window.location.hostname
const isLocalhost = isBrowser && isLocal(window.location.hostname)

Expand Down
24 changes: 24 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ describe('client', async () => {
expect(() => createClient({})).toThrow(/projectId/)
})

test('throws if encodeSourceMapAtPath is provided', () => {
// @ts-expect-error - we want to test that it throws an error
expect(() => createClient({projectId: 'abc123', encodeSourceMapAtPath: () => true})).toThrow(
/encodeSourceMapAtPath/
)
})

test('throws if encodeSourceMap is provided', () => {
// @ts-expect-error - we want to test that it throws an error
expect(() => createClient({projectId: 'abc123', encodeSourceMap: true})).toThrow(
/encodeSourceMap/
)
})

test('throws if studioUrl is provided', () => {
// @ts-expect-error - we want to test that it throws an error
expect(() => createClient({projectId: 'abc123', studioUrl: '/studio'})).toThrow(/studioUrl/)
})

test('throws if logger is provided', () => {
// @ts-expect-error - we want to test that it throws an error
expect(() => createClient({projectId: 'abc123', logger: console})).toThrow(/logger/)
})

test('throws on invalid project ids', () => {
expect(() => createClient({projectId: '*foo*'})).toThrow(/projectId.*?can only contain/i)
})
Expand Down

0 comments on commit 2507638

Please sign in to comment.