-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: broadcast changes to single clients
- Loading branch information
Showing
29 changed files
with
337 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
59 changes: 59 additions & 0 deletions
59
apps/server/src/lib/decorators/client-id/client-id.decorator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants'; | ||
import { randomUUID } from 'crypto'; | ||
import { XsighubHttpHeaders } from '../../http-headers'; | ||
import { ClientId } from './client-id.decorator'; | ||
|
||
describe('ClientId', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/ban-types, @typescript-eslint/explicit-function-return-type | ||
function getParamDecoratorFactory(decorator: Function) { | ||
class TestDecorator { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function | ||
public test(@ClientId() clientId: string): void {} | ||
} | ||
|
||
const args = Reflect.getMetadata(ROUTE_ARGS_METADATA, TestDecorator, 'test'); | ||
|
||
return args[Object.keys(args)[0]].factory; | ||
} | ||
|
||
it('should return the client ID if it exists in the request', () => { | ||
const uuid = randomUUID(); | ||
|
||
const mockRequest = { | ||
headers: { | ||
[XsighubHttpHeaders.HTTP_HEADER_CLIENT_ID]: uuid, | ||
}, | ||
}; | ||
|
||
const mockContext = { | ||
switchToHttp: jest.fn().mockReturnValue({ | ||
getRequest: jest.fn().mockReturnValue(mockRequest), | ||
}), | ||
} as unknown as ExecutionContext; | ||
|
||
const factory = getParamDecoratorFactory(ClientId); | ||
|
||
const result = factory(null, mockContext); | ||
|
||
expect(result).toBe(uuid); | ||
}); | ||
|
||
it('should return undefined if the client ID cannot be obtained from the request', () => { | ||
const mockRequest = { | ||
headers: {}, | ||
}; | ||
|
||
const mockContext = { | ||
switchToHttp: jest.fn().mockReturnValue({ | ||
getRequest: jest.fn().mockReturnValue(mockRequest), | ||
}), | ||
} as unknown as ExecutionContext; | ||
|
||
const factory = getParamDecoratorFactory(ClientId); | ||
|
||
const result = factory(null, mockContext); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
apps/server/src/lib/decorators/client-id/client-id.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { Request } from 'express'; | ||
import { XsighubHttpHeaders } from '../../http-headers'; | ||
|
||
/** | ||
* Decorador que extrae el socket client ID del objeto de solicitud. | ||
* @param ctx - El contexto de ejecución de la solicitud actual. | ||
* @returns Correlation ID asociado a la petición HTTP. | ||
*/ | ||
export const ClientId = createParamDecorator((_, ctx: ExecutionContext) => { | ||
const request = ctx.switchToHttp().getRequest<Request>(); | ||
|
||
const clientId = request.headers?.[XsighubHttpHeaders.HTTP_HEADER_CLIENT_ID]; | ||
|
||
return clientId; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './client-id/client-id.decorator'; | ||
export * from './client-ip/client-ip.decorator'; | ||
export * from './correlation-id/correlation-id.decorator'; | ||
export * from './user-agent/user-agent.decorator'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const XsighubHttpHeaders = { | ||
HTTP_HEADER_CORRELATION_ID: 'X-Correlation-ID', | ||
HTTP_HEADER_CORRELATION_ID: 'x-correlation-id', | ||
HTTP_HEADER_CLIENT_ID: 'x-xsighub-client-id', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export type ApiExtras = { correlationId: string }; | ||
export type ApiExtras = { correlationId: string; clientId: string }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.