From 381f00e79840543ea9ebb74063a0e71e37af8e7c Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Mon, 10 Aug 2020 22:32:07 -0500 Subject: [PATCH] refactor: simplify http emitter Signed-off-by: Grant Timmerman --- src/transport/emitter.ts | 2 +- .../http/{binary_emitter.ts => emitter.ts} | 23 ++++++++++++++----- src/transport/http/index.ts | 2 -- src/transport/http/structured_emitter.ts | 20 ---------------- test/integration/http_binding_03.ts | 2 +- test/integration/http_binding_1.ts | 2 +- 6 files changed, 20 insertions(+), 31 deletions(-) rename src/transport/http/{binary_emitter.ts => emitter.ts} (72%) delete mode 100644 src/transport/http/index.ts delete mode 100644 src/transport/http/structured_emitter.ts diff --git a/src/transport/emitter.ts b/src/transport/emitter.ts index 911339e3..40b24ee0 100644 --- a/src/transport/emitter.ts +++ b/src/transport/emitter.ts @@ -1,5 +1,5 @@ import { CloudEvent } from "../event/cloudevent"; -import { emitBinary, emitStructured } from "./http"; +import { emitBinary, emitStructured } from "./http/emitter"; import { Protocol } from "./protocols"; import { AxiosResponse } from "axios"; import { Agent } from "http"; diff --git a/src/transport/http/binary_emitter.ts b/src/transport/http/emitter.ts similarity index 72% rename from src/transport/http/binary_emitter.ts rename to src/transport/http/emitter.ts index 20f51662..7fea429a 100644 --- a/src/transport/http/binary_emitter.ts +++ b/src/transport/http/emitter.ts @@ -1,11 +1,16 @@ import axios, { AxiosRequestConfig, AxiosResponse } from "axios"; - import { CloudEvent, Version } from "../../event/cloudevent"; import { TransportOptions } from "../emitter"; import { Headers, headersFor } from "./headers"; import { asData } from "../../event/validation"; import CONSTANTS from "../../constants"; +const defaults = { + headers: { + [CONSTANTS.HEADER_CONTENT_TYPE]: CONSTANTS.DEFAULT_CE_CONTENT_TYPE, + }, +}; + /** * Send a CloudEvent over HTTP POST to the `options.url` provided. * @param {CloudEvent} event the event to send to the remote endpoint @@ -16,16 +21,22 @@ export async function emitBinary(event: CloudEvent, options: TransportOptions): if (event.specversion !== Version.V1 && event.specversion !== Version.V03) { return Promise.reject(`Unknown spec version ${event.specversion}`); } - return emit(event, options, headersFor(event)); -} - -async function emit(event: CloudEvent, options: TransportOptions, headers: Headers): Promise { const contentType: Headers = { [CONSTANTS.HEADER_CONTENT_TYPE]: CONSTANTS.DEFAULT_CONTENT_TYPE }; const config = { ...options, method: "POST", - headers: { ...contentType, ...headers, ...(options.headers as Headers) }, + headers: { ...contentType, ...headersFor(event), ...(options.headers as Headers) }, data: asData(event.data, event.datacontenttype as string), }; return axios.request(config as AxiosRequestConfig); } + +export function emitStructured(event: CloudEvent, options: TransportOptions): Promise { + const config = { + ...defaults, + ...options, + method: "POST", + data: event, + }; + return axios.request(config as AxiosRequestConfig); +} diff --git a/src/transport/http/index.ts b/src/transport/http/index.ts deleted file mode 100644 index f25036dc..00000000 --- a/src/transport/http/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./binary_emitter"; -export * from "./structured_emitter"; diff --git a/src/transport/http/structured_emitter.ts b/src/transport/http/structured_emitter.ts deleted file mode 100644 index af6ec6b5..00000000 --- a/src/transport/http/structured_emitter.ts +++ /dev/null @@ -1,20 +0,0 @@ -import axios, { AxiosRequestConfig, AxiosResponse } from "axios"; -import { CloudEvent } from "../../event/cloudevent"; -import { TransportOptions } from "../emitter"; -import CONSTANTS from "../../constants"; - -const defaults = { - headers: { - [CONSTANTS.HEADER_CONTENT_TYPE]: CONSTANTS.DEFAULT_CE_CONTENT_TYPE, - }, -}; - -export function emitStructured(event: CloudEvent, options: TransportOptions): Promise { - const config = { - ...defaults, - ...options, - method: "POST", - data: event, - }; - return axios.request(config as AxiosRequestConfig); -} diff --git a/test/integration/http_binding_03.ts b/test/integration/http_binding_03.ts index aae7f3b7..0717890e 100644 --- a/test/integration/http_binding_03.ts +++ b/test/integration/http_binding_03.ts @@ -2,7 +2,7 @@ import "mocha"; import { expect } from "chai"; import nock from "nock"; -import { emitBinary, emitStructured } from "../../src/transport/http"; +import { emitBinary, emitStructured } from "../../src/transport/http/emitter"; import { CloudEvent, Version } from "../../src"; import { AxiosResponse } from "axios"; diff --git a/test/integration/http_binding_1.ts b/test/integration/http_binding_1.ts index d64cd75e..5a683bc4 100644 --- a/test/integration/http_binding_1.ts +++ b/test/integration/http_binding_1.ts @@ -4,7 +4,7 @@ import { expect } from "chai"; import nock from "nock"; import { CloudEvent, Version } from "../../src"; -import { emitBinary, emitStructured } from "../../src/transport/http"; +import { emitBinary, emitStructured } from "../../src/transport/http/emitter"; import { asBase64 } from "../../src/event/validation"; import { AxiosResponse } from "axios";