Skip to content

Commit

Permalink
fixes #241
Browse files Browse the repository at this point in the history
  • Loading branch information
William committed Jun 19, 2021
1 parent 801120f commit 463712c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Methods `binary`, `html`, `json`, `redirect`, `xml` can be returned directly
- `response.getStatusCode()`- Get the status code of the response
- `response.html(body)`- Build an html response
- `response.json(body)`- Build a json response
- `response.file(filePath, contentType)`- Sends file
- `response.redirect(url, statusCode = 302)`- Build a redirect response
- `response.setBinaryResponse(value)`- Set the response as a binary response for API Gateway
- `response.setBody(body)`- Set the body of the response
Expand Down
4 changes: 4 additions & 0 deletions src/lib/helpers/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const binary = require('./binary');
const fs = require('fs');

module.exports = (res, filePath, contentType) => binary(res, fs.readFileSync(filePath), contentType);
6 changes: 6 additions & 0 deletions src/lib/response.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const binary = require('./helpers/binary');
const enableCors = require('./helpers/enableCors');
const html = require('./helpers/html');
const file = require('./helpers/file');
const json = require('./helpers/json');
const redirect = require('./helpers/redirect');
const setBinaryResponse = require('./setters/setBinaryResponse');
Expand Down Expand Up @@ -38,6 +39,11 @@ module.exports = () => {
return this;
},
enableCors: () => enableCors(response),
file(filePath, contentType) {
file(response, filePath, contentType);

return this;
},
html(body) {
html(response, body);

Expand Down
25 changes: 25 additions & 0 deletions tests/file.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const response = require('./../src/lib/response');
const fs = require('fs');

const svg = `<?xml version="1.0"><svg width="1" height="1" version="1.1" xmlns="http://www.w3.org/2000/svg"><rect x="0" y="0" width="1" height="1" fill="black" /></svg>`;

beforeEach(() => {
fs.writeFileSync('temp.svg', Buffer.from(svg));
});

afterEach(() => {
fs.unlinkSync('temp.svg');
});

test('file response', () => {
const res = response();

res.file('temp.svg', 'image/svg+xml');

expect(res.getResponse()).toEqual({
body: Buffer.from(svg).toString('base64'),
headers: { 'Content-Type': 'image/svg+xml' },
isBase64Encoded: true,
statusCode: 200,
});
});

0 comments on commit 463712c

Please sign in to comment.