From 3b6bf6e75fae6c6a629506ccc146eb67304b0106 Mon Sep 17 00:00:00 2001 From: Shane Jonas Date: Thu, 2 Nov 2023 10:22:50 -0400 Subject: [PATCH 1/3] WIP request templates --- package.json | 3 +++ src/index.ts | 24 ++++++++++++++++++++++-- src/theme/OpenRPCDocMethod.tsx | 4 +++- yarn.lock | 16 +++++++++++----- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index d894137..26419c2 100644 --- a/package.json +++ b/package.json @@ -111,5 +111,8 @@ "@metamask/open-rpc-docs-react>@stoplight/mosaic>@fortawesome/fontawesome-svg-core": false, "@metamask/open-rpc-docs-react>@stoplight/mosaic>@fortawesome/fontawesome-svg-core>@fortawesome/fontawesome-common-types": false } + }, + "resolutions": { + "@metamask/open-rpc-docs-react": "portal:/Users/shanejonas/code/metamask/openrpc-docs-react" } } diff --git a/src/index.ts b/src/index.ts index bd5c268..53ec08d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-template-curly-in-string */ /* eslint-disable no-restricted-globals */ /** * See https://v2.docusaurus.io/docs/lifecycle-apis if you need more help! @@ -14,8 +15,6 @@ import path, { join } from 'path'; // import {compile} from '@mdx-js/mdx' -// import openRPCToMarkdown from './openrpc-to-mdx'; - /** * Put your plugin's options in here. * @@ -28,6 +27,7 @@ export type DocusaurusOpenRPCOptions = { // either a file path, or uri to a document. openrpcDocument: string; path: string; + requestTemplate?: string | Record; }; /** @@ -64,6 +64,24 @@ export default function docusaurusOpenRpc( const aliasedSource = (source: string) => `${posixPath(path.resolve(pluginDataDirRoot, options.id, source))}`; + let reqTemplate: any = { + js: 'await window.fetch(\n "${serverUrl}",\n {\n method: "POST",\n headers: {\n "Content-Type": "application/json"\n },\n body: JSON.stringify(${jsonRpcRequest})\n }\n)', + curl: "curl -X POST -H 'Content-Type: application/json' --data ${jsonRpcRequest} ${serverUrl}", + python: + 'import requests\n\nrequests.request(\n "POST",\n "${serverUrl}",\n headers={\n "Content-Type": "application/json"\n },\n data=${jsonRpcRequest}\n)', + }; + + if (typeof options.requestTemplate === 'string') { + reqTemplate = { + js: options.requestTemplate, + }; + } else if (typeof options.requestTemplate === 'object') { + reqTemplate = { + ...reqTemplate, + ...options.requestTemplate, + }; + } + return { // change this to something unique, or caches may conflict! name: 'docusaurus-openrpc', @@ -92,10 +110,12 @@ export default function docusaurusOpenRpc( async contentLoaded({ content, actions }) { const { openrpcDocument, loadedVersions } = content; + console.log('reqTemplate', reqTemplate); const propsFilePath = await actions.createData( 'props.json', JSON.stringify({ path: options.path, + requestTemplate: reqTemplate, openrpcDocument, }), ); diff --git a/src/theme/OpenRPCDocMethod.tsx b/src/theme/OpenRPCDocMethod.tsx index 3e607b6..0db071f 100644 --- a/src/theme/OpenRPCDocMethod.tsx +++ b/src/theme/OpenRPCDocMethod.tsx @@ -105,6 +105,8 @@ export default function OpenRPCDocMethod(props: any) { method.examples = method.examples || getExamplesFromMethod(method); const [selectedExamplePairing, setSelectedExamplePairing] = React.useState(method.examples[0]); + // requestTemplate + return (
@@ -142,7 +144,7 @@ export default function OpenRPCDocMethod(props: any) {
- {method && } + {method && }
diff --git a/yarn.lock b/yarn.lock index b6f0804..7d72f5a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2856,6 +2856,13 @@ __metadata: languageName: node linkType: hard +"@json-rpc-specification/meta-schema@npm:^1.0.6": + version: 1.0.6 + resolution: "@json-rpc-specification/meta-schema@npm:1.0.6" + checksum: 2eb9c6c6c73bb38350c7180d1ad3c5b8462406926cae753741895b457d7b1b9f0b74148daf3462bb167cef39efdd1d9090308edf4d4938956863acb643c146eb + languageName: node + linkType: hard + "@json-schema-spec/json-pointer@npm:^0.1.2": version: 0.1.2 resolution: "@json-schema-spec/json-pointer@npm:0.1.2" @@ -3115,9 +3122,9 @@ __metadata: languageName: node linkType: hard -"@metamask/open-rpc-docs-react@npm:^0.1.2": - version: 0.1.2 - resolution: "@metamask/open-rpc-docs-react@npm:0.1.2" +"@metamask/open-rpc-docs-react@portal:/Users/shanejonas/code/metamask/openrpc-docs-react::locator=%40metamask%2Fdocusaurus-openrpc%40workspace%3A.": + version: 0.0.0-use.local + resolution: "@metamask/open-rpc-docs-react@portal:/Users/shanejonas/code/metamask/openrpc-docs-react::locator=%40metamask%2Fdocusaurus-openrpc%40workspace%3A." dependencies: "@json-schema-tools/traverse": ^1.10.1 "@open-rpc/examples": ^1.6.1 @@ -3136,9 +3143,8 @@ __metadata: peerDependencies: react: ^17.0.2 react-dom: ^17.0.2 - checksum: 2f9f0e1c8ed4292a9c7402273c02a84db78740966b64aec3cd325b8c279924b98dc8e63685e8fb37d4a05e0a0e491515f93af8b649e66106c3c2d9e529b2c740 languageName: node - linkType: hard + linkType: soft "@nodelib/fs.scandir@npm:2.1.5": version: 2.1.5 From 4c2815d69156a53e3a436b24f0d5a6f4d847789a Mon Sep 17 00:00:00 2001 From: Shane Jonas Date: Thu, 2 May 2024 09:48:20 -0400 Subject: [PATCH 2/3] Added yarn link:setup to help set up linked deps for development --- jest.config.js | 6 +++--- package.json | 4 +++- src/theme/OpenRPCDocMethod.tsx | 1 - yarn.lock | 19 ++++++------------- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/jest.config.js b/jest.config.js index 8da7cbf..3a39cca 100644 --- a/jest.config.js +++ b/jest.config.js @@ -42,10 +42,10 @@ module.exports = { // bump this back up to 80% when we have more tests coverageThreshold: { global: { - branches: 28.15, + branches: 28.3, functions: 22.72, - lines: 32.91, - statements: 32.77, + lines: 33.74, + statements: 33.6, }, }, diff --git a/package.json b/package.json index 26419c2..4568561 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "build": "tsc --project tsconfig.build.json && cp src/theme/OpenRPCDocMethod.css dist/theme/OpenRPCDocMethod.css", "build:clean": "rimraf dist && yarn build", "build:watch": "yarn build && tsc --project tsconfig.build.json --watch", + "link:setup": "rm -rf ./node_modules/@docusaurus/theme-common", "lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:dependencies && yarn lint:changelog", "lint:changelog": "auto-changelog validate", "lint:dependencies": "depcheck", @@ -113,6 +114,7 @@ } }, "resolutions": { - "@metamask/open-rpc-docs-react": "portal:/Users/shanejonas/code/metamask/openrpc-docs-react" + "@metamask/open-rpc-docs-react": "portal:/Users/shanejonas/code/metamask/openrpc-docs-react", + "joi": "17.6.0" } } diff --git a/src/theme/OpenRPCDocMethod.tsx b/src/theme/OpenRPCDocMethod.tsx index 0db071f..3c83fcb 100644 --- a/src/theme/OpenRPCDocMethod.tsx +++ b/src/theme/OpenRPCDocMethod.tsx @@ -4,7 +4,6 @@ import { Sidebar, } from "@docusaurus/plugin-content-docs/src/sidebars/types"; import Layout from '@theme/Layout'; -const {useDocRouteMetadata} = require('@docusaurus/theme-common/internal'); import {ExamplePairingObject, MethodObject, ContentDescriptorObject} from '@open-rpc/meta-schema'; import { InteractiveMethod, Method} from '@metamask/open-rpc-docs-react'; import { join } from 'path'; diff --git a/yarn.lock b/yarn.lock index 7d72f5a..c58cec1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2856,13 +2856,6 @@ __metadata: languageName: node linkType: hard -"@json-rpc-specification/meta-schema@npm:^1.0.6": - version: 1.0.6 - resolution: "@json-rpc-specification/meta-schema@npm:1.0.6" - checksum: 2eb9c6c6c73bb38350c7180d1ad3c5b8462406926cae753741895b457d7b1b9f0b74148daf3462bb167cef39efdd1d9090308edf4d4938956863acb643c146eb - languageName: node - linkType: hard - "@json-schema-spec/json-pointer@npm:^0.1.2": version: 0.1.2 resolution: "@json-schema-spec/json-pointer@npm:0.1.2" @@ -3586,7 +3579,7 @@ __metadata: languageName: node linkType: hard -"@sideway/formula@npm:^3.0.1": +"@sideway/formula@npm:^3.0.0": version: 3.0.1 resolution: "@sideway/formula@npm:3.0.1" checksum: e4beeebc9dbe2ff4ef0def15cec0165e00d1612e3d7cea0bc9ce5175c3263fc2c818b679bd558957f49400ee7be9d4e5ac90487e1625b4932e15c4aa7919c57a @@ -10959,16 +10952,16 @@ __metadata: languageName: node linkType: hard -"joi@npm:^17.6.0": - version: 17.11.0 - resolution: "joi@npm:17.11.0" +"joi@npm:17.6.0": + version: 17.6.0 + resolution: "joi@npm:17.6.0" dependencies: "@hapi/hoek": ^9.0.0 "@hapi/topo": ^5.0.0 "@sideway/address": ^4.1.3 - "@sideway/formula": ^3.0.1 + "@sideway/formula": ^3.0.0 "@sideway/pinpoint": ^2.0.0 - checksum: 3a4e9ecba345cdafe585e7ed8270a44b39718e11dff3749aa27e0001a63d578b75100c062be28e6f48f960b594864034e7a13833f33fbd7ad56d5ce6b617f9bf + checksum: eaf62f6c02f2edb1042f1ab04fc23a5918a2cb8f54bec84c6e1033624d8a462c10ae9518af55a3ba84f1793960450d58094eda308e7ef93c17edd4e3c8ef31d5 languageName: node linkType: hard From 34a783c175e561bc07232064367430c302364030 Mon Sep 17 00:00:00 2001 From: Shane Jonas Date: Thu, 2 May 2024 11:18:55 -0400 Subject: [PATCH 3/3] fix: options pass through + fix default template --- src/content-docs-enhanced-open-rpc/index.ts | 2 +- src/index.ts | 3 ++- src/theme/OpenRPCDocMethod.tsx | 2 +- yarn.lock | 8 ++++++++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/content-docs-enhanced-open-rpc/index.ts b/src/content-docs-enhanced-open-rpc/index.ts index 71eb9ff..8f3959d 100644 --- a/src/content-docs-enhanced-open-rpc/index.ts +++ b/src/content-docs-enhanced-open-rpc/index.ts @@ -42,9 +42,9 @@ async function docsPluginEnhanced( const path = join(options.path, options.openrpc.path); const openrpcPluginInstance: any = openrpcPlugin(context, { + ...options.openrpc, id: options.id, path, - openrpcDocument: options.openrpc.openrpcDocument, }); if (openrpcPluginInstance === undefined) { throw new Error('openrpcPluginInstance is undefined'); diff --git a/src/index.ts b/src/index.ts index 53ec08d..39ab7aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -64,8 +64,9 @@ export default function docusaurusOpenRpc( const aliasedSource = (source: string) => `${posixPath(path.resolve(pluginDataDirRoot, options.id, source))}`; + console.log('OPTIONS=', typeof options.requestTemplate); let reqTemplate: any = { - js: 'await window.fetch(\n "${serverUrl}",\n {\n method: "POST",\n headers: {\n "Content-Type": "application/json"\n },\n body: JSON.stringify(${jsonRpcRequest})\n }\n)', + js: 'await window.fetch(\n "${serverUrl}",\n {\n method: "POST",\n headers: {\n "Content-Type": "application/json"\n },\n body: JSON.stringify(${jsonRpcRequest})\n }\n).then((res) => res.json());', curl: "curl -X POST -H 'Content-Type: application/json' --data ${jsonRpcRequest} ${serverUrl}", python: 'import requests\n\nrequests.request(\n "POST",\n "${serverUrl}",\n headers={\n "Content-Type": "application/json"\n },\n data=${jsonRpcRequest}\n)', diff --git a/src/theme/OpenRPCDocMethod.tsx b/src/theme/OpenRPCDocMethod.tsx index 3c83fcb..a66678f 100644 --- a/src/theme/OpenRPCDocMethod.tsx +++ b/src/theme/OpenRPCDocMethod.tsx @@ -143,7 +143,7 @@ export default function OpenRPCDocMethod(props: any) {
- {method && } + {method && }
diff --git a/yarn.lock b/yarn.lock index c58cec1..7a10ac0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3130,6 +3130,7 @@ __metadata: "@stoplight/mosaic-code-viewer": ^1.32 hash-color-material: ^1.1.3 lodash: ^4.17.15 + openapi-url-resolver: ^1.0.8 qs: ^6.11.1 react-markdown: ^8.0.7 react-syntax-highlighter: ^15.4.3 @@ -12827,6 +12828,13 @@ __metadata: languageName: node linkType: hard +"openapi-url-resolver@npm:^1.0.8": + version: 1.0.8 + resolution: "openapi-url-resolver@npm:1.0.8" + checksum: 26ab7a6103f44b5c9435540e4dcd18e8103453ab597f05e38be897bf89953b817841416cf3e219fb4b4add14174550e07a20fbb7cb30cc972948fd210360944a + languageName: node + linkType: hard + "opener@npm:^1.5.2": version: 1.5.2 resolution: "opener@npm:1.5.2"