Skip to content

Commit

Permalink
fix: add support for using the default export (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Feb 9, 2023
1 parent 6d3ecff commit 1b1fb16
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 42 deletions.
3 changes: 3 additions & 0 deletions runtimes/node/test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -55,4 +57,5 @@ test('top-level imports', async (t) => {
}
)
})
// */
})
12 changes: 4 additions & 8 deletions runtimes/node/test.mjs
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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(
() => {
Expand All @@ -47,4 +42,5 @@ test('top-level imports', async (t) => {
}
)
})
// */
})
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
8 changes: 4 additions & 4 deletions src/data/dataMethods.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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'

Expand Down Expand Up @@ -301,7 +301,7 @@ export function _requestObservable<R>(
options.query = {tag: validate.requestTag(tag), ...options.query}
}

const reqOptions = getRequestOptions(
const reqOptions = requestOptions(
config,
Object.assign({}, options, {
url: _getUrl(client, uri, useCdn),
Expand Down
2 changes: 1 addition & 1 deletion src/data/encodeQueryString.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Any, QueryParams} from '../types'

export default ({
export const encodeQueryString = ({
query,
params = {},
options = {},
Expand Down
4 changes: 2 additions & 2 deletions src/data/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/data/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/generateHelpUrl.ts
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion src/http/requestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {Any} from '../types'

const projectHeader = 'X-Sanity-Project-ID'

export default (config: Any, overrides: Any = {}): Omit<RequestOptions, 'url'> => {
export function requestOptions(config: Any, overrides: Any = {}): Omit<RequestOptions, 'url'> {
const headers: Any = {}

const token = overrides.token || config.token
Expand Down
10 changes: 9 additions & 1 deletion src/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
}
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
}
9 changes: 0 additions & 9 deletions src/migrationNotice.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/util/getSelection.ts
Original file line number Diff line number Diff line change
@@ -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}
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/once.ts
Original file line number Diff line number Diff line change
@@ -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[]) => {
Expand Down
2 changes: 1 addition & 1 deletion src/util/pick.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/warnings.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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',
])
14 changes: 7 additions & 7 deletions test/encodeQueryString.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
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'
)
})

test('skips falsy options', () => {
const query = 'gamedb.game[maxPlayers == 64]'
expect(
encode({
encodeQueryString({
query,
options: {
extract: null,
Expand Down

0 comments on commit 1b1fb16

Please sign in to comment.