Skip to content

Commit

Permalink
fix: show a migration error when using the default export (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Feb 2, 2023
1 parent 775b5b5 commit adb582e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
26 changes: 26 additions & 0 deletions runtimes/node/test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,30 @@ test('top-level imports', async (t) => {
const esm = Object.keys(await import('@sanity/client'))
assert.deepEqual(cjs, esm)
})

await t.test('throws a deprecation error on the default export', () => {
const {default: createClient} = require('@sanity/client')

assert.throws(
() => {
createClient()
},
{
name: /^TypeError$/,
message: /deprecated/,
}
)

const {default: SanityClient} = require('@sanity/client')

assert.throws(
() => {
new SanityClient()
},
{
name: /^TypeError$/,
message: /deprecated/,
}
)
})
})
31 changes: 30 additions & 1 deletion runtimes/node/test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import test from 'node:test'
import assert from 'node:assert/strict'

import {createClient, Patch, Transaction, ClientError, ServerError, requester} from '@sanity/client'
import deprecatedClient, {
createClient,
Patch,
Transaction,
ClientError,
ServerError,
requester,
} from '@sanity/client'
import pkg from '@sanity/client/package.json' assert {type: 'json'}

test('top-level imports', async (t) => {
Expand All @@ -18,4 +25,26 @@ test('top-level imports', async (t) => {
const {version} = pkg
assert.equal(typeof version, 'string')
})

await t.test('throws a deprecation error on the default export', () => {
assert.throws(
() => {
deprecatedClient()
},
{
name: /^TypeError$/,
message: /deprecated/,
}
)

assert.throws(
() => {
new deprecatedClient()
},
{
name: /^TypeError$/,
message: /deprecated/,
}
)
})
})
2 changes: 2 additions & 0 deletions src/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export const requester = httpRequest.defaultRequester

/** @public */
export const createClient = (config: ClientConfig) => new SanityClient(httpRequest, config)

export {migrationNotice as default} from './migrationNotice'
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export const requester = httpRequest.defaultRequester

/** @public */
export const createClient = (config: ClientConfig) => new SanityClient(httpRequest, config)

export {migrationNotice as default} from './migrationNotice'
9 changes: 9 additions & 0 deletions src/migrationNotice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @public
* @deprecated Use the named export `createClient` instead of the `default` export
*/
export function migrationNotice() {
throw new TypeError(
'The default export of @sanity/client has been deprecated. Use the named export `createClient` instead'
)
}
4 changes: 3 additions & 1 deletion test/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ describe('pkg.exports["."]', () => {
const browser = await import('../src/index.browser')
expect(Object.keys(source)).toEqual(Object.keys(browser))
})
test('default exports should not be used', async () => {
// eslint-disable-next-line no-warning-comments
// @TODO disabling this test until we no longer have the migrationNotice.ts
test.skip('default exports should not be used', async () => {
await expect(
import('../src/index'),
`src/index.ts shouldn't have a default export`
Expand Down

0 comments on commit adb582e

Please sign in to comment.