Skip to content

Commit

Permalink
refactor: update config transformer to build full image urls (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
wax911 authored May 18, 2024
1 parent 9e2b043 commit 984e99c
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/common/middleware/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isHttpError, Status, STATUS_TEXT } from 'x/oak';
import { logger } from '../core/logger.ts';
import type { Error } from '../types/core.d.ts';
import type { ErrorResponse } from '../types/core.d.ts';
import type { AppContext } from '../types/core.d.ts';

export default async (
Expand All @@ -14,12 +14,12 @@ export default async (
logger.error(err);
const status = err.status;
response.status = status;
response.body = <Error> { message: STATUS_TEXT[status] };
response.body = <ErrorResponse> { message: STATUS_TEXT[status] };
} else {
logger.error(err);
const status = Status.InternalServerError;
response.status = Status.InternalServerError;
response.body = <Error> { message: STATUS_TEXT[status] };
response.body = <ErrorResponse> { message: STATUS_TEXT[status] };
}
}
};
4 changes: 2 additions & 2 deletions src/common/middleware/header.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HTTPMethods, Status } from 'x/oak';
import { logger } from '../core/logger.ts';
import type { AppContext, Error } from '../types/core.d.ts';
import type { AppContext, ErrorResponse } from '../types/core.d.ts';

const bodyTypes: HTTPMethods[] = [
'PATCH',
Expand Down Expand Up @@ -29,7 +29,7 @@ const fail = (header: string, ctx: AppContext) => {
const { response } = ctx;
response.status = Status.BadRequest;
response.type = 'application/json';
response.body = <Error> {
response.body = <ErrorResponse> {
message: 'Missing required header',
};
logger.error(
Expand Down
2 changes: 1 addition & 1 deletion src/common/types/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AppFeatures } from '../experiment/types.d.ts';

export type RCF822Date = string;

export type Error = {
export type ErrorResponse = {
message: string;
};

Expand Down
15 changes: 13 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { AppContext } from '../common/types/core.d.ts';
import { AppContext, ErrorResponse } from '../common/types/core.d.ts';
import { Repository } from './repository/index.ts';
import { LocalSource } from './local/index.ts';
import { collection } from '../common/mongo/index.ts';
import { Status } from 'jsr:@oak/[email protected]/status';

export const config = async ({ state, response }: AppContext) => {
const localSource = new LocalSource(collection('config', state.local));
const repository = new Repository(
state.features,
localSource,
);
const configuration = await repository.getConfiguration();

response.type = 'application/json';
response.body = await repository.getConfiguration();
if (configuration) {
response.status = Status.OK;
response.body = await repository.getConfiguration();
} else {
response.status = Status.InternalServerError;
response.body = <ErrorResponse> {
message: 'An error occured while fetching configuration',
};
}
};
2 changes: 1 addition & 1 deletion src/config/local/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class LocalSource {
const config = await this.collection?.findOne()
?.catch((e) => {
logger.error(
`config.local.source.LocalSource:getConfig: Unable to find config in collection`,
`config.local.source:getConfig: Unable to find config in collection`,
e,
);
return undefined;
Expand Down
1 change: 0 additions & 1 deletion src/config/local/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export interface ImageConfig extends Document {
}

export interface ConfigDocument extends Document {
_id: string;
navigation: NavigationConfig[];
genres: GenreConfig[];
image: ImageConfig;
Expand Down
20 changes: 15 additions & 5 deletions src/config/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ import { Features } from '../../common/types/core.d.ts';
import { ClientConfiguration } from '../transformer/types.d.ts';
import { LocalSource } from '../local/index.ts';
import { transform } from '../transformer/index.ts';
import { logger } from '../../common/core/index.ts';

export class Repository {
constructor(
private growth: Features,
private local: LocalSource,
) {}

// get settings from our database or something based on an app version?
getConfiguration = async (): Promise<ClientConfiguration> => {
getConfiguration = async (): Promise<ClientConfiguration | undefined> => {
const config = await this.local.getConfig();

return {
...transform({ document: config, features: this.growth }),
};
if (config) {
try {
const result = transform({ document: config, features: this.growth });
return result;
} catch (e) {
logger.error(
`config.repository.index:getConfiguration: Error while transforming document`,
e,
);
}
}

return undefined;
};
}
38 changes: 23 additions & 15 deletions src/config/transformer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,44 @@ import {
getPlatformSource,
isAnalyticsEnabled,
} from '../../common/experiment/index.ts';
import { Optional } from '../../common/mongo/types.d.ts';
import { Transform } from '../../common/transformer/types.d.ts';
import { Features } from '../../common/types/core.d.ts';
import { ConfigDocument } from '../local/types.d.ts';
import { ClientConfiguration } from './types.d.ts';
import { PlatformSource } from '../../common/experiment/types.d.ts';
import { idOf } from '../../common/mongo/index.ts';

const toImageUrl = (image: string, source?: PlatformSource): string => {
if (source) {
return `${source.media}${image}`;
}
return image;
};

export const transform: Transform<
{
document: Optional<WithId<ConfigDocument>>;
document: WithId<ConfigDocument>;
features: Features;
},
ClientConfiguration
> = (data) => {
const platformSource = getPlatformSource(data.features);
const image = data.document?.image;
> = ({ document, features }) => {
const platformSource = getPlatformSource(features);
const { image, _id, genres, navigation } = document;
return {
id: data.document?._id,
id: idOf(_id),
settings: {
analyticsEnabled: isAnalyticsEnabled(data.features),
analyticsEnabled: isAnalyticsEnabled(features),
platformSource: platformSource?.api,
},
genres: data.document?.genres,
genres: genres,
image: {
banner: image?.banner ?? '',
poster: image?.poster ?? '',
loading: image?.loading ?? '',
error: image?.error ?? '',
info: image?.info ?? '',
default: image?.default ?? '',
banner: toImageUrl(image.banner, platformSource),
poster: toImageUrl(image.poster, platformSource),
loading: toImageUrl(image.loading, platformSource),
error: toImageUrl(image.error, platformSource),
info: toImageUrl(image.info, platformSource),
default: toImageUrl(image.default, platformSource),
},
navigation: data.document?.navigation,
navigation: navigation,
};
};
3 changes: 2 additions & 1 deletion src/news/mapper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { News, NewsEntity } from './types.d.ts';
import { NewsDocument } from './local/types.d.ts';
import { OptionalId, WithId } from 'npm/mongodb';
import { idOf } from '../common/mongo/index.ts';

export const toEntity = (data: WithId<NewsDocument>): NewsEntity => {
return {
id: data._id.toHexString(),
id: idOf(data._id),
slug: data.slug,
title: data.title,
author: data.author,
Expand Down
4 changes: 2 additions & 2 deletions src/series/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Status } from 'x/oak';
import { AppContext, Error } from '../common/types/core.d.ts';
import { AppContext, ErrorResponse } from '../common/types/core.d.ts';
import LocalSource from './local/source.ts';
import SeriesRepository from './repository/series.ts';
import SeasonRepository from './repository/season.ts';
Expand All @@ -21,7 +21,7 @@ export const series = async ({ request, response, state }: AppContext) => {
} else {
response.type = 'application/json';
response.status = Status.BadRequest;
response.body = <Error> {
response.body = <ErrorResponse> {
message: `Missing required query parameter: 'id'`,
};
}
Expand Down

0 comments on commit 984e99c

Please sign in to comment.