Skip to content

Commit

Permalink
Merge branch 'hotfix/v1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
kuyoonjo committed Jun 28, 2018
2 parents 59f2b71 + bdb8cd7 commit f44e987
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 9 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: node_js
node_js:
- '8'
- '9'
script:
- npm run coverage # jest test with coverage flag does coverage too
after_script:
- 'cat coverage/lcov.info | ./node_modules/.bin/coveralls' # sends the coverage report to coveralls
43 changes: 43 additions & 0 deletions __tests__/response-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Js2Xml } from 'js2xml';
import { response } from '../src';

console.log = jest.fn();
console.error = jest.fn();

test('is should response without body', () => {
const ctx: any = {
headers: {
accept: 'application/json',
},
};
const body = { foo: 'bar' };
response(ctx, 200);
expect(ctx.status).toBe(200);
});

test('is should response json', () => {
const ctx: any = {
headers: {
accept: 'application/json',
},
};
const body = { foo: 'bar' };
response(ctx, 200, body);
expect(ctx.body).toBe(body);
expect(ctx.status).toBe(200);
});

test('is should response xml', () => {
const ctx: any = {
headers: {
accept: 'application/xml',
},
};
const body = { foo: 'bar' };
const json = JSON.stringify(body);
const obj = JSON.parse(json);
const xml: any = new Js2Xml('xml', obj);
response(ctx, 200, body);
expect(ctx.body).toBe(xml.toString());
expect(ctx.status).toBe(200);
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ycs/db",
"version": "1.0.0",
"version": "1.0.1",
"description": "db",
"license": "MIT",
"repository": "https://github.com/yc-server/db.git",
Expand Down Expand Up @@ -36,6 +36,7 @@
"@ycs/docs": "^1.0.0",
"@ycs/error": "^1.0.1",
"@ycs/interfaces": "^1.0.1",
"js2xml": "^1.0.8",
"lodash": "^4.17.10",
"mongoose": "^5.1.7",
"mongoose-paginate": "^5.0.3"
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './db';
export * from './IModelOptions';
export * from './paginate';
export * from './patchUpdates';
export * from './response';
export * from './show';
25 changes: 25 additions & 0 deletions src/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Js2Xml } from 'js2xml';
import { IContext } from '@ycs/interfaces';

/**
* Generating output
* @param ctx {IContext} Ycs context
* @param status {number} status
* @param body {body} body
*/
export function response(ctx: IContext, status: number, body?: any) {
if (!body) {
ctx.type = 'text/plain';
}
if ('application/xml' === ctx.headers.accept) {
const json = JSON.stringify(body);
const obj = JSON.parse(json);
const xml: any = new Js2Xml('xml', obj);
ctx.status = status;
ctx.type = `${ctx.headers.accept}; charset=utf-8`;
ctx.body = xml.toString();
} else {
ctx.status = status;
ctx.body = body;
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": false,
"lib": [
"esnext"
],
Expand Down

0 comments on commit f44e987

Please sign in to comment.