diff --git a/.nvmrc b/.nvmrc index 158c006..d0d1cd5 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -v14.16.0 +v16.9 diff --git a/package.json b/package.json index 8d5817d..2ef58b5 100755 --- a/package.json +++ b/package.json @@ -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\"" diff --git a/src/lib/converter.ts b/src/lib/converter.ts index 1f442bf..90cf539 100644 --- a/src/lib/converter.ts +++ b/src/lib/converter.ts @@ -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 } /** @@ -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) @@ -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, } break default: @@ -356,7 +410,6 @@ export function buildResponsesObject( } break } - return responses } diff --git a/src/lib/exporter.ts b/src/lib/exporter.ts index 12a0030..4368d33 100644 --- a/src/lib/exporter.ts +++ b/src/lib/exporter.ts @@ -8,7 +8,13 @@ import { buildServerObject, } from './converter' -const { identifier, title, inputs, fileExtensions } = config +const { + identifier, + title, + inputs, + fileExtensions, + languageHighlighter, +} = config /** * @class @@ -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] /** diff --git a/src/types/paw.d.ts b/src/types/paw.d.ts index 0af51d5..33bf0c0 100755 --- a/src/types/paw.d.ts +++ b/src/types/paw.d.ts @@ -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 {} diff --git a/src/utils/dynamic-values.ts b/src/utils/dynamic-values.ts index e56cb68..5be041a 100644 --- a/src/utils/dynamic-values.ts +++ b/src/utils/dynamic-values.ts @@ -65,7 +65,7 @@ export const createFileValues = (): DynamicValue => createDynamicValue(FILE_DYNAMIC_VALUE, { bookmarkData: null }) /** - * @exports transformString + * @exports convertEnvString * @summary * * @param {Object} opts @@ -74,7 +74,7 @@ export const createFileValues = (): DynamicValue => * @param {String} opts.stringInput * @param {Object} opts.requestInput * - * @returns {DynamicValue} class instance + * @returns {string} */ export function convertEnvString( dynamicString: DynamicString,