Skip to content

Commit

Permalink
refactor(interfaces): split interfaces into multiple files and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
tdolsen committed Jul 26, 2018
1 parent bfe3bcc commit f8e29fb
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 230 deletions.
34 changes: 18 additions & 16 deletions src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
ForecastResponse,
Language,
Options,
RequestQuery,
Query,
Response,
Units,
WeatherResponse,
} from "./interfaces";
import { isEndpoint, isCoord, isRequestQuery, isCountryCode } from "./utils";
import { isEndpoint, isCoord, isCountryCode, isQuery } from "./utils";
import { ApiException } from "./api-exception";

const DEFAULT_API_URL = "https://api.openweathermap.org/data/2.5";
Expand All @@ -32,6 +32,8 @@ export class ApiClient {
axios: AxiosInstance;
options: Options;

constructor(apiKey: string);
constructor(options: Options);
constructor(options: string | Options) {
if (typeof options === "string") options = { apiKey: options };

Expand All @@ -52,17 +54,17 @@ export class ApiClient {
}

// Returns the current weather for given query.
async current(query: number | string | Coord | RequestQuery<Endpoint.Weather>): Promise<WeatherResponse> {
async current(query: number | string | Coord | Query<Endpoint.Weather>): Promise<WeatherResponse> {
return this.request("weather", this._prepQuery(query));
}

// Alias to `.current()`.
async weather(query: number | string | Coord | RequestQuery<Endpoint.Weather>) {
async weather(query: number | string | Coord | Query<Endpoint.Weather>) {
return this.current(query);
}

// Returns forecast for given query.
async forecast(query: number | string | Coord | RequestQuery<Endpoint.Forecast>): Promise<ForecastResponse> {
async forecast(query: number | string | Coord | Query<Endpoint.Forecast>): Promise<ForecastResponse> {
return this.request("forecast", this._prepQuery(query));
}

Expand Down Expand Up @@ -120,19 +122,19 @@ export class ApiClient {
}

// Returns weather or forecast for given `query`.
async request<T extends Endpoint>(query: RequestQuery<T | undefined>): Promise<Response<T>>;
async request<T extends Endpoint>(endpoint: T | undefined, query: RequestQuery<any>): Promise<Response<T>>;
async request<T extends Endpoint>(query: Query<T>): Promise<Response<T>>;
async request<T extends Endpoint>(endpoint: T | undefined, query: Query<any>): Promise<Response<T>>;
async request<T extends Endpoint>(
queryOrEndpoint: RequestQuery<T | undefined> | T | undefined,
maybeQuery?: RequestQuery<T | undefined>
queryOrEndpoint: Query<T> | T | undefined,
maybeQuery?: Query<T>
): Promise<Response<T>> {
const query: RequestQuery<T> | undefined = isRequestQuery(maybeQuery)
const query: Query<T> | undefined = isQuery(maybeQuery)
? maybeQuery
: isRequestQuery(queryOrEndpoint)
: isQuery(queryOrEndpoint)
? queryOrEndpoint
: undefined;

if (!isRequestQuery(query)) {
if (!isQuery(query)) {
throw new Error("A proper request query must be provided to in order to make a request.");
}

Expand Down Expand Up @@ -178,14 +180,14 @@ export class ApiClient {

// Preps `query` param for `.current()` and `.forecast()`, assuming `number`
// represents a city ID, `string` represents city name and `Coord` represents
// lat/lon coordiantes. If `query` is a valid `RequestQuery` object will use
// lat/lon coordiantes. If `query` is a valid `Query` object will use
// that. Throws an error if no valid type was identified.
protected _prepQuery(query: number | string | Coord | RequestQuery<Endpoint | undefined>) {
protected _prepQuery(query: number | string | Coord | Query<Endpoint>) {
if (typeof query === "number") query = { id: query };
else if (typeof query === "string") query = { q: query };
else if (isCoord(query) || isRequestQuery(query)) query = { ...query };
else if (isCoord(query) || isQuery(query)) query = { ...query };
else throw new Error("Invalid value passed as request query.");

return query as RequestQuery<Endpoint | undefined>;
return query as Query<Endpoint>;
}
}
201 changes: 0 additions & 201 deletions src/interfaces.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/interfaces/endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export namespace Endpoint {
export type Forecast = "forecast";
export type Weather = "weather";

export type Types = Forecast | Weather;

export const Forecast: Forecast = "forecast";
export const Weather: Weather = "weather";

export const TypeMap: { [type: string]: string } = {
[Forecast]: typeof Forecast,
[Weather]: typeof Weather,
};

export function typeOf(x: string) {
return x in TypeMap ? TypeMap[x] : undefined;
}
}

export type Endpoint = Endpoint.Types;
4 changes: 4 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./endpoint";
export * from "./options";
export * from "./query";
export * from "./response";
50 changes: 50 additions & 0 deletions src/interfaces/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { AxiosInstance } from "axios";

import { Endpoint } from "./endpoint";

export type Units = "imperial" | "metric" | "standard";
// export type Endpoint = "forecast" | "weather";
export type AccuracyType = "accurate" | "like";
export type Language =
| "ar"
| "bg"
| "ca"
| "cz"
| "de"
| "el"
| "en"
| "fa"
| "fi"
| "fr"
| "gl"
| "hr"
| "hu"
| "it"
| "ja"
| "kr"
| "la"
| "lt"
| "mk"
| "nl"
| "pl"
| "pt"
| "ro"
| "ru"
| "se"
| "sk"
| "sl"
| "es"
| "tr"
| "ua"
| "vi"
| "zh_cn"
| "zh_tw";

export interface Options {
apiKey: string;
apiUrl?: string;
axios?: AxiosInstance;
endpoint?: Endpoint;
lang?: Language;
units?: Units;
}

0 comments on commit f8e29fb

Please sign in to comment.