@kawapress/plugin-openapi 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README-zh-CN.md +75 -0
  3. package/README.md +74 -2
  4. package/bin/kawapress-openapi.mjs +10 -0
  5. package/package.json +67 -3
  6. package/src/cli.ts +35 -0
  7. package/src/commands.ts +28 -0
  8. package/src/components/KawaOpenApiBody.vue +20 -0
  9. package/src/components/KawaOpenApiBodyView.vue +41 -0
  10. package/src/components/KawaOpenApiCard.vue +33 -0
  11. package/src/components/KawaOpenApiEndpoint.vue +186 -0
  12. package/src/components/KawaOpenApiFieldList.vue +35 -0
  13. package/src/components/KawaOpenApiJourney.vue +100 -0
  14. package/src/components/KawaOpenApiMap.vue +50 -0
  15. package/src/components/KawaOpenApiParameters.vue +24 -0
  16. package/src/components/KawaOpenApiPayload.vue +77 -0
  17. package/src/components/KawaOpenApiRef.vue +21 -0
  18. package/src/components/KawaOpenApiResponses.vue +37 -0
  19. package/src/components/KawaOpenApiSamples.vue +39 -0
  20. package/src/components/KawaOpenApiTabs.vue +68 -0
  21. package/src/components/openapi.css +550 -0
  22. package/src/components/validate.ts +138 -0
  23. package/src/data/body.ts +115 -0
  24. package/src/data/components.ts +687 -0
  25. package/src/data/fields.ts +129 -0
  26. package/src/data/payload.ts +214 -0
  27. package/src/data/rules.ts +67 -0
  28. package/src/data/samples.ts +187 -0
  29. package/src/data/text.ts +85 -0
  30. package/src/data/typescript.ts +105 -0
  31. package/src/env.d.ts +8 -0
  32. package/src/index.ts +123 -0
  33. package/src/options.ts +53 -0
  34. package/src/pages.ts +120 -0
  35. package/src/read/arazzo.ts +168 -0
  36. package/src/read/document.ts +91 -0
  37. package/src/read/example.ts +143 -0
  38. package/src/read/model.ts +341 -0
  39. package/src/read/schema.ts +212 -0
  40. package/src/render/labels.ts +283 -0
  41. package/src/render/pages.ts +237 -0
  42. package/src/render/routes.ts +48 -0
  43. package/src/runtime-plugin.ts +12 -0
  44. package/src/service.ts +128 -0
  45. package/src/snapshot/history.ts +188 -0
  46. package/src/snapshot/snapshot.ts +91 -0
  47. package/src/tag.ts +114 -0
  48. package/src/types.ts +207 -0
  49. package/src/vite.ts +169 -0
