Skip to content

Commit

Permalink
chore: refactor package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wax911 committed Dec 7, 2024
1 parent ae6c917 commit 3ecfa0f
Show file tree
Hide file tree
Showing 87 changed files with 1,118 additions and 1,034 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"request": "launch",
"name": "server",
"type": "node",
"program": "${workspaceFolder}/src/index.ts",
"program": "${workspaceFolder}/src/server.ts",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
"runtimeArgs": [
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ RUN apt-get upgrade -y
RUN apt-get install unzip

FROM scaffold AS cache
RUN deno cache src/index.ts
RUN deno cache src/server.ts

# Fallback and compilation has broken in some instances
#ENTRYPOINT ["deno", "run", "--allow-net", "--allow-env", "--allow-read", "--allow-sys", "src/index.ts"]
#ENTRYPOINT ["deno", "run", "--allow-net", "--allow-env", "--allow-read", "--allow-sys", "src/server.ts"]

FROM cache AS build
RUN deno check src/index.ts
RUN deno compile --allow-net --allow-env --allow-read --allow-sys --output=/usr/on-the-edge src/index.ts
RUN deno check src/server.ts
RUN deno compile --allow-net --allow-env --allow-read --allow-sys --output=/usr/on-the-edge src/server.ts

FROM build AS final
RUN rm -r /usr/app
Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"proseWrap": "preserve"
},
"tasks": {
"check": "deno check src/index.ts",
"check": "deno check src/server.ts",
"test": "deno test --allow-read --allow-env --allow-net",
"start": "deno run --allow-read --allow-env --allow-net --allow-sys ./src/index.ts",
"start": "deno run --allow-read --allow-env --allow-net --allow-sys ./src/server.ts",
"check-deps": "deno run -A jsr:@check/deps"
}
}
20 changes: 18 additions & 2 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AppContext, ErrorResponse } from '../common/types/core.ts';
import { Repository } from './repository/index.ts';
import { ConfigRepository } 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';
import { Status } from 'oak';

export const config = async ({ state, response }: AppContext) => {
const localSource = new LocalSource(collection('config', state.local));
const repository = new Repository(
const repository = new ConfigRepository(
state.features,
localSource,
);
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/config/local/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './source.ts';
export * from './config-local-source.ts';
export * from './types.ts';
30 changes: 30 additions & 0 deletions src/config/repository/config-repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Features } from '../../common/types/core.ts';
import { ClientConfiguration } from '../transformer/types.ts';
import { LocalSource } from '../local/index.ts';
import { transform } from '../transformer/index.ts';
import { logger } from '../../common/core/index.ts';

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

getConfiguration = async (): Promise<ClientConfiguration | undefined> => {
const config = await this.local.getConfig();

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;
};
}
31 changes: 1 addition & 30 deletions src/config/repository/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
import { Features } from '../../common/types/core.ts';
import { ClientConfiguration } from '../transformer/types.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,
) {}

