Skip to content

Commit

Permalink
refactor(datasources): unify all metro data sources into one
Browse files Browse the repository at this point in the history
refactor(datasources): unify all metro data sources into one
  • Loading branch information
aalises authored Oct 23, 2020
2 parents 99ba07c + 9532f27 commit 9144116
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 338 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { TMB_API_BASE_URL } from "../config";
import { ApolloError, ValidationError } from "apollo-server-lambda";
import {
import TmbApiDataSource from "./TmbApiDataSource";
import type {
FindByInput,
MetroLine as MetroLineType,
MetroStation as MetroStationType,
MetroLine as MetroLineType,
} from "../../types";
import MetroStationDataSource from "./MetroStationsDataSource";
import TmbApiDataSource from "./TmbApiDataSource";
import { ApolloError, ValidationError } from "apollo-server-lambda";

export interface MetroLineAPIType {
type: string;
Expand Down Expand Up @@ -55,10 +53,104 @@ interface MetroLinesAPIType {
};
}

export default class MetroLinesDataSource extends TmbApiDataSource {
constructor() {
super();
this.baseURL = TMB_API_BASE_URL;
export interface MetroStationAPIType {
type: string;
id: string;
geometry: {
type: string;
coordinates: number[];
};
geometry_name: string;
properties: {
CODI_GRUP_STACIO: number;
NOM_STACIO: string;
PICTO: string;
DATA: string;
};
}

interface MetroStationsAPIType {
type: "FeatureCollection";
features: ReadonlyArray<MetroStationAPIType>;
totalFeatures: number;
numberMatched: number;
numberReturned: number;
timeStamp: string;
crs: {
type: "name";
properties: {
name: string;
};
};
}

export default class MetroDataSource extends TmbApiDataSource {
//Transforms e.g. L1L2 into [L1, L2]
parseLines(lines: string): string[] {
return lines.replace(/L/g, ",L").split(",").filter(Boolean);
}

metroStationReducer(data: MetroStationAPIType): MetroStationType {
return {
id: data.properties["CODI_GRUP_ESTACIO"],
name: data.properties["NOM_ESTACIO"],
location: {
longitude: data.geometry.coordinates[0],
latitude: data.geometry.coordinates[1],
},
lines: this.parseLines(data.properties["PICTO"]),
};
}

async getStation({
id,
name,
}: FindByInput): Promise<MetroStationType | null> {
if (!id && !name) {
return new ValidationError(
"You need to provide either a valid ID or a valid name"
);
}

const path = ["estacions", id].filter(Boolean).join("/");
const nameFilterParameter = name ? { filter: `NOM_ESTACIO='${name}'` } : {};

const response: MetroStationsAPIType | null = await this.get(
path,
nameFilterParameter
);

if (Array.isArray(response?.features) && response?.features.length === 0) {
return new ApolloError(
`No stations were found with these parameters: ${JSON.stringify({
id,
name,
})}`
);
}

const station: MetroStationAPIType | null = response?.features?.[0] ?? null;

if (station == null) {
return new ApolloError("The station object returned did not exist");
}

return this.metroStationReducer(station);
}

async getAllStations(): Promise<{
numberOfStations: number | null;
stations: MetroStationType[];
}> {
const response: MetroStationsAPIType | null = await this.get("estacions");

return {
numberOfStations: response?.numberReturned ?? null,
stations:
response?.features?.map((station: MetroStationAPIType) =>
this.metroStationReducer(station)
) ?? [],
};
}

metroLineReducer({ properties }: MetroLineAPIType): MetroLineType {
Expand All @@ -82,9 +174,8 @@ export default class MetroLinesDataSource extends TmbApiDataSource {
const response = await this.get(path, nameFilterParameter);

const stations =
response?.features?.map((station) =>
new MetroStationDataSource().metroStationReducer(station)
) ?? [];
response?.features?.map((station) => this.metroStationReducer(station)) ??
[];

return stations;
}
Expand Down
107 changes: 0 additions & 107 deletions src/datasources/MetroStationsDataSource.ts

This file was deleted.

Loading

0 comments on commit 9144116

Please sign in to comment.