refactor: extract context creation

This commit is contained in:
WebStreamr 2025-07-18 08:18:43 +00:00
parent 21ad16861a
commit 5555ba7040
No known key found for this signature in database
5 changed files with 69 additions and 9 deletions

View file

@ -2,8 +2,7 @@ import { Request, Response, Router } from 'express';
import { ContentType } from 'stremio-addon-sdk';
import winston from 'winston';
import { Source } from '../source';
import { Config, Context } from '../types';
import { envIsProd, getDefaultConfig, ImdbId, StreamResolver } from '../utils';
import { contextFromRequest, envIsProd, ImdbId, StreamResolver } from '../utils';
export class StreamController {
public readonly router: Router;
@ -23,16 +22,10 @@ export class StreamController {
}
private async getStream(req: Request, res: Response) {
const config: Config = req.params['config'] ? JSON.parse(req.params['config']) : getDefaultConfig();
const type: ContentType = (req.params['type'] || '') as ContentType;
const id: string = req.params['id'] || '';
const ctx: Context = {
hostUrl: new URL(`${req.protocol}://${req.host}`),
id: res.getHeader('X-Request-ID') as string,
...(req.ip && { ip: req.ip }),
config,
};
const ctx = contextFromRequest(req);
this.logger.info(`Search stream for type "${type}" and id "${id}" for ip ${ctx.ip}`, ctx);

View file

@ -0,0 +1,23 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`contextFromRequest with config and ip 1`] = `
{
"config": {
"de": "on",
},
"hostUrl": "https://localhost/",
"id": "fake-id",
"ip": "127.0.0.1",
}
`;
exports[`contextFromRequest without config 1`] = `
{
"config": {
"en": "on",
"multi": "on",
},
"hostUrl": "https://localhost/",
"id": "fake-id",
}
`;

31
src/utils/context.test.ts Normal file
View file

@ -0,0 +1,31 @@
import { Request } from 'express';
import { contextFromRequest } from './context';
describe('contextFromRequest', () => {
test('with config and ip', () => {
const req = {
protocol: 'https',
host: 'localhost',
headers: {
'X-Request-ID': 'fake-id',
},
ip: '127.0.0.1',
params: { config: '{"de":"on"}' },
};
expect(contextFromRequest(req as unknown as Request)).toMatchSnapshot();
});
test('without config', () => {
const req = {
protocol: 'https',
host: 'localhost',
headers: {
'X-Request-ID': 'fake-id',
},
params: { },
};
expect(contextFromRequest(req as unknown as Request)).toMatchSnapshot();
});
});

12
src/utils/context.ts Normal file
View file

@ -0,0 +1,12 @@
import { Request } from 'express';
import { Context } from '../types';
import { getDefaultConfig } from './config';
export const contextFromRequest = (req: Request): Context => {
return {
hostUrl: new URL(`${req.protocol}://${req.host}`),
id: req.headers['X-Request-ID'] as string,
...(req.ip && { ip: req.ip }),
config: req.params['config'] ? JSON.parse(req.params['config']) : getDefaultConfig(),
};
};

View file

@ -2,6 +2,7 @@ export * from './Fetcher';
export * from './FetcherMock';
export * from './StreamResolver';
export * from './config';
export * from './context';
export * from './embed';
export * from './env';
export * from './height';