Created
September 24, 2019 19:31
-
-
Save swashcap/b3606ea8f4892005c3a6a924be3ad9c4 to your computer and use it in GitHub Desktop.
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
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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why you default to
4
spaces, VS Code 🤦♂️