Skip to content

Commit

Permalink
init: refactor param converter
Browse files Browse the repository at this point in the history
  • Loading branch information
pongstr committed May 10, 2021
1 parent 821000e commit 2b313cb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 290 deletions.
82 changes: 35 additions & 47 deletions src/lib/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OpenAPIV3 } from 'openapi-types'
import toJsonSchema from 'to-json-schema'
import qs from 'query-string'
import Paw from 'types/paw'
import { logger, PawURL } from 'utils'
import { PawURL } from 'utils'

type PawToOAS3 = OpenAPIV3.OperationObject & {
path: string
Expand Down Expand Up @@ -31,7 +31,7 @@ function extendToJsonSchema(refSchema: any, reference: any): any {
if (refSchema.type === 'object') {
const props = schema.properties

if (!props) return schema
if (!props) return { ...schema }

Object.keys(props).forEach((i: string) => {
if (props[i].type === 'null' || props[i].type === 'undefined') {
Expand Down Expand Up @@ -206,9 +206,7 @@ export function buildServerObject(
)
}

return [...requests]
.map(mapServers)
.filter(filterDuplicates) as OpenAPIV3.ServerObject[]
return [...requests].map(mapServers).filter(filterDuplicates)
}

/**
Expand Down Expand Up @@ -242,7 +240,7 @@ export function buildRequestBodyObject(
) as OpenAPIV3.SchemaObject,
}

return output as OpenAPIV3.RequestBodyObject
return output
}

return undefined
Expand Down Expand Up @@ -383,18 +381,18 @@ export function buildParameterObjectArray(
headers: RequestParameter,
): OpenAPIV3.ParameterObject[] {
if (Object.keys(headers).length === 0) return []
return Object.keys(headers).map((name) => ({
name,
in: 'header',
schema: {
type:
toJsonSchema(headers[name]).type !== null
? toJsonSchema(headers[name]).type
: 'string',
default: headers[name],
description: '',
},
})) as OpenAPIV3.ParameterObject[]
return Object.keys(headers).map((name) => {
const getType = toJsonSchema(headers[name])
return {
name,
in: 'header',
schema: {
type: getType && getType.type !== 'null' ? getType.type : 'string',
default: headers[name].toString() || '',
description: '',
},
}
}) as OpenAPIV3.ParameterObject[]
}

/**
Expand All @@ -404,35 +402,28 @@ export function buildParameterObjectArray(
*
* @todo - find a way to access request variable type to avoid using conditional checks.
*/
function fromPathParams(request: Paw.Request): OpenAPIV3.ParameterObject[] {
const variables = request.getVariablesNames() || []
function fromPathParams(req: Paw.Request): OpenAPIV3.ParameterObject[] {
const variables = req.getVariablesNames() || []
if (variables.length === 0) return []

const createObject = variables
.map((name: string) => {
const variable = request.getVariableByName(name) as Paw.RequestVariable
const variable = req.getVariableByName(name) as Paw.RequestVariable
const isTruthy = isVariableInString(
request.getUrlBase(true) as DynamicString,
req.getUrlBase(true) as DynamicString,
variable,
)

if (!isTruthy) return null

const currentValue = variable.getCurrentValue()

const getType = toJsonSchema(currentValue)
return {
name,
in: 'path',
required: variable.required,
schema: {
/**
* @todo
* how to access request variable type? all types will fall back to string
*/
type:
toJsonSchema(currentValue).type !== null
? toJsonSchema(currentValue).type
: 'string',
type: getType && getType.type !== 'null' ? getType.type : 'string',
default: currentValue || '',
description: variable.description || '',
},
Expand All @@ -452,19 +443,18 @@ export function buildParameterObjectArray(
function fromQueryParams(queryString: string): OpenAPIV3.ParameterObject[] {
if (queryString.trim() === '') return []
const createQsObject = qs.parse(queryString)
logger.log(toJsonSchema(createQsObject['additionalMetadata']))
return Object.keys(createQsObject).map((name) => ({
name,
in: 'query',
schema: {
type:
toJsonSchema(createQsObject[name]).type !== 'null'
? toJsonSchema(createQsObject[name]).type
: 'string',
default: createQsObject[name] !== 'null' ? createQsObject[name] : '',
description: '',
},
})) as OpenAPIV3.ParameterObject[]
return Object.keys(createQsObject).map((name) => {
const getType = toJsonSchema(createQsObject[name])
return {
name,
in: 'query',
schema: {
type: getType && getType.type !== 'null' ? getType.type : 'string',
default: createQsObject[name]?.toString() || '',
description: '',
},
}
}) as OpenAPIV3.ParameterObject[]
}

return ([] as OpenAPIV3.ParameterObject[]).concat(
Expand Down Expand Up @@ -648,14 +638,12 @@ export function buildSecurityShemeObject(requests: Paw.Request[]): any {
return { ...acc, [curr.label]: { ...curr.value } }
}

const output = [...requests]
return [...requests]
.map(mapRequestSecurityData)
.filter((item: SecuritySchemeMapping) => item !== null)
.filter(filterDuplicates)
.reduce(
mapSecuritySchema,
{} as { [key: string]: OpenAPIV3.SecuritySchemeObject },
)

return output
}
2 changes: 0 additions & 2 deletions src/utils/dynamic-values.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import EnvironmentManager from './environment'
import Paw from 'types/paw'
import logger from './console'

const ENVIRONMENT_DYNAMIC_VALUE =
'com.luckymarmot.EnvironmentVariableDynamicValue'
Expand Down
12 changes: 1 addition & 11 deletions src/utils/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function createGroup(
-1,
)

let objectvalue = accumulator[occurence] as CreateRequestGroupType
let objectvalue = accumulator[occurence]
if (occurence >= 0) {
objectvalue.paths = objectvalue.paths.concat(current.paths)
return accumulator
Expand All @@ -41,13 +41,3 @@ export function createGroup(
accumulator = accumulator.concat([currentObject as never])
return accumulator
}

// request body utility

// parameter utilities

export function paramInHeader(): void {}

export function paramInPath(): void {}

export function paramInCookie(): void {}
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export { default as logger } from './console'
export { default as PawURL } from './paw-url'
export { default as EnvironmentManager } from './environment'
export { default as jsonSchemaParser } from './json-schema-parser'
export * from './dynamic-values'
export * as group from './group'

0 comments on commit 2b313cb

Please sign in to comment.