@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.
- package/LICENSE +21 -0
- package/README-zh-CN.md +75 -0
- package/README.md +74 -2
- package/bin/kawapress-openapi.mjs +10 -0
- package/package.json +67 -3
- package/src/cli.ts +35 -0
- package/src/commands.ts +28 -0
- package/src/components/KawaOpenApiBody.vue +20 -0
- package/src/components/KawaOpenApiBodyView.vue +41 -0
- package/src/components/KawaOpenApiCard.vue +33 -0
- package/src/components/KawaOpenApiEndpoint.vue +186 -0
- package/src/components/KawaOpenApiFieldList.vue +35 -0
- package/src/components/KawaOpenApiJourney.vue +100 -0
- package/src/components/KawaOpenApiMap.vue +50 -0
- package/src/components/KawaOpenApiParameters.vue +24 -0
- package/src/components/KawaOpenApiPayload.vue +77 -0
- package/src/components/KawaOpenApiRef.vue +21 -0
- package/src/components/KawaOpenApiResponses.vue +37 -0
- package/src/components/KawaOpenApiSamples.vue +39 -0
- package/src/components/KawaOpenApiTabs.vue +68 -0
- package/src/components/openapi.css +550 -0
- package/src/components/validate.ts +138 -0
- package/src/data/body.ts +115 -0
- package/src/data/components.ts +687 -0
- package/src/data/fields.ts +129 -0
- package/src/data/payload.ts +214 -0
- package/src/data/rules.ts +67 -0
- package/src/data/samples.ts +187 -0
- package/src/data/text.ts +85 -0
- package/src/data/typescript.ts +105 -0
- package/src/env.d.ts +8 -0
- package/src/index.ts +123 -0
- package/src/options.ts +53 -0
- package/src/pages.ts +120 -0
- package/src/read/arazzo.ts +168 -0
- package/src/read/document.ts +91 -0
- package/src/read/example.ts +143 -0
- package/src/read/model.ts +341 -0
- package/src/read/schema.ts +212 -0
- package/src/render/labels.ts +283 -0
- package/src/render/pages.ts +237 -0
- package/src/render/routes.ts +48 -0
- package/src/runtime-plugin.ts +12 -0
- package/src/service.ts +128 -0
- package/src/snapshot/history.ts +188 -0
- package/src/snapshot/snapshot.ts +91 -0
- package/src/tag.ts +114 -0
- package/src/types.ts +207 -0
- package/src/vite.ts +169 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import type { ChangeKind, ChangeReason } from '../snapshot/history'
|
|
2
|
+
import type { OpenApiAction, OpenApiParameter } from '../types'
|
|
3
|
+
|
|
4
|
+
/** Fixed texts of the pages and components, in the language of a locale. */
|
|
5
|
+
export interface OpenApiLabels {
|
|
6
|
+
overview: string
|
|
7
|
+
resources: string
|
|
8
|
+
journeys: string
|
|
9
|
+
models: string
|
|
10
|
+
changes: string
|
|
11
|
+
webhooks: string
|
|
12
|
+
baseUrl: string
|
|
13
|
+
counts: (operations: number, models: number, journeys: number) => string
|
|
14
|
+
actions: Record<OpenApiAction, string>
|
|
15
|
+
parameters: string
|
|
16
|
+
parameterGroups: Record<OpenApiParameter['in'], string>
|
|
17
|
+
requestBody: string
|
|
18
|
+
responses: string
|
|
19
|
+
samples: string
|
|
20
|
+
required: string
|
|
21
|
+
deprecated: string
|
|
22
|
+
deprecatedOperation: string
|
|
23
|
+
since: (version: string) => string
|
|
24
|
+
auth: string
|
|
25
|
+
noAuth: string
|
|
26
|
+
copyCurl: string
|
|
27
|
+
copied: string
|
|
28
|
+
copyFailed: string
|
|
29
|
+
tryIt: string
|
|
30
|
+
send: string
|
|
31
|
+
sending: string
|
|
32
|
+
mock: string
|
|
33
|
+
server: string
|
|
34
|
+
token: string
|
|
35
|
+
mockNote: string
|
|
36
|
+
serverNote: (url: string) => string
|
|
37
|
+
notSent: string
|
|
38
|
+
invalidJson: string
|
|
39
|
+
validationFailed: string
|
|
40
|
+
views: { payload: string, table: string, typescript: string }
|
|
41
|
+
generated: string
|
|
42
|
+
moreItems: (count: number) => string
|
|
43
|
+
columns: { field: string, type: string, constraints: string, description: string }
|
|
44
|
+
defaultValue: string
|
|
45
|
+
example: string
|
|
46
|
+
noBody: string
|
|
47
|
+
usedBy: string
|
|
48
|
+
notUsed: string
|
|
49
|
+
receive: string
|
|
50
|
+
respond: string
|
|
51
|
+
sendLabel: string
|
|
52
|
+
getLabel: string
|
|
53
|
+
fromStep: (step: number, output: string) => string
|
|
54
|
+
fromInput: (name: string) => string
|
|
55
|
+
runJourney: string
|
|
56
|
+
runAgain: string
|
|
57
|
+
pending: string
|
|
58
|
+
running: string
|
|
59
|
+
done: string
|
|
60
|
+
inputs: string
|
|
61
|
+
source: string
|
|
62
|
+
steps: string
|
|
63
|
+
viewOperation: string
|
|
64
|
+
details: string
|
|
65
|
+
changeKinds: Record<ChangeKind, string>
|
|
66
|
+
changeReasons: Record<ChangeReason, string>
|
|
67
|
+
noChanges: string
|
|
68
|
+
compare: (previous: string) => string
|
|
69
|
+
items: (count: number) => string
|
|
70
|
+
chars: (count: number) => string
|
|
71
|
+
/** Messages of the mock validation; `{n}` is replaced with the limit or type. */
|
|
72
|
+
validation: ValidationMessages
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface ValidationMessages {
|
|
76
|
+
required: string
|
|
77
|
+
type: string
|
|
78
|
+
enum: string
|
|
79
|
+
min: string
|
|
80
|
+
max: string
|
|
81
|
+
minLength: string
|
|
82
|
+
maxLength: string
|
|
83
|
+
minItems: string
|
|
84
|
+
maxItems: string
|
|
85
|
+
pattern: string
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const zh: OpenApiLabels = {
|
|
89
|
+
overview: 'API 总览',
|
|
90
|
+
resources: '资源',
|
|
91
|
+
journeys: '旅程',
|
|
92
|
+
models: '模型',
|
|
93
|
+
changes: '版本变更',
|
|
94
|
+
webhooks: 'Webhook',
|
|
95
|
+
baseUrl: '基础地址',
|
|
96
|
+
counts: (operations, models, journeys) => `${operations} 个接口 · ${models} 个模型 · ${journeys} 条旅程`,
|
|
97
|
+
actions: { list: '列表', create: '创建', read: '读取', update: '更新', delete: '删除', other: '其他动作' },
|
|
98
|
+
parameters: '参数',
|
|
99
|
+
parameterGroups: { path: '路径参数', query: '查询参数', header: '请求头', cookie: 'Cookie' },
|
|
100
|
+
requestBody: '请求体',
|
|
101
|
+
responses: '响应',
|
|
102
|
+
samples: '代码示例',
|
|
103
|
+
required: '必填',
|
|
104
|
+
deprecated: '已弃用',
|
|
105
|
+
deprecatedOperation: '这个接口已弃用。',
|
|
106
|
+
since: version => `${version} 起`,
|
|
107
|
+
auth: '鉴权',
|
|
108
|
+
noAuth: '不需要鉴权',
|
|
109
|
+
copyCurl: '复制 curl',
|
|
110
|
+
copied: '已复制',
|
|
111
|
+
copyFailed: '复制失败',
|
|
112
|
+
tryIt: '试一试',
|
|
113
|
+
send: '发送',
|
|
114
|
+
sending: '发送中…',
|
|
115
|
+
mock: 'Mock',
|
|
116
|
+
server: '服务器',
|
|
117
|
+
token: '令牌',
|
|
118
|
+
mockNote: '在浏览器里按 schema 校验请求并返回示例响应,不发送任何网络请求。',
|
|
119
|
+
serverNote: url => `请求会发往 ${url},令牌只保存在当前页面里。`,
|
|
120
|
+
notSent: '尚未发送',
|
|
121
|
+
invalidJson: '请求体不是合法的 JSON',
|
|
122
|
+
validationFailed: '请求没有通过 schema 校验',
|
|
123
|
+
views: { payload: '报文', table: '字段表', typescript: 'TypeScript' },
|
|
124
|
+
generated: '示例由 schema 生成',
|
|
125
|
+
moreItems: count => `… 还有 ${count} 项`,
|
|
126
|
+
columns: { field: '字段', type: '类型', constraints: '约束', description: '说明' },
|
|
127
|
+
defaultValue: '默认值',
|
|
128
|
+
example: '示例',
|
|
129
|
+
noBody: '没有响应体。',
|
|
130
|
+
usedBy: '用到它的接口',
|
|
131
|
+
notUsed: '没有接口直接用到它。',
|
|
132
|
+
receive: '你会收到',
|
|
133
|
+
respond: '你需要返回',
|
|
134
|
+
sendLabel: '发送',
|
|
135
|
+
getLabel: '得到',
|
|
136
|
+
fromStep: (step, output) => `← 第 ${step} 步的 ${output}`,
|
|
137
|
+
fromInput: name => `← 输入 ${name}`,
|
|
138
|
+
runJourney: '在 Mock 中跑通',
|
|
139
|
+
runAgain: '再跑一次',
|
|
140
|
+
pending: '未运行',
|
|
141
|
+
running: '运行中…',
|
|
142
|
+
done: '完成',
|
|
143
|
+
inputs: '输入',
|
|
144
|
+
source: '来源',
|
|
145
|
+
steps: '步骤',
|
|
146
|
+
viewOperation: '接口详情',
|
|
147
|
+
details: '完整说明',
|
|
148
|
+
changeKinds: { breaking: '破坏性', added: '新增', deprecated: '弃用', changed: '兼容改动' },
|
|
149
|
+
changeReasons: {
|
|
150
|
+
'operation-added': '新接口',
|
|
151
|
+
'operation-removed': '删除了接口',
|
|
152
|
+
'operation-deprecated': '接口弃用',
|
|
153
|
+
'removed': '删除',
|
|
154
|
+
'type-changed': '类型改变',
|
|
155
|
+
'required-added': '新增必填项',
|
|
156
|
+
'became-required': '变为必填',
|
|
157
|
+
'added': '新增',
|
|
158
|
+
'status-added': '新增响应状态',
|
|
159
|
+
'status-removed': '删除了响应状态',
|
|
160
|
+
'deprecated': '弃用',
|
|
161
|
+
'became-optional': '变为可选',
|
|
162
|
+
'undeprecated': '取消弃用',
|
|
163
|
+
},
|
|
164
|
+
noChanges: '没有接口变化。',
|
|
165
|
+
compare: previous => `对比 ${previous}`,
|
|
166
|
+
items: count => `${count} 项`,
|
|
167
|
+
chars: count => `${count} 字符`,
|
|
168
|
+
validation: {
|
|
169
|
+
required: '缺少必填项',
|
|
170
|
+
type: '应为 {n}',
|
|
171
|
+
enum: '应为 {n} 之一',
|
|
172
|
+
min: '应 ≥ {n}',
|
|
173
|
+
max: '应 ≤ {n}',
|
|
174
|
+
minLength: '至少 {n} 个字符',
|
|
175
|
+
maxLength: '最多 {n} 个字符',
|
|
176
|
+
minItems: '至少 {n} 项',
|
|
177
|
+
maxItems: '最多 {n} 项',
|
|
178
|
+
pattern: '应匹配 {n}',
|
|
179
|
+
},
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const en: OpenApiLabels = {
|
|
183
|
+
overview: 'API overview',
|
|
184
|
+
resources: 'Resources',
|
|
185
|
+
journeys: 'Journeys',
|
|
186
|
+
models: 'Models',
|
|
187
|
+
changes: 'API changes',
|
|
188
|
+
webhooks: 'Webhooks',
|
|
189
|
+
baseUrl: 'Base URL',
|
|
190
|
+
counts: (operations, models, journeys) => `${operations} ${plural(operations, 'operation')} · ${models} ${plural(models, 'model')} · ${journeys} ${plural(journeys, 'journey')}`,
|
|
191
|
+
actions: { list: 'List', create: 'Create', read: 'Read', update: 'Update', delete: 'Delete', other: 'Other' },
|
|
192
|
+
parameters: 'Parameters',
|
|
193
|
+
parameterGroups: { path: 'Path', query: 'Query', header: 'Headers', cookie: 'Cookies' },
|
|
194
|
+
requestBody: 'Request body',
|
|
195
|
+
responses: 'Responses',
|
|
196
|
+
samples: 'Code samples',
|
|
197
|
+
required: 'required',
|
|
198
|
+
deprecated: 'deprecated',
|
|
199
|
+
deprecatedOperation: 'This operation is deprecated.',
|
|
200
|
+
since: version => `Since ${version}`,
|
|
201
|
+
auth: 'Auth',
|
|
202
|
+
noAuth: 'No authentication',
|
|
203
|
+
copyCurl: 'Copy curl',
|
|
204
|
+
copied: 'Copied',
|
|
205
|
+
copyFailed: 'Copy failed',
|
|
206
|
+
tryIt: 'Try it',
|
|
207
|
+
send: 'Send',
|
|
208
|
+
sending: 'Sending…',
|
|
209
|
+
mock: 'Mock',
|
|
210
|
+
server: 'Server',
|
|
211
|
+
token: 'Token',
|
|
212
|
+
mockNote: 'Checks the request against the schema in the browser and answers with the example response. Nothing is sent over the network.',
|
|
213
|
+
serverNote: url => `Requests go to ${url}. The token stays in this page only.`,
|
|
214
|
+
notSent: 'Not sent yet',
|
|
215
|
+
invalidJson: 'The request body is not valid JSON',
|
|
216
|
+
validationFailed: 'The request does not match the schema',
|
|
217
|
+
views: { payload: 'Payload', table: 'Fields', typescript: 'TypeScript' },
|
|
218
|
+
generated: 'Example generated from the schema',
|
|
219
|
+
moreItems: count => `… ${count} more`,
|
|
220
|
+
columns: { field: 'Field', type: 'Type', constraints: 'Constraints', description: 'Description' },
|
|
221
|
+
defaultValue: 'Default',
|
|
222
|
+
example: 'Example',
|
|
223
|
+
noBody: 'No response body.',
|
|
224
|
+
usedBy: 'Used by',
|
|
225
|
+
notUsed: 'No operation uses it directly.',
|
|
226
|
+
receive: 'You receive',
|
|
227
|
+
respond: 'You respond',
|
|
228
|
+
sendLabel: 'Send',
|
|
229
|
+
getLabel: 'Get',
|
|
230
|
+
fromStep: (step, output) => `← ${output} from step ${step}`,
|
|
231
|
+
fromInput: name => `← input ${name}`,
|
|
232
|
+
runJourney: 'Run with mocks',
|
|
233
|
+
runAgain: 'Run again',
|
|
234
|
+
pending: 'Not run',
|
|
235
|
+
running: 'Running…',
|
|
236
|
+
done: 'Done',
|
|
237
|
+
inputs: 'Inputs',
|
|
238
|
+
source: 'Source',
|
|
239
|
+
steps: 'Steps',
|
|
240
|
+
viewOperation: 'Operation',
|
|
241
|
+
details: 'Details',
|
|
242
|
+
changeKinds: { breaking: 'Breaking', added: 'Added', deprecated: 'Deprecated', changed: 'Compatible changes' },
|
|
243
|
+
changeReasons: {
|
|
244
|
+
'operation-added': 'New operation',
|
|
245
|
+
'operation-removed': 'Operation removed',
|
|
246
|
+
'operation-deprecated': 'Operation deprecated',
|
|
247
|
+
'removed': 'Removed',
|
|
248
|
+
'type-changed': 'Type changed',
|
|
249
|
+
'required-added': 'New required input',
|
|
250
|
+
'became-required': 'Now required',
|
|
251
|
+
'added': 'Added',
|
|
252
|
+
'status-added': 'New response status',
|
|
253
|
+
'status-removed': 'Response status removed',
|
|
254
|
+
'deprecated': 'Deprecated',
|
|
255
|
+
'became-optional': 'Now optional',
|
|
256
|
+
'undeprecated': 'No longer deprecated',
|
|
257
|
+
},
|
|
258
|
+
noChanges: 'No operation changed.',
|
|
259
|
+
compare: previous => `Compared with ${previous}`,
|
|
260
|
+
items: count => `${count} ${plural(count, 'item')}`,
|
|
261
|
+
chars: count => `${count} ${plural(count, 'char')}`,
|
|
262
|
+
validation: {
|
|
263
|
+
required: 'is required',
|
|
264
|
+
type: 'should be {n}',
|
|
265
|
+
enum: 'should be one of {n}',
|
|
266
|
+
min: 'should be ≥ {n}',
|
|
267
|
+
max: 'should be ≤ {n}',
|
|
268
|
+
minLength: 'needs at least {n} characters',
|
|
269
|
+
maxLength: 'allows at most {n} characters',
|
|
270
|
+
minItems: 'needs at least {n} items',
|
|
271
|
+
maxItems: 'allows at most {n} items',
|
|
272
|
+
pattern: 'should match {n}',
|
|
273
|
+
},
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function plural(count: number, word: string): string {
|
|
277
|
+
return count === 1 ? word : `${word}s`
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Chinese for `zh*`, English otherwise. */
|
|
281
|
+
export function labelsForLanguage(lang: string | undefined): OpenApiLabels {
|
|
282
|
+
return lang?.toLowerCase().startsWith('zh') ? zh : en
|
|
283
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import type { PluginPageContent } from 'kawapress'
|
|
2
|
+
import type { LoadedApi } from '../service'
|
|
3
|
+
import type { ApiChange, VersionChanges } from '../snapshot/history'
|
|
4
|
+
import type { OpenApiModel, OpenApiOperation, OpenApiParameter, OpenApiResource, OpenApiSchemaEntry, OpenApiWorkflow, SchemaNode } from '../types'
|
|
5
|
+
import type { OpenApiLabels } from './labels'
|
|
6
|
+
import type { OpenApiLinks } from './routes'
|
|
7
|
+
import { firstLine } from '../data/text'
|
|
8
|
+
import { versionChanges } from '../snapshot/history'
|
|
9
|
+
|
|
10
|
+
/** Tag of the plugin's own components in generated pages. */
|
|
11
|
+
export const INTERNAL_TAG = 'KawaOpenApi'
|
|
12
|
+
|
|
13
|
+
export interface PageContext {
|
|
14
|
+
labels: OpenApiLabels
|
|
15
|
+
/** Site-relative links of the locale. */
|
|
16
|
+
links: OpenApiLinks
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function tag(kind: string, attributes: Record<string, string> = {}): string {
|
|
20
|
+
const rest = Object.entries(attributes).map(([name, value]) => ` ${name}="${value.replaceAll('"', '"')}"`).join('')
|
|
21
|
+
return `<${INTERNAL_TAG} kind="${kind}"${rest} />`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function methodOf(operation: OpenApiOperation): string {
|
|
25
|
+
return operation.webhook ? 'HOOK' : operation.method.toUpperCase()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The resource map: what the API is, its resources by action, journeys and models. */
|
|
29
|
+
export function renderOverviewPage(api: LoadedApi, { labels, links }: PageContext): PluginPageContent {
|
|
30
|
+
const { model } = api
|
|
31
|
+
const sections = [`# ${model.info.title}`]
|
|
32
|
+
if (model.info.summary) {
|
|
33
|
+
sections.push(prose(model.info.summary))
|
|
34
|
+
}
|
|
35
|
+
if (model.info.description) {
|
|
36
|
+
sections.push(prose(model.info.description))
|
|
37
|
+
}
|
|
38
|
+
const facts = [
|
|
39
|
+
model.servers[0] ? `${labels.baseUrl} \`${model.servers[0].url}\`` : '',
|
|
40
|
+
`OpenAPI ${model.openapi}${model.info.version ? ` · v${model.info.version}` : ''}`,
|
|
41
|
+
labels.counts(model.operations.length, model.schemas.length, model.workflows.length),
|
|
42
|
+
].filter(Boolean)
|
|
43
|
+
sections.push(facts.join(' · '))
|
|
44
|
+
sections.push(`## ${labels.resources}`, tag('map'))
|
|
45
|
+
if (model.workflows.length) {
|
|
46
|
+
sections.push(`## ${labels.journeys}`, model.workflows.map(workflow => (
|
|
47
|
+
`- [${workflow.summary || workflow.id}](${links.journey(workflow.id)})${workflow.description ? ` — ${inline(firstLine(workflow.description))}` : ''}`
|
|
48
|
+
)).join('\n'))
|
|
49
|
+
}
|
|
50
|
+
if (model.schemas.length) {
|
|
51
|
+
sections.push(`## ${labels.models}`, model.schemas.map(entry => (
|
|
52
|
+
`- [\`${entry.name}\`](${links.model(entry.name)})${entry.schema.description ? ` — ${inline(firstLine(entry.schema.description))}` : ''}`
|
|
53
|
+
)).join('\n'))
|
|
54
|
+
}
|
|
55
|
+
if (api.snapshots.length > 1) {
|
|
56
|
+
sections.push(`[${labels.changes}](${links.changes})`)
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
markdown: `${sections.join('\n\n')}\n`,
|
|
60
|
+
frontmatter: {
|
|
61
|
+
title: model.info.title,
|
|
62
|
+
...(model.info.summary || model.info.description ? { description: plain(firstLine(model.info.summary || model.info.description)) } : {}),
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** One resource: its description, then a line per operation. */
|
|
68
|
+
export function renderResourcePage(resource: OpenApiResource, model: OpenApiModel, { labels, links }: PageContext): PluginPageContent {
|
|
69
|
+
const sections = [`# ${resource.name}`]
|
|
70
|
+
if (resource.description) {
|
|
71
|
+
sections.push(prose(resource.description))
|
|
72
|
+
}
|
|
73
|
+
sections.push(model.operations.filter(operation => operation.resource === resource.name).map((operation) => {
|
|
74
|
+
const notes = [operation.deprecated ? `**${labels.deprecated}**` : '', operation.since ? labels.since(operation.since) : ''].filter(Boolean)
|
|
75
|
+
return `- \`${methodOf(operation)} ${operation.path}\` [${operation.summary || operation.id}](${links.operation(operation.id)})${operation.description ? ` — ${inline(firstLine(operation.description))}` : ''}${notes.length ? ` ${notes.join(' · ')}` : ''}`
|
|
76
|
+
}).join('\n'))
|
|
77
|
+
return {
|
|
78
|
+
markdown: `${sections.join('\n\n')}\n`,
|
|
79
|
+
frontmatter: { title: resource.name, ...(resource.description ? { description: plain(firstLine(resource.description)) } : {}) },
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** One operation: endpoint bar, description, then parameters, body, responses and samples. */
|
|
84
|
+
export function renderOperationPage(operation: OpenApiOperation, { labels }: PageContext): PluginPageContent {
|
|
85
|
+
const op = { op: operation.id }
|
|
86
|
+
const title = operation.summary || operation.id
|
|
87
|
+
const sections = [`# ${title}`, tag('endpoint', op)]
|
|
88
|
+
if (operation.description) {
|
|
89
|
+
sections.push(prose(operation.description))
|
|
90
|
+
}
|
|
91
|
+
if (operation.parameters.length) {
|
|
92
|
+
sections.push(`## ${labels.parameters}`, tag('parameters', op))
|
|
93
|
+
}
|
|
94
|
+
if (operation.requestBody) {
|
|
95
|
+
sections.push(`## ${operation.webhook ? labels.receive : labels.requestBody}`, tag('request', op))
|
|
96
|
+
}
|
|
97
|
+
if (operation.responses.length) {
|
|
98
|
+
sections.push(`## ${operation.webhook ? labels.respond : labels.responses}`, tag('responses', op))
|
|
99
|
+
}
|
|
100
|
+
if (!operation.webhook) {
|
|
101
|
+
sections.push(`## ${labels.samples}`, tag('samples', op))
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
markdown: `${sections.join('\n\n')}\n`,
|
|
105
|
+
frontmatter: {
|
|
106
|
+
title,
|
|
107
|
+
...(operation.description ? { description: plain(firstLine(operation.description)) } : {}),
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** One model: its description, the three views, and the operations that use it. */
|
|
113
|
+
export function renderModelPage(entry: OpenApiSchemaEntry, model: OpenApiModel, { labels, links }: PageContext): PluginPageContent {
|
|
114
|
+
const sections = [`# ${entry.name}`]
|
|
115
|
+
if (entry.schema.description) {
|
|
116
|
+
sections.push(prose(entry.schema.description))
|
|
117
|
+
}
|
|
118
|
+
sections.push(`<OpenApiSchema name="${entry.name}" />`)
|
|
119
|
+
const users = model.operations.filter(operation => usesModel(operation, entry.name))
|
|
120
|
+
sections.push(`## ${labels.usedBy}`, users.length
|
|
121
|
+
? users.map(operation => `- \`${methodOf(operation)} ${operation.path}\` [${operation.summary || operation.id}](${links.operation(operation.id)})`).join('\n')
|
|
122
|
+
: labels.notUsed)
|
|
123
|
+
return {
|
|
124
|
+
markdown: `${sections.join('\n\n')}\n`,
|
|
125
|
+
frontmatter: { title: entry.name, ...(entry.schema.description ? { description: plain(firstLine(entry.schema.description)) } : {}) },
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** One journey: what it achieves, its inputs, and the steps component. */
|
|
130
|
+
export function renderJourneyPage(workflow: OpenApiWorkflow, root: string, { labels }: PageContext): PluginPageContent {
|
|
131
|
+
const title = workflow.summary || workflow.id
|
|
132
|
+
const sections = [`# ${title}`]
|
|
133
|
+
if (workflow.description) {
|
|
134
|
+
sections.push(prose(workflow.description))
|
|
135
|
+
}
|
|
136
|
+
if (workflow.inputs.length) {
|
|
137
|
+
sections.push(`**${labels.inputs}**`, workflow.inputs.map(input => `- \`${input.name}\` \`${input.type}\`${input.description ? ` — ${inline(firstLine(input.description))}` : ''}`).join('\n'))
|
|
138
|
+
}
|
|
139
|
+
sections.push(`## ${labels.steps}`, tag('journey', { workflow: workflow.id }))
|
|
140
|
+
const file = workflow.file.startsWith(`${root}/`) ? workflow.file.slice(root.length + 1) : workflow.file.split('/').pop()!
|
|
141
|
+
sections.push(`${labels.source}: \`${file}\` · \`${workflow.id}\``)
|
|
142
|
+
return {
|
|
143
|
+
markdown: `${sections.join('\n\n')}\n`,
|
|
144
|
+
frontmatter: { title, ...(workflow.description ? { description: plain(firstLine(workflow.description)) } : {}) },
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Changes between neighbouring snapshots, newest version first. */
|
|
149
|
+
export function renderChangesPage(api: LoadedApi, { labels, links }: PageContext): PluginPageContent {
|
|
150
|
+
const keys = new Map(api.model.operations.map(operation => [operation.webhook ? `WEBHOOK ${operation.path}` : `${operation.method.toUpperCase()} ${operation.path}`, operation]))
|
|
151
|
+
const sections = [`# ${labels.changes}`]
|
|
152
|
+
for (const version of versionChanges(api.snapshots)) {
|
|
153
|
+
sections.push(`## ${version.version}`, labels.compare(version.previous))
|
|
154
|
+
sections.push(version.changes.length ? changeGroups(version, keys, labels, links) : labels.noChanges)
|
|
155
|
+
}
|
|
156
|
+
return { markdown: `${sections.join('\n\n')}\n`, frontmatter: { title: labels.changes } }
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function changeGroups(version: VersionChanges, keys: Map<string, OpenApiOperation>, labels: OpenApiLabels, links: OpenApiLinks): string {
|
|
160
|
+
const groups: string[] = []
|
|
161
|
+
for (const kind of ['breaking', 'added', 'deprecated', 'changed'] as const) {
|
|
162
|
+
const changes = version.changes.filter(change => change.kind === kind)
|
|
163
|
+
if (changes.length) {
|
|
164
|
+
groups.push(`### ${labels.changeKinds[kind]}`, changes.map(change => changeLine(change, keys, labels, links)).join('\n'))
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return groups.join('\n\n')
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function changeLine(change: ApiChange, keys: Map<string, OpenApiOperation>, labels: OpenApiLabels, links: OpenApiLinks): string {
|
|
171
|
+
const operation = keys.get(change.operation)
|
|
172
|
+
const name = operation && change.reason !== 'operation-removed'
|
|
173
|
+
? `[\`${change.operation}\`](${links.operation(operation.id)})`
|
|
174
|
+
: `\`${change.operation}\``
|
|
175
|
+
const subject = change.subject ? ` ${subjectText(change.subject, labels)}` : ''
|
|
176
|
+
const values = change.before && change.after
|
|
177
|
+
? `: \`${change.before}\` → \`${change.after}\``
|
|
178
|
+
: change.after ? `: \`${change.after}\`` : change.before ? `: \`${change.before}\`` : ''
|
|
179
|
+
return `- ${name}${subject} — ${labels.changeReasons[change.reason]}${values}`
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** `request items[].quantity` → `Request body · \`items[].quantity\``. */
|
|
183
|
+
function subjectText(subject: string, labels: OpenApiLabels): string {
|
|
184
|
+
const parameter = /^param (path|query|header|cookie) (.+)$/.exec(subject)
|
|
185
|
+
if (parameter) {
|
|
186
|
+
return `${labels.parameterGroups[parameter[1] as OpenApiParameter['in']]} · \`${parameter[2]}\``
|
|
187
|
+
}
|
|
188
|
+
const request = /^request (.+)$/.exec(subject)
|
|
189
|
+
if (request) {
|
|
190
|
+
return request[1] === 'body' ? labels.requestBody : `${labels.requestBody} · \`${request[1]}\``
|
|
191
|
+
}
|
|
192
|
+
const response = /^response (\S+)(?: (.+))?$/.exec(subject)
|
|
193
|
+
if (response) {
|
|
194
|
+
return `${labels.responses} ${response[1]}${response[2] && response[2] !== 'body' ? ` · \`${response[2]}\`` : ''}`
|
|
195
|
+
}
|
|
196
|
+
return `\`${subject}\``
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Whether an operation names the model directly in its parameters or bodies (not through another model). */
|
|
200
|
+
export function usesModel(operation: OpenApiOperation, name: string): boolean {
|
|
201
|
+
const nodes: (SchemaNode | undefined)[] = [
|
|
202
|
+
...operation.parameters.map(parameter => parameter.schema),
|
|
203
|
+
operation.requestBody?.schema,
|
|
204
|
+
...operation.responses.map(response => response.body?.schema),
|
|
205
|
+
]
|
|
206
|
+
return nodes.some(node => node && refersTo(node, name))
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function refersTo(node: SchemaNode, name: string): boolean {
|
|
210
|
+
if (node.kind === 'ref') {
|
|
211
|
+
return node.ref === name
|
|
212
|
+
}
|
|
213
|
+
return Boolean(
|
|
214
|
+
node.properties?.some(property => refersTo(property.schema, name))
|
|
215
|
+
|| (node.items && refersTo(node.items, name))
|
|
216
|
+
|| node.variants?.some(variant => refersTo(variant, name))
|
|
217
|
+
|| (node.additionalProperties && node.additionalProperties !== true && refersTo(node.additionalProperties, name)),
|
|
218
|
+
)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Description Markdown as page content. Pages are Vue templates, so `{{`
|
|
223
|
+
* outside code would be read as an interpolation.
|
|
224
|
+
*/
|
|
225
|
+
export function prose(markdown: string): string {
|
|
226
|
+
return markdown.trim().split(/(`+[^`]*`+)/).map((part, index) => (index % 2 === 1 ? part : part.replaceAll('{{', '{{'))).join('')
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** One line of description for a list item. */
|
|
230
|
+
function inline(markdown: string): string {
|
|
231
|
+
return prose(markdown).replaceAll(/\s*\n\s*/g, ' ')
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Plain text of a short Markdown line, for frontmatter. */
|
|
235
|
+
function plain(markdown: string): string {
|
|
236
|
+
return markdown.replaceAll(/[*_`]/g, '').replaceAll(/\[([^\]]*)\]\([^)]*\)/g, '$1').trim()
|
|
237
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { OpenApiModel } from '../types'
|
|
2
|
+
|
|
3
|
+
/** Where the pages of one locale live. Links are site-relative, without `base`. */
|
|
4
|
+
export interface OpenApiLinks {
|
|
5
|
+
overview: string
|
|
6
|
+
changes: string
|
|
7
|
+
resource: (name: string) => string | undefined
|
|
8
|
+
operation: (id: string) => string | undefined
|
|
9
|
+
model: (name: string) => string | undefined
|
|
10
|
+
journey: (id: string) => string | undefined
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createOpenApiLinks(model: OpenApiModel, prefix: string, route: string): OpenApiLinks {
|
|
14
|
+
const base = `${prefix}${route}`
|
|
15
|
+
const resources = new Map(model.resources.map(resource => [resource.name, resource.slug]))
|
|
16
|
+
const operations = new Map(model.operations.map(operation => [operation.id, `${resources.get(operation.resource)}/${operation.slug}`]))
|
|
17
|
+
const models = new Set(model.schemas.map(entry => entry.name))
|
|
18
|
+
const journeys = new Map(model.workflows.map(workflow => [workflow.id, workflow.slug]))
|
|
19
|
+
return {
|
|
20
|
+
overview: base,
|
|
21
|
+
changes: `${base}changes`,
|
|
22
|
+
resource: name => (resources.has(name) ? `${base}${resources.get(name)}/` : undefined),
|
|
23
|
+
operation: id => (operations.has(id) ? `${base}${operations.get(id)}` : undefined),
|
|
24
|
+
model: name => (models.has(name) ? `${base}models/${modelSlug(name)}` : undefined),
|
|
25
|
+
journey: id => (journeys.has(id) ? `${base}journeys/${journeys.get(id)}` : undefined),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The same links with the site base in front, for HTML. */
|
|
30
|
+
export function withBaseLinks(links: OpenApiLinks, withBase: (path: string) => string): OpenApiLinks {
|
|
31
|
+
const wrap = (fn: (key: string) => string | undefined) => (key: string): string | undefined => {
|
|
32
|
+
const href = fn(key)
|
|
33
|
+
return href === undefined ? undefined : withBase(href)
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
overview: withBase(links.overview),
|
|
37
|
+
changes: withBase(links.changes),
|
|
38
|
+
resource: wrap(links.resource),
|
|
39
|
+
operation: wrap(links.operation),
|
|
40
|
+
model: wrap(links.model),
|
|
41
|
+
journey: wrap(links.journey),
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Path segment of a model page: the name, with `/` (which cannot be in a segment) replaced. */
|
|
46
|
+
export function modelSlug(name: string): string {
|
|
47
|
+
return name.replaceAll('/', '-')
|
|
48
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { defineRuntimePlugin } from 'kawapress'
|
|
2
|
+
import './components/openapi.css'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The components of the OpenAPI pages are page components the Markdown
|
|
6
|
+
* plugin imports into the pages that use them, so this runtime plugin only
|
|
7
|
+
* contributes their styles from the first paint.
|
|
8
|
+
*/
|
|
9
|
+
export default defineRuntimePlugin({
|
|
10
|
+
name: '@kawapress/plugin-openapi',
|
|
11
|
+
setup() {},
|
|
12
|
+
})
|