ChainReactApp2023 icon indicating copy to clipboard operation
ChainReactApp2023 copied to clipboard

Refactor Webflow types to do runtime checking using Zod

Open joshuayoes opened this issue 2 years ago • 2 comments
trafficstars

Description

Trello Card

So with the most recent changes to the CMS, it appears that several properties have been changed and now there are undefined errors.

The Webflow types are derived from Typescript types, which is fine for many cases, but when an API changes, it does not save you from runtime errors.

This PR adds Zod to do runtime checking and throw an error if the Webflow API returns an unexpected shape. This causes the app to blow up, but with more specific error messages, which makes it much simpler to figure out what properties are broken and what are not.

This PR also includes a few miscellaneous fixes to reflect the type changes:

  • programmatically generates types using scripts/webflow-codegen.ts
  • tests that types are correct against actual API data using scripts/webflow-codegen-test.ts
  • creates new type file at webflow-api.generated.ts
  • adds app/services/api/webflow-api.schema.ts with statically known webflow types
  • updates useWebflowAPI to validate API data against collection schema
  • updates prettier to handle parsing satisfies
  • Fixes Text component type
  • Updates app/services/api/webflow-consts.ts to include a schema for validation
  • Adds missing collection ids for schedule names

Graphics

Before After

Checklist:

  • [x] I have done a thorough self-review of my code
  • [ ] I have tested with a screen reader and font-scaling turned on and added necessary accessibility features
  • [x] I have run tests and linter

joshuayoes avatar Apr 14 '23 03:04 joshuayoes

I found a way to programmatically generate our types using the fields key on from the List Collections endpoint. However, this PR still needs some work to actually consume those generated types.

I'll mark this as a draft for now until I get those types converted to the generated ones

joshuayoes avatar Apr 15 '23 01:04 joshuayoes

Tried rebasing this, it's getting pretty gnarly.

This PR could potentially to expand to normalize the choices and their collectionIds as well, but it may be kind of late in the change to get the benefits out of this PR.

joshuayoes avatar May 08 '23 18:05 joshuayoes