Skip to content

Commit

Permalink
Add XML response helper (sergiodxa#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkulpinski authored Jan 12, 2023
1 parent 79eaf73 commit 7477fd3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,18 @@ export let loader: LoaderFunction = async ({ request }) => {
};
```

#### XML

Helper function to create a XML file response with any header.

This is useful to create XML files based on data inside a Resource Route.

```ts
export let loader: LoaderFunction = async ({ request }) => {
return xml("<?xml version='1.0'?><catalog></catalog>");
};
```

### Typed Cookies

Cookie objects in Remix allows any type, the typed cookies from Remix Utils lets you use Zod to parse the cookie values and ensure they conform to a schema.
Expand Down
28 changes: 28 additions & 0 deletions src/server/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,34 @@ export function html(
});
}

/**
* Create a response with a XML file response.
* It receives a string with the XML content and set the Content-Type header to
* `application/xml; charset=utf-8` always.
*
* This is useful to dynamically create a XML file from a Resource Route.
* @example
* export let loader: LoaderFunction = async ({ request }) => {
* return xml("<?xml version='1.0'?><catalog></catalog>");
* }
*/
export function xml(
content: string,
init: number | ResponseInit = {}
): Response {
let responseInit = typeof init === "number" ? { status: init } : init;

let headers = new Headers(responseInit.headers);
if (!headers.has("Content-Type")) {
headers.set("Content-Type", "application/xml; charset=utf-8");
}

return new Response(content, {
...responseInit,
headers,
});
}

export type ImageType =
| "image/jpeg"
| "image/png"
Expand Down
34 changes: 34 additions & 0 deletions test/server/responses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
badRequest,
forbidden,
html,
xml,
image,
ImageType,
javascript,
Expand Down Expand Up @@ -295,6 +296,39 @@ describe("Responses", () => {
});
});

describe(xml, () => {
let content = "<?xml version='1.0'?><catalog></catalog>";
test("Should return Response with status 200", async () => {
let response = xml(content);
await expect(response.text()).resolves.toBe(content);
expect(response.status).toBe(200);
expect(response.headers.get("Content-Type")).toBe(
"application/xml; charset=utf-8"
);
});

test("Should allow defining the status as second options", async () => {
let response = xml(content, 201);
await expect(response.text()).resolves.toBe(content);
expect(response.status).toBe(201);
expect(response.headers.get("Content-Type")).toBe(
"application/xml; charset=utf-8"
);
});

test("Should allow changing the Response headers", async () => {
let response = xml(content, {
headers: { "X-Test": "it worked" },
});
await expect(response.text()).resolves.toBe(content);
expect(response.status).toBe(200);
expect(response.headers.get("Content-Type")).toBe(
"application/xml; charset=utf-8"
);
expect(response.headers.get("X-Test")).toBe("it worked");
});
});

describe(image, () => {
let content = new ArrayBuffer(0);
test.each([
Expand Down

0 comments on commit 7477fd3

Please sign in to comment.