Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. /* @flow */
  2.  
  3. import typify from "typify"
  4.  
  5. class Ok<A> {
  6. value: A
  7. constructor(value: A) {
  8. this.value = value
  9. }
  10. }
  11.  
  12. class Err {
  13. msg: string
  14. constructor(msg: string) {
  15. this.msg = msg
  16. }
  17. }
  18.  
  19. typify.record("results", {
  20. results: "array string",
  21. })
  22.  
  23. type Results = {
  24. results: Array<string>
  25. }
  26.  
  27. type ResultsResponse =
  28. | Ok<Results>
  29. | Err
  30.  
  31. function fetchItems() : Promise<Results> {
  32. return fetch("/api/items").then((res) => res.json())
  33. }
  34.  
  35. function fetchItemsWithValidation() : Promise<ResultsResponse> {
  36. return fetchItems().then((data) => {
  37. if (typify.check("results", data)) {
  38. return new Ok(data)
  39. }
  40. return new Err("Invalid JSON")
  41. })
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement