Skip to content
Source
Sign inSign up
samwillis/neon-realtime-electric
Open History to see the last commit

shared.ts

21 lines778 Bytes

1import { FetchError, type ShapeStreamOptions } from '@electric-sql/client'
2​
3export type Item = { id: number; label: string; enabled: boolean }
4export const abort = new AbortController()
5let authReads = 0
6export const options: ShapeStreamOptions = {
7 url: new URL('/application/shapes?example=integration', location.origin).href,
8 params: { table: 'electric_items' },
9 liveSse: new URLSearchParams(location.search).get('mode')?.endsWith('-sse'),
10 headers: { authorization: async () => `example-token-${++authReads}` },
11 onError: (error) => {
12 if (error instanceof FetchError && error.status === 401)
13 return {
14 headers: {
15 authorization: async () => `refreshed-token-${++authReads}`,
16 },
17 }
18 throw error
19 },
20 signal: abort.signal,