Skip to content

Instantly share code, notes, and snippets.

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 shreyakupadhyay/20c21ef312f92199ebde01c8c980e0dd to your computer and use it in GitHub Desktop.
Save shreyakupadhyay/20c21ef312f92199ebde01c8c980e0dd to your computer and use it in GitHub Desktop.
Intermediate level typescript implementation of API response handling
<!-- common payload: stats associated with data -->
type StatsRange = {
max: number, min: number
}
type StatsKeys = 'count' | 'aum' | 'priceusd' | 'value'
interface PartialStats<Type extends string|number|symbol, RangeType> {
stats?: Partial<Record<Type, RangeType>>
}
<!-- common payload: pagination info -->
type Pagination = {
pagination?: {
currentPage: number,
perPage: number,
total: number,
totalPages: number
}
}
<!-- api: onboarding data -->
type APIResponseOnboards = {
count: number,
unixTimeStamp: number,
timestamp: string,
}
<!-- api: historical data -->
type APIResponseHistorical = {
aum: number,
unixTimeStamp: number,
}
<!-- api: wallets data -->
type APIResponseWallets = {
wid: number,
tenant: number,
portfolioValue: number,
address: string,
}
<!-- all api data handling -->
interface APIResponseData<Type> extends PartialStats<StatsKeys, StatsRange>, Pagination {
data : {
data: Type[],
field?: string,
grouping?: string
}[] | APIResponseWallets[],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment