fix(flags): SSE stream endpoint + client — pass productId via query string
EventSource API cannot set custom headers, so the SSE /flags/stream endpoint and feature-flag-client were broken for streaming mode: - Server: accept productId and token from query string as fallback when x-product-id / authorization headers are absent - Client: pass productId (and optional auth token) as query params when constructing the EventSource URL
This commit is contained in:
parent
0b16cb4d63
commit
4a47db72ae
@ -194,7 +194,12 @@ export function createFeatureFlagClient(config: FeatureFlagClientConfig): Featur
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = `${baseUrl}/flags/stream`;
|
const parts = [`productId=${encodeURIComponent(productId)}`];
|
||||||
|
if (getAccessToken) {
|
||||||
|
const token = getAccessToken();
|
||||||
|
if (token) parts.push(`token=${encodeURIComponent(token)}`);
|
||||||
|
}
|
||||||
|
const url = `${baseUrl}/flags/stream?${parts.join('&')}`;
|
||||||
eventSource = new globalThis.EventSource(url);
|
eventSource = new globalThis.EventSource(url);
|
||||||
|
|
||||||
eventSource.onmessage = event => {
|
eventSource.onmessage = event => {
|
||||||
|
|||||||
@ -524,6 +524,14 @@ export async function flagRoutes(app: FastifyInstance) {
|
|||||||
// ── SSE stream ────────────────────────────────────────────────────────
|
// ── SSE stream ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
app.get('/flags/stream', async (req, reply) => {
|
app.get('/flags/stream', async (req, reply) => {
|
||||||
|
// EventSource cannot set custom headers, so accept productId + token from query string
|
||||||
|
const qs = req.query as { productId?: string; token?: string };
|
||||||
|
if (qs.productId && !req.headers['x-product-id']) {
|
||||||
|
(req.headers as Record<string, string>)['x-product-id'] = qs.productId;
|
||||||
|
}
|
||||||
|
if (qs.token && !req.headers['authorization']) {
|
||||||
|
(req.headers as Record<string, string>)['authorization'] = `Bearer ${qs.token}`;
|
||||||
|
}
|
||||||
const productId = getRequestProductId(req);
|
const productId = getRequestProductId(req);
|
||||||
|
|
||||||
reply.raw.writeHead(200, {
|
reply.raw.writeHead(200, {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user