Skip to content

Commit

Permalink
fix(stega): merge stega options in .config() and .withConfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Nov 15, 2023
1 parent 07b343c commit ef2d282
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ export const initConfig = (
)
}

if ('stega' in newConfig && newConfig['stega'] !== undefined) {
if ('stega' in newConfig && newConfig['stega'] !== undefined && newConfig['stega'] !== false) {
throw new Error(
`It looks like you're using options meant for '@sanity/client/stega'. Make sure you're using the right import. Or set 'stega' in 'createClient' to 'undefined'.`,
`It looks like you're using options meant for '@sanity/client/stega'. Make sure you're using the right import. Or set 'stega' in 'createClient' to 'false'.`,
)
}

Expand Down
32 changes: 24 additions & 8 deletions src/stega/SanityStegaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ export class ObservableSanityStegaClient extends ObservableSanityClient {
return {...super.config(), stega: {...this.stegaConfig}}
}

super.config(newConfig)

const {stegaConfig} = splitConfig(newConfig)
const {clientConfig, stegaConfig} = splitConfig(newConfig)
super.config(clientConfig)

this.stegaConfig = initStegaConfig(stegaConfig, this.stegaConfig || {})
return this
Expand All @@ -66,7 +65,16 @@ export class ObservableSanityStegaClient extends ObservableSanityClient {
* @param newConfig - New client configuration properties, shallowly merged with existing configuration
*/
withConfig(newConfig?: Partial<ClientConfig>): ObservableSanityStegaClient {
return new ObservableSanityStegaClient(this.#httpRequest, {...this.config(), ...newConfig})
const thisConfig = this.config()
const {stegaConfig} = splitConfig(newConfig || {})
return new ObservableSanityStegaClient(this.#httpRequest, {
...thisConfig,
...newConfig,
stega: {
...(thisConfig.stega || {}),
...(stegaConfig || {}),
},
})
}

/**
Expand Down Expand Up @@ -177,9 +185,8 @@ export class SanityStegaClient extends SanityClient {
return {...super.config(), stega: {...this.stegaConfig}}
}

super.config(newConfig)

const {stegaConfig} = splitConfig(newConfig)
const {clientConfig, stegaConfig} = splitConfig(newConfig)
super.config(clientConfig)

this.stegaConfig = initStegaConfig(stegaConfig, {...(this.stegaConfig || {})})
return this
Expand All @@ -191,7 +198,16 @@ export class SanityStegaClient extends SanityClient {
* @param newConfig - New client configuration properties, shallowly merged with existing configuration
*/
withConfig(newConfig?: Partial<ClientStegaConfig>): SanityStegaClient {
return new SanityStegaClient(this.#httpRequest, {...this.config(), ...newConfig})
const thisConfig = this.config()
const {stegaConfig} = splitConfig(newConfig || {})
return new SanityStegaClient(this.#httpRequest, {
...thisConfig,
...newConfig,
stega: {
...(thisConfig.stega || {}),
...(stegaConfig || {}),
},
})
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/stega/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function splitConfig(config: ClientStegaConfig): {
stegaConfig: StegaConfig
} {
const {stega = {}, ...clientConfig} = config
return {clientConfig, stegaConfig: stega}
return {clientConfig, stegaConfig: typeof stega === 'boolean' ? {enabled: stega} : stega}
}

export const initStegaConfig = (
Expand Down
2 changes: 1 addition & 1 deletion src/stega/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type InitializedStegaConfig = Omit<StegaConfig, StegaConfigRequiredKeys>

/** @public */
export interface ClientStegaConfig extends ClientConfig {
stega?: StegaConfig
stega?: StegaConfig | boolean
}

/** @public */
Expand Down
97 changes: 97 additions & 0 deletions test/stega/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {createClient as createCoreClient} from '@sanity/client'
import {
ClientStegaConfig,
ContentSourceMap,
Expand Down Expand Up @@ -34,6 +35,51 @@ describe('@sanity/client/stega', async () => {
expect(client).toBeInstanceOf(SanityStegaClient)
})

test('config() returns a stega config property', () => {
const client = createClient({projectId: 'foo', dataset: 'bar'})
expect(client.config().stega).toMatchInlineSnapshot(`
{
"enabled": false,
"filter": [Function],
}
`)
})

test('withConfig merges stega', () => {
const client = createClient({projectId: 'foo', dataset: 'bar', stega: {studioUrl: '/studio'}})
expect(client.withConfig({stega: {enabled: true}}).config().stega.studioUrl).toBe('/studio')
})

test('the stega option accepts booleans as a shortcut to toggle `enabled`', () => {
const client1 = createClient({
projectId: 'foo',
dataset: 'bar',
stega: {enabled: true, studioUrl: '/studio'},
})
expect(client1.withConfig({stega: false}).config().stega.enabled).toBe(false)
const client2 = createClient({
projectId: 'foo',
dataset: 'bar',
stega: {enabled: false, studioUrl: '/studio'},
})
expect(client2.withConfig({stega: true}).config().stega.enabled).toBe(true)
})

test('config merges stega', () => {
const client = createClient({projectId: 'foo', dataset: 'bar', stega: {studioUrl: '/studio'}})
expect(client.config({stega: {enabled: true}}).config().stega.studioUrl).toBe('/studio')
})

test('the stega option accepts booleans as a shortcut to toggle `enabled`', () => {
const client = createClient({
projectId: 'foo',
dataset: 'bar',
stega: {enabled: true, studioUrl: '/studio'},
})
expect(client.config({stega: false}).config().stega.enabled).toBe(false)
expect(client.config({stega: true}).config().stega.enabled).toBe(true)
})

test.skipIf(isEdge)('it returns stega strings in the response', async () => {
const result = [{_id: 'njgNkngskjg', title: 'IPA', rating: 4, country: 'Norway'}]
const resultSourceMap = {
Expand Down Expand Up @@ -117,3 +163,54 @@ describe('@sanity/client/stega', async () => {
`)
})
})

describe('@sanity/client', () => {
test('throws an error if trying to use stega options that should use the stega client', () => {
expect(() =>
createCoreClient({
projectId: 'abc123',
// @ts-expect-error - we want to test that it throws an error
stega: {
enabled: true,
},
}),
).toThrowErrorMatchingInlineSnapshot(
'"It looks like you\'re using options meant for \'@sanity/client/stega\'. Make sure you\'re using the right import. Or set \'stega\' in \'createClient\' to \'false\'."',

Check warning on line 178 in test/stega/client.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint and test coverage

Replace `'"It·looks·like·you\'re·using·options·meant·for·\'@sanity/client/stega\'.·Make·sure·you\'re·using·the·right·import.·Or·set·\'stega\'·in·\'createClient\'·to·\'false\'."'` with `"\"It·looks·like·you're·using·options·meant·for·'@sanity/client/stega'.·Make·sure·you're·using·the·right·import.·Or·set·'stega'·in·'createClient'·to·'false'.\""`
)
expect(() =>
// @ts-expect-error - we want to test that it throws an error
createCoreClient({projectId: 'abc123', stega: null}),
).toThrowErrorMatchingInlineSnapshot(
'"It looks like you\'re using options meant for \'@sanity/client/stega\'. Make sure you\'re using the right import. Or set \'stega\' in \'createClient\' to \'false\'."',

Check warning on line 184 in test/stega/client.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint and test coverage

Replace `'"It·looks·like·you\'re·using·options·meant·for·\'@sanity/client/stega\'.·Make·sure·you\'re·using·the·right·import.·Or·set·\'stega\'·in·\'createClient\'·to·\'false\'."'` with `"\"It·looks·like·you're·using·options·meant·for·'@sanity/client/stega'.·Make·sure·you're·using·the·right·import.·Or·set·'stega'·in·'createClient'·to·'false'.\""`
)
})
test('allows passing stega: undefined', () => {
expect(() =>
createCoreClient({
projectId: 'abc123',
// @ts-expect-error - we want to test that it throws an error
stega: undefined,
}),
).not.toThrow()
})
test('allows passing stega: false', () => {
expect(() =>
createCoreClient({
projectId: 'abc123',
// @ts-expect-error - we want to test that it throws an error
stega: false,
}),
).not.toThrow()
})
test('disallows passing stega: true', () => {
expect(() =>
createCoreClient({
projectId: 'abc123',
// @ts-expect-error - we want to test that it throws an error
stega: true,
}),
).toThrowErrorMatchingInlineSnapshot(
'"It looks like you\'re using options meant for \'@sanity/client/stega\'. Make sure you\'re using the right import. Or set \'stega\' in \'createClient\' to \'false\'."',

Check warning on line 213 in test/stega/client.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint and test coverage

Replace `'"It·looks·like·you\'re·using·options·meant·for·\'@sanity/client/stega\'.·Make·sure·you\'re·using·the·right·import.·Or·set·\'stega\'·in·\'createClient\'·to·\'false\'."'` with `"\"It·looks·like·you're·using·options·meant·for·'@sanity/client/stega'.·Make·sure·you're·using·the·right·import.·Or·set·'stega'·in·'createClient'·to·'false'.\""`
)
})
})

0 comments on commit ef2d282

Please sign in to comment.