Skip to content

Commit

Permalink
fix: transform to correct params array for by-name
Browse files Browse the repository at this point in the history
  • Loading branch information
vh committed Nov 7, 2021
1 parent 537d3c5 commit 8390289
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface MockModeSettings {

export type TMethodHandler = (...args: any) => Promise<any>;

const sortParamKeys = (method?: MethodObject, params?: Record<string, unknown>) => {
const toArray = (method?: MethodObject, params?: Record<string, unknown>) => {
if (!method) {
return [];
}
Expand All @@ -34,8 +34,10 @@ const sortParamKeys = (method?: MethodObject, params?: Record<string, unknown>)
.reduce((m, pn, i) => ({ ...m, [pn]: i }), {});

return Object.entries(params)
.sort((v1, v2) => methodParamsOrder[v1[0]] - methodParamsOrder[v2[0]])
.map(([key, val]) => val);
.reduce((params: unknown[], [key, val]) => {
params[methodParamsOrder[key]] = val;
return params;
}, []);
};

export class Router {
Expand Down Expand Up @@ -79,7 +81,7 @@ export class Router {

const methodObject = (this.openrpcDocument.methods as MethodObject[]).find((m) => m.name === methodName) as MethodObject;

const paramsAsArray = params instanceof Array ? params : sortParamKeys(methodObject, params);
const paramsAsArray = params instanceof Array ? params : toArray(methodObject, params);

try {
return { result: await this.methods[methodName](...paramsAsArray) };
Expand Down

0 comments on commit 8390289

Please sign in to comment.