Nanyx

Todo app: shared types

The types and protocol a todo client and server have in common.

module todo.shared

import nanyx/time

-- Types shared by the client and the server of a todo application

export type TodoId = int

export type Todo = (
  id: TodoId
  description: string
  completed: bool
  dateAdded: Instant
)

export type TodoError =
  | #todoDoesNotExist
  | #insertNotSuccessful
  | #updateNotSuccessful
  | #deleteNotSuccessful

export type DeleteResult = #deleted | #deleteError(TodoError)
export type UpdateResult = #updated(Todo) | #updateError(TodoError)

-- The protocol between client and server. Every field is a function from a
-- request to an asynchronous response, so one record type describes both the
-- server implementation and the client proxy.
export type TodoProtocol = (
  allTodos: () -> Async(list(Todo))
  addTodo: string -> Async(#some(Todo) | #error(TodoError))
  toggleCompleted: TodoId -> Async(UpdateResult)
  deleteTodo: TodoId -> Async(DeleteResult)
)

-- How routes are generated on the server and mapped from the client
export def routeBuilder: (typeName: string, methodName: string) -> string = { typeName, methodName ->
  "/api/{typeName}/{methodName}"
}

This program lives in the repository as examples/web-server-shared.nyx.