Skip to content

Commit

Permalink
expand .json() to include text/json and XML; fixes #918
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed May 28, 2024
1 parent 2a68cf1 commit 4fed190
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-emus-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": minor
---

expand the .json() shortcut to include variations of JSON and XML content types
6 changes: 3 additions & 3 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ The `$.response` object is used to build a valid response for the URL and reques
- `.random()` returns random data, using `examples` and other metadata from the OpenAPI document.
- `.header(name, value)` adds a response header. It will only show up when a response header is expected and you haven't already provided it.
- `.match(contentType, content)` is used to return content which matches the content type. If the API is intended to serve one of multiple content types, depending on the client's `Accepts:` header, you can chain multiple `match()` calls.
- `.json(content)` is shorthand for `.match("application/json", content)`
- `.text(content)` is shorthand for `.match("text/plain", content)`
- `.html(content)` is shorthand for `.match("text/html", content)`
- `.json(content)`, `.text(content)`, `.html(content)`, and `.xml(content)` are shorthands for the `match()` function, e.g. `.text(content)` is shorthand for `.match("text/plain", content)`.
- if the content type is XML, you can pass a JSON object, and Counterfact will automatically convert it to XML for you
- The `.json()` shortcut handles both JSON and XML.

You can build a response by chaining one or more of these functions, e.g.

Expand Down
6 changes: 5 additions & 1 deletion src/server/response-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ export function createResponseBuilder(
},

json(this: ResponseBuilder, body: unknown) {
return this.match("application/json", body);
return this.match("application/json", body)
.match("text/json", body)
.match("text/x-json", body)
.match("application/xml", body)
.match("text/xml", body);
},

match(
Expand Down
2 changes: 1 addition & 1 deletion src/server/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type GenericResponseBuilderInner<
? never
: HeaderFunction<Response>;
html: MaybeShortcut<"text/html", Response>;
json: MaybeShortcut<"application/json", Response>;
json: MaybeShortcut<"application/json" | "text/json" | "text/x-json" | "application/xml" | "text/xml", Response>;
match: [keyof Response["content"]] extends [never]
? never
: MatchFunction<Response>;
Expand Down
10 changes: 10 additions & 0 deletions test/server/response-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ describe("a response builder", () => {
expect(response?.content).toStrictEqual([
{ body: "hello", type: "text/plain" },
{ body: { hello: "world" }, type: "application/json" },
{ body: { hello: "world" }, type: "text/json" },
{ body: { hello: "world" }, type: "text/x-json" },
{
body: "<root><hello>world</hello></root>",
type: "application/xml",
},
{
body: "<root><hello>world</hello></root>",
type: "text/xml",
},
{ body: "<h1>Hello World</h1>", type: "text/html" },
{
body: "<root><hello>world</hello></root>",
Expand Down

0 comments on commit 4fed190

Please sign in to comment.