Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

FRONTEND-8643 :: Feature :: Create NetworkDataSource repository providers #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ApiRequestService } from "./api-request.service";
import { Type } from "../../helpers";
import { DataSource, GetDataSource, PutDataSource } from "./data-source";
import { NetworkDataSource } from "./network.data-source";
import { DataSourceMapper, GetDataSourceMapper, PutDataSourceMapper } from "./data-source-mapper";
import { ArrayMapper, BlankMapper, JsonDeserializerMapper } from "../mapper/mapper";

export function provideGetArrayNetworkDataSource<T extends unknown | void>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by T extends unknown | void? Isn't this the same as just T? Also, regarding the void, does GetDataSource<void[]> make sense? We can probably leave it as:

Suggested change
export function provideGetArrayNetworkDataSource<T extends unknown | void>(
export function provideGetArrayNetworkDataSource<T>(

Copy link
Contributor Author

@orioljp orioljp Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doup The thing is that the generic type should allow void as a type, and having T you cannot create a GetDataSource<void>.

Since this is a generic library, I didn't want to restrict the usage because the API might not be ours and might return nothing at some endpoint with a call of type GET. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me

requestService: ApiRequestService,
type?: Type<T>,
): GetDataSource<T[]> {
const dataSource = new NetworkDataSource(requestService);
return new GetDataSourceMapper(
dataSource,
type
? new ArrayMapper(new JsonDeserializerMapper(type))
: new ArrayMapper(new BlankMapper())
);
}

export function providePutArrayNetworkDataSource<T extends unknown | void>(
requestService: ApiRequestService,
type?: Type<T>,
): PutDataSource<T[]> {
const dataSource = new NetworkDataSource(requestService);
return new PutDataSourceMapper(
dataSource,
type
? new ArrayMapper(new JsonDeserializerMapper(type))
: new ArrayMapper(new BlankMapper()),
new ArrayMapper(new BlankMapper()),
);
}

export function provideArrayNetworkDataSource<T extends unknown | void>(
requestService: ApiRequestService,
type?: Type<T>,
): DataSource<T[]> {
const dataSource = new NetworkDataSource(requestService);
return new DataSourceMapper(
dataSource,
dataSource,
dataSource,
type
? new ArrayMapper(new JsonDeserializerMapper(type))
: new ArrayMapper(new BlankMapper()),
new ArrayMapper(new BlankMapper()),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ApiRequestService } from "./api-request.service";
import { Type } from "../../helpers";
import { DataSource, DeleteDataSource, GetDataSource, PutDataSource } from "./data-source";
import { NetworkDataSource } from "./network.data-source";
import { DataSourceMapper, GetDataSourceMapper, PutDataSourceMapper } from "./data-source-mapper";
import { BlankMapper, JsonDeserializerMapper } from "../mapper/mapper";

export function provideNetworkDataSource<T extends unknown | void>(requestService: ApiRequestService, type?: Type<T>): DataSource<T> {
const dataSource = new NetworkDataSource(requestService);
return new DataSourceMapper(
dataSource,
dataSource,
dataSource,
type ? new JsonDeserializerMapper(type) : new BlankMapper<T>(),
new BlankMapper<T>(),
);
}

export function provideGetNetworkDataSource<T extends unknown | void>(requestService: ApiRequestService, type?: Type<T>): GetDataSource<T> {
const dataSource = new NetworkDataSource(requestService);
return new GetDataSourceMapper(
dataSource,
type ? new JsonDeserializerMapper(type) : new BlankMapper<T>(),
);
}

export function providePutNetworkDataSource<T extends unknown | void>(requestService: ApiRequestService, type?: Type<T>): PutDataSource<T> {
const dataSource = new NetworkDataSource(requestService);
return new PutDataSourceMapper(
dataSource,
type ? new JsonDeserializerMapper(type) : new BlankMapper<T>(),
new BlankMapper<T>(),
);
}

export function provideDeleteNetworkDataSource(requestService: ApiRequestService): DeleteDataSource {
return new NetworkDataSource(requestService);
}

25 changes: 0 additions & 25 deletions packages/core/src/repository/data-source/network.data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,3 @@ export class NetworkDataSource implements DataSource<unknown> {
throw new MethodNotImplementedError();
}
}

export function provideDefaultNetworkDataSource<T extends unknown | void>(requestService: ApiRequestService, type?: Type<T>): DataSource<T> {
const dataSource = new NetworkDataSource(requestService);
return new DataSourceMapper(
dataSource,
dataSource,
dataSource,
type ? new JsonDeserializerMapper(type) : new BlankMapper<T>(),
new BlankMapper<T>(),
);
}

export function provideDefaultArrayNetworkDataSource<T>(
requestService: ApiRequestService,
type: Type<T>,
): DataSource<T[]> {
const dataSource = new NetworkDataSource(requestService);
return new DataSourceMapper(
dataSource,
dataSource,
dataSource,
new ArrayMapper(new JsonDeserializerMapper(type)),
new ArrayMapper(new BlankMapper()),
);
}
62 changes: 62 additions & 0 deletions packages/core/src/repository/network-array.repository-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { GetRepository, PutRepository, Repository } from "./repository";
import { ArrayMapper, Mapper} from "./mapper/mapper";
import {
SingleDataSourceRepository,
SingleGetDataSourceRepository,
SinglePutDataSourceRepository
} from "./single-data-source.repository";
import { GetRepositoryMapper, PutRepositoryMapper, RepositoryMapper } from "./repository-mapper";
import { ApiRequestService } from "./data-source/api-request.service";
import { Type } from "../helpers";
import {
provideArrayNetworkDataSource,
provideGetArrayNetworkDataSource,
providePutArrayNetworkDataSource
} from "./data-source/array-network.data-source.provider";

export function provideGetArrayNetworkRepository<In, Out>(
apiRequestService: ApiRequestService,
toOutMapper: Mapper<In, Out>,
type: Type<In>
): GetRepository<Out[]> {
const networkDatasource = provideGetArrayNetworkDataSource<In>(apiRequestService, type);
const repository = new SingleGetDataSourceRepository(networkDatasource);
return new GetRepositoryMapper<In[], Out[]>(repository, new ArrayMapper(toOutMapper));
}

export function providePutArrayNetworkRepository<In, Out>(
apiRequestService: ApiRequestService,
toInMapper: Mapper<Out, In>,
toOutMapper: Mapper<In, Out>,
type: Type<In>
): PutRepository<Out[]> {
const networkDatasource = providePutArrayNetworkDataSource<In>(apiRequestService, type);
const repository = new SinglePutDataSourceRepository(networkDatasource);
return new PutRepositoryMapper<In[], Out[]>(
repository,
new ArrayMapper(toOutMapper),
new ArrayMapper(toInMapper)
);
}

export function provideArrayNetworkRepository<In, Out>(
apiRequestService: ApiRequestService,
toInMapper: Mapper<Out, In>,
toOutMapper: Mapper<In, Out>,
type: Type<In>
): Repository<Out[]> {
const networkDatasource = provideArrayNetworkDataSource<In>(apiRequestService, type);
const repository = new SingleDataSourceRepository(
networkDatasource,
networkDatasource,
networkDatasource
);

return new RepositoryMapper<In[], Out[]>(
repository,
repository,
repository,
new ArrayMapper(toOutMapper),
new ArrayMapper(toInMapper)
);
}
67 changes: 67 additions & 0 deletions packages/core/src/repository/network.repository-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { DeleteRepository, GetRepository, PutRepository } from "./repository";
import { Mapper } from "./mapper/mapper";
import {
SingleDataSourceRepository,
SingleDeleteDataSourceRepository,
SingleGetDataSourceRepository,
SinglePutDataSourceRepository
} from "./single-data-source.repository";
import { GetRepositoryMapper, PutRepositoryMapper, RepositoryMapper } from "./repository-mapper";
import { ApiRequestService } from "./data-source/api-request.service";
import { Type } from "../helpers";
import {
provideDeleteNetworkDataSource,
provideGetNetworkDataSource,
provideNetworkDataSource,
providePutNetworkDataSource
} from "./data-source/network.data-source.provider";

export function provideGetNetworkRepository<In, Out>(
apiRequestService: ApiRequestService,
toOutMapper: Mapper<In, Out>,
type?: Type<In>
): GetRepository<Out> {
const networkDatasource = provideGetNetworkDataSource<In>(apiRequestService, type);
const repository = new SingleGetDataSourceRepository(networkDatasource);
return new GetRepositoryMapper<In, Out>(repository, toOutMapper);
}

export function providePutNetworkRepository<In, Out>(
apiRequestService: ApiRequestService,
toInMapper: Mapper<Out, In>,
toOutMapper: Mapper<In, Out>,
type?: Type<In>
): PutRepository<Out> {
const networkDatasource = providePutNetworkDataSource<In>(apiRequestService, type);
const repository = new SinglePutDataSourceRepository(networkDatasource);
return new PutRepositoryMapper<In, Out>(repository, toOutMapper, toInMapper);
}

export function provideDeleteNetworkRepository(
apiRequestService: ApiRequestService
): DeleteRepository {
const networkDatasource = provideDeleteNetworkDataSource(apiRequestService);
return new SingleDeleteDataSourceRepository(networkDatasource);
}

export function provideNetworkRepository<In, Out>(
apiRequestService: ApiRequestService,
toInMapper: Mapper<Out, In>,
toOutMapper: Mapper<In, Out>,
type?: Type<In>
): PutRepository<Out> {
const networkDatasource = provideNetworkDataSource<In>(apiRequestService, type);
const repository = new SingleDataSourceRepository(
networkDatasource,
networkDatasource,
networkDatasource
);

return new RepositoryMapper<In, Out>(
repository,
repository,
repository,
toOutMapper,
toInMapper
);
}