Skip to content

Migrate API usage from TypeScript code to typesafe @hey-api#3575

Merged
florian-h05 merged 8 commits intoopenhab:mainfrom
jsjames:api
Jan 26, 2026
Merged

Migrate API usage from TypeScript code to typesafe @hey-api#3575
florian-h05 merged 8 commits intoopenhab:mainfrom
jsjames:api

Conversation

@jsjames
Copy link
Contributor

@jsjames jsjames commented Dec 12, 2025

For post 5.1 release

  • Initial draft of api using fetch and @hey-api to autogenerate api code.
  • modified a few instances of api calls (WIP), pending review of overall structure

@relativeci
Copy link

relativeci bot commented Dec 12, 2025

#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  Change 2 changes Regression 1 regression
                 Current
#4402
     Baseline
#4398
Regression  Initial JS 1.57MiB(+1.17%) 1.56MiB
No change  Initial CSS 0B 0B
No change  Cache Invalidation 6.93% 6.93%
No change  Chunks 615 615
No change  Assets 696 696
Change  Modules 2450(+0.53%) 2437
No change  Duplicate Modules 0 0
No change  Duplicate Code 0% 0%
No change  Packages 128 128
No change  Duplicate Packages 1 1
Bundle size by type  Change 1 change Regression 1 regression
                 Current
#4402
     Baseline
#4398
Regression  JS 11.05MiB (+0.17%) 11.03MiB
No change  CSS 876.93KiB 876.93KiB
No change  Fonts 526.1KiB 526.1KiB
No change  Media 295.6KiB 295.6KiB
No change  IMG 45.73KiB 45.73KiB
No change  Other 847B 847B

Bundle analysis reportBranch jsjames:apiProject dashboard


Generated by RelativeCIDocumentationReport issue

@jsjames
Copy link
Contributor Author

jsjames commented Dec 12, 2025

@florian-h05 - a few things to get your input:

  • in the existing api, all the framework7 request promises were wrapped to reject the promise on a caught error. Is there any reason we wouldn't just throw the error?
  • I haven't done much error checking yet in this implementation - so once we are okay with the structure, I can continue to make progress.
  • I'd like to pull the openAPI spec from the source vs. demo.openhab.org - I have an enquiry on the forum for this - but if you might be able to point me in the right direction here.

any other feedback on overall structure? If not, I can continue to port calls over to this.

@jsjames jsjames force-pushed the api branch 2 times, most recently from 6af6169 to 26f046f Compare December 12, 2025 02:57
@florian-h05 florian-h05 added enhancement New feature or request main ui Main UI labels Dec 12, 2025
@florian-h05
Copy link
Contributor

First of all, great PR.
I've already used hey-api in a university project and having a fully typed API client without needing to write type defs on our own is great!

in the existing api, all the framework7 request promises were wrapped to reject the promise on a caught error. Is there any reason we wouldn't just throw the error?

I think this was done to "simplify" error handling so you can simply check err === 'Not found' in the .catch block of a promise. But I always found this quite confusing, to let's throw the full error and then check err.statusCode === 404 instead.

I'd like to pull the openAPI spec from the source vs. demo.openhab.org - I have an enquiry on the forum for this - but if you might be able to point me in the right direction here.

I have to look into how to get the OpenAPI spec from the source.

@florian-h05
Copy link
Contributor

WRT to the general structure, I will have a look at this PR after the 5.1 release.

@jsjames jsjames force-pushed the api branch 2 times, most recently from b57f24f to d172470 Compare December 13, 2025 00:53
@jsjames
Copy link
Contributor Author

jsjames commented Dec 13, 2025

WRT to the general structure, I will have a look at this PR after the 5.1 release.

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.

@jsjames
Copy link
Contributor Author

jsjames commented Dec 18, 2025

