apiDocHelper.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import SwaggerParser from 'swagger-parser'
  2. export const parseApiDocFile = async (remoteUrl: string) => {
  3. let apiDoc
  4. try {
  5. // @ts-ignore
  6. apiDoc = await SwaggerParser.parse(remoteUrl)
  7. } catch (e) {
  8. console.error('[parse api doc failed]', e)
  9. }
  10. return apiDoc
  11. }
  12. export const isJsonString = (input: string) => {
  13. return input ? /^\{\s*".+}$/.test(input.trim()) : false
  14. }
  15. export const formatJsonString = (input: string) => {
  16. let formatted: string
  17. try {
  18. formatted = JSON.stringify(JSON.parse(input), null, 2)
  19. } catch (e) {
  20. formatted = input.trim()
  21. }
  22. return formatted
  23. }
  24. const convertKeyValuePropertiesToArray = (obj: any) => {
  25. if (!obj || !Object.keys(obj).length) {
  26. return null
  27. }
  28. const params: any[] = []
  29. if (obj) {
  30. for (const key in obj) {
  31. const row = obj[key]
  32. if (row.properties || row.items && row.items.properties) {
  33. row.children = convertKeyValuePropertiesToArray(row.properties || row.items.properties)
  34. }
  35. params.push({
  36. name: key,
  37. ...row
  38. })
  39. }
  40. }
  41. return params
  42. }
  43. export const formatApiDocToRoutes = (data: any, opts?: {
  44. injectAccessKey?: boolean
  45. }) => {
  46. const {
  47. host = '',
  48. basePath = '',
  49. schemes,
  50. paths
  51. } = data
  52. const {
  53. injectAccessKey = true
  54. } = opts || {}
  55. const routes: any[] = []
  56. if (paths) {
  57. let index = 0
  58. for (const path in paths) {
  59. for (const method in paths[path]) {
  60. const item = paths[path][method]
  61. const url = `${schemes[schemes.length -1]}://${host}${basePath}${path}`.replace(/\/+$/, '')
  62. const paramsMap: any = {}
  63. let successResponse: any
  64. if (injectAccessKey) {
  65. paramsMap.query = [{
  66. name: 'key',
  67. in: 'query',
  68. description: '请求 AccessKey, 请在控制台中查看',
  69. required: true,
  70. type: 'string',
  71. _certif: true
  72. }]
  73. }
  74. if (item.parameters && item.parameters.length) {
  75. for (const param of item.parameters) {
  76. // header, path, query, body
  77. const _in = param.in
  78. if (!_in) {
  79. continue
  80. }
  81. if (!paramsMap[_in]) {
  82. paramsMap[_in] = []
  83. }
  84. paramsMap[_in].push(param)
  85. }
  86. }
  87. if (item.responses) {
  88. for (const code in item.responses) {
  89. const schema = item.responses[code].schema
  90. if (/^20/.test(code) && schema) {
  91. successResponse = {
  92. schema,
  93. params: convertKeyValuePropertiesToArray(schema.properties),
  94. example: schema.example
  95. }
  96. break
  97. }
  98. }
  99. }
  100. routes.push({
  101. index,
  102. url,
  103. method,
  104. paramsMap,
  105. successResponse,
  106. ...item
  107. })
  108. index++
  109. }
  110. }
  111. }
  112. return routes
  113. }