Skip to content

Commit 8ff6a38

Browse files
committed
feat: update FastifySseReplyFromResponseBuilder
update builder with custom cors origins
1 parent ced464d commit 8ff6a38

File tree

8 files changed

+45
-7
lines changed

8 files changed

+45
-7
lines changed

.changeset/happy-months-stare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cornie-js/backend-http": minor
3+
---
4+
5+
Updated FastifySseReplyFromResponseBuilder with custom cors origins

.changeset/nine-days-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cornie-js/backend-service-game": patch
3+
---
4+
5+
Updated SseControllers to rely on custom FastifySseReplyFromResponseBuilder

packages/backend/libraries/backend-http/src/http/adapter/fastify/builders/FastifySseReplyFromResponseBuilder.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,24 @@ import { FastifyReply } from 'fastify';
77

88
import { Response } from '../../../application/models/Response';
99

10+
const CORS_ORIGINS_SEPARATOR: string = ',';
11+
1012
@Injectable()
1113
export class FastifySseReplyFromResponseBuilder
1214
implements Builder<FastifyReply, [Response, FastifyReply]>
1315
{
16+
readonly #accessControlAllowOriginValue: string;
17+
18+
constructor(corsOrigins: string[]) {
19+
this.#accessControlAllowOriginValue = corsOrigins.join(
20+
CORS_ORIGINS_SEPARATOR,
21+
);
22+
}
23+
1424
public build(response: Response, fastifyReply: FastifyReply): FastifyReply {
1525
const sseHeaders: http.OutgoingHttpHeaders | http2.OutgoingHttpHeaders = {
1626
...response.headers,
27+
'access-control-allow-origin': this.#accessControlAllowOriginValue,
1728
// Consider https://html.spec.whatwg.org/multipage/server-sent-events.html as reference
1829
// Disable cache, even for old browsers and proxies
1930
'cache-control':

packages/backend/libraries/backend-http/src/http/adapter/nest/controllers/HttpNestFastifySseController.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { DelayedSseConsumer } from '../../../application/modules/DelayedSseConsu
1515
import { SsePublisher } from '../../../application/modules/SsePublisher';
1616
import { SseTeardownExecutor } from '../../../application/modules/SseTeardownExecutor';
1717
import { FastifyReplySseConsumerFromFastifyReplyBuilder } from '../../fastify/builders/FastifyReplySseConsumerFromFastifyReplyBuilder';
18-
import { FastifySseReplyFromResponseBuilder } from '../../fastify/builders/FastifySseReplyFromResponseBuilder';
1918
import { HttpNestFastifySseController } from './HttpNestFastifySseController';
2019

2120
describe(HttpNestFastifySseController.name, () => {
@@ -35,7 +34,9 @@ describe(HttpNestFastifySseController.name, () => {
3534
let resultBuilderMock: jest.Mocked<
3635
Builder<FastifyReply, [Response | ResponseWithBody<unknown>, FastifyReply]>
3736
>;
38-
let sseResultBuilderMock: jest.Mocked<FastifySseReplyFromResponseBuilder>;
37+
let sseResultBuilderMock: jest.Mocked<
38+
Builder<FastifyReply, [Response, FastifyReply]>
39+
>;
3940

4041
let httpNestFastifySseController: HttpNestFastifySseController;
4142

packages/backend/libraries/backend-http/src/http/adapter/nest/controllers/HttpNestFastifySseController.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { DelayedSseConsumer } from '../../../application/modules/DelayedSseConsu
1111
import { SsePublisher } from '../../../application/modules/SsePublisher';
1212
import { SseTeardownExecutor } from '../../../application/modules/SseTeardownExecutor';
1313
import { FastifyReplySseConsumerFromFastifyReplyBuilder } from '../../fastify/builders/FastifyReplySseConsumerFromFastifyReplyBuilder';
14-
import { FastifySseReplyFromResponseBuilder } from '../../fastify/builders/FastifySseReplyFromResponseBuilder';
1514

1615
@Injectable()
1716
export class HttpNestFastifySseController<
@@ -31,7 +30,7 @@ export class HttpNestFastifySseController<
3130
FastifyReply,
3231
[Response | ResponseWithBody<unknown>, FastifyReply]
3332
>;
34-
readonly #sseResultBuilder: FastifySseReplyFromResponseBuilder;
33+
readonly #sseResultBuilder: Builder<FastifyReply, [Response, FastifyReply]>;
3534

3635
constructor(
3736
fastifyReplyConsumerFromFastifyReplyBuilder: FastifyReplySseConsumerFromFastifyReplyBuilder,
@@ -47,7 +46,7 @@ export class HttpNestFastifySseController<
4746
FastifyReply,
4847
[Response | ResponseWithBody<unknown>, FastifyReply]
4948
>,
50-
sseResultBuilder: FastifySseReplyFromResponseBuilder,
49+
sseResultBuilder: Builder<FastifyReply, [Response, FastifyReply]>,
5150
) {
5251
this.#fastifyReplySseConsumerFromFastifyReplyBuilder =
5352
fastifyReplyConsumerFromFastifyReplyBuilder;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { EnvironmentService } from '@cornie-js/backend-app-game-env';
2+
import { FastifySseReplyFromResponseBuilder as BaseFastifySseReplyFromResponseBuilder } from '@cornie-js/backend-http';
3+
import { Inject, Injectable } from '@nestjs/common';
4+
5+
@Injectable()
6+
export class FastifySseReplyFromResponseBuilder extends BaseFastifySseReplyFromResponseBuilder {
7+
constructor(
8+
@Inject(EnvironmentService)
9+
environmentService: EnvironmentService,
10+
) {
11+
super(environmentService.getEnvironment().corsOrigins);
12+
}
13+
}

packages/backend/services/backend-service-game/src/foundation/http/adapter/nest/modules/HttpModule.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import { EnvModule } from '@cornie-js/backend-app-game-env';
12
import {
23
ErrorV1ResponseFromErrorBuilder,
34
FastifyReplyFromResponseBuilder,
45
FastifyReplySseConsumerFromFastifyReplyBuilder,
5-
FastifySseReplyFromResponseBuilder,
66
HttpStatusCodeFromErrorBuilder,
77
RequestFromFastifyRequestBuilder,
88
RequestWithBodyFromFastifyRequestBuilder,
99
StringifiedSseFromMessageEventBuilder,
1010
} from '@cornie-js/backend-http';
1111
import { Module } from '@nestjs/common';
1212

13+
import { FastifySseReplyFromResponseBuilder } from '../../fastify/builders/FastifySseReplyFromResponseBuilder';
14+
1315
@Module({
1416
exports: [
1517
ErrorV1ResponseFromErrorBuilder,
@@ -19,6 +21,7 @@ import { Module } from '@nestjs/common';
1921
RequestFromFastifyRequestBuilder,
2022
RequestWithBodyFromFastifyRequestBuilder,
2123
],
24+
imports: [EnvModule],
2225
providers: [
2326
ErrorV1ResponseFromErrorBuilder,
2427
FastifyReplyFromResponseBuilder,

packages/backend/services/backend-service-game/src/games/adapter/nest/controllers/GetV2EventsGamesGameIdRequestNestController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { GetV2GamesGameIdEventsSseController } from '@cornie-js/backend-game-app
33
import {
44
FastifyReplyFromResponseBuilder,
55
FastifyReplySseConsumerFromFastifyReplyBuilder,
6-
FastifySseReplyFromResponseBuilder,
76
HttpNestFastifySseController,
87
MessageEvent,
98
Request,
@@ -16,6 +15,8 @@ import {
1615
import { Controller, Get, Inject, Req, Res } from '@nestjs/common';
1716
import { FastifyReply, FastifyRequest } from 'fastify';
1817

18+
import { FastifySseReplyFromResponseBuilder } from '../../../../foundation/http/adapter/fastify/builders/FastifySseReplyFromResponseBuilder';
19+
1920
@Controller(`v2/events/games`)
2021
export class GetV2EventsGamesGameIdRequestNestController extends HttpNestFastifySseController {
2122
constructor(

0 commit comments

Comments
 (0)