Last active
November 13, 2023 22:13
-
-
Save matthewmueller/96494c758f69a8b6784d263537773138 to your computer and use it in GitHub Desktop.
Protocol example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { API } from './protocol' | |
import XHR from './xhr' | |
const xhr = new XHR<API>("https://example.com") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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