Skip to content

Commit

Permalink
fix: apply includeTypes to the listen query (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Jun 28, 2023
1 parent 7735609 commit 2fed96b
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ const addEventSourceListener = (
}
}

const encodeQueryString = ({
query,
params = {},
options = {effectFormat: 'mendoza'},
}: {
query: string
params?: Record<string, unknown>
options?: Record<string, unknown>
}) => {
const searchParams = new URLSearchParams()
searchParams.set('query', query)

// Iterate params, the keys are prefixed with `$` and their values JSON stringified
for (const [key, value] of Object.entries(params)) {
searchParams.set(`$${key}`, JSON.stringify(value))
}
// Options are passed as-is
for (const [key, value] of Object.entries(options)) {
// Skip falsy values
if (value) searchParams.set(key, `${value}`)
}

return `?${searchParams}`
}

export function listen(
EventSourceImpl: EnvImplementations['EventSource'],
config: Config,
Expand All @@ -49,9 +74,16 @@ export function listen(
next: (event: MutationEvent) => void
}
): Subscription {
const {projectId, dataset, token} = config
const {projectId, dataset, token, includeTypes} = config
const headers = token ? {Authorization: `Bearer ${token}`} : undefined
const url = `https://${projectId}.api.sanity.io/v1/data/listen/${dataset}?query=*&effectFormat=mendoza`

// Make sure we only listen to mutations on documents part of the `includeTypes` allowlist, if provided
const searchParams = encodeQueryString(
Array.isArray(includeTypes) && includeTypes.length > 0
? {query: `*[_type in $includeTypes]`, params: {includeTypes}}
: {query: '*'}
)
const url = `https://${projectId}.api.sanity.io/v1/data/listen/${dataset}${searchParams}`
const es = new EventSourceImpl(url, {withCredentials: true, headers})

addEventSourceListener(es, 'welcome', handlers.open)
Expand Down

0 comments on commit 2fed96b

Please sign in to comment.