open-rpc / server-js

JSON-RPC 2.0 Server implementation that supports multiple transport protocols.
Apache License 2.0
44 stars 11 forks source link

Inconsistent request/response params structure #763

Open Sourcebite opened 1 year ago

Sourcebite commented 1 year ago

Is your feature request related to a problem? Please describe. Server implementation converts params of request to array with order specified in my own open-rpc document. I found this a little strange and inconsistent with behavior of client implementation (it is doesn't change params structure if i send params as object with fields). It also does not allow to have same method interface between frontend and backend. Altogether, why it is bad?:

  1. Different client/server method interface, cannot be used in front/back monorepo
  2. Cannot be used as drop-in replacement lib without breaking backward compatibility of current services
  3. Under the hood params convertation magic

Describe alternatives you've considered I could move all params under the user or data parent object, but [see 2nd clause]

Describe the solution you'd like I would prefer global or method-by-method option to choose params structure

  1. Option to choose preferred params structure
  2. Choose params structure from open-rpc document methods[#].paramStructure (breaking current behavior)

Additional context Example:

interface CreateUserParams {
    name: string;
    age: number;
    // many other fields
}

// Client side:
class UserService {
    client: Client; // open-rpc client

    create(params: CreateUserParams) {
        this.client.request({
            method: "users.create",
            params: params
        });
    }
}

// Server side:
class UserService {
    create(params: CreateUserParams) {
        console.log(params.name, params.age);
    }
}

const userService = new UserService();

const methodMapping = {
    "users.create": userService.create.bind(userService)
};