Skip to content

Commit

Permalink
fix: move middleware out
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed Apr 6, 2019
1 parent 1cd3790 commit c43a4e0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/transports/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down
12 changes: 6 additions & 6 deletions src/transports/http.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
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;

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);
Expand Down
10 changes: 9 additions & 1 deletion src/transports/https.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@ 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 });

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,
});

Expand Down

0 comments on commit c43a4e0

Please sign in to comment.