Skip to content

Conversation

@david-crespo
Copy link
Collaborator

@david-crespo david-crespo commented Apr 9, 2021

Closes #209

Slight change to the calling API for useApiData:

- const { data } = useApiData(api.apiProjectInstancesGet, { projectName })
+ const { data } = useApi('apiProjectInstancesGet', { projectName })

No difference in type safety. data still gets the right type. But we get rid of two different hacks:

  • not minifying function names, which made the bundle bigger for no reason
  • pre-binding all the methods on api to api, which made me want to cry

Plus we don't have to import or call api either because the hook wraps it!

Next

One quirk right now is that for empty params objects, you need to pass an empty object, which feels silly.

const { data: projects } = useApiData(api, 'apiProjectsGet', {})

On the other hand there won't like be many of those because we'll be passing the optional page param pretty much all the time.

export interface ApiProjectsGetRequest {
  limit?: number
  pageToken?: string
  sortBy?: ApiNameOrIdSortMode
}

@vercel
Copy link

vercel bot commented Apr 9, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/oxidecomputer/console-ui-storybook/7GNsbrqkfBcfvpevZ25zK5FTYQqA
✅ Preview: https://console-ui-storybook-git-fix-api-data-type-oxidecomputer.vercel.app

@github-actions
Copy link
Contributor

github-actions bot commented Apr 9, 2021

Preview will be deployed at https://console-git-fix-api-data-type.internal.oxide.computer

const key = prop as keyof DefaultApi
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(api as any)[key] = api[key].bind(api)
})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good riddance. blech

@david-crespo david-crespo temporarily deployed to Preview VM April 9, 2021 15:16 Inactive
@david-crespo david-crespo temporarily deployed to Preview VM April 9, 2021 16:40 Inactive
@david-crespo david-crespo temporarily deployed to Preview VM April 9, 2021 17:08 Inactive
@david-crespo david-crespo changed the title Remove api method name hack Cleaner useApi hook, remove method name hack Apr 9, 2021
@david-crespo david-crespo temporarily deployed to Preview VM April 9, 2021 20:19 Inactive
@david-crespo david-crespo merged commit 67441b6 into main Apr 9, 2021
@david-crespo david-crespo deleted the fix-api-data-type branch April 9, 2021 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Turn minifying function names back on (eventually)

2 participants