UNPKG

@instantdb/core

Version:
56 lines 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createInstantRouteHandler = void 0; function createUserSyncResponse(config, user) { if (user && user.refresh_token) { return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json', // 7 day expiry 'Set-Cookie': `instant_user_${config.appId}=${encodeURIComponent(JSON.stringify(user))}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=604800`, }, }); } else { return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json', // remove the cookie (some browsers) 'Set-Cookie': `instant_user_${config.appId}=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=-1`, }, }); } } function errorResponse(status, message) { return new Response(JSON.stringify({ ok: false, error: message }), { status, headers: { 'Content-Type': 'application/json' }, }); } const createInstantRouteHandler = (config) => { return { POST: async (req) => { let body; try { body = await req.json(); } catch { return errorResponse(400, 'Invalid JSON body'); } if (!body.type) { return errorResponse(400, 'Missing "type" field'); } if (body.appId !== config.appId) { return errorResponse(403, 'App ID mismatch'); } switch (body.type) { case 'sync-user': return createUserSyncResponse(config, body.user ?? null); default: return errorResponse(400, `Unknown type: ${body.type}`); } }, }; }; exports.createInstantRouteHandler = createInstantRouteHandler; //# sourceMappingURL=createRouteHandler.js.map