Skip to content

Commit

Permalink
fix(request-body): output request body types
Browse files Browse the repository at this point in the history
this commit fixes the issue where body types other than json and form-url-encoded
are not included in the output.
  • Loading branch information
pongstr committed Oct 3, 2021
1 parent 3e6d7c3 commit 227f120
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
@@ -1 +1 @@
v14.16.0
v16.9
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -61,6 +61,7 @@
"yaml",
"yml"
],
"languageHighlighter": "json",
"inputs": "json",
"help": "https://github.com/luckymarmot/Paw-OpenAPI3Generator/wiki",
"outputDir": "\"/Library/Containers/com.luckymarmot.Paw/Data/Library/Application Support/com.luckymarmot.Paw/Extensions\""
Expand Down
95 changes: 74 additions & 21 deletions src/lib/converter.ts
Expand Up @@ -224,26 +224,78 @@ export function buildRequestBodyObject(
request.method.toString().toLowerCase(),
),
}
// extract content type, currently we have support to parse the schema for these
// content types: application/x-www-form-urlencoded, application/json

const getContentType = Object.keys(request.headers)
.filter((header) => header.toLowerCase() === 'content-type')
.map((header) => request.headers[header])
const contentType =
getContentType.length > 0 ? getContentType[0] : 'text/plain'

if (request.jsonBody && contentType === 'application/json') {
output.content[contentType as string] = {
schema: extendToJsonSchema(
toJsonSchema(request.jsonBody),
request.jsonBody,
) as OpenAPIV3.SchemaObject,
}
.map((header) => (request.headers[header] as string).toLowerCase())

const type = getContentType
.map((header) => header.match(/((\w+)\/\'?\w+([-']\w+)*\'?)/g))
.join()
.toString()

const body = request.getBody(true) as DynamicString
switch (type) {
case 'multipart/form-data':
output.content[type as string] = body
? {
schema: extendToJsonSchema(
toJsonSchema(request.getMultipartBody()),
request.getMultipartBody(),
) as OpenAPIV3.SchemaObject,
}
: {}
break

return output
case 'application/x-www-form-urlencoded':
output.content[type as string] = body
? {
schema: extendToJsonSchema(
toJsonSchema(body.getEvaluatedString()),
body.getEvaluatedString(),
) as OpenAPIV3.SchemaObject,
}
: {}
break

case 'application/json':
output.content[type as string] = body
? {
schema: extendToJsonSchema(
toJsonSchema(JSON.parse(body.getEvaluatedString())),
JSON.parse(body.getEvaluatedString()),
) as OpenAPIV3.SchemaObject,
}
: {}
break

case 'application/octet-stream':
const content = body.getOnlyDynamicValue() as DynamicValue
output.content[type as string] =
content.type === 'com.luckymarmot.FileContentDynamicValue'
? {
schema: {
type: 'string',
format: 'base64',
example: content.bookmarkData ? content.bookmarkData : '',
},
}
: {}
break

case 'text/plain':
output.content['text/plain'] = body
? {
schema: {
type: 'string',
example: request.getBody() || '',
},
}
: {}
break
}

return undefined
return output
}

/**
Expand Down Expand Up @@ -319,16 +371,18 @@ export function buildResponsesObject(
Object.create({}) as OpenAPIV3.HeaderObject,
)

const statusCode = getHttpExchange.responseStatusCode.toString()
const statusCode =
getHttpExchange.responseStatusCode === 0
? 200
: getHttpExchange.responseStatusCode

const contentType =
getContentType.length > 0
? getContentType[0].replace(/(; .*)$/g, '')
: 'text/plain'

const responses: OpenAPIV3.ResponsesObject = {
[statusCode]: {
description: '',
},
[statusCode]: { description: '' },
}

const getResponseType = jsonParseCheck(getHttpExchange.responseBody)
Expand All @@ -340,7 +394,7 @@ export function buildResponsesObject(
case isURLEncoded.test(contentType) && typeof getResponseType === 'object':
responses[statusCode] = {
...buildResponseObject(contentType, getResponseType),
headers: getHeaders as { [key: string]: OpenAPIV3.HeaderObject },
headers: getHeaders as Record<string, OpenAPIV3.HeaderObject>,
}
break
default:
Expand All @@ -356,7 +410,6 @@ export function buildResponsesObject(
}
break
}

return responses
}

Expand Down
9 changes: 8 additions & 1 deletion src/lib/exporter.ts
Expand Up @@ -8,7 +8,13 @@ import {
buildServerObject,
} from './converter'

const { identifier, title, inputs, fileExtensions } = config
const {
identifier,
title,
inputs,
fileExtensions,
languageHighlighter,
} = config

/**
* @class
Expand All @@ -18,6 +24,7 @@ export default class OpenAPIv3Generator implements Paw.Generator {
public static title = title
public static inputs = inputs
public static identifier = identifier
public static languageHighlighter = languageHighlighter
public static fileExtensions = [...fileExtensions]

/**
Expand Down
2 changes: 2 additions & 0 deletions src/types/paw.d.ts
Expand Up @@ -50,6 +50,8 @@ declare global {
getEvaluatedString(): string
copy(): DynamicValue
variableUUID: string
// @todo: confirm whether bookmarkData field should be accessed publicly
bookmarkData?: string
}

class NetworkHTTPRequest {}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/dynamic-values.ts
Expand Up @@ -65,7 +65,7 @@ export const createFileValues = (): DynamicValue =>
createDynamicValue(FILE_DYNAMIC_VALUE, { bookmarkData: null })

/**
* @exports transformString
* @exports convertEnvString
* @summary
*
* @param {Object<TransformStringType>} opts
Expand All @@ -74,7 +74,7 @@ export const createFileValues = (): DynamicValue =>
* @param {String} opts.stringInput
* @param {Object<Paw.Request>} opts.requestInput
*
* @returns {DynamicValue} class instance
* @returns {string}
*/
export function convertEnvString(
dynamicString: DynamicString,
Expand Down

0 comments on commit 227f120

Please sign in to comment.