diff --git a/src/api/api.ts b/src/api/api.ts index 4934fca..3e113eb 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -1,8 +1,9 @@ import axios from "axios" import TurndownService from "turndown" -import { NetworkResponse } from "../types/network" -import { OperationsResponse } from "../types/operation" -import { Network as MinimalNetwork, ProductsResponse } from "../types/product" +import { ApiResponse } from "../types/api" +import { NetworkData } from "../types/network" +import { OperationsData } from "../types/operation" +import { ProductNetwork, ProductsData } from "../types/product" import { findToken, getSessionId } from "../util/credential" const api = axios.create({ @@ -36,7 +37,7 @@ const getOperationsStatus = async ( url: string, token: string, sessionId: string, - callback: (operations: OperationsResponse) => void + callback: (operations: OperationsData) => void ) => { try { const response = await api.get(url, { @@ -46,7 +47,12 @@ const getOperationsStatus = async ( "X-Requested-With": "XMLHttpRequest", }, }) - callback(response.data) + const data: ApiResponse = response.data + if (data.status != "ok") { + console.error("Could not get outage data:", JSON.stringify(data)) + } else { + callback(data.data) + } } catch (err) { console.error(err) } @@ -57,7 +63,7 @@ const getAvailableNetworks = async ( address: string, token: string, sessionId: string, - callback: (response: NetworkResponse) => void + callback: (response: NetworkData) => void ) => { try { const response = await api.post( @@ -73,7 +79,30 @@ const getAvailableNetworks = async ( }, } ) - callback(response.data) + + const data: ApiResponse = response.data + + if (data.status != "ok") { + console.error("Could not get outage data:", JSON.stringify(data)) + } else { + switch (data.data.type) { + case "DEFAULT": + callback(data.data) + break + + case "EMPTY_NETWORK": + case "COVERAGE_NOT_FOUND": + console.error( + "Could not find any networks available for address:", + address + ) + break + + default: + console.log("Unhandled network list type:", data.data.type) + break + } + } } catch (err) { console.error(err) } @@ -81,10 +110,10 @@ const getAvailableNetworks = async ( const getAvailableProducts = async ( url: string, - networks: MinimalNetwork[], + networks: ProductNetwork[], token: string, sessionId: string, - callback: (response: ProductsResponse) => void + callback: (response: ProductsData) => void ) => { try { const response = await api.post( @@ -100,7 +129,12 @@ const getAvailableProducts = async ( }, } ) - callback(response.data) + const data: ApiResponse = response.data + if (data.status != "ok") { + console.error("Could not get available products:", JSON.stringify(data)) + } else { + callback(data.data) + } } catch (err) { console.error(err) } @@ -108,7 +142,7 @@ const getAvailableProducts = async ( export const getProducts = ( address: string, - callback: (response: ProductsResponse) => void + callback: (response: ProductsData) => void ) => { getTokens(tokenUrl, (csrfToken: string, cookieSession: string) => { getAvailableNetworks( @@ -116,8 +150,8 @@ export const getProducts = ( address, csrfToken, cookieSession, - (response: NetworkResponse) => { - const minimalNetworks: MinimalNetwork[] = response.data.networks.map( + (data: NetworkData) => { + const productNetworks: ProductNetwork[] = data.networks.map( (network) => { return { city: network.city, @@ -127,7 +161,7 @@ export const getProducts = ( ) getAvailableProducts( apiProductsUrl, - minimalNetworks, + productNetworks, csrfToken, cookieSession, (response) => { @@ -141,7 +175,7 @@ export const getProducts = ( export const getOperations = ( postalCode: string, - callback: (operations: OperationsResponse) => void + callback: (operations: OperationsData) => void ) => { const apiUrl = apiOperationsUrl + "/" + postalCode getTokens(tokenUrl, (csrfToken: string, cookieSession: string) => { diff --git a/src/app.ts b/src/app.ts index 6f86826..fd0a00e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -43,8 +43,8 @@ const doPatrol = async (callback: (report: string) => void) => { zonePromises.push( new Promise((resolve, reject) => { getOperations(process.env.POSTAL_CODE, (operations) => { - const currentOutages = operations.data.open - const plannedOutages = operations.data.future + const currentOutages = operations.open + const plannedOutages = operations.future if (currentOutages.length > 0 || plannedOutages.length > 0) { const message = generateOutageMessage( currentOutages, @@ -68,9 +68,9 @@ const doPatrol = async (callback: (report: string) => void) => { } else { zonePromises.push( new Promise((resolve, reject) => { - getProducts(process.env.ADDRESS, async (result) => { + getProducts(process.env.ADDRESS, async (data) => { const listedSubscription = getListedSubscription( - result.data.products, + data.products, currentSubscription.speed ) diff --git a/src/types/api.ts b/src/types/api.ts new file mode 100644 index 0000000..d314bdc --- /dev/null +++ b/src/types/api.ts @@ -0,0 +1,4 @@ +export type ApiResponse = { + status: string + data: T +} diff --git a/src/types/network.ts b/src/types/network.ts index 0f7d414..ad5e52a 100644 --- a/src/types/network.ts +++ b/src/types/network.ts @@ -7,10 +7,7 @@ export type Network = { redirectUrl: string } -export type NetworkResponse = { - status: string - data: { - type: string - networks: Network[] - } -} \ No newline at end of file +export type NetworkData = { + type: string + networks: Network[] +} diff --git a/src/types/operation.ts b/src/types/operation.ts index d092728..b144b89 100644 --- a/src/types/operation.ts +++ b/src/types/operation.ts @@ -10,12 +10,9 @@ export type Operation = { messages: Message[] } -export type OperationsResponse = { - status: string - data: { - open: Operation[] - future: Operation[] - closed: Operation[] - all: Operation[] - } +export type OperationsData = { + open: Operation[] + future: Operation[] + closed: Operation[] + all: Operation[] } diff --git a/src/types/product.ts b/src/types/product.ts index ed7c9c3..1effbee 100644 --- a/src/types/product.ts +++ b/src/types/product.ts @@ -29,7 +29,7 @@ export type Category = { hidden: boolean } -export type Network = { +export type ProductNetwork = { city: string network: string } @@ -44,7 +44,7 @@ export type Product = { params: Param[] category: Category title: string - network: Network + network: ProductNetwork id: number internalTitle: string description: string @@ -55,12 +55,7 @@ export type Product = { hidden: boolean } -export type Data = { +export type ProductsData = { products: Product[] hasOtherProducts: boolean } - -export type ProductsResponse = { - status: string - data: Data -}