@@ -0,0 +1,115 @@
1
+ import type { Direction, SchemaTable } from '../read/example'
2
+ import type { OpenApiLabels } from '../render/labels'
3
+ import type { SchemaNode } from '../types'
4
+ import type { PayloadData } from './payload'
5
+ import { flattenFields, typeLabel } from './fields'
6
+ import { buildPayload, payloadMarkdown } from './payload'
7
+ import { constraintTexts, firstLine, renderProse } from './text'
8
+ import { typescriptOf } from './typescript'
9
+
10
+ export interface FieldRow {
11
+ path: string
12
+ depth: number
13
+ type: string
14
+ typeHref?: string
15
+ required: boolean
16
+ constraints: string[]
17
+ descriptionHtml: string
18
+ deprecated: boolean
19
+ since?: string
20
+ }
21
+
22
+ /** One body (request, response or model) in its three views. */
23
+ export interface BodyData {
24
+ contentType: string
25
+ descriptionHtml: string
26
+ generated: boolean
27
+ payload: PayloadData
28
+ fields: FieldRow[]
29
+ typescriptHtml: string
30
+ /** Same content for the Markdown output. */
31
+ markdown: string
32
+ }
33
+
34
+ export interface BodyContext {
35
+ schemas: SchemaTable
36
+ labels: OpenApiLabels
37
+ modelHref: (name: string) => string | undefined
38
+ /** Highlights code with the site's highlighter. */
39
+ highlight: (code: string, lang: string) => Promise<string>
40
+ }
41
+
42
+ export interface BodyInput {
43
+ schema: SchemaNode
44
+ example: unknown
45
+ generated: boolean
46
+ contentType: string
47
+ description: string
48
+ direction: Direction
49
+ /** Name of the TypeScript declaration when the schema is not a named model. */
50
+ typeName: string
51
+ sinceOf?: (path: string) => string | undefined
52
+ }
53
+
54
+ export async function buildBodyData(input: BodyInput, context: BodyContext): Promise<BodyData> {
55
+ const { schemas, labels } = context
56
+ const payload = buildPayload(input.example, input.schema, {
57
+ schemas,
58
+ direction: input.direction,
59
+ labels,
60
+ modelHref: context.modelHref,
61
+ ...(input.sinceOf ? { sinceOf: input.sinceOf } : {}),
62
+ })
63
+ const fields = fieldRows(input.schema, input.direction, context, input.sinceOf)
64
+ const typescript = typescriptOf(input.schema, schemas, input.typeName, input.direction)
65
+ const markdownFields = fields.map((row) => {
66
+ const notes = [row.required ? labels.required : '', ...row.constraints, row.deprecated ? labels.deprecated : '', row.since ? labels.since(row.since) : ''].filter(Boolean)
67
+ const description = row.descriptionHtml ? ` — ${firstLine(plainOf(row.descriptionHtml))}` : ''
68
+ return `${' '.repeat(row.depth)}- \`${row.path}\` \`${row.type}\`${notes.length ? ` (${notes.join(', ')})` : ''}${description}`
69
+ })
70
+ return {
71
+ contentType: input.contentType,
72
+ descriptionHtml: renderProse(input.description),
73
+ generated: input.generated,
74
+ payload,
75
+ fields,
76
+ typescriptHtml: await context.highlight(typescript, 'ts'),
77
+ markdown: [
78
+ input.description,
79
+ `\`${input.contentType}\``,
80
+ payloadMarkdown(payload),
81
+ markdownFields.join('\n'),
82
+ ].filter(Boolean).join('\n\n'),
83
+ }
84
+ }
85
+
86
+ export function fieldRows(
87
+ schema: SchemaNode,
88
+ direction: Direction,
89
+ context: Pick<BodyContext, 'schemas' | 'labels' | 'modelHref'>,
90
+ sinceOf?: (path: string) => string | undefined,
91
+ ): FieldRow[] {
92
+ const { schemas, labels } = context
93
+ return flattenFields(schema, schemas, { direction, expandOnce: true }).map((field) => {
94
+ const node = field.schema
95
+ const ref = node.kind === 'ref' ? node.ref : field.target.kind === 'array' && field.target.items?.kind === 'ref' ? field.target.items.ref : undefined
96
+ const typeHref = ref ? context.modelHref(ref) : undefined
97
+ const since = node.since ?? field.target.since ?? sinceOf?.(field.path)
98
+ return {
99
+ path: field.path,
100
+ depth: field.depth,
101
+ type: typeLabel(node, schemas),
102
+ ...(typeHref ? { typeHref } : {}),
103
+ required: field.required,
104
+ constraints: constraintTexts(node, schemas, labels),
105
+ descriptionHtml: renderProse(node.description || field.target.description),
106
+ deprecated: node.deprecated || field.target.deprecated,
107
+ ...(since ? { since } : {}),
108
+ }
109
+ })
110
+ }
111
+
112
+ /** Text of a small HTML fragment, for Markdown lists. */
113
+ function plainOf(html: string): string {
114
+ return html.replaceAll(/<[^>]+>/g, '').replaceAll('&lt;', '<').replaceAll('&gt;', '>').replaceAll('&quot;', '"').replaceAll('&amp;', '&').trim()
115
+ }