Skip to content

Commit dec8382

Browse files
self review
1 parent 13f6404 commit dec8382

File tree

10 files changed

+35
-33
lines changed

10 files changed

+35
-33
lines changed

src/commands/utils/metar.ts

Lines changed: 3 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, fetchForeignAPI, makeEmbed, makeLines, slashCommand, slashCommandStructure, Metar, MetarSchema } from '../../lib';
4+
import { constantsConfig, fetchForeignAPI, makeEmbed, makeLines, slashCommand, slashCommandStructure, Metar, MetarSchema, Logger } from '../../lib';
55

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

4242
let metar: Metar;
4343
try {
44-
metar = await fetchForeignAPI<Metar>(new Request(`https://avwx.rest/api/metar/${icao}`, {
44+
metar = await fetchForeignAPI(new Request(`https://avwx.rest/api/metar/${icao}`, {
4545
method: 'GET',
4646
headers: { Authorization: metarToken },
4747
}), MetarSchema);
4848
} catch (e) {
4949
if (e instanceof ZodError) {
5050
return interaction.editReply({ embeds: [errorEmbed('The API returned unknown data.')] });
5151
}
52+
Logger.error(`Error occured while fetching METAR: ${String(e)}`);
5253
return interaction.editReply({ embeds: [errorEmbed(`An error occurred while fetching the latest METAR for ${icao.toUpperCase()}.`)] });
5354
}
5455

src/commands/utils/simbriefData.ts

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

66
const data = slashCommandStructure({
77
name: 'simbrief-data',
@@ -41,9 +41,9 @@ const simbriefdatarequestEmbed = makeEmbed({
4141
]),
4242
});
4343

44-
const errorEmbed = (errorMessage: string) => makeEmbed({
44+
const errorEmbed = (error: string) => makeEmbed({
4545
title: 'SimBrief Error',
46-
description: errorMessage,
46+
description: error,
4747
color: Colors.Red,
4848
});
4949

@@ -85,6 +85,7 @@ export default slashCommand(data, async ({ interaction }) => {
8585
if (e instanceof ZodError) {
8686
return interaction.editReply({ embeds: [errorEmbed('The API returned unknown data.')] });
8787
}
88+
Logger.error(`Error while fetching SimBrief flightplan: ${String(e)}`);
8889
return interaction.editReply({ embeds: [errorEmbed('An error occurred while fetching the SimBrief flightplan.')] });
8990
}
9091

src/commands/utils/station.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default slashCommand(data, async ({ interaction }) => {
5151

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

src/commands/utils/taf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default slashCommand(data, async ({ interaction }) => {
5151

5252
let taf: TAF;
5353
try {
54-
taf = await fetchForeignAPI<TAF>(new Request(`https://avwx.rest/api/taf/${icao}`, {
54+
taf = await fetchForeignAPI(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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ChatInputCommandInteraction, Colors, EmbedField } from 'discord.js';
2-
import { Logger, VatsimEvents, VatsimEventsSchema, fetchForeignAPI, makeEmbed } from '../../../../lib';
2+
import { Logger, VatsimEventsSchema, fetchForeignAPI, makeEmbed } from '../../../../lib';
33

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

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

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

2121
const filteredEvents = response.data.filter((event) => event.type === 'Event');
2222
const finalList = filteredEvents.slice(0, 5);
@@ -69,7 +69,7 @@ export async function handleVatsimEvents(interaction: ChatInputCommandInteractio
6969

7070
return interaction.editReply({ embeds: [eventsEmbed] });
7171
} catch (e) {
72-
Logger.error(String(e));
72+
Logger.error(e);
7373
const errorEmbed = makeEmbed({
7474
title: 'Events Error',
7575
description: String(e),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const listEmbed = (type: string, fields: EmbedField[], totalCount: number, shown
1212
description: `A list of ${shownCount} online ${type} matching ${callsign}.`,
1313
fields,
1414
});
15-
const pilotsListEmbedFields = (callsign: string, rating?: PilotRating, flightPlan?: FlightPlan) => {
15+
const pilotsListEmbedFields = (callsign: string, flightPlan: FlightPlan | null, rating?: PilotRating) => {
1616
const fields = [
1717
{
1818
name: 'Callsign',
@@ -56,7 +56,7 @@ export async function handleVatsimPilots(interaction: ChatInputCommandInteractio
5656
const { callsign, flight_plan } = pilot;
5757
const rating = vatsimData.pilot_ratings.find((rating) => rating.id === pilot.pilot_rating);
5858

59-
return pilotsListEmbedFields(callsign, rating, flight_plan ?? undefined);
59+
return pilotsListEmbedFields(callsign, flight_plan, rating);
6060
}).splice(0, 5);
6161

6262
return interaction.editReply({ embeds: [listEmbed('Pilots', fields.flat(), pilots.length, fields.length, callsignSearch)] });

src/commands/utils/vatsim/vatsim.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ export default slashCommand(data, async ({ interaction }) => {
9191
// Fetch VATSIM data
9292
let vatsimData: VatsimData;
9393
try {
94-
vatsimData = await fetchForeignAPI<VatsimData>('https://data.vatsim.net/v3/vatsim-data.json', VatsimDataSchema);
94+
vatsimData = await fetchForeignAPI('https://data.vatsim.net/v3/vatsim-data.json', VatsimDataSchema);
9595
} catch (e) {
9696
if (e instanceof ZodError) {
97-
e.issues.forEach((issue) => Logger.error(`[zod Issue VATSIM Data] Code: ${issue.code}, Path: ${issue.path.join('.')}, Message: ${issue.message}`));
9897
return interaction.editReply({ embeds: [fetchErrorEmbed('The VATSIM API returned unknown data.')] });
9998
}
100-
return interaction.editReply({ embeds: [fetchErrorEmbed('The VATSIM API returned unknown data.')] });
99+
Logger.error(`Error while fetching VATSIM data: ${String(e)}.`);
100+
return interaction.editReply({ embeds: [fetchErrorEmbed('An error occurred while fetching data from VATSIM.')] });
101101
}
102102

103103
// Grap the callsign from the interaction

src/commands/utils/wolframAlpha.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ const noQueryEmbed = makeEmbed({
2323
color: Colors.Red,
2424
});
2525

26-
const errorEmbed = (errorMessage: string) => makeEmbed({
26+
const errorEmbed = (error: string) => makeEmbed({
2727
title: 'Wolfram Alpha Error',
28-
description: errorMessage,
28+
description: error,
2929
color: Colors.Red,
3030
});
3131

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

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

src/lib/apis/fetchForeignAPI.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Logger } from '../logger';
77
* @typeParam ReturnType - The expected type of the returned data.
88
* @param request The [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object to be passed to `fetch()`.
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.
10-
* @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).
10+
* @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) or a {@link ZodError} if the validation failed.
1111
*/
1212
export const fetchForeignAPI = async <ReturnType = unknown>(request: RequestInfo, zodSchema: ZodSchema<ReturnType>, debug?: boolean): Promise<ReturnType> => {
1313
const req = new Request(request);
@@ -33,10 +33,10 @@ export const fetchForeignAPI = async <ReturnType = unknown>(request: RequestInfo
3333
const result = zodSchema.safeParse(data);
3434

3535
if (!result.success) {
36-
Logger.error("[zod] Data validation failed! Pass the 'debug' flag to 'fetchForeignAPI()' to print the retrieved data to the console.");
36+
Logger.error("[zod] Data validation failed! Pass the 'debug' flag to 'fetchForeignAPI()' to dump the retrieved data to the console.");
3737
Logger.error(`Endpoint location: ${req.url}.`);
3838
if (debug) {
39-
// winston doesn't usefully log object at the moment
39+
// winston doesn't log objects in a useful way at the moment
4040
// eslint-disable-next-line no-console
4141
console.log('RETRIEVED DATA:', data);
4242
}

src/lib/apis/zodSchemas/simbrief/simbriefSchemas.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import { z } from 'zod';
22

3-
const FetchSchema = z.object({ status: z.string() });
3+
const SimBriefFetchSchema = z.object({ status: z.string() });
44

5-
const ParamsSchema = z.object({
5+
const SimBriefParamsSchema = z.object({
66
user_id: z.string(),
77
time_generated: z.string(),
88
airac: z.string(),
99
});
1010

11-
const AircraftSchema = z.object({
11+
const SimBriefAircraftSchema = z.object({
1212
name: z.string(),
1313
internal_id: z.string(),
1414
});
1515

16-
const OriginSchema = z.object({ icao_code: z.string() });
16+
const SimBriefOriginSchema = z.object({ icao_code: z.string() });
1717

18-
const DestinationSchema = z.object({ icao_code: z.string() });
18+
const SimBriefDestinationSchema = z.object({ icao_code: z.string() });
1919

20-
const GeneralSchema = z.object({ route: z.string() });
20+
const SimBriefGeneralSchema = z.object({ route: z.string() });
2121

2222
/**
2323
* This schema only contains currently used fields. If you wish to use other fields returned by the API add them in this file.
2424
*/
2525
export const SimbriefFlightPlanSchema = z.object({
26-
fetch: FetchSchema,
27-
params: ParamsSchema,
28-
aircraft: AircraftSchema,
29-
origin: OriginSchema,
30-
destination: DestinationSchema,
31-
general: GeneralSchema,
26+
fetch: SimBriefFetchSchema,
27+
params: SimBriefParamsSchema,
28+
aircraft: SimBriefAircraftSchema,
29+
origin: SimBriefOriginSchema,
30+
destination: SimBriefDestinationSchema,
31+
general: SimBriefGeneralSchema,
3232
});
3333

3434
/**

0 commit comments

Comments
 (0)