Skip to content

Instantly share code, notes, and snippets.

@shreyakupadhyay
Last active December 28, 2022 09:13
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/f933eab68e92de308e3f021a63e79a92 to your computer and use it in GitHub Desktop.
Save shreyakupadhyay/f933eab68e92de308e3f021a63e79a92 to your computer and use it in GitHub Desktop.
Beginner 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'
type PartialStats = {
stats?: {
[key in StatsKeys]?: StatsRange
}
}
<!-- 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 extends PartialStats, Pagination {
data : {
data: APIResponseOnboards[] | APIResponseHistorical[],
field?: string,
grouping?: string
}[] | APIResponseWallets[],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment