Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* @flow */
- import typify from "typify"
- class Ok<A> {
- value: A
- constructor(value: A) {
- this.value = value
- }
- }
- class Err {
- msg: string
- constructor(msg: string) {
- this.msg = msg
- }
- }
- typify.record("results", {
- results: "array string",
- })
- type Results = {
- results: Array<string>
- }
- type ResultsResponse =
- | Ok<Results>
- | Err
- function fetchItems() : Promise<Results> {
- return fetch("/api/items").then((res) => res.json())
- }
- function fetchItemsWithValidation() : Promise<ResultsResponse> {
- return fetchItems().then((data) => {
- if (typify.check("results", data)) {
- return new Ok(data)
- }
- return new Err("Invalid JSON")
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement