Skip to content

Instantly share code, notes, and snippets.

@swashcap
Created September 24, 2019 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swashcap/b3606ea8f4892005c3a6a924be3ad9c4 to your computer and use it in GitHub Desktop.
Save swashcap/b3606ea8f4892005c3a6a924be3ad9c4 to your computer and use it in GitHub Desktop.
interface GreatClientConfig {
url: string
}
interface GreatClientResponse {
data: {
datetime: string
isGreat: boolean
}
}
const myGreatClientFactory = (config: GreatClientConfig) => {
return {
getConfig: (): GreatClientConfig => config,
doSomethingAsync: async (path: string): Promise<GreatClientResponse> => {
const response = await fetch(`${config.url}/${path}`)
if (!response.ok) {
throw new Error(`Something failed: ${response.status}`)
}
return await response.json()
}
}
}
const myGreatClient = myGreatClientFactory({ url: 'https://it.is-great.com/api' })
type doSomethingAsyncParams = Parameters<typeof myGreatClient.doSomethingAsync>
type doSomethingasyncReturnType = ReturnType<typeof myGreatClient.doSomethingAsync>
@swashcap
Copy link
Author

Why you default to 4 spaces, VS Code 🤦‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment