From 1b1fb16371ad323f7bd05d2086acea8a5bdd8237 Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Thu, 9 Feb 2023 17:39:59 +0100 Subject: [PATCH] fix: add support for using the default export (#117) --- runtimes/node/test.cjs | 3 +++ runtimes/node/test.mjs | 12 ++++-------- src/config.ts | 2 +- src/data/dataMethods.ts | 8 ++++---- src/data/encodeQueryString.ts | 2 +- src/data/listen.ts | 4 ++-- src/data/patch.ts | 2 +- src/generateHelpUrl.ts | 2 +- src/http/requestOptions.ts | 2 +- src/index.browser.ts | 10 +++++++++- src/index.ts | 10 +++++++++- src/migrationNotice.ts | 9 --------- src/util/getSelection.ts | 2 +- src/util/once.ts | 2 +- src/util/pick.ts | 2 +- src/warnings.ts | 8 ++++++-- test/encodeQueryString.test.ts | 14 +++++++------- 17 files changed, 52 insertions(+), 42 deletions(-) delete mode 100644 src/migrationNotice.ts diff --git a/runtimes/node/test.cjs b/runtimes/node/test.cjs index 7f558ca3..89c2ac17 100644 --- a/runtimes/node/test.cjs +++ b/runtimes/node/test.cjs @@ -30,6 +30,8 @@ test('top-level imports', async (t) => { assert.deepEqual(cjs, esm) }) + // @TODO re-enable in v6 + /* await t.test('throws a deprecation error on the default export', () => { const {default: createClient} = require('@sanity/client') @@ -55,4 +57,5 @@ test('top-level imports', async (t) => { } ) }) + // */ }) diff --git a/runtimes/node/test.mjs b/runtimes/node/test.mjs index dc4b800b..b48e9bf2 100644 --- a/runtimes/node/test.mjs +++ b/runtimes/node/test.mjs @@ -1,14 +1,7 @@ import test from 'node:test' import assert from 'node:assert/strict' -import deprecatedClient, { - createClient, - Patch, - Transaction, - ClientError, - ServerError, - requester, -} from '@sanity/client' +import {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) => { @@ -26,6 +19,8 @@ test('top-level imports', async (t) => { assert.equal(typeof version, 'string') }) + // @TODO re-enable in v6 + /* await t.test('throws a deprecation error on the default export', () => { assert.throws( () => { @@ -47,4 +42,5 @@ test('top-level imports', async (t) => { } ) }) + // */ }) diff --git a/src/config.ts b/src/config.ts index 9262abeb..56b8d4e9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,4 +1,4 @@ -import generateHelpUrl from './generateHelpUrl' +import {generateHelpUrl} from './generateHelpUrl' import type {ClientConfig, InitializedClientConfig} from './types' import * as validate from './validators' import * as warnings from './warnings' diff --git a/src/data/dataMethods.ts b/src/data/dataMethods.ts index e2183959..c7c05e31 100644 --- a/src/data/dataMethods.ts +++ b/src/data/dataMethods.ts @@ -1,7 +1,7 @@ import {type MonoTypeOperatorFunction, Observable} from 'rxjs' import {filter, map} from 'rxjs/operators' -import getRequestOptions from '../http/requestOptions' +import {requestOptions} from '../http/requestOptions' import type {ObservableSanityClient, SanityClient} from '../SanityClient' import type { AllDocumentIdsMutationOptions, @@ -25,10 +25,10 @@ import type { SingleMutationResult, UnfilteredResponseQueryOptions, } from '../types' -import getSelection from '../util/getSelection' +import {getSelection} from '../util/getSelection' import * as validate from '../validators' import * as validators from '../validators' -import encodeQueryString from './encodeQueryString' +import {encodeQueryString} from './encodeQueryString' import {ObservablePatch, Patch} from './patch' import {ObservableTransaction, Transaction} from './transaction' @@ -301,7 +301,7 @@ export function _requestObservable( options.query = {tag: validate.requestTag(tag), ...options.query} } - const reqOptions = getRequestOptions( + const reqOptions = requestOptions( config, Object.assign({}, options, { url: _getUrl(client, uri, useCdn), diff --git a/src/data/encodeQueryString.ts b/src/data/encodeQueryString.ts index 6af2fddb..ebfca41b 100644 --- a/src/data/encodeQueryString.ts +++ b/src/data/encodeQueryString.ts @@ -1,6 +1,6 @@ import type {Any, QueryParams} from '../types' -export default ({ +export const encodeQueryString = ({ query, params = {}, options = {}, diff --git a/src/data/listen.ts b/src/data/listen.ts index f0b7a7d6..30a0810d 100644 --- a/src/data/listen.ts +++ b/src/data/listen.ts @@ -4,9 +4,9 @@ import {Observable} from 'rxjs' import type {ObservableSanityClient, SanityClient} from '../SanityClient' import type {Any, ListenEvent, ListenOptions, MutationEvent, QueryParams} from '../types' import defaults from '../util/defaults' -import pick from '../util/pick' +import {pick} from '../util/pick' import {_getDataUrl} from './dataMethods' -import encodeQueryString from './encodeQueryString' +import {encodeQueryString} from './encodeQueryString' // Limit is 16K for a _request_, eg including headers. Have to account for an // unknown range of headers, but an average EventSource request from Chrome seems diff --git a/src/data/patch.ts b/src/data/patch.ts index f68a3d5f..cc198a7a 100644 --- a/src/data/patch.ts +++ b/src/data/patch.ts @@ -16,7 +16,7 @@ import type { SanityDocument, SingleMutationResult, } from '../types' -import getSelection from '../util/getSelection' +import {getSelection} from '../util/getSelection' import {validateInsert, validateObject} from '../validators' /** @internal */ diff --git a/src/generateHelpUrl.ts b/src/generateHelpUrl.ts index 2b8daec5..808e3f4f 100644 --- a/src/generateHelpUrl.ts +++ b/src/generateHelpUrl.ts @@ -1,5 +1,5 @@ const BASE_URL = 'https://www.sanity.io/help/' -export default function generateHelpUrl(slug: string) { +export function generateHelpUrl(slug: string) { return BASE_URL + slug } diff --git a/src/http/requestOptions.ts b/src/http/requestOptions.ts index f20fc38e..35ab29ec 100644 --- a/src/http/requestOptions.ts +++ b/src/http/requestOptions.ts @@ -4,7 +4,7 @@ import type {Any} from '../types' const projectHeader = 'X-Sanity-Project-ID' -export default (config: Any, overrides: Any = {}): Omit => { +export function requestOptions(config: Any, overrides: Any = {}): Omit { const headers: Any = {} const token = overrides.token || config.token diff --git a/src/index.browser.ts b/src/index.browser.ts index c1013e6f..3a467bea 100644 --- a/src/index.browser.ts +++ b/src/index.browser.ts @@ -2,6 +2,7 @@ import envMiddleware from './http/browserMiddleware' import {defineHttpRequest} from './http/request' import {SanityClient} from './SanityClient' import type {ClientConfig} from './types' +import {printNoDefaultExport} from './warnings' export * from './data/patch' export * from './data/transaction' @@ -17,4 +18,11 @@ export const requester = httpRequest.defaultRequester /** @public */ export const createClient = (config: ClientConfig) => new SanityClient(httpRequest, config) -export {migrationNotice as default} from './migrationNotice' +/** + * @public + * @deprecated Use the named export `createClient` instead of the `default` export + */ +export default function deprecatedCreateClient(config: ClientConfig) { + printNoDefaultExport() + return new SanityClient(httpRequest, config) +} diff --git a/src/index.ts b/src/index.ts index c3fba5ec..67cf69d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import envMiddleware from './http/nodeMiddleware' import {defineHttpRequest} from './http/request' import {SanityClient} from './SanityClient' import type {ClientConfig} from './types' +import {printNoDefaultExport} from './warnings' export * from './data/patch' export * from './data/transaction' @@ -17,4 +18,11 @@ export const requester = httpRequest.defaultRequester /** @public */ export const createClient = (config: ClientConfig) => new SanityClient(httpRequest, config) -export {migrationNotice as default} from './migrationNotice' +/** + * @public + * @deprecated Use the named export `createClient` instead of the `default` export + */ +export default function deprecatedCreateClient(config: ClientConfig) { + printNoDefaultExport() + return new SanityClient(httpRequest, config) +} diff --git a/src/migrationNotice.ts b/src/migrationNotice.ts deleted file mode 100644 index 339060b3..00000000 --- a/src/migrationNotice.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * @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' - ) -} diff --git a/src/util/getSelection.ts b/src/util/getSelection.ts index c57e0798..8bd93ab9 100644 --- a/src/util/getSelection.ts +++ b/src/util/getSelection.ts @@ -1,6 +1,6 @@ import type {MutationSelection} from '../types' -export default function getSelection(sel: unknown): MutationSelection { +export function getSelection(sel: unknown): MutationSelection { if (typeof sel === 'string' || Array.isArray(sel)) { return {id: sel} } diff --git a/src/util/once.ts b/src/util/once.ts index ed2fc7ad..2f26384d 100644 --- a/src/util/once.ts +++ b/src/util/once.ts @@ -1,6 +1,6 @@ import type {Any} from '../types' -export default (fn: Any) => { +export function once(fn: Any) { let didCall = false let returnValue: Any return (...args: Any[]) => { diff --git a/src/util/pick.ts b/src/util/pick.ts index 23870d70..86c1662b 100644 --- a/src/util/pick.ts +++ b/src/util/pick.ts @@ -1,6 +1,6 @@ import {Any} from '../types' -export default (obj: Any, props: Any) => +export const pick = (obj: Any, props: Any) => props.reduce((selection: Any, prop: Any) => { if (typeof obj[prop] === 'undefined') { return selection diff --git a/src/warnings.ts b/src/warnings.ts index 2f98b435..72568d29 100644 --- a/src/warnings.ts +++ b/src/warnings.ts @@ -1,6 +1,6 @@ -import generateHelpUrl from './generateHelpUrl' +import {generateHelpUrl} from './generateHelpUrl' import {Any} from './types' -import once from './util/once' +import {once} from './util/once' const createWarningPrinter = (message: string[]) => // eslint-disable-next-line no-console @@ -24,3 +24,7 @@ export const printNoApiVersionSpecifiedWarning = createWarningPrinter([ 'Using the Sanity client without specifying an API version is deprecated.', `See ${generateHelpUrl('js-client-api-version')}`, ]) + +export const printNoDefaultExport = createWarningPrinter([ + 'The default export of @sanity/client has been deprecated. Use the named export `createClient` instead', +]) diff --git a/test/encodeQueryString.test.ts b/test/encodeQueryString.test.ts index 391f42b0..9ce635bd 100644 --- a/test/encodeQueryString.test.ts +++ b/test/encodeQueryString.test.ts @@ -1,37 +1,37 @@ import {expect, test} from 'vitest' -import encode from '../src/data/encodeQueryString' +import {encodeQueryString} from '../src/data/encodeQueryString' test('can encode basic query without parameters', () => { const query = 'gamedb.game[maxPlayers == 64]' - expect(encode({query})).toEqual('?query=gamedb.game%5BmaxPlayers+%3D%3D+64%5D') + expect(encodeQueryString({query})).toEqual('?query=gamedb.game%5BmaxPlayers+%3D%3D+64%5D') }) test('can encode queries with basic numeric parameters', () => { const query = 'gamedb.game[maxPlayers == $maxPlayers && score == $score]' - expect(encode({query, params: {maxPlayers: 64, score: 3.45678}})).toEqual( + expect(encodeQueryString({query, params: {maxPlayers: 64, score: 3.45678}})).toEqual( '?query=gamedb.game%5BmaxPlayers+%3D%3D+%24maxPlayers+%26%26+score+%3D%3D+%24score%5D&%24maxPlayers=64&%24score=3.45678' ) }) test('can encode queries with basic string parameters', () => { const query = 'gamedb.game[name == $name]' - expect(encode({query, params: {name: 'foobar'}})).toEqual( + expect(encodeQueryString({query, params: {name: 'foobar'}})).toEqual( '?query=gamedb.game%5Bname+%3D%3D+%24name%5D&%24name=%22foobar%22' ) }) test('can encode queries with booleans', () => { const query = 'gamedb.game[isReleased == $released]' - expect(encode({query, params: {released: true}})).toEqual( + expect(encodeQueryString({query, params: {released: true}})).toEqual( '?query=gamedb.game%5BisReleased+%3D%3D+%24released%5D&%24released=true' ) }) test('handles options', () => { const query = 'gamedb.game[maxPlayers == 64]' - expect(encode({query, options: {includeResult: true}})).toEqual( + expect(encodeQueryString({query, options: {includeResult: true}})).toEqual( '?query=gamedb.game%5BmaxPlayers+%3D%3D+64%5D&includeResult=true' ) }) @@ -39,7 +39,7 @@ test('handles options', () => { test('skips falsy options', () => { const query = 'gamedb.game[maxPlayers == 64]' expect( - encode({ + encodeQueryString({ query, options: { extract: null,