@florian-h05 - a few updates:

  • I've submitted a PR to core to enhance/correct some ares of the openapi spec - which is generated at runtime using the swagger annotations. I have build a version of core and extracted the openapi spec that is currently in the build directory. So, assuming the PR will get approved, the spec will match what we feed into hey-api.
  • I do expect we will find additional issues as we expand typescript in the ui that we might need to address in the core openapi spec.
  • Lastly, the one thing that is not in the current openapi spec is whether a field is required or optional and as such, all generated typescript interfaces have all the fields as mandatory. As we find fields that are optional, we can annotate them in core so they will be automatically generated. I don't know of a more automated process to do this - so I think this will be manual.

@jsjames jsjames force-pushed the api branch 3 times, most recently from 4a11bb6 to 3b42961 Compare December 24, 2025 01:08
@jsjames jsjames force-pushed the api branch 3 times, most recently from 41c217f to 4e27e40 Compare January 1, 2026 02:13
@jsjames jsjames marked this pull request as ready for review January 1, 2026 02:15
@jsjames
Copy link
Contributor Author

jsjames commented Jan 1, 2026

@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.

@jsjames jsjames changed the title Draft - initial implementation of api using @hey-api Implement api methods via @hey-api Jan 1, 2026
@jsjames jsjames requested a review from Copilot January 1, 2026 02:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 in src/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'
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

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

Unused variable dest.

Suggested change
const dest = './openapi.json'

Copilot uses AI. Check for mistakes.
@jsjames
Copy link
Contributor Author

jsjames commented Jan 23, 2026

No, not yet. I should probably do.

I just looked at the core code and it looks like the schema is correct and aligns with the core code. I think we should be good here.

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.

@jsjames
Copy link
Contributor Author

jsjames commented Jan 24, 2026

No, not yet. I should probably do.

I just looked at the core code and it looks like the schema is correct and aligns with the core code. I think we should be good here.

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?

@jsjames
Copy link
Contributor Author

jsjames commented Jan 24, 2026

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?

jsjames and others added 5 commits January 24, 2026 11:25
…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>
@jsjames
Copy link
Contributor Author

jsjames commented Jan 24, 2026

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?

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>
@jsjames
Copy link
Contributor Author

jsjames commented Jan 24, 2026

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.

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>
@florian-h05
Copy link
Contributor

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 florian-h05 changed the title Implement api methods via @hey-api Migrate API usage to typesafe @hey-api Jan 26, 2026
@florian-h05 florian-h05 merged commit fcded71 into openhab:main Jan 26, 2026
5 checks passed
@florian-h05 florian-h05 changed the title Migrate API usage to typesafe @hey-api Migrate API usage from TypeScript code to typesafe @hey-api Jan 26, 2026
@florian-h05
Copy link
Contributor

@jsjames I have created #3812 which refactors some code to TypeScript and makes use of @hey-api there.
I would propose to migrate to TS Composition API step-by-step, whenever we like to rewrite something.

@mrlubos
Copy link

mrlubos commented Jan 26, 2026

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!

@florian-h05
Copy link
Contributor

@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!

@jsjames
Copy link
Contributor Author

jsjames commented Jan 26, 2026

@jsjames I have created #3812 which refactors some code to TypeScript and makes use of @hey-api there. I would propose to migrate to TS Composition API step-by-step, whenever we like to rewrite something.

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?

@florian-h05
Copy link
Contributor

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?

@jsjames
Copy link
Contributor Author

jsjames commented Jan 26, 2026

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:

  • more robust call format to the api - consistent parameters
  • If we want to improve error handling in api calls - i think we should migrate to the new calls to be consistent and eliminate the need to recode error handling in the future
  • new api handles 204 (so at a minimum we should migrate those)
  • we are bound to find new issues with the openapi spec as we expand to other areas - i'd rather find those out sooner rather than later

Having said that, we risk breaking things by converting - so its a risk.

@florian-h05
Copy link
Contributor

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request main ui Main UI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants