diff --git a/src/transports/http.test.ts b/src/transports/http.test.ts index cfb57fa2..98be6923 100644 --- a/src/transports/http.test.ts +++ b/src/transports/http.test.ts @@ -3,11 +3,21 @@ import { parse } from "@open-rpc/schema-utils-js"; import { Router } from "../router"; import { HTTPServerTransport } from "./http"; import fetch from "node-fetch"; +import cors from "cors"; +import { json as jsonParser } from "body-parser"; +import { HandleFunction } from "connect"; describe("http transport", () => { it("can start an http server that works", async () => { const simpleMathExample = await parse(JSON.stringify(examples.simpleMath)); - const httpTransport = new HTTPServerTransport({ port: 9696 }); + const corsOptions = { origin: "*" } as cors.CorsOptions; + const httpTransport = new HTTPServerTransport({ + middleware: [ + cors(corsOptions) as HandleFunction, + jsonParser(), + ], + port: 9696, + }); const router = new Router(simpleMathExample, { mockMode: true }); diff --git a/src/transports/http.ts b/src/transports/http.ts index 71c3557c..b4c06782 100644 --- a/src/transports/http.ts +++ b/src/transports/http.ts @@ -1,12 +1,13 @@ import cors from "cors"; import { json as jsonParser } from "body-parser"; import connect, { HandleFunction } from "connect"; -import http from "http"; +import http, { ServerOptions } from "http"; import { ServerTransport } from "./server-transport"; -interface IHTTPServerTransportOptions { +type IHTTPServerTransportOptions = { + middleware: HandleFunction[], port: number; -} +} & ServerOptions; export class HTTPServerTransport extends ServerTransport { private server: http.Server; @@ -14,10 +15,9 @@ export class HTTPServerTransport extends ServerTransport { constructor(private options: IHTTPServerTransportOptions) { super(); const app = connect(); - const corsOptions = { origin: "*" } as cors.CorsOptions; - app.use(cors(corsOptions) as HandleFunction); - app.use(jsonParser()); + this.options.middleware.forEach((mw) => app.use(mw)); + app.use(this.httpRouterHandler.bind(this) as HandleFunction); this.server = http.createServer(app); diff --git a/src/transports/https.test.ts b/src/transports/https.test.ts index 515370e8..20fb5bd5 100644 --- a/src/transports/https.test.ts +++ b/src/transports/https.test.ts @@ -7,6 +7,9 @@ import { promisify } from "util"; import { HTTPSServerTransport } from "./https"; const readFile = promisify(fs.readFile); import https from "https"; +import cors from "cors"; +import { json as jsonParser } from "body-parser"; +import { HandleFunction } from "connect"; const agent = new https.Agent({ rejectUnauthorized: false }); @@ -14,10 +17,15 @@ describe("https transport", () => { it("can start an https server that works", async () => { const simpleMathExample = await parse(JSON.stringify(examples.simpleMath)); + const corsOptions = { origin: "*" } as cors.CorsOptions; + const httpsTransport = new HTTPSServerTransport({ cert: await readFile(`${process.cwd()}/test-cert/server.cert`), key: await readFile(`${process.cwd()}/test-cert/server.key`), - middleware: [], + middleware: [ + cors(corsOptions) as HandleFunction, + jsonParser(), + ], port: 9697, });