handleSyncRequest
handleSyncRequest<
TEnv
,TDurableObjectRpc
,CFHostMetada
>(__namedParameters
):Promise
<Response
>
Defined in: packages/@livestore/sync-cf/src/cf-worker/worker.ts:166
Handles /sync
endpoint.
Type Parameters
Section titled “Type Parameters”TDurableObjectRpc
Section titled “TDurableObjectRpc”TDurableObjectRpc
extends undefined
| DurableObjectBranded
= undefined
CFHostMetada
Section titled “CFHostMetada”CFHostMetada
= unknown
Parameters
Section titled “Parameters”__namedParameters
Section titled “__namedParameters”Only there for type-level reasons
TEnv
options?
Section titled “options?”{ durableObject?
: { name?
: ExtractDurableObjectKeys
<TEnv
>; }; headers?
: HeadersInit
; validatePayload?
: (payload
, context
) => void
| Promise
<void
>; } = {}
options.durableObject?
Section titled “options.durableObject?”{ name?
: ExtractDurableObjectKeys
<TEnv
>; }
options.durableObject.name?
Section titled “options.durableObject.name?”ExtractDurableObjectKeys
<TEnv
>
Needs to match the binding name from the wrangler config
Default
'SYNC_BACKEND_DO'
options.headers?
Section titled “options.headers?”options.validatePayload?
Section titled “options.validatePayload?”(payload
, context
) => void
| Promise
<void
>
request
Section titled “request”Request
<CFHostMetada
>
searchParams
Section titled “searchParams”{ payload
: undefined
| JsonValue
; storeId
: string
; transport
: "http"
| "ws"
; }
searchParams.payload
Section titled “searchParams.payload”undefined
| JsonValue
= ...
searchParams.storeId
Section titled “searchParams.storeId”string
= Schema.String
searchParams.transport
Section titled “searchParams.transport”"http"
| "ws"
= ...
Returns
Section titled “Returns”Promise
<Response
>
Example
Section titled “Example”const validatePayload = (payload: Schema.JsonValue | undefined, context: { storeId: string }) => { console.log(`Validating connection for store: ${context.storeId}`) if (payload?.authToken !== 'insecure-token-change-me') { throw new Error('Invalid auth token') }}
export default { fetch: async (request, env, ctx) => { const requestParamsResult = getSyncRequestSearchParams(request)
// Is LiveStore sync request if (requestParamsResult._tag === 'Some') { return handleSyncRequest({ request, searchParams: requestParamsResult.value, env, ctx, options: { headers: {}, validatePayload } }) }
return new Response('Invalid path', { status: 400 }) }}
Throws
Section titled “Throws”If the payload is invalid