1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-19 01:17:18 +02:00

Try to make events work properly

This commit is contained in:
sjaanus 2024-12-16 16:35:02 +02:00
parent e8f699458f
commit 3e84274149
No known key found for this signature in database
GPG Key ID: 20E007C0248BA7FF
2 changed files with 55 additions and 42 deletions

View File

@ -73,6 +73,47 @@ export default class ClientFeatureToggleDeltaController extends Controller {
}); });
} }
async getDelta(
req: IAuthRequest,
res: Response<RevisionDeltaEntry>,
): Promise<void> {
if (!this.flagResolver.isEnabled('deltaApi')) {
throw new NotFoundError();
}
const query = await this.resolveQuery(req);
const etag = req.headers['if-none-match'];
const currentSdkRevisionId = etag ? Number.parseInt(etag) : undefined;
const changedFeatures =
await this.clientFeatureToggleService.getClientDelta(
currentSdkRevisionId,
query,
);
if (!changedFeatures) {
res.status(304);
res.getHeaderNames().forEach((header) => res.removeHeader(header));
res.end();
return;
}
if (changedFeatures.revisionId === currentSdkRevisionId) {
res.status(304);
res.getHeaderNames().forEach((header) => res.removeHeader(header));
res.end();
return;
}
res.setHeader('ETag', changedFeatures.revisionId.toString());
this.openApiService.respondWithValidation(
200,
res,
clientFeaturesDeltaSchema.$id,
changedFeatures,
);
}
private async resolveQuery( private async resolveQuery(
req: IAuthRequest, req: IAuthRequest,
): Promise<IFeatureToggleQuery> { ): Promise<IFeatureToggleQuery> {
@ -139,45 +180,4 @@ export default class ClientFeatureToggleDeltaController extends Controller {
return query; return query;
} }
async getDelta(
req: IAuthRequest,
res: Response<RevisionDeltaEntry>,
): Promise<void> {
if (!this.flagResolver.isEnabled('deltaApi')) {
throw new NotFoundError();
}
const query = await this.resolveQuery(req);
const etag = req.headers['if-none-match'];
const currentSdkRevisionId = etag ? Number.parseInt(etag) : undefined;
const changedFeatures =
await this.clientFeatureToggleService.getClientDelta(
currentSdkRevisionId,
query,
);
if (!changedFeatures) {
res.status(304);
res.getHeaderNames().forEach((header) => res.removeHeader(header));
res.end();
return;
}
if (changedFeatures.revisionId === currentSdkRevisionId) {
res.status(304);
res.getHeaderNames().forEach((header) => res.removeHeader(header));
res.end();
return;
}
res.setHeader('ETag', changedFeatures.revisionId.toString());
this.openApiService.respondWithValidation(
200,
res,
clientFeaturesDeltaSchema.$id,
changedFeatures,
);
}
} }

View File

@ -170,11 +170,24 @@ test('should match with /api/client/delta', async () => {
test('should get 304 if asked for latest revision', async () => { test('should get 304 if asked for latest revision', async () => {
await setupFeatures(db, app); await setupFeatures(db, app);
// const waitForEvent : Promise<void> = new Promise((resolve) => {
// app.services.configurationRevisionService.once(UPDATE_REVISION, () => {
// resolve();
// });
// });
await app.services.configurationRevisionService.updateMaxRevisionId();
// app.services.configurationRevisionService.emit(UPDATE_REVISION);
await new Promise((resolve) => setTimeout(resolve, 100));
// await waitForEvent;
const events = await db.rawDatabase('events').select('*'); const events = await db.rawDatabase('events').select('*');
console.log(events); console.log(events);
await app.request await app.request
.set('If-None-Match', '10') .set('If-None-Match', '14')
.get('/api/client/delta') .get('/api/client/delta')
.expect(304); .expect(304);
}); });