Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
support json-api content type
Browse files Browse the repository at this point in the history
  • Loading branch information
sjkaliski committed Mar 4, 2019
1 parent 65d8e7b commit 4778161
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
18 changes: 18 additions & 0 deletions packages/api-explorer/__tests__/lib/content-type-is-json.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const contentTypeIsJson = require('../../src/lib/content-type-is-json');

describe('contentTypeIsJson', () => {
it('should return true if application/json', () => {
const contentType = 'application/json';
expect(contentTypeIsJson(contentType)).toBe(true);
});

it('should return true if application/vnd.api+json', () => {
const contentType = 'application/vnd.api+json';
expect(contentTypeIsJson(contentType)).toBe(true);
});

it('should return false otherwise', () => {
const contentType = 'text/html';
expect(contentTypeIsJson(contentType)).toBe(false);
});
});
7 changes: 6 additions & 1 deletion packages/api-explorer/__tests__/lib/parse-response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ test('isBinary should be true if there is a content-disposition response header'
);
});

test('should parse json response', async () => {
test('should parse application/json response as json', async () => {
expect((await parseResponse(har, response)).responseBody).toEqual(JSON.parse(responseBody));
});

test('should parse application/vnd.api+json as json', async () => {
response.headers['Content-Type'] = 'application/vnd.api+json';
expect((await parseResponse(har, response)).responseBody).toEqual(JSON.parse(responseBody));
});

Expand Down
3 changes: 2 additions & 1 deletion packages/api-explorer/src/Example.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const React = require('react');
const PropTypes = require('prop-types');
const ReactJson = require('react-json-view').default;
const showCodeResults = require('./lib/show-code-results');
const contentTypeIsJson = require('./lib/content-type-is-json');

// const { replaceVars } = require('./lib/replace-vars');
const syntaxHighlighter = require('@readme/syntax-highlighter');
Expand All @@ -25,7 +26,7 @@ function Example({ operation, result, oas, selected, setExampleTab, exampleRespo
<ExampleTabs examples={examples} selected={selected} setExampleTab={setExampleTab} />
<div className="code-sample-body">
{examples.map((example, index) => {
const isJson = example.language && example.language === 'application/json';
const isJson = example.language && contentTypeIsJson(example.language);
return (
<pre
className={`tomorrow-night tabber-body tabber-body-${index}`}
Expand Down
4 changes: 2 additions & 2 deletions packages/api-explorer/src/ResponseBody.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const React = require('react');
const PropTypes = require('prop-types');
const syntaxHighlighter = require('@readme/syntax-highlighter');
const ReactJson = require('react-json-view').default;

const contentTypeIsJson = require('./lib/content-type-is-json');
const oauthHref = require('./lib/oauth-href');

function Authorized({ result }) {
const isJson =
result.type &&
result.type.includes('application/json') &&
contentTypeIsJson(result.type) &&
typeof result.responseBody === 'object';
return (
<div>
Expand Down
6 changes: 6 additions & 0 deletions packages/api-explorer/src/lib/content-type-is-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function contentTypeIsJson(contentType) {
const jsonContentTypes = ['application/json', 'application/vnd.api+json'];
return jsonContentTypes.some(ct => contentType.includes(ct));
}

module.exports = contentTypeIsJson;
3 changes: 2 additions & 1 deletion packages/api-explorer/src/lib/parse-response.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { stringify } = require('querystring');
const contentTypeIsJson = require('./content-type-is-json');

function getQuerystring(har) {
// Converting [{ name: a, value: '123456' }] => { a: '123456' } so we can use querystring.stringify
Expand All @@ -11,7 +12,7 @@ function getQuerystring(har) {

async function getResponseBody(response) {
const contentType = response.headers.get('Content-Type');
const isJson = contentType && contentType.includes('application/json');
const isJson = contentType && contentTypeIsJson(contentType);

// We have to clone it before reading, just incase
// we cannot parse it as JSON later, then we can
Expand Down

0 comments on commit 4778161

Please sign in to comment.