Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(live): includeDrafts no longer require vX #991

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/data/live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {map} from 'rxjs/operators'
import {CorsOriginError} from '../http/errors'
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
import type {
LiveEvent,
LiveEventMessage,
LiveEventReconnect,
LiveEventRestart,
Expand All @@ -15,7 +16,7 @@ import {connectEventSource} from './eventsource'
import {eventSourcePolyfill} from './eventsourcePolyfill'
import {reconnectOnConnectionFailure} from './reconnectOnConnectionFailure'

const requiredApiVersion = '2021-03-26'
const requiredApiVersion = '2021-03-25'

/**
* @public
Expand All @@ -27,21 +28,20 @@ export class LiveClient {
}

/**
* Requires `apiVersion` to be `2021-03-26` or later.
* Requires `apiVersion` to be `2021-03-25` or later.
*/
events({
includeDrafts = false,
tag: _tag,
}: {
/** @alpha this API is experimental and may change or even be removed */
includeDrafts?: boolean
/**
* Optional request tag for the listener. Use to identify the request in logs.
*
* @defaultValue `undefined`
*/
tag?: string
} = {}): Observable<LiveEventMessage | LiveEventRestart | LiveEventReconnect | LiveEventWelcome> {
} = {}): Observable<LiveEvent> {
const {
projectId,
apiVersion: _apiVersion,
Expand All @@ -62,11 +62,6 @@ export class LiveClient {
`The live events API requires a token or withCredentials when 'includeDrafts: true'. Please update your client configuration. The token should have the lowest possible access role.`,
)
}
if (includeDrafts && apiVersion !== 'X') {
throw new Error(
`The live events API requires API version X when 'includeDrafts: true'. This API is experimental and may change or even be removed.`,
)
}
const path = _getDataUrl(this.#client, 'live/events')
const url = new URL(this.#client.getUrl(path, false))
const tag = _tag && requestTagPrefix ? [requestTagPrefix, _tag].join('.') : _tag
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ export interface RawQueryResponse<R> {
ms: number
result: R
resultSourceMap?: ContentSourceMap
/** Requires `apiVersion` to be `2021-03-26` or later. */
/** Requires `apiVersion` to be `2021-03-25` or later. */
syncTags?: SyncTag[]
}

Expand Down Expand Up @@ -1292,6 +1292,9 @@ export interface LiveEventWelcome {
type: 'welcome'
}

/** @public */
export type LiveEvent = LiveEventRestart | LiveEventReconnect | LiveEventMessage | LiveEventWelcome

/** @public */
export interface SanityQueries {}

Expand Down
24 changes: 11 additions & 13 deletions test/live.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,32 @@ describe.skipIf(typeof EdgeRuntime === 'string' || typeof document !== 'undefine
const client = getClient({apiVersion: '2024-06-07', port: 1234})
expect(() => client.live.events()).not.toThrow()
})
test('allows apiVersion v2021-03-26', () => {
const client = getClient({apiVersion: 'v2021-03-26', port: 1234})
test('allows apiVersion v2021-03-25', () => {
const client = getClient({apiVersion: 'v2021-03-25', port: 1234})
expect(() => client.live.events()).not.toThrow()
})
test('disallows apiVersion 1', () => {
const client = getClient({apiVersion: '1', port: 1234})
expect(() => client.live.events()).toThrowErrorMatchingInlineSnapshot(
`[Error: The live events API requires API version 2021-03-26 or later. The current API version is 1. Please update your API version to use this feature.]`,
`[Error: The live events API requires API version 2021-03-25 or later. The current API version is 1. Please update your API version to use this feature.]`,
)
})
test('disallows apiVersion v1', () => {
const client = getClient({apiVersion: 'v1', port: 1234})
expect(() => client.live.events()).toThrowErrorMatchingInlineSnapshot(
`[Error: The live events API requires API version 2021-03-26 or later. The current API version is 1. Please update your API version to use this feature.]`,
`[Error: The live events API requires API version 2021-03-25 or later. The current API version is 1. Please update your API version to use this feature.]`,
)
})
test('disallows apiVersion 2021-03-25', () => {
const client = getClient({apiVersion: '2021-03-25', port: 1234})
test('disallows apiVersion 2021-03-24', () => {
const client = getClient({apiVersion: '2021-03-24', port: 1234})
expect(() => client.live.events()).toThrowErrorMatchingInlineSnapshot(
`[Error: The live events API requires API version 2021-03-26 or later. The current API version is 2021-03-25. Please update your API version to use this feature.]`,
`[Error: The live events API requires API version 2021-03-25 or later. The current API version is 2021-03-24. Please update your API version to use this feature.]`,
)
})
test('disallows apiVersion v2020-01-01', () => {
const client = getClient({apiVersion: 'v2020-01-01', port: 1234})
expect(() => client.live.events()).toThrowErrorMatchingInlineSnapshot(
`[Error: The live events API requires API version 2021-03-26 or later. The current API version is 2020-01-01. Please update your API version to use this feature.]`,
`[Error: The live events API requires API version 2021-03-25 or later. The current API version is 2020-01-01. Please update your API version to use this feature.]`,
)
})
test('requires token when includeDrafts is true', () => {
Expand All @@ -97,11 +97,9 @@ describe.skipIf(typeof EdgeRuntime === 'string' || typeof document !== 'undefine
`[Error: The live events API requires a token or withCredentials when 'includeDrafts: true'. Please update your client configuration. The token should have the lowest possible access role.]`,
)
})
test('requires apiVersion X when includeDrafts is true', () => {
const client = getClient({apiVersion: 'v2021-03-26', token: 'abc123', port: 1234})
expect(() => client.live.events({includeDrafts: true})).toThrowErrorMatchingInlineSnapshot(
`[Error: The live events API requires API version X when 'includeDrafts: true'. This API is experimental and may change or even be removed.]`,
)
test('allows apiVersion 2021-03-26 when includeDrafts is true', () => {
const client = getClient({apiVersion: 'v2021-03-25', token: 'abc123', port: 1234})
expect(() => client.live.events({includeDrafts: true})).not.toThrowError()
})

test('can listen for tags', async () => {
Expand Down
Loading