Skip to content

Commit

Permalink
feat: adding some new isMultipart and isJson methods on the operation (
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Aug 13, 2020
1 parent 2ad1c19 commit 4dd52b3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
58 changes: 58 additions & 0 deletions packages/tooling/__tests__/operation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,64 @@ describe('#getContentType', () => {
});
});

describe('#isMultipart', () => {
it.each([['multipart/mixed'], ['multipart/related'], ['multipart/form-data'], ['multipart/alternative']])(
'should identify `%s` as multipart',
contentType => {
const op = new Operation(petstore, '/multipart', 'get', {
requestBody: {
content: {
[contentType]: {
schema: {
type: 'object',
properties: {
documentFile: {
type: 'string',
format: 'binary',
},
},
},
},
},
},
});

expect(op.getContentType()).toBe(contentType);
expect(op.isMultipart()).toBe(true);
expect(op.isJson()).toBe(false);
}
);
});

describe('#isJson', () => {
it.each([
['application/json'],
['application/x-json'],
['text/json'],
['text/x-json'],
['application/vnd.github.v3.star+json'],
])('should identify `%s` as json', contentType => {
const op = new Operation(petstore, '/json', 'get', {
requestBody: {
content: {
[contentType]: {
schema: {
type: 'array',
items: {
type: 'string',
},
},
},
},
},
});

expect(op.getContentType()).toBe(contentType);
expect(op.isJson()).toBe(true);
expect(op.isMultipart()).toBe(false);
});
});

describe('#getSecurity()', () => {
const security = [{ auth: [] }];

Expand Down
32 changes: 28 additions & 4 deletions packages/tooling/src/operation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/* eslint-disable no-underscore-dangle */
const findSchemaDefinition = require('./lib/find-schema-definition');

function matchesMimeType(arr, contentType) {
return arr.some(function (type) {
return contentType.indexOf(type) > -1;
});
}

class Operation {
constructor(oas, path, method, operation) {
Object.assign(this, operation);
Expand All @@ -10,6 +16,10 @@ class Operation {
}

getContentType() {
if (typeof this.contentType !== 'undefined') {
return this.contentType;
}

let types = [];
if (this.requestBody) {
if ('$ref' in this.requestBody) {
Expand All @@ -21,19 +31,33 @@ class Operation {
}
}

let type = 'application/json';
this.contentType = 'application/json';
if (types && types.length) {
type = types[0];
this.contentType = types[0];
}

// Favor JSON if it exists
types.forEach(t => {
if (t.match(/json/)) {
type = t;
this.contentType = t;
}
});

return type;
return this.contentType;
}

isMultipart() {
return matchesMimeType(
['multipart/mixed', 'multipart/related', 'multipart/form-data', 'multipart/alternative'],
this.getContentType()
);
}

isJson() {
return matchesMimeType(
['application/json', 'application/x-json', 'text/json', 'text/x-json', '+json'],
this.getContentType()
);
}

getSecurity() {
Expand Down

0 comments on commit 4dd52b3

Please sign in to comment.