Skip to content

rpc: RPC over HTTP, any and enum arguments lose their type #767

Open

Description

@lionelhorn

Over HTTP, an action whose arguments are all primitives is called with GET and its arguments in the query string, where they lose their type: an any parameter gets the string '5', and a numeric enum argument is rejected. Over WebSocket both arrive as sent.

import { App } from '@deepkit/app';
import { ApplicationServer, FrameworkModule } from '@deepkit/framework';
import { rpc, RpcClient, RpcHttpClientAdapter } from '@deepkit/rpc';

enum Priority {
    low = 1,
    high = 2,
}

@rpc.controller('settings')
class SettingsController {
    @rpc.action()
    set(key: string, value: any): string {
        return `${key} = ${typeof value} ${value}`;
    }

    @rpc.action()
    priority(priority: Priority): string {
        return `${typeof priority} ${priority}`;
    }
}

const app = new App({
    controllers: [SettingsController],
    imports: [new FrameworkModule({ port: 8080, httpRpcBasePath: '/api' })],
});
await app.get(ApplicationServer).start();

const settings = new RpcClient(new RpcHttpClientAdapter('http://127.0.0.1:8080/api')).controller<SettingsController>('settings');
console.log(await settings.set('retries', 5));
// actual: retries = string 5, should be: retries = number 5 (as over WebSocket)
console.log(await settings.priority(Priority.high).catch((error: Error) => error.message));
// actual: 'Validation error:\npriority(type): Cannot convert 2 to enum. No valid value of 1, 2', should be: number 2 (as over WebSocket)

Related: #766 (a string | number argument loses its type over HTTP).

Master 0bb9fba, Node 26.13.0.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions