Get Enums dynamically #12

Open
opened 2 years ago by alexis.drai · 0 comments
Owner

Enums of values such as TypeName, MoveCategoryName, and PokemongName, are defined by the API. The API provides OpenAPI Specification (OAS), so we could get those enums dynamically indstead of copying and pasting them in this front-end project.

Indeed, this would require manual updates whenever the OAS changes.

A more dynamic approach would be to fetch the OAS from the API at runtime, parse the enums, and use them to validate strings and populate pickers in this app. This would ensure that we always uses the latest version of the enum. However, this approach would also add complexity to this app and could impact performance, as it involves fetching and parsing the OAS at every launch, and / or implementing caching.

Here's a rough example of how you could fetch and parse the PokemongName enum from the OAS:

import axios from 'axios';
import yaml from 'js-yaml';

// Fetch the OAS
const response = await axios.get(`${API_BASE_URL}/q/META-INF/openapi.yaml`);

// Parse the OAS
const oas = yaml.safeLoad(response.data);

// Extract the PokemongName enum
const pokemongNameEnum = oas.components.schemas.PokemongName.enum;

// Now we can use pokemongNameEnum to validate names

Error handling is not included in this example, but should definitely be present in our production code.

Enums of values such as `TypeName`, `MoveCategoryName`, and `PokemongName`, are defined by the API. The API provides OpenAPI Specification (OAS), so we could get those enums dynamically indstead of copying and pasting them in this front-end project. Indeed, this would require manual updates whenever the OAS changes. A more dynamic approach would be to fetch the OAS from the API at runtime, parse the enums, and use them to validate strings and populate pickers in this app. This would ensure that we always uses the latest version of the enum. However, this approach would also add complexity to this app and could impact performance, as it involves fetching and parsing the OAS at every launch, and / or implementing caching. Here's a rough example of how you could fetch and parse the `PokemongName` enum from the OAS: ```typescript import axios from 'axios'; import yaml from 'js-yaml'; // Fetch the OAS const response = await axios.get(`${API_BASE_URL}/q/META-INF/openapi.yaml`); // Parse the OAS const oas = yaml.safeLoad(response.data); // Extract the PokemongName enum const pokemongNameEnum = oas.components.schemas.PokemongName.enum; // Now we can use pokemongNameEnum to validate names ``` Error handling is not included in this example, but should definitely be present in our production code.
alexis.drai added the wontfix label 2 years ago
alexis.drai added this to the this project 2 years ago
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

No dependencies set.

Reference: alexis.drai/pokemong_app#12
Loading…
There is no content yet.