Skip to content

Commit

Permalink
feat: allow serving examples forever
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed Mar 5, 2020
1 parent bd28c4c commit 5340e95
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"dependencies": {
"@open-rpc/schema-utils-js": "^1.11.6",
"@open-rpc/server-js": "^1.5.2",
"lodash": "^4.17.11"
"lodash": "^4.17.11",
"@open-rpc/examples": "^1.3.5"
},
"devDependencies": {
"@open-rpc/examples": "^1.3.5",
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { OpenrpcDocument as OpenRPC, MethodObject } from "@open-rpc/meta-schema";
import { OpenrpcDocument as OpenRPC, MethodObject, OpenrpcDocument } from "@open-rpc/meta-schema";
import { Server, IServerOptions, Router } from "@open-rpc/server-js";
import _ from "lodash";
import { IMethodMapping } from "@open-rpc/server-js/build/router";
import { parseOpenRPCDocument } from "@open-rpc/schema-utils-js";
import examples from "@open-rpc/examples";

const makePrefix = (sluggedDocumentTitle: string, version: string) => {
return `${_.camelCase(sluggedDocumentTitle)}-${version}-`;
};

const exNames = Object.values(examples).map((doc: OpenrpcDocument) => makePrefix(doc.info.title, doc.info.version));

const createServiceMethodMapping = (s: Server, document: OpenRPC): IMethodMapping => {
return {
mock: async (openrpcDocument: OpenRPC) => {
Expand All @@ -20,9 +23,11 @@ const createServiceMethodMapping = (s: Server, document: OpenRPC): IMethodMappin
} as OpenRPC;

const parsedDoc = await parseOpenRPCDocument(prefixedOpenRPCDocument);
const router = s.addRouter(prefixedOpenRPCDocument, { mockMode: true });
const router = s.addRouter(parsedDoc, { mockMode: true });

setTimeout(() => s.removeRouter(router), 15 * 60 * 1000);
if (exNames.indexOf(openrpcDocument.info.title) === -1) {
setTimeout(() => s.removeRouter(router), 15 * 60 * 1000);
}

return prefix.slice(0, -1);
},
Expand Down

0 comments on commit 5340e95

Please sign in to comment.