Skip to content

Type guards for response types #12

Description

@swantzter

The lvs-types library is quite nice, and the zod schemas for request body validation is a great touch. However, it'd be nice to get a similar level of type safety for API responses using type guard functions

For example, instead of doing something like const body = await response.json() as ISeasonV1 I prefer doing something like the following to be sure I have a valid response and at least the fields that are interesting in my implementation:

function isISeasonV1 (x: unknown): x is ISeasonV1 {
  return isObject(x) && typeof x.inviteURL === 'string' // ...
}

const response = await fetch(...)

if (!response.ok) { ... }

const body = await response.json()

if (!isISeasonV1(body) {
  throw new TypeError('AAAAA, WE GOT BAD DATA FROM THE API AND NOW EVERYTHING IS ON FIRE. SAVE YOURSELF WHILE YOU STILL CAN!!!!!!!')
}

// continue processing the response

It'd be a nice addition imo to both get type safe input and output

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions