feat(api): implement Riot tournament webhook endpoint
Adds a controller to capture and log incoming webhook notifications from Riot Games. Enables raw body parsing at the application level to facilitate payload processing and future signature verification.
This commit is contained in:
1 parent
35b7db5344
commit
a197185ad0
4 files changed
+112
-1
No files matched your search
@@ -2,7 +2,9 @@ import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
rawBody: true,
|
||||
});
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
@@ -0,0 +1,60 @@
|
||||
import type { Request } from 'express';
|
||||
import { RiotWebhookController } from './riot-webhook.controller';
|
||||
|
||||
type RiotWebhookRequest = Request & {
|
||||
rawBody?: Buffer;
|
||||
};
|
||||
|
||||
describe('RiotWebhookController', () => {
|
||||
let controller: RiotWebhookController;
|
||||
|
||||
beforeEach(() => {
|
||||
controller = new RiotWebhookController();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('logs the incoming webhook payload and returns an acknowledgement', () => {
|
||||
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => undefined);
|
||||
const dirSpy = jest.spyOn(console, 'dir').mockImplementation(() => undefined);
|
||||
const request = {
|
||||
method: 'POST',
|
||||
originalUrl: '/riot/webhooks/tournament',
|
||||
protocol: 'https',
|
||||
hostname: 'example.test',
|
||||
ip: '127.0.0.1',
|
||||
ips: ['127.0.0.1'],
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
query: {
|
||||
shard: 'kr',
|
||||
},
|
||||
params: {},
|
||||
body: {
|
||||
eventType: 'MATCH_FINISHED',
|
||||
},
|
||||
rawBody: Buffer.from('{"eventType":"MATCH_FINISHED"}', 'utf8'),
|
||||
} as RiotWebhookRequest;
|
||||
|
||||
const response = controller.captureTournamentWebhook(request);
|
||||
|
||||
expect(response.ok).toBe(true);
|
||||
expect(response.message).toBe('Riot webhook received.');
|
||||
expect(response.receivedAt).toEqual(expect.any(String));
|
||||
expect(logSpy).toHaveBeenCalledTimes(2);
|
||||
expect(dirSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
url: '/riot/webhooks/tournament',
|
||||
body: {
|
||||
eventType: 'MATCH_FINISHED',
|
||||
},
|
||||
rawBodyText: '{"eventType":"MATCH_FINISHED"}',
|
||||
}),
|
||||
{ depth: null },
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,47 @@
|
||||
import {
|
||||
All,
|
||||
Controller,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
|
||||
type RiotWebhookRequest = Request & {
|
||||
rawBody?: Buffer;
|
||||
};
|
||||
|
||||
@Controller('riot')
|
||||
export class RiotWebhookController {
|
||||
@All('webhooks/tournament')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public captureTournamentWebhook(@Req() req: RiotWebhookRequest) {
|
||||
const receivedAt = new Date().toISOString();
|
||||
const rawBodyText = req.rawBody?.toString('utf8') ?? null;
|
||||
const logPayload = {
|
||||
receivedAt,
|
||||
method: req.method,
|
||||
url: req.originalUrl,
|
||||
protocol: req.protocol,
|
||||
hostname: req.hostname,
|
||||
ip: req.ip,
|
||||
ips: req.ips,
|
||||
headers: req.headers,
|
||||
query: req.query,
|
||||
params: req.params,
|
||||
body: req.body,
|
||||
rawBodyLength: req.rawBody?.length ?? null,
|
||||
rawBodyText,
|
||||
};
|
||||
|
||||
console.log('\n[RIOT TOURNAMENT WEBHOOK] Incoming request');
|
||||
console.dir(logPayload, { depth: null });
|
||||
console.log('[RIOT TOURNAMENT WEBHOOK] End request log\n');
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
message: 'Riot webhook received.',
|
||||
receivedAt,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,11 @@ import { SecretModule } from '../global/secret/secret.module';
|
||||
import { RiotAccountService } from './riot-account.service';
|
||||
import { RiotClientService } from './riot.client.service';
|
||||
import { RiotMatchService } from './riot-match.service';
|
||||
import { RiotWebhookController } from './riot-webhook.controller';
|
||||
|
||||
@Module({
|
||||
imports: [SecretModule],
|
||||
controllers: [RiotWebhookController],
|
||||
providers: [RiotClientService, RiotAccountService, RiotMatchService],
|
||||
exports: [RiotClientService, RiotAccountService, RiotMatchService],
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user