Skip to content

Commit eae9c22

Browse files
rename fetchData to fetchForeignAPI
1 parent 698074e commit eae9c22

File tree

10 files changed

+19
-19
lines changed

10 files changed

+19
-19
lines changed

src/commands/utils/liveFlights.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApplicationCommandType, Colors } from 'discord.js';
22
import { Request } from 'node-fetch';
33
import { ZodError } from 'zod';
4-
import { slashCommand, slashCommandStructure, makeEmbed, Logger, fetchData, TelexCountSchema } from '../../lib';
4+
import { slashCommand, slashCommandStructure, makeEmbed, Logger, fetchForeignAPI, TelexCountSchema } from '../../lib';
55

66
const data = slashCommandStructure({
77
name: 'live-flights',
@@ -16,7 +16,7 @@ export default slashCommand(data, async ({ interaction }) => {
1616
await interaction.deferReply();
1717

1818
try {
19-
const flights = await fetchData(new Request(`${FBW_API_BASE_URL}/txcxn/_count`), TelexCountSchema);
19+
const flights = await fetchForeignAPI(new Request(`${FBW_API_BASE_URL}/txcxn/_count`), TelexCountSchema);
2020
const flightsEmbed = makeEmbed({
2121
title: 'Live Flights',
2222
description: `There are currently **${flights}** active flights with TELEX enabled.`,

src/commands/utils/metar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApplicationCommandOptionType, ApplicationCommandType, Colors } from 'discord.js';
22
import { Request } from 'node-fetch';
33
import { ZodError } from 'zod';
4-
import { constantsConfig, fetchData, makeEmbed, makeLines, slashCommand, slashCommandStructure, Metar, MetarSchema } from '../../lib';
4+
import { constantsConfig, fetchForeignAPI, makeEmbed, makeLines, slashCommand, slashCommandStructure, Metar, MetarSchema } from '../../lib';
55

66
const data = slashCommandStructure({
77
name: 'metar',
@@ -41,7 +41,7 @@ export default slashCommand(data, async ({ interaction }) => {
4141

4242
let metar: Metar;
4343
try {
44-
metar = await fetchData<Metar>(new Request(`https://avwx.rest/api/metar/${icao}`, {
44+
metar = await fetchForeignAPI<Metar>(new Request(`https://avwx.rest/api/metar/${icao}`, {
4545
method: 'GET',
4646
headers: { Authorization: metarToken },
4747
}), MetarSchema);

src/commands/utils/simbriefData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ApplicationCommandOptionType, ApplicationCommandType, Colors } from 'di
22
import moment from 'moment';
33
import { Request } from 'node-fetch';
44
import { ZodError } from 'zod';
5-
import { slashCommand, makeEmbed, makeLines, slashCommandStructure, SimbriefFlightPlan, fetchData, SimbriefFlightPlanSchema } from '../../lib';
5+
import { slashCommand, makeEmbed, makeLines, slashCommandStructure, SimbriefFlightPlan, fetchForeignAPI, SimbriefFlightPlanSchema } from '../../lib';
66

77
const data = slashCommandStructure({
88
name: 'simbrief-data',
@@ -81,7 +81,7 @@ export default slashCommand(data, async ({ interaction }) => {
8181

8282
let flightplan: SimbriefFlightPlan;
8383
try {
84-
flightplan = await fetchData<SimbriefFlightPlan>(new Request(`https://www.simbrief.com/api/xml.fetcher.php?json=1&userid=${simbriefId}&username=${simbriefId}`), SimbriefFlightPlanSchema);
84+
flightplan = await fetchForeignAPI<SimbriefFlightPlan>(new Request(`https://www.simbrief.com/api/xml.fetcher.php?json=1&userid=${simbriefId}&username=${simbriefId}`), SimbriefFlightPlanSchema);
8585
} catch (e) {
8686
if (e instanceof ZodError) {
8787
return interaction.editReply({ embeds: [errorEmbed('The API returned unknown data.')] });

src/commands/utils/station.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApplicationCommandOptionType, ApplicationCommandType, Colors } from 'discord.js';
22
import { Request } from 'node-fetch';
33
import { z, ZodError } from 'zod';
4-
import { AVWXRunwaySchema, AVWXStation, AVWXStationSchema, fetchData, Logger, makeEmbed, makeLines, slashCommand, slashCommandStructure } from '../../lib';
4+
import { AVWXRunwaySchema, AVWXStation, AVWXStationSchema, fetchForeignAPI, Logger, makeEmbed, makeLines, slashCommand, slashCommandStructure } from '../../lib';
55

66
type Runway = z.infer<typeof AVWXRunwaySchema>;
77

@@ -51,7 +51,7 @@ export default slashCommand(data, async ({ interaction }) => {
5151

5252
let station: AVWXStation;
5353
try {
54-
station = await fetchData<AVWXStation>(new Request(`https://avwx.rest/api/station/${icao}`, {
54+
station = await fetchForeignAPI<AVWXStation>(new Request(`https://avwx.rest/api/station/${icao}`, {
5555
method: 'GET',
5656
headers: { Authorization: stationToken },
5757
}), AVWXStationSchema);

src/commands/utils/taf.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApplicationCommandOptionType, ApplicationCommandType, Colors } from 'discord.js';
22
import { Request } from 'node-fetch';
33
import { ZodError } from 'zod';
4-
import { Logger, TAF, TafSchema, fetchData, makeEmbed, makeLines, slashCommand, slashCommandStructure } from '../../lib';
4+
import { Logger, TAF, TafSchema, fetchForeignAPI, makeEmbed, makeLines, slashCommand, slashCommandStructure } from '../../lib';
55

66
const data = slashCommandStructure({
77
name: 'taf',
@@ -51,7 +51,7 @@ export default slashCommand(data, async ({ interaction }) => {
5151

5252
let taf: TAF;
5353
try {
54-
taf = await fetchData<TAF>(new Request(`https://avwx.rest/api/taf/${icao}`, {
54+
taf = await fetchForeignAPI<TAF>(new Request(`https://avwx.rest/api/taf/${icao}`, {
5555
method: 'GET',
5656
headers: { Authorization: tafToken },
5757
}), TafSchema);

src/commands/utils/vatsim/functions/vatsimEvents.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChatInputCommandInteraction, Colors, EmbedField } from 'discord.js';
22
import { Request } from 'node-fetch';
3-
import { Logger, VatsimEvents, VatsimEventsSchema, fetchData, makeEmbed } from '../../../../lib';
3+
import { Logger, VatsimEvents, VatsimEventsSchema, fetchForeignAPI, makeEmbed } from '../../../../lib';
44

55
const BASE_VATSIM_URL = 'https://my.vatsim.net';
66

@@ -17,7 +17,7 @@ const handleLocaleDateString = (date: Date) => date.toLocaleDateString('en-US',
1717

1818
export async function handleVatsimEvents(interaction: ChatInputCommandInteraction<'cached'>) {
1919
try {
20-
const response = await fetchData<VatsimEvents>(new Request(`${BASE_VATSIM_URL}/api/v1/events/all`), VatsimEventsSchema);
20+
const response = await fetchForeignAPI<VatsimEvents>(new Request(`${BASE_VATSIM_URL}/api/v1/events/all`), VatsimEventsSchema);
2121

2222
const filteredEvents = response.data.filter((event) => event.type === 'Event');
2323
const finalList = filteredEvents.slice(0, 5);

src/commands/utils/vatsim/vatsim.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApplicationCommandOptionType, ApplicationCommandType, Colors } from 'discord.js';
22
import { Request } from 'node-fetch';
33
import { ZodError } from 'zod';
4-
import { Logger, VatsimData, VatsimDataSchema, fetchData, makeEmbed, slashCommand, slashCommandStructure } from '../../../lib';
4+
import { Logger, VatsimData, VatsimDataSchema, fetchForeignAPI, makeEmbed, slashCommand, slashCommandStructure } from '../../../lib';
55
import { handleVatsimControllers } from './functions/vatsimControllers';
66
import { handleVatsimEvents } from './functions/vatsimEvents';
77
import { handleVatsimObservers } from './functions/vatsimObservers';
@@ -92,7 +92,7 @@ export default slashCommand(data, async ({ interaction }) => {
9292
// Fetch VATSIM data
9393
let vatsimData: VatsimData;
9494
try {
95-
vatsimData = await fetchData<VatsimData>(new Request('https://data.vatsim.net/v3/vatsim-data.json'), VatsimDataSchema);
95+
vatsimData = await fetchForeignAPI<VatsimData>(new Request('https://data.vatsim.net/v3/vatsim-data.json'), VatsimDataSchema);
9696
} catch (e) {
9797
if (e instanceof ZodError) {
9898
e.issues.forEach((issue) => Logger.error(`[zod Issue VATSIM Data] Code: ${issue.code}, Path: ${issue.path.join('.')}, Message: ${issue.message}`));

src/commands/utils/wolframAlpha.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApplicationCommandOptionType, ApplicationCommandType, Colors } from 'discord.js';
22
import { Request } from 'node-fetch';
33
import { z, ZodError } from 'zod';
4-
import { fetchData, Logger, makeEmbed, makeLines, slashCommand, slashCommandStructure, WolframAlphaData, WolframAlphaDataSchema, WolframAlphaPodSchema, WolframAlphaSubpodSchema } from '../../lib';
4+
import { fetchForeignAPI, Logger, makeEmbed, makeLines, slashCommand, slashCommandStructure, WolframAlphaData, WolframAlphaDataSchema, WolframAlphaPodSchema, WolframAlphaSubpodSchema } from '../../lib';
55

66
type Pod = z.infer<typeof WolframAlphaPodSchema>;
77
type Subpod = z.infer<typeof WolframAlphaSubpodSchema>;
@@ -62,7 +62,7 @@ export default slashCommand(data, async ({ interaction }) => {
6262

6363
let response: WolframAlphaData;
6464
try {
65-
response = await fetchData<WolframAlphaData>(new Request(`${WOLFRAMALPHA_API_URL}${searchParams.toString()}`), WolframAlphaDataSchema);
65+
response = await fetchForeignAPI<WolframAlphaData>(new Request(`${WOLFRAMALPHA_API_URL}${searchParams.toString()}`), WolframAlphaDataSchema);
6666
} catch (e) {
6767
if (e instanceof ZodError) {
6868
return interaction.editReply({ embeds: [errorEmbed('Wolfram Alpha returned unknown data.')] });

src/lib/apis/fetchData.ts renamed to src/lib/apis/fetchForeignAPI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Logger } from '../logger';
99
* @param zodSchema The [Zod](https://github.com/colinhacks/zod) schema that the returned data conforms to. The promise will reject if the returned data does not conform to the schema provided.
1010
* @returns A promise that resolves to the expected type or rejects with an [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error).
1111
*/
12-
export const fetchData = async <ReturnType = unknown>(request: Request, zodSchema: ZodSchema<ReturnType>): Promise<ReturnType> => {
12+
export const fetchForeignAPI = async <ReturnType = unknown>(request: Request, zodSchema: ZodSchema<ReturnType>): Promise<ReturnType> => {
1313
let response: Response;
1414
try {
1515
response = await fetch(request);
@@ -32,8 +32,8 @@ export const fetchData = async <ReturnType = unknown>(request: Request, zodSchem
3232

3333
if (!result.success) {
3434
Logger.error('[zod] Data validation failed:');
35+
console.log(data); // winston doesn't correctly print object at the moment
3536
result.error.issues.forEach((issue) => Logger.error(`Code: ${issue.code}, Path: ${issue.path.join('.')}, Message: ${issue.message}`));
36-
console.log(data);
3737
throw result.error;
3838
}
3939

src/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export * from './schedulerJobs/sendHeartbeat';
2323
export * from './schedulerJobs/postBirthdays';
2424

2525
// API Wrapper
26-
export * from './apis/fetchData';
26+
export * from './apis/fetchForeignAPI';
2727
export * from './apis/zodSchemas/vatsim/vatsimEventsSchemas';
2828
export * from './apis/zodSchemas/vatsim/vatsimDataSchemas';
2929
export * from './apis/zodSchemas/avwx/metarSchemas';

0 commit comments

Comments
 (0)