Skip to content

Commit

Permalink
feat(headers): added parse operation to convert strings into an object
Browse files Browse the repository at this point in the history
fix: header keys incorrectly being format
  • Loading branch information
Michael committed Nov 23, 2022
1 parent 752fbd4 commit cac69de
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
55 changes: 50 additions & 5 deletions lib/core/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ import {
merge,
capitalize,
argumentIsNotProvided,
isEmpty
isEmpty,
lowercase,
stringReplacer
} from "../utils";
import { debug } from "../debug";
import { environment } from "../environment";
import { DEFAULT_BROWSER_HEADERS, DEFAULT_NODE_HEADERS } from "./constants";

function removeNewLines(value: string): string {
return stringReplacer(value, "\n", "");
}

function parseHeaderKey(key: string): string {
if (key) {
return formatHeaderKey(key.toLowerCase()).trim();
const format = formatHeaderKey(lowercase(key)).trim();
return removeNewLines(format);
}

return undefined;
Expand All @@ -36,7 +43,8 @@ function parseHeaderValue(value: HeaderValues): string {
return JSON.stringify(value);
}

return String(value);
const newValue = value.toString().trim();
return removeNewLines(newValue);
}

function normalizeHeaders(headers: Headers): NormalizedHeaders {
Expand All @@ -59,7 +67,19 @@ function normalizeHeaders(headers: Headers): NormalizedHeaders {
}

function formatHeaderKey(key: string): string {
return key.split("-").map(capitalize).join("-");
// split by the dash
// capitalize each word
// join the words back together

const words = key.split("-");
const formattedWords = words.map((word) => {
const parsedWord = removeNewLines(word).trim();

if (parsedWord) {
return capitalize(parsedWord);
}
});
return formattedWords.join("-");
}

export class UrbexHeaders {
Expand All @@ -79,6 +99,32 @@ export class UrbexHeaders {
return new UrbexHeaders(headers, withDefaults);
}

/**
* Parse a headers string into an object
*/
static parse(headers: string): NormalizedHeaders {
if (argumentIsNotProvided(headers)) {
return {};
}

const parsedHeaders: NormalizedHeaders = {};

const lines = headers.split("\r");

forEach(lines, (index, pair) => {
const [pairKey, pairValue] = pair.toString().split(":");

const key = parseHeaderKey(pairKey);
const value = parseHeaderValue(pairValue);

if (key && value) {
parsedHeaders[key] = value;
}
});

return parsedHeaders;
}

get defaults(): typeof DEFAULT_NODE_HEADERS | typeof DEFAULT_BROWSER_HEADERS {
return environment.isNode ? DEFAULT_NODE_HEADERS : DEFAULT_BROWSER_HEADERS;
}
Expand Down Expand Up @@ -145,7 +191,6 @@ export class UrbexHeaders {
/**
* Normalize an incoming headers object
*/

public normalize(headers: Headers): NormalizedHeaders {
if (argumentIsNotProvided(headers) || !isObject(headers)) {
return {};
Expand Down
7 changes: 6 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ export function isEmpty(value: any): boolean {

export function capitalize(value: string): string {
value = String(value);
return value.charAt(0).toUpperCase() + value.slice(1);

if (value.length === 1) {
return value.toUpperCase();
} else {
return value.charAt(0).toUpperCase() + value.slice(1);
}
}

export function uppercase<T extends string>(value: T): T {
Expand Down
12 changes: 12 additions & 0 deletions test/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,16 @@ describe("UrbexHeaders", () => {
"X-Baz": "FOO"
});
});

it("should parse a header string into an object", () => {
const string = "X-Foo: bar\r X-Bar: foo\r X-Baz: FOO";

const result = UrbexHeaders.parse(string);

chai.expect(result).to.deep.equal({
"X-Foo": "bar",
"X-Bar": "foo",
"X-Baz": "FOO"
});
});
});

0 comments on commit cac69de

Please sign in to comment.