getConfiguration = async (): Promise<ClientConfiguration | undefined> => {
const config = await this.local.getConfig();

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;
};
}
export * from './config-repository.ts';
46 changes: 46 additions & 0 deletions src/config/transformer/config-transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { WithId } from 'npm/mongodb';
import {
getPlatformSource,
isAnalyticsEnabled,
} from '../../common/experiment/index.ts';
import { Transform } from '../../common/transformer/types.ts';
import { Features } from '../../common/types/core.ts';
import { ConfigDocument } from '../local/types.ts';
import { ClientConfiguration } from './types.ts';
import { PlatformSource } from '../../common/experiment/types.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: WithId<ConfigDocument>;
features: Features;
},
ClientConfiguration
> = ({ document, features }) => {
const platformSource = getPlatformSource(features);
const { image, _id, genres, navigation } = document;
return {
id: idOf(_id),
settings: {
analyticsEnabled: isAnalyticsEnabled(features),
platformSource: platformSource?.api,
},
genres: genres,
image: {
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: navigation,
};
};
48 changes: 2 additions & 46 deletions src/config/transformer/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,2 @@
import { WithId } from 'npm/mongodb';
import {
getPlatformSource,
isAnalyticsEnabled,
} from '../../common/experiment/index.ts';
import { Transform } from '../../common/transformer/types.ts';
import { Features } from '../../common/types/core.ts';
import { ConfigDocument } from '../local/types.ts';
import { ClientConfiguration } from './types.ts';
import { PlatformSource } from '../../common/experiment/types.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: WithId<ConfigDocument>;
features: Features;
},
ClientConfiguration
> = ({ document, features }) => {
const platformSource = getPlatformSource(features);
const { image, _id, genres, navigation } = document;
return {
id: idOf(_id),
settings: {
analyticsEnabled: isAnalyticsEnabled(features),
platformSource: platformSource?.api,
},
genres: genres,
image: {
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: navigation,
};
};
export * from './config-transformer.ts';
export * from './types.ts';
31 changes: 31 additions & 0 deletions src/news/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { AppContext } from '../common/types/core.ts';
import { isNewsApiv2Enabled } from '../common/experiment/index.ts';
import LocalSource from './local/news.local.source.ts';
import { STATUS_CODE } from 'std/http';
import { collection } from '../common/mongo/index.ts';
import { NewsRepository } from './repository/index.ts';

export const newsWorker = async ({ response, state }: AppContext) => {
const { local } = state;
const repository = new NewsRepository(
new LocalSource(collection('news', local)),
);

await repository.sync();
response.status = STATUS_CODE.NoContent;
};

export const news = async ({ response, state }: AppContext) => {
const { local, features } = state;
const repository = new NewsRepository(
new LocalSource(collection('news', local)),
);

if (isNewsApiv2Enabled(features)) {
response.type = 'application/json';
response.body = await repository.getLatest();
} else {
response.type = 'application/xml';
response.body = await repository.getLatestLegacy();
}
};
32 changes: 1 addition & 31 deletions src/news/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1 @@
import type { AppContext } from '../common/types/core.ts';
import { isNewsApiv2Enabled } from '../common/experiment/index.ts';
import NewsRepository, {} from './repository/index.ts';
import LocalSource from './local/source.ts';
import { STATUS_CODE } from 'std/http';
import { collection } from '../common/mongo/index.ts';

export const newsWorker = async ({ response, state }: AppContext) => {
const { local } = state;
const repository = new NewsRepository(
new LocalSource(collection('news', local)),
);

await repository.sync();
response.status = STATUS_CODE.NoContent;
};

export const news = async ({ response, state }: AppContext) => {
const { local, features } = state;
const repository = new NewsRepository(
new LocalSource(collection('news', local)),
);

if (isNewsApiv2Enabled(features)) {
response.type = 'application/json';
response.body = await repository.getLatest();
} else {
response.type = 'application/xml';
response.body = await repository.getLatestLegacy();
}
};
export * from './controller.ts';
3 changes: 2 additions & 1 deletion src/news/local/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './transformer.ts';
export * from './news.local.transformer.ts';
export * from './news.local.source.ts';
export * from './types.ts';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Collection, Filter, FindOptions, ObjectId, WithId } from 'npm/mongodb';
import { logger } from '../../common/core/logger.ts';
import { IPaging } from '../../common/types/paging.ts';
import { IResponse } from '../../common/types/response.ts';
import { toDocument, toEntity } from '../mapper.ts';
import { toDocument, toEntity } from '../mapper/index.ts';
import { News } from '../types.ts';
import { NewsDocument, NewsId } from './types.ts';
import { between } from 'optic';
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/news/mapper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './news.mapper.ts';
6 changes: 3 additions & 3 deletions src/news/mapper.ts → src/news/mapper/news.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { News, NewsEntity } from './types.ts';
import { NewsDocument } from './local/types.ts';
import { News, NewsEntity } from '../types.ts';
import { NewsDocument } from '../local/types.ts';
import { OptionalId, WithId } from 'npm/mongodb';
import { idOf } from '../common/mongo/index.ts';
import { idOf } from '../../common/mongo/index.ts';

export const toEntity = (data: WithId<NewsDocument>): NewsEntity => {
return {
Expand Down
Loading

0 comments on commit 3ecfa0f

Please sign in to comment.