Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve api typing #129

Merged
merged 7 commits into from
Mar 20, 2024
Merged

Improve api typing #129

merged 7 commits into from
Mar 20, 2024

Conversation

Redm4x
Copy link
Contributor

@Redm4x Redm4x commented Mar 19, 2024

  • Add missing types in Api project
  • Enable noImplicitAny in tsconfig
  • Remove unused proposal code (deploy-web)

@Redm4x Redm4x changed the title Improve api typings Improve api typing Mar 19, 2024
@@ -6,7 +6,7 @@ import { chainModels, getChainModels, userModels } from "@shared/dbSchemas";
import { Template, TemplateFavorite, UserAddressName, UserSetting } from "@shared/dbSchemas/user";
import { chainDefinitions } from "@shared/chainDefinitions";

const csMap = {
const csMap: { [key: string]: string } = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it needed to be this flexible? if you skip this type it's implicitly typed as object with specified keys and values. It seems that what really needed here is typed env

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the type and added a typeguard instead. But yeah ideally we would have env var typing & validation.


const route = createRoute({
method: "get",
path: "/graph-data/{dataName}",
tags: ["Analytics"],
request: {
params: z.object({
dataName: z.string().openapi({ example: "dailyUAktSpent", enum: authorizedDataNames })
dataName: z.string().openapi({ example: "dailyUAktSpent", enum: Array.from(AuthorizedGraphDataNames) })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.from is already a hack here needed to overcome as const cast. To me it would be the same in terms of code being clean to do:

AuthorizedGraphDataNames as unknown as string[]

However I'd just created a dedicated type in the source instead of deriving type from an array. Yes, some duplication but ts would enforce to follow and it works consistently without hacks

type AuthorizedGraphDataName = (
  "dailyUAktSpent" |
  "dailyUUsdcSpent" |
  "dailyUUsdSpent" |
  "dailyLeaseCount" |
  "totalUAktSpent" |
  "totalUUsdcSpent" |
  "totalUUsdSpent" |
  "activeLeaseCount" |
  "totalLeaseCount" |
  "activeCPU" |
  "activeGPU" |
  "activeMemory" |
  "activeStorage"
  )

export const authorizedGraphDataNames: AuthorizedGraphDataName[] = [
  "dailyUAktSpent",
  "dailyUUsdcSpent",
  "dailyUUsdSpent",
  "dailyLeaseCount",
  "totalUAktSpent",
  "totalUUsdcSpent",
  "totalUUsdSpent",
  "activeLeaseCount",
  "totalLeaseCount",
  "activeCPU",
  "activeGPU",
  "activeMemory",
  "activeStorage"
];

// avatarUrl: validatorFromDb?.keybaseAvatarUrl
// };
}
// const proposer = null; // TODO: Fix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason we can't remove this code? I know it's here only because of prettier so just asking

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have kept it in hope of fixing it one day. There is an issue with proposals created during genesis. I created #132 to keep track of it.

@@ -75,7 +75,22 @@ export const getDashboardData = async () => {
};
};

export const AuthorizedGraphDataNames = [
type AuthorizedGraphDataName =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@Redm4x Redm4x merged commit eca93a6 into main Mar 20, 2024
5 checks passed
@Redm4x Redm4x deleted the refactor/improve-api-typing branch March 20, 2024 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants