Migrate API usage from TypeScript code to typesafe @hey-api#3575
Migrate API usage from TypeScript code to typesafe @hey-api#3575florian-h05 merged 8 commits intoopenhab:mainfrom
Conversation
#4402 Bundle Size — 12.75MiB (+0.15%).7152b76(current) vs 520c0bb main#4398(baseline) Warning Bundle contains 2 duplicate packages – View duplicate packages Bundle metrics
Bundle size by type
Bundle analysis report Branch jsjames:api Project dashboard Generated by RelativeCI Documentation Report issue |
|
@florian-h05 - a few things to get your input:
any other feedback on overall structure? If not, I can continue to port calls over to this. |
6af6169 to
26f046f
Compare
|
First of all, great PR.
I think this was done to "simplify" error handling so you can simply check
I have to look into how to get the OpenAPI spec from the source. |
|
WRT to the general structure, I will have a look at this PR after the 5.1 release. |
b57f24f to
d172470
Compare
FYI - @wborn helped me understand how the openapi spec is generated. I think this will be a bit iterative as there may be some enhancements to the openapi spec which will help to make the interface generation better (for instance removing the DTO from some of the object names and further specifying which fields cannot be blank. For now, I've been modifying a local version of openhab-core and generating the spec and use that to build the interface. Eventually though, if we get all the changes done and submitted to core, we can pull the spec from there. |
|
@florian-h05 - a few updates:
|
4a11bb6 to
3b42961
Compare
41c217f to
4e27e40
Compare
|
@florian-h05 - this one is ready for your review. I have a parallel PR into core to enhance the open-api spec schema - however it does not need to be submitted before this one as I've built a version locally to generated the openapi spec currently in the build directory. One enhancement is that the spec will now specifiy required vs. optional fields - unfortunately, there doesn't seem to be a systematic way to do this. It might be worth you looking at the generated api/types.gen.ts file to see if there are other enhancements. I can make those then in core if you see areas to update as we should keep the master spec the "source of truth". All-in-all, I think its a good way to setup the api - and have modified several components/files with the new setup. The old one still exists and we can remove over time. |
There was a problem hiding this comment.
Pull request overview
This PR implements a new API layer using @hey-api/openapi-ts to auto-generate type-safe API methods from OpenAPI specifications. The changes replace manual type definitions and API calls with auto-generated ones, improving type safety and maintainability.
Key changes:
- Auto-generated API types and methods from OpenAPI specification
- Replaced manual type definitions in
src/types/openhab/with generated types insrc/api/types.gen.ts - Updated multiple components and stores to use the new generated API methods
- Added API interceptors for authentication and error handling in
src/js/initApi.ts
Reviewed changes
Copilot reviewed 46 out of 58 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/types/openhab/*.d.ts |
Removed manual type definitions (replaced by generated types) |
src/api/types.gen.ts |
Auto-generated TypeScript type definitions from OpenAPI spec |
src/api/index.ts |
Exports all generated API methods and types |
src/js/initApi.ts |
Configures API client with request/response interceptors |
src/pages/settings/transformations/transformations-list.vue |
Updated to use generated getTransformations() and deleteTransformation() |
src/pages/settings/menu/settings-menu.vue |
Updated multiple API calls with error handling |
src/pages/analyzer/*.ts |
Updated type imports and API usage |
src/js/stores/*.ts |
Updated stores to use generated API methods with improved error handling |
src/components/config/controls/*.vue |
Updated components to use new API methods |
bundles/org.openhab.ui/web/eslint.config.js |
Excluded generated API files from linting |
Files not reviewed (1)
- bundles/org.openhab.ui/web/package-lock.json: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // const url = `https://demo.openhab.org/rest/spec` | ||
| const url = null | ||
| const file = './build/openhab-api-spec.json' | ||
| const dest = './openapi.json' |
There was a problem hiding this comment.
Unused variable dest.
| const dest = './openapi.json' |
I was expanding this to the thing-details page and it looks like there is an issue with how the PUT is described or I'm calling it wrong. Let me figure this out first before merging. |
I'm at a loss on this one - I have the code here: thing-detail-api. SImply enabling/disabling a thing will not work. I believe hey-api is generating incorrect code for this and have filed an issue. If you get a sec, maybe you can spot an issue? |
So, in digging around in the hey-api code more, I think this is definitely a bug in generated api client. See my explanation here: hey-api/openapi-ts#3250 Not sure if you have suggestions on how to fix? |
…3656) JS Scripting supports global return thanks to the wrapper, but ESLint displays a parser error. Reported on the community: https://community.openhab.org/t/openhab-5-1-release-discussion/167620/53 Signed-off-by: Florian Hotze <dev@florianhotze.com> oh-swiper: Don't set swiper-slide width to 100% (openhab#3653) Fixes a style regression of openhab#3350. Reported on the community: https://community.openhab.org/t/5-1-0-m3-main-ui-shows-blank-screen-on-android/167432/41 Signed-off-by: Florian Hotze <dev@florianhotze.com> Draft - initial implementation of api using @hey-api Additional edits updated propertiesRequiredByDefault Updates to openapi spec per proposed changes in core. import * as api so its easy to identify api calls/types Updated to have responseStyle: 'data' Implement optional for ConfigDescriptionParameter and ConfigDescriptionParameterGroup (based on updates to core api) Signed-off-by: Jeff James <jeff@james-online.com> WIP Signed-off-by: Jeff James <jeff@james-online.com> Ready for review Signed-off-by: Jeff James <jeff@james-online.com> oxc-update run format Signed-off-by: Jeff James <jeff@james-online.com>
Signed-off-by: Florian Hotze <dev@florianhotze.com>
Signed-off-by: Florian Hotze <dev@florianhotze.com>
Signed-off-by: Florian Hotze <dev@florianhotze.com>
Signed-off-by: Florian Hotze <dev@florianhotze.com>
This is definitely an error in the hey-api code - I have submitted a PR to openapi-ts: #3255 and included an interim patch for this PR. Things are now working for put/post. |
Implemented new api in thing-details.vue Signed-off-by: Jeff James <jeff@james-online.com>
hey-api has already merged in this PR (they did make the fix in a different way). I have updated this PR with this new hey-api version and its working well. So, we can move forward with this merge. |
Signed-off-by: Jeff James <jeff@james-online.com>
Signed-off-by: Jeff James <jeff@james-online.com>
|
Glad they are still that fast in fixing bugs and releasing a new version ... I've also discovered a bug and proposed a fix in hey-api about a year ago when using it in a university project. |
@florian-h05 I'm everywhere 🕵️♀️ let me know if you have any more feedback in the future! |
|
@mrlubos You indeed seem to be everywhere, for now I don't have further feedback. Many thanks for your great work at hey-api, my experience so far has been really good, no major issues! |
Okay - so we don't want to migrate existing non-TypeScript code to use the hey-api functions? We'll just migrate when we migrate that code to typescript? I did convert several over already to the new api. Should we add anything to CONTRIBUTING.md regarding using the new hey-api protocol? |
|
I was thinking about this as well, theoretically we can migrate anything, but if something changes with the OpenAPI Spec and the API client changes, does Vue-tsc catch invalid usage of the API client in pure JS code? |
I don't think you get any typescript benefits in incorporating in plain js code. I see the benefits:
Having said that, we risk breaking things by converting - so its a risk. |
|
Yeah, overall I agree everything should be migrated, I am just not sure about the exact schedule. But if you want to go ahead and convert all usages, fine for me 👍 |
For post 5.1 release