Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jnaglick committed Jun 14, 2024
1 parent da754ac commit 44abb11
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
19 changes: 3 additions & 16 deletions packages/core/src/providers/http/httpModelProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
StaticHeadersStrategy,
} from "./strategies";

import { escape } from "./utils";

/**
* @category Core Implementations
*/
Expand Down Expand Up @@ -103,22 +105,7 @@ export class HttpModelProvider<
}

protected getBody(options: TRequestOptions) {
// TODO move this to "JsonBodyStrategy" (or something like that)
const escapedOptions = Object.entries(options).reduce(
(acc, [key, value]) => {
if (typeof value === "string") {
return {
...acc,
[key]: value.replace(/\n/g, "\\n"),
};
}
return {
...acc,
[key]: value as unknown,
};
},
{} as TRequestOptions,
);
const escapedOptions = escape<TRequestOptions>(options);

return this.api.requestTemplate.render(escapedOptions);
}
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/providers/http/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function escape<T>(obj: object) {
return Object.entries(obj).reduce((acc, [key, value]) => {
if (typeof value === "string") {
return {
...acc,
[key]: value.replace(/\n/g, "\\n"),
};
}
return {
...acc,
[key]: value as unknown,
};
}, {} as T);
}

0 comments on commit 44abb11

Please sign in to comment.