Skip to content

Instantly share code, notes, and snippets.

@matthewmueller
Last active November 13, 2023 22:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewmueller/96494c758f69a8b6784d263537773138 to your computer and use it in GitHub Desktop.
Save matthewmueller/96494c758f69a8b6784d263537773138 to your computer and use it in GitHub Desktop.
Protocol example
export type API = {
'get /teams/:key': {
request: {
key: string
}
response: {
key: string
name: string
}
}
'get /teams': {
request: {}
response: {
key: string
name: string
}[]
}
// signup a new user
'post /users': {
request: {
email: string
password: string
}
response: {
key: string
avatar_url: string
email: string
}
}
'get /users/me': {
request: {}
response: API['post /users']['response']
}
'post /recordings': {
request: {
title: string
}
response: {
key: string
}
}
}
import { API } from './protocol'
import XHR from './xhr'
const xhr = new XHR<API>("https://example.com")
/**
* Protocol
*/
type Protocol = {
[path: string]: {
request: {}
response: {}
}
}
/**
* HTTP Client
*/
export default class Client<P extends Protocol> {
constructor(private readonly baseURL: string) {}
async send<K extends keyof P>(action: K, body: P[K]['request']): Promise<P[K]['response'] | Error> {
console.log('fetching', action, body)
throw new Error("unimplemented")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment