Skip to content

Commit

Permalink
fix(openapi): support paths common parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed May 7, 2020
1 parent dfc10a6 commit edbc5b2
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
22 changes: 22 additions & 0 deletions packages/openapi2aspida/samples/simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@ info:
title: "simple"
version: "1.0"
paths:
/user/{id}:
parameters:
- in: path
name: id
schema:
type: integer
required: true
description: The user ID
get:
responses:
202:
description: "accept"
patch:
summary: Updates an existing user with the specified ID
responses:
202:
description: "accept"
delete:
summary: Deletes the user with the specified ID
responses:
202:
description: "accept"
/dummy/{id}/simple:
put:
summary: "simple"
Expand Down
16 changes: 16 additions & 0 deletions packages/openapi2aspida/samples/simple/$api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ const api = <T>(client: AspidaClient<T>) => {
(await client.fetch<void>(prefix, `/dummy/${val0}/simple`, 'PUT', option).send()).data
}
})
},
user: {
_id: (val1: number) => ({
get: (option?: { config?: T }) =>
client.fetch<void>(prefix, `/user/${val1}`, 'GET', option).send(),
$get: async (option?: { config?: T }) =>
(await client.fetch<void>(prefix, `/user/${val1}`, 'GET', option).send()).data,
patch: (option?: { config?: T }) =>
client.fetch<void>(prefix, `/user/${val1}`, 'PATCH', option).send(),
$patch: async (option?: { config?: T }) =>
(await client.fetch<void>(prefix, `/user/${val1}`, 'PATCH', option).send()).data,
delete: (option?: { config?: T }) =>
client.fetch<void>(prefix, `/user/${val1}`, 'DELETE', option).send(),
$delete: async (option?: { config?: T }) =>
(await client.fetch<void>(prefix, `/user/${val1}`, 'DELETE', option).send()).data
})
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions packages/openapi2aspida/samples/simple/user/_id@number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */
export type Methods = {
get: {
}

patch: {
}

delete: {
}
}

export default {
get: () => ({ status: 200 }),
patch: () => ({ status: 200 }),
delete: () => ({ status: 200 })
}
16 changes: 13 additions & 3 deletions packages/openapi2aspida/src/buildV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,24 @@ export default (
.replace(/\/$/, '')
.split('/')
.slice(1)
.map(p => getDirName(p /* , openapi.paths[path] */)),
.map(p =>
getDirName(
p,
openapi.paths[path].parameters?.map(p =>
isRefObject(p) ? resolveParamsRef(openapi, p.$ref) : p
)
)
),
...(isParent ? ['index'] : [])
]
const methods = Object.keys(openapi.paths[path])
.filter((method): method is typeof methodNames[number] =>
methodNames.includes(method as typeof methodNames[number])
)
.map<Prop | null>(method => {
const target = openapi.paths[path][method as typeof methodNames[number]]
const target = openapi.paths[path][method]!

if (!target || target.deprecated) return null
if (target.deprecated) return null

const params: Prop[] = []

Expand Down
13 changes: 9 additions & 4 deletions packages/openapi2aspida/src/builderUtils/getDirName.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export default (text: string /* , methods: OpenAPIV3.PathItemObject */) => {
import { OpenAPIV3 } from 'openapi-types'
import { getPropertyName, schema2value } from './converters'

export default (text: string, params?: OpenAPIV3.ParameterObject[]) => {
if (text === '*') return '_any'
if (!/^{/.test(text)) return text

const valName = text.slice(1, -1)
// const method = methodNames.find(name => methods[name]?.parameters?.find((param) => !isRefObject(param) && param.name === valName && param.in === 'path'))
// return `_${valName}${method ? `@${type2value(methods[method]!.parameters!.filter((param) => !isRefObject(param) && param.name === valName && param.in === 'path')[0], '')}` : ''}`
return `_${valName}`
const schemaVal = schema2value(params?.find(p => p.in === 'path' && p.name === valName)?.schema)

return `_${getPropertyName(valName)}${
schemaVal && typeof schemaVal.value === 'string' ? `@${schemaVal.value}` : ''
}`
}

0 comments on commit edbc5b2

Please sign in to comment.