From 80935281dc3e361d17b099f885dc551001fa8041 Mon Sep 17 00:00:00 2001 From: Zachary Belford Date: Tue, 23 May 2023 12:28:17 -0700 Subject: [PATCH 1/3] fix duplicate methods in built result --- merge-openrpc.js | 47 +++------------------------------ merge-utils.js | 52 +++++++++++++++++++++++++++++++++++++ openrpc.json | 1 + tests/merge-openrpc.test.ts | 26 +++++++++++++++++++ 4 files changed, 82 insertions(+), 44 deletions(-) create mode 100644 merge-utils.js create mode 100644 tests/merge-openrpc.test.ts diff --git a/merge-openrpc.js b/merge-openrpc.js index 0319de5..00f7801 100644 --- a/merge-openrpc.js +++ b/merge-openrpc.js @@ -1,7 +1,7 @@ const MetaMaskOpenRPC = require("./openrpc.json"); const fs = require("fs"); const fetch = require("node-fetch"); - +const mergeOpenRPC = require("./merge-utils"); const getFilteredExecutionAPIs = () => { return fetch("https://raw.githubusercontent.com/ethereum/execution-apis/assembled-spec/refs-openrpc.json") @@ -19,7 +19,7 @@ getFilteredExecutionAPIs().then((EthereumOpenRPC) => { 4 ) ); -}) +}); const unneeded = [ /eth_signTransaction/, @@ -30,49 +30,8 @@ const unneeded = [ const filterExecutionAPIs = (openrpcDocument) => { openrpcDocument.methods = openrpcDocument.methods.filter((method) => { - const matches = unneeded.some((regex) => regex.test(method.name)) + const matches = unneeded.some((regex) => regex.test(method.name)); return !matches; }); return openrpcDocument; -} - -const mergeOpenRPC = (first, second) => { - return { - openrpc: first.openrpc, - info: first.info, - methods: [ - ...first.methods, - ...second.methods - ], - components: { - errors: { - ...first.components.errors, - ...second.components.errors - }, - schemas: { - ...first.components.schemas, - ...second.components.schemas - }, - tags: { - ...first.components.tags, - ...second.components.tags - }, - contentDescriptors: { - ...first.components.contentDescriptors, - ...second.components.contentDescriptors - }, - examplePairings: { - ...first.components.examplePairings, - ...second.components.examplePairings - }, - links: { - ...first.components.links, - ...second.components.links - }, - examples: { - ...first.components.examples, - ...second.components.examples - } - } - } }; diff --git a/merge-utils.js b/merge-utils.js new file mode 100644 index 0000000..b46f1d3 --- /dev/null +++ b/merge-utils.js @@ -0,0 +1,52 @@ + +const dedupe = (methods1, methods2) => { + return methods1.reduce( + (arr, method) => { + if (arr.find((m2) => m2.name === method.name) === undefined) { + arr.push(method); + } + return arr; + }, + methods2 + ); +}; + +const mergeOpenRPC = (first, second) => { + return { + openrpc: first.openrpc, + info: first.info, + methods: dedupe(second.methods, first.methods), + components: { + errors: { + ...first.components.errors, + ...second.components.errors + }, + schemas: { + ...first.components.schemas, + ...second.components.schemas + }, + tags: { + ...first.components.tags, + ...second.components.tags + }, + contentDescriptors: { + ...first.components.contentDescriptors, + ...second.components.contentDescriptors + }, + examplePairings: { + ...first.components.examplePairings, + ...second.components.examplePairings + }, + links: { + ...first.components.links, + ...second.components.links + }, + examples: { + ...first.components.examples, + ...second.components.examples + } + } + }; +}; + +module.exports = mergeOpenRPC; diff --git a/openrpc.json b/openrpc.json index ebc7d84..a849ea3 100644 --- a/openrpc.json +++ b/openrpc.json @@ -23,6 +23,7 @@ { "name": "filterOptions", "schema": { + "title": "filterOptions", "type": "object", "description": "An optional object containing filter options specific to the subscription type. Only applicable for 'logs' subscription type.", "properties": { diff --git a/tests/merge-openrpc.test.ts b/tests/merge-openrpc.test.ts new file mode 100644 index 0000000..22320b5 --- /dev/null +++ b/tests/merge-openrpc.test.ts @@ -0,0 +1,26 @@ +// @ts-ignore +import mergeOpenRPC from '../merge-utils'; + +describe("merge openrpc document", () => { + describe('mergeOpenRPC', () => { + it('result does not contain duplicate methods', () => { + const doc1 = { + info: {}, + methods: [ + { name: 'foo', description: 'abc' }, + { name: 'bar' }, + { name: 'baz' }, + ] + }; + const doc2 = { + info: {}, + methods: [ + { name: 'foo', description: '123' }, + { name: 'bling' }, + { name: 'blang' }, + ] + }; + expect(mergeOpenRPC(doc1, doc2).methods[0].description).toBe("123"); + }); + }); +}); From 2f647d980cfb337746f0f4a6d5a8f6d2518d7f5b Mon Sep 17 00:00:00 2001 From: Zachary Belford Date: Tue, 23 May 2023 12:32:04 -0700 Subject: [PATCH 2/3] fix test --- tests/{merge-openrpc.test.ts => merge-utils.test.ts} | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) rename tests/{merge-openrpc.test.ts => merge-utils.test.ts} (88%) diff --git a/tests/merge-openrpc.test.ts b/tests/merge-utils.test.ts similarity index 88% rename from tests/merge-openrpc.test.ts rename to tests/merge-utils.test.ts index 22320b5..51d387e 100644 --- a/tests/merge-openrpc.test.ts +++ b/tests/merge-utils.test.ts @@ -10,7 +10,8 @@ describe("merge openrpc document", () => { { name: 'foo', description: 'abc' }, { name: 'bar' }, { name: 'baz' }, - ] + ], + components: {} }; const doc2 = { info: {}, @@ -18,9 +19,10 @@ describe("merge openrpc document", () => { { name: 'foo', description: '123' }, { name: 'bling' }, { name: 'blang' }, - ] + ], + components: {} }; - expect(mergeOpenRPC(doc1, doc2).methods[0].description).toBe("123"); + expect(mergeOpenRPC(doc1, doc2).methods[0].description).toBe("abc"); }); }); }); From de128223710b6caff10b56e52c140c767f8c74d4 Mon Sep 17 00:00:00 2001 From: Zachary Belford Date: Tue, 23 May 2023 12:35:37 -0700 Subject: [PATCH 3/3] rearrange the order of params --- merge-utils.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/merge-utils.js b/merge-utils.js index b46f1d3..85c7b87 100644 --- a/merge-utils.js +++ b/merge-utils.js @@ -1,13 +1,13 @@ const dedupe = (methods1, methods2) => { - return methods1.reduce( + return methods2.reduce( (arr, method) => { - if (arr.find((m2) => m2.name === method.name) === undefined) { + if (arr.find((m) => m.name === method.name) === undefined) { arr.push(method); } return arr; }, - methods2 + methods1 ); }; @@ -15,7 +15,7 @@ const mergeOpenRPC = (first, second) => { return { openrpc: first.openrpc, info: first.info, - methods: dedupe(second.methods, first.methods), + methods: dedupe(first.methods, second.methods), components: { errors: { ...first.components.errors,