@kawapress/plugin-openapi 0.0.0-stage → 0.1.0
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
package/src/types.ts
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/** Languages of the code samples. */
|
|
2
|
+
export type OpenApiSampleLanguage = 'curl' | 'javascript' | 'python'
|
|
3
|
+
|
|
4
|
+
export interface OpenApiPluginOptions {
|
|
5
|
+
/** OpenAPI 3.0 or 3.1 document, JSON or YAML, relative to the site root. */
|
|
6
|
+
spec: string
|
|
7
|
+
/** Arazzo 1.x workflow files, relative to the site root. */
|
|
8
|
+
workflows?: string | string[]
|
|
9
|
+
/** Where the pages are mounted, `openapi/` by default. Every locale gets its own copy under its prefix. */
|
|
10
|
+
route?: string
|
|
11
|
+
/** Only generate pages for these locale keys; all by default. */
|
|
12
|
+
locales?: string[]
|
|
13
|
+
/** Snapshot folder, relative to the site root, `openapi-snapshots` by default. Without it there is no changes page. */
|
|
14
|
+
snapshotDir?: string
|
|
15
|
+
/**
|
|
16
|
+
* The "Try it" panel. On by default with the mock mode only; `server`
|
|
17
|
+
* adds a mode that sends the request to that base URL; `false` removes it.
|
|
18
|
+
*/
|
|
19
|
+
tryIt?: false | { server?: string }
|
|
20
|
+
/** Languages and order of the code samples, `['curl', 'javascript', 'python']` by default. */
|
|
21
|
+
samples?: OpenApiSampleLanguage[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head' | 'options' | 'trace'
|
|
25
|
+
|
|
26
|
+
/** Which cell of the resource map an operation fills. */
|
|
27
|
+
export type OpenApiAction = 'list' | 'create' | 'read' | 'update' | 'delete' | 'other'
|
|
28
|
+
|
|
29
|
+
export type SchemaKind = 'object' | 'array' | 'string' | 'number' | 'integer' | 'boolean' | 'null' | 'any' | 'union' | 'ref'
|
|
30
|
+
|
|
31
|
+
export interface SchemaConstraints {
|
|
32
|
+
minimum?: number
|
|
33
|
+
maximum?: number
|
|
34
|
+
exclusiveMinimum?: number
|
|
35
|
+
exclusiveMaximum?: number
|
|
36
|
+
multipleOf?: number
|
|
37
|
+
minLength?: number
|
|
38
|
+
maxLength?: number
|
|
39
|
+
pattern?: string
|
|
40
|
+
minItems?: number
|
|
41
|
+
maxItems?: number
|
|
42
|
+
uniqueItems?: boolean
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A schema with every in-document `$ref` resolved, except references to
|
|
47
|
+
* `components.schemas`: those stay as `{ kind: 'ref', ref }` leaves, so
|
|
48
|
+
* recursive models stay finite and keep their names.
|
|
49
|
+
*/
|
|
50
|
+
export interface SchemaNode {
|
|
51
|
+
kind: SchemaKind
|
|
52
|
+
/** Name in `components.schemas` when `kind` is `ref`. */
|
|
53
|
+
ref?: string
|
|
54
|
+
format?: string
|
|
55
|
+
title?: string
|
|
56
|
+
description: string
|
|
57
|
+
nullable: boolean
|
|
58
|
+
deprecated: boolean
|
|
59
|
+
readOnly: boolean
|
|
60
|
+
writeOnly: boolean
|
|
61
|
+
enum?: unknown[]
|
|
62
|
+
default?: unknown
|
|
63
|
+
/** `example`, or the first of `examples`. */
|
|
64
|
+
example?: unknown
|
|
65
|
+
hasExample: boolean
|
|
66
|
+
constraints: SchemaConstraints
|
|
67
|
+
properties?: SchemaProperty[]
|
|
68
|
+
/** `true` for any value, a schema for typed values, absent when not allowed or not said. */
|
|
69
|
+
additionalProperties?: SchemaNode | true
|
|
70
|
+
items?: SchemaNode
|
|
71
|
+
/** `oneOf` / `anyOf` members. */
|
|
72
|
+
variants?: SchemaNode[]
|
|
73
|
+
/** `x-since`. */
|
|
74
|
+
since?: string
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface SchemaProperty {
|
|
78
|
+
name: string
|
|
79
|
+
required: boolean
|
|
80
|
+
schema: SchemaNode
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface OpenApiParameter {
|
|
84
|
+
name: string
|
|
85
|
+
in: 'path' | 'query' | 'header' | 'cookie'
|
|
86
|
+
required: boolean
|
|
87
|
+
deprecated: boolean
|
|
88
|
+
description: string
|
|
89
|
+
schema: SchemaNode
|
|
90
|
+
example?: unknown
|
|
91
|
+
since?: string
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface OpenApiBody {
|
|
95
|
+
contentType: string
|
|
96
|
+
required: boolean
|
|
97
|
+
description: string
|
|
98
|
+
schema: SchemaNode
|
|
99
|
+
/** The document's example; generated from the schema when there is none. */
|
|
100
|
+
example: unknown
|
|
101
|
+
exampleGenerated: boolean
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface OpenApiResponse {
|
|
105
|
+
/** `201`, `4XX` or `default`. */
|
|
106
|
+
status: string
|
|
107
|
+
description: string
|
|
108
|
+
body?: OpenApiBody
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface OpenApiOperation {
|
|
112
|
+
/** `operationId`; derived from the method and path when missing. */
|
|
113
|
+
id: string
|
|
114
|
+
/** `id` in kebab-case, the last segment of the page URL. */
|
|
115
|
+
slug: string
|
|
116
|
+
/** Name of the resource the operation belongs to. */
|
|
117
|
+
resource: string
|
|
118
|
+
method: HttpMethod
|
|
119
|
+
/** URL path template; the event name for a webhook. */
|
|
120
|
+
path: string
|
|
121
|
+
webhook: boolean
|
|
122
|
+
action: OpenApiAction
|
|
123
|
+
summary: string
|
|
124
|
+
description: string
|
|
125
|
+
deprecated: boolean
|
|
126
|
+
/** `x-since`; the snapshots fill it in when missing. */
|
|
127
|
+
since?: string
|
|
128
|
+
/** Names of the security schemes that apply. */
|
|
129
|
+
security: string[]
|
|
130
|
+
parameters: OpenApiParameter[]
|
|
131
|
+
requestBody?: OpenApiBody
|
|
132
|
+
responses: OpenApiResponse[]
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface OpenApiResource {
|
|
136
|
+
name: string
|
|
137
|
+
slug: string
|
|
138
|
+
description: string
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface OpenApiSchemaEntry {
|
|
142
|
+
name: string
|
|
143
|
+
schema: SchemaNode
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface OpenApiSecurityScheme {
|
|
147
|
+
name: string
|
|
148
|
+
type: 'http' | 'apiKey' | 'oauth2' | 'openIdConnect' | 'mutualTLS'
|
|
149
|
+
/** `bearer` / `basic` for http. */
|
|
150
|
+
scheme?: string
|
|
151
|
+
/** Header, query or cookie name of an API key. */
|
|
152
|
+
paramName?: string
|
|
153
|
+
in?: 'header' | 'query' | 'cookie'
|
|
154
|
+
description: string
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface OpenApiServer {
|
|
158
|
+
url: string
|
|
159
|
+
description: string
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** A value inside a workflow step: a literal, or a runtime expression. */
|
|
163
|
+
export type WorkflowValue
|
|
164
|
+
= | { kind: 'literal', value: unknown }
|
|
165
|
+
| { kind: 'input', name: string }
|
|
166
|
+
| { kind: 'output', step: string, output: string }
|
|
167
|
+
|
|
168
|
+
export interface WorkflowStep {
|
|
169
|
+
id: string
|
|
170
|
+
description: string
|
|
171
|
+
/** The operation the step calls. */
|
|
172
|
+
operation: string
|
|
173
|
+
parameters: { name: string, in?: string, value: WorkflowValue }[]
|
|
174
|
+
/** The request payload, with runtime expressions as `WorkflowValue` objects under `$kawa`. */
|
|
175
|
+
payload?: unknown
|
|
176
|
+
/** Expected status code from the success criteria, when one says `$statusCode == N`. */
|
|
177
|
+
status?: string
|
|
178
|
+
/** Output name → JSON pointer into the response body (`/id`), or another expression as text. */
|
|
179
|
+
outputs: { name: string, pointer?: string, expression: string }[]
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface OpenApiWorkflow {
|
|
183
|
+
id: string
|
|
184
|
+
slug: string
|
|
185
|
+
summary: string
|
|
186
|
+
description: string
|
|
187
|
+
/** Absolute path of the Arazzo file. */
|
|
188
|
+
file: string
|
|
189
|
+
inputs: { name: string, type: string, description: string }[]
|
|
190
|
+
steps: WorkflowStep[]
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface OpenApiModel {
|
|
194
|
+
info: { title: string, version: string, summary: string, description: string }
|
|
195
|
+
/** `3.0.3`, `3.1.0`… */
|
|
196
|
+
openapi: string
|
|
197
|
+
servers: OpenApiServer[]
|
|
198
|
+
securitySchemes: OpenApiSecurityScheme[]
|
|
199
|
+
resources: OpenApiResource[]
|
|
200
|
+
/** Webhooks included, grouped by resource in resource order. */
|
|
201
|
+
operations: OpenApiOperation[]
|
|
202
|
+
/** `components.schemas`, sorted by name. */
|
|
203
|
+
schemas: OpenApiSchemaEntry[]
|
|
204
|
+
workflows: OpenApiWorkflow[]
|
|
205
|
+
/** Absolute paths of the files the model was read from. */
|
|
206
|
+
sourceFiles: string[]
|
|
207
|
+
}
|
package/src/vite.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { MarkdownExit } from 'markdown-exit'
|
|
2
|
+
import type { EnvironmentModuleNode, Plugin } from 'vite'
|
|
3
|
+
import type { DataContext, RefTarget } from './data/components'
|
|
4
|
+
import type { ResolvedOpenApiOptions } from './options'
|
|
5
|
+
import type { OpenApiService } from './service'
|
|
6
|
+
import { withBase } from 'kawapress'
|
|
7
|
+
import { normalizePath } from 'vite'
|
|
8
|
+
import {
|
|
9
|
+
buildCardData,
|
|
10
|
+
buildEndpointData,
|
|
11
|
+
buildJourneyData,
|
|
12
|
+
buildMapData,
|
|
13
|
+
buildParametersData,
|
|
14
|
+
buildRefData,
|
|
15
|
+
buildRequestData,
|
|
16
|
+
buildResponsesData,
|
|
17
|
+
buildSamplesData,
|
|
18
|
+
buildSchemaData,
|
|
19
|
+
createSchemaTable,
|
|
20
|
+
} from './data/components'
|
|
21
|
+
import { escapeHtml } from './data/text'
|
|
22
|
+
import { labelsForLanguage } from './render/labels'
|
|
23
|
+
import { createOpenApiLinks, withBaseLinks } from './render/routes'
|
|
24
|
+
import { COMPONENT_MODULE } from './tag'
|
|
25
|
+
|
|
26
|
+
export interface OpenApiVitePluginOptions {
|
|
27
|
+
service: OpenApiService
|
|
28
|
+
options: () => ResolvedOpenApiOptions
|
|
29
|
+
/** Site base, for links in the HTML. */
|
|
30
|
+
base: () => string
|
|
31
|
+
markdown: () => MarkdownExit | undefined
|
|
32
|
+
/** The `lang` of the locale whose pages have the given URL prefix. */
|
|
33
|
+
languageOf: (prefix: string) => string | undefined
|
|
34
|
+
/** Absolute path of the module that default-exports the component of a kind. */
|
|
35
|
+
component: (kind: string) => string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const RESOLVED_PREFIX = '\0kawa-openapi-component?'
|
|
39
|
+
|
|
40
|
+
/** Kinds that take an operation, and the tag attribute that names it. */
|
|
41
|
+
const OPERATION_KINDS = new Set(['endpoint', 'parameters', 'request', 'responses', 'samples', 'card'])
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Resolves the `virtual:kawapress-openapi/component?...` modules the tags
|
|
45
|
+
* import. Each one default-exports a component that renders its data in the
|
|
46
|
+
* language of the page's locale.
|
|
47
|
+
*/
|
|
48
|
+
export function createOpenApiVitePlugin(config: OpenApiVitePluginOptions): Plugin {
|
|
49
|
+
const { service } = config
|
|
50
|
+
return {
|
|
51
|
+
name: 'kawapress:openapi-components',
|
|
52
|
+
enforce: 'pre',
|
|
53
|
+
|
|
54
|
+
resolveId(source) {
|
|
55
|
+
return source.startsWith(`${COMPONENT_MODULE}?`) ? `${RESOLVED_PREFIX}${source.slice(COMPONENT_MODULE.length + 1)}` : null
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
async load(id) {
|
|
59
|
+
if (!id.startsWith(RESOLVED_PREFIX)) {
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
const query = new URLSearchParams(id.slice(RESOLVED_PREFIX.length))
|
|
63
|
+
const kind = query.get('kind') ?? ''
|
|
64
|
+
const page = query.get('page') ?? ''
|
|
65
|
+
const prefix = query.get('prefix') ?? ''
|
|
66
|
+
const fail = (message: string): never => this.error(`KawaPress OpenAPI: ${message} (on ${page})`)
|
|
67
|
+
|
|
68
|
+
for (const file of service.watchedFiles()) {
|
|
69
|
+
this.addWatchFile(file)
|
|
70
|
+
}
|
|
71
|
+
const api = await service.get()
|
|
72
|
+
const options = config.options()
|
|
73
|
+
const pageLinks = createOpenApiLinks(api.model, prefix, options.route)
|
|
74
|
+
const markdown = config.markdown()
|
|
75
|
+
const context: DataContext = {
|
|
76
|
+
api,
|
|
77
|
+
schemas: createSchemaTable(api),
|
|
78
|
+
labels: labelsForLanguage(config.languageOf(prefix)),
|
|
79
|
+
pageLinks,
|
|
80
|
+
links: withBaseLinks(pageLinks, path => withBase(path, config.base())),
|
|
81
|
+
highlight: async (code, lang) => (await markdown?.options.highlight?.(code, lang, '', {})) || `<pre><code>${escapeHtml(code)}</code></pre>`,
|
|
82
|
+
options,
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const operationOf = (attribute: string) => {
|
|
86
|
+
const name = query.get(attribute) ?? ''
|
|
87
|
+
const operation = api.model.operations.find(item => item.id === name)
|
|
88
|
+
return operation ?? fail(`there is no operation "${name}" in ${options.spec}. Known operations: ${api.model.operations.slice(0, 8).map(item => item.id).join(', ')}${api.model.operations.length > 8 ? ', …' : ''}.`)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let data: unknown
|
|
92
|
+
if (OPERATION_KINDS.has(kind)) {
|
|
93
|
+
const operation = operationOf('op')
|
|
94
|
+
switch (kind) {
|
|
95
|
+
case 'endpoint':
|
|
96
|
+
data = buildEndpointData(operation, context)
|
|
97
|
+
break
|
|
98
|
+
case 'parameters':
|
|
99
|
+
data = buildParametersData(operation, context)
|
|
100
|
+
break
|
|
101
|
+
case 'request':
|
|
102
|
+
data = await buildRequestData(operation, context)
|
|
103
|
+
break
|
|
104
|
+
case 'responses':
|
|
105
|
+
data = await buildResponsesData(operation, context)
|
|
106
|
+
break
|
|
107
|
+
case 'samples':
|
|
108
|
+
data = await buildSamplesData(operation, context)
|
|
109
|
+
break
|
|
110
|
+
default: {
|
|
111
|
+
const fields = (query.get('fields') ?? '').split(',').map(field => field.trim()).filter(Boolean)
|
|
112
|
+
data = buildCardData(operation, context, fields)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else if (kind === 'map') {
|
|
117
|
+
data = buildMapData(context)
|
|
118
|
+
}
|
|
119
|
+
else if (kind === 'schema') {
|
|
120
|
+
const name = query.get('name') ?? ''
|
|
121
|
+
data = await buildSchemaData(name, context) ?? fail(`there is no schema "${name}" in components.schemas.`)
|
|
122
|
+
}
|
|
123
|
+
else if (kind === 'journey') {
|
|
124
|
+
const name = query.get('workflow') ?? ''
|
|
125
|
+
const workflow = api.model.workflows.find(item => item.id === name)
|
|
126
|
+
data = workflow ? buildJourneyData(workflow, context) : fail(`there is no workflow "${name}" in the Arazzo files.`)
|
|
127
|
+
}
|
|
128
|
+
else if (kind === 'ref') {
|
|
129
|
+
const target: RefTarget | undefined = query.has('op')
|
|
130
|
+
? { op: query.get('op')! }
|
|
131
|
+
: query.has('schema') ? { schema: query.get('schema')! } : query.has('journey') ? { journey: query.get('journey')! } : undefined
|
|
132
|
+
if (!target) {
|
|
133
|
+
fail('`:api{}` needs op=, schema= or journey=, for example :api{op=createOrder}.')
|
|
134
|
+
}
|
|
135
|
+
data = buildRefData(target!, context) ?? fail(`:api{${query.has('op') ? 'op' : query.has('schema') ? 'schema' : 'journey'}=${Object.values(target!)[0]}} does not exist in the spec.`)
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
fail(`unknown component kind "${kind}".`)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return `
|
|
142
|
+
import { h } from 'vue'
|
|
143
|
+
import Component from ${JSON.stringify(normalizePath(config.component(kind)))}
|
|
144
|
+
|
|
145
|
+
const data = ${JSON.stringify(data ?? null)}
|
|
146
|
+
|
|
147
|
+
export default function KawaOpenApiComponent() {
|
|
148
|
+
return h(Component, { data })
|
|
149
|
+
}
|
|
150
|
+
`
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
hotUpdate: {
|
|
154
|
+
handler({ file, modules }) {
|
|
155
|
+
if (!service.watchedFiles().has(normalizePath(file)) && !normalizePath(file).startsWith(`${normalizePath(config.options().snapshotDir)}/`)) {
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
const affected: EnvironmentModuleNode[] = []
|
|
159
|
+
for (const [id, module] of this.environment.moduleGraph.idToModuleMap) {
|
|
160
|
+
if (id.startsWith(RESOLVED_PREFIX)) {
|
|
161
|
+
this.environment.moduleGraph.invalidateModule(module)
|
|
162
|
+
affected.push(module)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return affected.length > 0 ? [...new Set([...modules, ...affected])] : undefined
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
}
|
|
169
|
+
}
|