Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export endpoint types from generated endpoint modules #2789

Open
OlliTietavainenVaadin opened this issue Oct 3, 2024 · 0 comments
Open

Export endpoint types from generated endpoint modules #2789

OlliTietavainenVaadin opened this issue Oct 3, 2024 · 0 comments
Labels
enhancement New feature or request hilla Issues related to Hilla

Comments

@OlliTietavainenVaadin
Copy link
Member

Describe your motivation

Related issue: #2788

In order to make the development experience better in service workers, it would be useful if the generated endpoint modules contained explicit exports for the method signatures' types. This would allow typesafe development for method calls in service worker code.

Describe the solution you'd like

An endpoint method signature contains a name, 0...n input arguments, and a return type. The generated modules could export a list of TypeScript signature types that contain this information which could be safely imported by a service worker.

Pseudo-code example

//java
@BrowserCallable
@PermitAll
public class ProductEndpoint {

	@Nonnull
	public Product getProduct(int id, boolean x) {
		// Implementation details
	}
	
}

@Data
class Product() {
	private @Nonnull String name;
	private int id;
}
// ts
import { getSignatures } from 'Frontend/generated/endpoints/ProductEndpoint.js';

// Example 
type KnownSignatures = 'getProduct' | ...
type SignatureType = [ <method name>, <arguments>, <return type> ]

function getSignature(methodName : KnownSignatures) : SignatureType
// End of example


const [methodName, arguments, returnType] = getSignatures("get")
...
// Mock definitions
methodName : string
arguments : [number, boolean] // Option 1
arguments : {id: number, x: boolean} // Option 2
returnValue : Product;

Describe alternatives you've considered

Like in #2788 , the signature generation could be optional and configurable.

A longer-term solution could be transparent service worker call support in endpoints; not sure what that would look like exactly.

Additional context

Currently generated endpoint TS modules can't be imported in a service worker, as the Flux dependency breaks the TS build. Presumably, this wouldn't be an issue for exporting only the signature types.

Flux error when importing in sw.ts:

RollupError: node_modules/@vaadin/hilla-frontend/FluxConnection.js (1:7): "default" is not exported by "node_modules/atmosphere.js/lib/browser.js", imported by "node_modules/@vaadin/hilla-frontend/FluxConnection.js".
    at getRollupError (file:///app/node_modules/rollup/dist/es/shared/parseAst.js:396:41)
    at error (file:///app/node_modules/rollup/dist/es/shared/parseAst.js:392:42)
    at Module.error (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:13858:16)
    at Module.traceVariable (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:14306:29)
    at ModuleScope.findVariable (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:11984:39)
    at ChildScope.findVariable (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:7432:38)
    at ClassBodyScope.findVariable (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:7432:38)
    at ChildScope.findVariable (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:7432:38)
    at ChildScope.findVariable (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:7432:38)
    at FunctionScope.findVariable (file:///app/node_modules/rollup/dist/es/shared/node-entry.js:7432:38)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hilla Issues related to Hilla
Projects
None yet
Development

No branches or pull requests

1 participant