Skip to content

refactor: simplify http emitter #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transport/emitter.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<AxiosResponse> {
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<AxiosResponse> {
const config = {
...defaults,
...options,
method: "POST",
data: event,
};
return axios.request(config as AxiosRequestConfig);
}
2 changes: 0 additions & 2 deletions src/transport/http/index.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/transport/http/structured_emitter.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/integration/http_binding_03.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion test/integration/http_binding_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down