Skip to content

Commit

Permalink
Allow chancing of data source
Browse files Browse the repository at this point in the history
  • Loading branch information
nistei committed Nov 20, 2020
1 parent 4acc02c commit 78848c3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flybywiresim/api-client",
"version": "0.1.2",
"version": "0.1.3",
"description": "Client library for the FlyByWire Simulations API",
"main": "lib/index.js",
"scripts": {
Expand Down
59 changes: 37 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export class TelexNotConnectedError extends Error {
}


class NXApi {
public static url = "https://api.flybywiresim.com";
export class NXApi {
public static url = new URL("https://api.flybywiresim.com");
}

export class Metar {
Expand All @@ -99,12 +99,12 @@ export class Metar {
throw new Error("No ICAO provided");
}

let url = `${NXApi.url}/metar/${icao}`;
let url = new URL(`/metar/${icao}`, NXApi.url);
if (source) {
url += `?source=${source}`;
url.searchParams.set("source", source);
}

return fetch(url)
return fetch(url.href)
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand All @@ -121,12 +121,12 @@ export class Atis {
throw new Error("No ICAO provided");
}

let url = `${NXApi.url}/atis/${icao}`;
let url = new URL(`/atis/${icao}`, NXApi.url);
if (source) {
url += `?source=${source}`;
url.searchParams.set("source", source);
}

return fetch(url)
return fetch(url.href)
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand All @@ -143,12 +143,12 @@ export class Taf {
throw new Error("No ICAO provided");
}

let url = `${NXApi.url}/taf/${icao}`;
let url = new URL(`/taf/${icao}`, NXApi.url);
if (source) {
url += `?source=${source}`;
url.searchParams.set("source", source);
}

return fetch(url)
return fetch(url.href)
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand All @@ -170,7 +170,9 @@ export class Telex {
"Content-Type": "application/json"
};

return fetch(`${NXApi.url}/txcxn`, {method: "POST", body: JSON.stringify(connectBody), headers})
let url = new URL(`/txcxn`, NXApi.url);

return fetch(url.href, {method: "POST", body: JSON.stringify(connectBody), headers})
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand All @@ -193,7 +195,9 @@ export class Telex {
Authorization: Telex.buildToken()
};

return fetch(`${NXApi.url}/txcxn`, {method: "PUT", body: JSON.stringify(connectBody), headers})
let url = new URL(`/txcxn`, NXApi.url);

return fetch(url.href, {method: "PUT", body: JSON.stringify(connectBody), headers})
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand All @@ -210,7 +214,9 @@ export class Telex {
Authorization: Telex.buildToken()
};

return fetch(`${NXApi.url}/txcxn`, {method: "DELETE", headers})
let url = new URL(`/txcxn`, NXApi.url);

return fetch(url.href, {method: "DELETE", headers})
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand All @@ -232,7 +238,9 @@ export class Telex {
Authorization: Telex.buildToken()
};

return fetch(`${NXApi.url}/txmsg`, {method: "POST", body: JSON.stringify(body), headers})
let url = new URL(`/txmsg`, NXApi.url);

return fetch(url.href, {method: "POST", body: JSON.stringify(body), headers})
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand All @@ -249,7 +257,9 @@ export class Telex {
Authorization: Telex.buildToken()
};

return fetch(`${NXApi.url}/txmsg`, {method: "GET", headers})
let url = new URL(`/txmsg`, NXApi.url);

return fetch(url.href, {method: "GET", headers})
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand All @@ -260,15 +270,15 @@ export class Telex {
}

public static fetchConnections(skip?: number, take?: number): Promise<Paginated<TelexConnection>> {
let url = `${NXApi.url}/txcxn?`;
let url = new URL(`/txcxn`, NXApi.url);
if (skip) {
url += `skip=${skip}&`;
url.searchParams.set("skip", skip.toString());
}
if (take) {
url += `take=${take}`;
url.searchParams.append("take", take.toString());
}

return fetch(url, {method: "GET"})
return fetch(url.href, {method: "GET"})
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand All @@ -279,7 +289,9 @@ export class Telex {
}

public static fetchConnection(id: string): Promise<TelexConnection> {
return fetch(`${NXApi.url}/txcxn/${id}`, {method: "GET"})
let url = new URL(`/txcxn/${id}`, NXApi.url);

return fetch(url.href, {method: "GET"})
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand All @@ -290,7 +302,10 @@ export class Telex {
}

public static findConnection(flightNumber: string): Promise<TelexConnection> {
return fetch(`${NXApi.url}/txcxn/_find?flight=${flightNumber}`, {method: "GET"})
let url = new URL(`/txcxn/_find`, NXApi.url);
url.searchParams.set("flight", flightNumber);

return fetch(url.href, {method: "GET"})
.then((response) => {
if (!response.ok) {
throw new HttpError(response.status);
Expand Down

0 comments on commit 78848c3

Please sign in to comment.