@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) KawaPress contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README-zh-CN.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# @kawapress/plugin-openapi
|
|
2
|
+
|
|
3
|
+
KawaPress 的 HTTP API 参考文档插件:读取 OpenAPI 3.0 / 3.1 规格,支持 Arazzo 旅程。
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@kawapress/plugin-openapi) · [English](https://github.com/kawapress/kawapress/blob/main/packages/plugin-openapi/README.md)
|
|
6
|
+
|
|
7
|
+
`@kawapress/plugin-openapi` 是 [KawaPress](https://github.com/kawapress/kawapress)(以 Vue 为核心的静态站点生成器)的官方插件。
|
|
8
|
+
|
|
9
|
+
## 特性
|
|
10
|
+
|
|
11
|
+
- 构建时从规格生成页面:资源地图,每个资源、接口、Webhook、模型各一页,不需要提交生成的 `.md` 文件。
|
|
12
|
+
- 报文即文档:请求体和响应以示例 JSON 展示,类型与约束贴在每行末尾,点一行看说明;字段表和 TypeScript 声明是另两种视图。
|
|
13
|
+
- Arazzo 1.x 工作流生成旅程页,标出值如何从上一步的响应流进下一步的请求,并能在 Mock 里跑通。
|
|
14
|
+
- 「试一试」在浏览器里按 schema 校验请求并返回示例响应,也可以配置真实服务器。
|
|
15
|
+
- 任意文章里用 `:api{op=createOrder}` 和 `<OpenApiOperation op="createOrder" />` 引用接口与模型,引用不存在时构建失败。
|
|
16
|
+
- 每个版本存一份快照,生成「版本变更」页(破坏性、新增、弃用),字段旁标出起始版本。
|
|
17
|
+
- 页面是普通 Markdown 页面,搜索、站点地图和 `llms.txt` 都自动包含;固定文字按页面语言显示中文或英文。
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install --save-dev @kawapress/plugin-openapi
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 用法
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import openapiPlugin from '@kawapress/plugin-openapi'
|
|
29
|
+
import { nagi } from 'kawapress/nagi'
|
|
30
|
+
|
|
31
|
+
export default nagi({
|
|
32
|
+
plugins: [
|
|
33
|
+
openapiPlugin({
|
|
34
|
+
spec: './openapi.yaml',
|
|
35
|
+
workflows: './order-flow.arazzo.yaml',
|
|
36
|
+
}),
|
|
37
|
+
],
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
页面是 `/openapi/`、`/openapi/<资源>/`、`/openapi/<资源>/<接口>`、`/openapi/models/<模型>`、`/openapi/journeys/<工作流>` 和 `/openapi/changes`,每个语言各一份。
|
|
42
|
+
|
|
43
|
+
### 版本变更
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
npx kawapress-openapi snapshot --spec ./openapi.yaml
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
写入 `openapi-snapshots/<info.version>.json`;已有同版本快照时不覆盖,除非加 `--force`。构建本身不写快照。有两份快照后出现变更页。
|
|
50
|
+
|
|
51
|
+
## 选项
|
|
52
|
+
|
|
53
|
+
| 选项 | 说明 |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| `spec` | 必填。OpenAPI 3.0 / 3.1 文档,JSON 或 YAML,相对站点根目录。 |
|
|
56
|
+
| `workflows` | Arazzo 1.x 文件。 |
|
|
57
|
+
| `route` | 页面挂载路径,默认 `openapi/`。 |
|
|
58
|
+
| `locales` | 只为这些语言生成页面,默认全部。 |
|
|
59
|
+
| `snapshotDir` | 快照目录,默认 `openapi-snapshots`。 |
|
|
60
|
+
| `tryIt` | `false` 关闭「试一试」;`{ server }` 在 Mock 之外增加真实服务器。 |
|
|
61
|
+
| `samples` | 代码示例的语言与顺序,默认 `['curl', 'javascript', 'python']`。 |
|
|
62
|
+
|
|
63
|
+
## 限制
|
|
64
|
+
|
|
65
|
+
- `$ref` 只能指向文档自身;多文件规格先打包(例如 `redocly bundle`)。
|
|
66
|
+
- 调用其他 workflow 的 Arazzo 步骤暂不支持。
|
|
67
|
+
- Mock 校验类型、必填、枚举、取值范围、长度和正则,不覆盖全部 JSON Schema 关键字。
|
|
68
|
+
|
|
69
|
+
## 文档
|
|
70
|
+
|
|
71
|
+
[文档](https://kawapress.dev/plugins/openapi) · [反馈问题](https://github.com/kawapress/kawapress/issues)
|
|
72
|
+
|
|
73
|
+
## 许可
|
|
74
|
+
|
|
75
|
+
[MIT](https://github.com/kawapress/kawapress/blob/main/LICENSE) © KawaPress contributors
|
package/README.md
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kawapress/plugin-openapi
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
HTTP API reference for KawaPress, read from an OpenAPI 3.0 / 3.1 spec, with Arazzo journeys.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@kawapress/plugin-openapi) · [简体中文](https://github.com/kawapress/kawapress/blob/main/packages/plugin-openapi/README-zh-CN.md)
|
|
6
|
+
|
|
7
|
+
`@kawapress/plugin-openapi` is part of [KawaPress](https://github.com/kawapress/kawapress), a Vue-first static site generator.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Pages are generated at build time from the spec: a resource map, one page per resource, operation, webhook and model. No generated `.md` files to commit.
|
|
12
|
+
- The payload is the documentation: request and response bodies are example JSON with the type and constraints at the end of each line; click a line for its description. A field list and TypeScript declarations are two more views.
|
|
13
|
+
- Arazzo 1.x workflows become journey pages that show how values flow from one step's response into the next step's request, and run with mocks.
|
|
14
|
+
- "Try it" checks the request against the schema in the browser and answers with the example response; a real server is optional.
|
|
15
|
+
- `:api{op=createOrder}` and `<OpenApiOperation op="createOrder" />` refer to operations and models inside any article; a missing target fails the build.
|
|
16
|
+
- API snapshots per version give an "API changes" page (breaking, added, deprecated) and since versions on fields.
|
|
17
|
+
- Pages are ordinary Markdown pages, so search, the sitemap and `llms.txt` have them. Fixed texts are Chinese or English by page language.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install --save-dev @kawapress/plugin-openapi
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import openapiPlugin from '@kawapress/plugin-openapi'
|
|
29
|
+
import { nagi } from 'kawapress/nagi'
|
|
30
|
+
|
|
31
|
+
export default nagi({
|
|
32
|
+
plugins: [
|
|
33
|
+
openapiPlugin({
|
|
34
|
+
spec: './openapi.yaml',
|
|
35
|
+
workflows: './order-flow.arazzo.yaml',
|
|
36
|
+
}),
|
|
37
|
+
],
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The pages are `/openapi/`, `/openapi/<resource>/`, `/openapi/<resource>/<operation>`, `/openapi/models/<Model>`, `/openapi/journeys/<workflow>` and `/openapi/changes`, once per locale.
|
|
42
|
+
|
|
43
|
+
### API changes
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
npx kawapress-openapi snapshot --spec ./openapi.yaml
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
writes `openapi-snapshots/<info.version>.json` and keeps a snapshot that exists unless `--force` is given. A build never writes snapshots. The changes page appears once there are two snapshots.
|
|
50
|
+
|
|
51
|
+
## Options
|
|
52
|
+
|
|
53
|
+
| Option | Description |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| `spec` | Required. OpenAPI 3.0 / 3.1 document, JSON or YAML, relative to the site root. |
|
|
56
|
+
| `workflows` | Arazzo 1.x files. |
|
|
57
|
+
| `route` | Where the pages are mounted, default `openapi/`. |
|
|
58
|
+
| `locales` | Generate pages for these locales only. Default: all. |
|
|
59
|
+
| `snapshotDir` | Snapshot folder, default `openapi-snapshots`. |
|
|
60
|
+
| `tryIt` | `false` turns "Try it" off; `{ server }` adds a real server next to the mock. |
|
|
61
|
+
| `samples` | Code sample languages and order, default `['curl', 'javascript', 'python']`. |
|
|
62
|
+
|
|
63
|
+
## Limitations
|
|
64
|
+
|
|
65
|
+
- `$ref` must point inside the document; bundle a multi-file spec first (for example `redocly bundle`).
|
|
66
|
+
- Arazzo steps that call another workflow are not supported yet.
|
|
67
|
+
- The mock checks types, required fields, enums, ranges, lengths and patterns, not every JSON Schema keyword.
|
|
68
|
+
|
|
69
|
+
## Documentation
|
|
70
|
+
|
|
71
|
+
[Documentation](https://kawapress.dev/en/plugins/openapi) · [Report a problem](https://github.com/kawapress/kawapress/issues)
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
[MIT](https://github.com/kawapress/kawapress/blob/main/LICENSE) © KawaPress contributors
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import process from 'node:process'
|
|
3
|
+
import { consola } from 'consola'
|
|
4
|
+
import { createJiti } from 'jiti'
|
|
5
|
+
|
|
6
|
+
const jiti = createJiti(import.meta.url)
|
|
7
|
+
jiti.import('../src/cli.ts').catch((error) => {
|
|
8
|
+
consola.error(error)
|
|
9
|
+
process.exit(1)
|
|
10
|
+
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kawapress/plugin-openapi",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "HTTP API reference pages for KawaPress from OpenAPI 3.x: example-first payload views, a resource map, Arazzo journeys, mock try-it and API change pages",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://kawapress.dev/",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/kawapress/kawapress.git",
|
|
11
|
+
"directory": "packages/plugin-openapi"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/kawapress/kawapress/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"kawapress",
|
|
18
|
+
"kawapress-plugin",
|
|
19
|
+
"openapi",
|
|
20
|
+
"arazzo",
|
|
21
|
+
"api-docs"
|
|
22
|
+
],
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./src/index.ts",
|
|
26
|
+
"import": "./src/index.ts",
|
|
27
|
+
"default": "./src/index.ts"
|
|
28
|
+
},
|
|
29
|
+
"./runtime-plugin": {
|
|
30
|
+
"types": "./src/runtime-plugin.ts",
|
|
31
|
+
"import": "./src/runtime-plugin.ts",
|
|
32
|
+
"default": "./src/runtime-plugin.ts"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"bin": {
|
|
37
|
+
"kawapress-openapi": "./bin/kawapress-openapi.mjs"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"README-zh-CN.md",
|
|
41
|
+
"bin",
|
|
42
|
+
"src/**/!(*.test.*)"
|
|
43
|
+
],
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=22.12.0"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public",
|
|
49
|
+
"registry": "https://registry.npmjs.org/"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"kawapress": ">=0.9.0 <0.10.0",
|
|
53
|
+
"markdown-exit": "^1.3.0",
|
|
54
|
+
"vite": "^8.2.2",
|
|
55
|
+
"vue": "^3.5.41"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"citty": "^0.2.2",
|
|
59
|
+
"consola": "^3.4.2",
|
|
60
|
+
"jiti": "^2.7.0",
|
|
61
|
+
"tinyglobby": "^0.2.17",
|
|
62
|
+
"yaml": "^2.8.1"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"markdown-exit": "^1.3.0",
|
|
66
|
+
"vite": "^8.2.2",
|
|
67
|
+
"vue": "^3.5.41",
|
|
68
|
+
"kawapress": "0.0.1"
|
|
69
|
+
}
|
|
6
70
|
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { resolve } from 'node:path'
|
|
2
|
+
import { defineCommand, runMain } from 'citty'
|
|
3
|
+
import { runSnapshot } from './commands'
|
|
4
|
+
import { DEFAULT_SNAPSHOT_DIR } from './options'
|
|
5
|
+
|
|
6
|
+
const snapshot = defineCommand({
|
|
7
|
+
meta: { name: 'snapshot', description: 'Write the API snapshot of the spec\'s current info.version' },
|
|
8
|
+
args: {
|
|
9
|
+
spec: {
|
|
10
|
+
type: 'string',
|
|
11
|
+
description: 'The OpenAPI document',
|
|
12
|
+
required: true,
|
|
13
|
+
},
|
|
14
|
+
dir: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
description: `Snapshot folder (default: ${DEFAULT_SNAPSHOT_DIR})`,
|
|
17
|
+
},
|
|
18
|
+
force: {
|
|
19
|
+
type: 'boolean',
|
|
20
|
+
description: 'Overwrite the snapshot of this version',
|
|
21
|
+
default: false,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
async run({ args }) {
|
|
25
|
+
const result = await runSnapshot(resolve(args.spec), resolve(args.dir ?? DEFAULT_SNAPSHOT_DIR), { force: args.force })
|
|
26
|
+
console.log(result.written ? `Wrote ${result.file}` : `Kept ${result.file}; pass --force to overwrite it.`)
|
|
27
|
+
},
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const main = defineCommand({
|
|
31
|
+
meta: { name: 'kawapress-openapi', description: 'Tools for @kawapress/plugin-openapi' },
|
|
32
|
+
subCommands: { snapshot },
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
runMain(main)
|
package/src/commands.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { dirname } from 'node:path'
|
|
4
|
+
import { parseDocument } from './read/document'
|
|
5
|
+
import { readOpenApi } from './read/model'
|
|
6
|
+
import { snapshotFile } from './service'
|
|
7
|
+
import { createSnapshot, serializeSnapshot } from './snapshot/snapshot'
|
|
8
|
+
|
|
9
|
+
export interface SnapshotResult {
|
|
10
|
+
file: string
|
|
11
|
+
written: boolean
|
|
12
|
+
version: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Writes the snapshot of the spec's current `info.version`, keeping an existing one unless `force`. */
|
|
16
|
+
export async function runSnapshot(spec: string, dir: string, { force = false } = {}): Promise<SnapshotResult> {
|
|
17
|
+
const model = readOpenApi(parseDocument(await readFile(spec, 'utf8'), spec), spec)
|
|
18
|
+
if (!model.info.version) {
|
|
19
|
+
throw new Error(`${spec} has no info.version; the snapshot is named after it.`)
|
|
20
|
+
}
|
|
21
|
+
const file = snapshotFile(dir, model.info.version)
|
|
22
|
+
if (existsSync(file) && !force) {
|
|
23
|
+
return { file, written: false, version: model.info.version }
|
|
24
|
+
}
|
|
25
|
+
await mkdir(dirname(file), { recursive: true })
|
|
26
|
+
await writeFile(file, serializeSnapshot(createSnapshot(model)))
|
|
27
|
+
return { file, written: true, version: model.info.version }
|
|
28
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { BodyViewData } from '../data/components'
|
|
3
|
+
import KawaOpenApiBodyView from './KawaOpenApiBodyView.vue'
|
|
4
|
+
|
|
5
|
+
defineProps<{
|
|
6
|
+
data: BodyViewData | null
|
|
7
|
+
}>()
|
|
8
|
+
|
|
9
|
+
// The llms build compiles pages with this flag on; Markdown keeps the content identical.
|
|
10
|
+
const isSsgMarkdown = import.meta.env.SSG_MD
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<template>
|
|
14
|
+
<template v-if="data">
|
|
15
|
+
<div v-if="isSsgMarkdown" data-kawa-ssg-md-raw>
|
|
16
|
+
{{ `\n\n${data.body.markdown}\n\n` }}
|
|
17
|
+
</div>
|
|
18
|
+
<KawaOpenApiBodyView v-else :body="data.body" :text="data.text" />
|
|
19
|
+
</template>
|
|
20
|
+
</template>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { BodyData } from '../data/body'
|
|
3
|
+
import type { BodyText } from '../data/components'
|
|
4
|
+
import { shallowRef } from 'vue'
|
|
5
|
+
import KawaOpenApiFieldList from './KawaOpenApiFieldList.vue'
|
|
6
|
+
import KawaOpenApiPayload from './KawaOpenApiPayload.vue'
|
|
7
|
+
|
|
8
|
+
defineProps<{
|
|
9
|
+
body: BodyData
|
|
10
|
+
text: BodyText
|
|
11
|
+
}>()
|
|
12
|
+
|
|
13
|
+
type View = 'payload' | 'table' | 'typescript'
|
|
14
|
+
const views: View[] = ['payload', 'table', 'typescript']
|
|
15
|
+
const view = shallowRef<View>('payload')
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<template>
|
|
19
|
+
<div class="kawa-openapi-body">
|
|
20
|
+
<div class="kawa-openapi-body__bar">
|
|
21
|
+
<span class="kawa-openapi-body__type">{{ body.contentType }}</span>
|
|
22
|
+
<span v-if="body.generated && view === 'payload'" class="kawa-openapi-body__generated">{{ text.generated }}</span>
|
|
23
|
+
<span class="kawa-openapi-body__spacer" />
|
|
24
|
+
<div class="kawa-openapi-segmented" role="group">
|
|
25
|
+
<button
|
|
26
|
+
v-for="item in views"
|
|
27
|
+
:key="item"
|
|
28
|
+
type="button"
|
|
29
|
+
:aria-pressed="view === item"
|
|
30
|
+
@click="view = item"
|
|
31
|
+
>
|
|
32
|
+
{{ text.views[item] }}
|
|
33
|
+
</button>
|
|
34
|
+
</div>
|
|
35
|
+
</div>
|
|
36
|
+
<div v-if="body.descriptionHtml" class="kawa-openapi-body__description" v-html="body.descriptionHtml" />
|
|
37
|
+
<KawaOpenApiPayload v-show="view === 'payload'" :payload="body.payload" :text="text" />
|
|
38
|
+
<KawaOpenApiFieldList v-if="view === 'table'" :rows="body.fields" :text="text" />
|
|
39
|
+
<div v-if="view === 'typescript'" class="kawa-openapi-body__ts" v-html="body.typescriptHtml" />
|
|
40
|
+
</div>
|
|
41
|
+
</template>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { CardData } from '../data/components'
|
|
3
|
+
|
|
4
|
+
defineProps<{
|
|
5
|
+
data: CardData
|
|
6
|
+
}>()
|
|
7
|
+
|
|
8
|
+
const isSsgMarkdown = import.meta.env.SSG_MD
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<template>
|
|
12
|
+
<div v-if="isSsgMarkdown" data-kawa-ssg-md-raw>
|
|
13
|
+
{{ `\n\n${data.markdown}\n\n` }}
|
|
14
|
+
</div>
|
|
15
|
+
<div v-else class="kawa-openapi-card">
|
|
16
|
+
<div class="kawa-openapi-card__head">
|
|
17
|
+
<span :class="`kawa-openapi-method kawa-openapi-method--${data.method.toLowerCase()}`">{{ data.method }}</span>
|
|
18
|
+
<code>{{ data.path }}</code>
|
|
19
|
+
<a :href="data.href" class="kawa-openapi-card__title">{{ data.title }}</a>
|
|
20
|
+
</div>
|
|
21
|
+
<div v-if="data.summaryHtml" class="kawa-openapi-card__summary" v-html="data.summaryHtml" />
|
|
22
|
+
<div v-for="field in data.fields" :key="field.name" class="kawa-openapi-card__row">
|
|
23
|
+
<span class="kawa-openapi-card__name">{{ field.name }}</span>
|
|
24
|
+
<span class="kawa-openapi-card__note">{{ field.type }}<template v-if="field.note"> · {{ field.note }}</template></span>
|
|
25
|
+
</div>
|
|
26
|
+
<div v-if="data.response" class="kawa-openapi-card__row">
|
|
27
|
+
<span :class="`kawa-openapi-status kawa-openapi-status--${/^[23]/.test(data.response.status) ? 'ok' : 'error'}`">{{ data.response.status }}</span>
|
|
28
|
+
<a v-if="data.response.href" :href="data.response.href" class="kawa-openapi-card__note">{{ data.response.type }}</a>
|
|
29
|
+
<span v-else class="kawa-openapi-card__note">{{ data.response.type }}</span>
|
|
30
|
+
</div>
|
|
31
|
+
<a :href="data.href" class="kawa-openapi-card__more">{{ data.text.details }} →</a>
|
|
32
|
+
</div>
|
|
33
|
+
</template>
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { EndpointData } from '../data/components'
|
|
3
|
+
import type { Rule } from './validate'
|
|
4
|
+
import { computed, reactive, shallowRef } from 'vue'
|
|
5
|
+
import { validate } from './validate'
|
|
6
|
+
|
|
7
|
+
const props = defineProps<{
|
|
8
|
+
data: EndpointData
|
|
9
|
+
}>()
|
|
10
|
+
|
|
11
|
+
const isSsgMarkdown = import.meta.env.SSG_MD
|
|
12
|
+
const methodClass = computed(() => `kawa-openapi-method kawa-openapi-method--${props.data.method.toLowerCase()}`)
|
|
13
|
+
|
|
14
|
+
const copyState = shallowRef<'idle' | 'copied' | 'failed'>('idle')
|
|
15
|
+
async function copyCurl(): Promise<void> {
|
|
16
|
+
try {
|
|
17
|
+
await navigator.clipboard.writeText(props.data.curl ?? '')
|
|
18
|
+
copyState.value = 'copied'
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
copyState.value = 'failed'
|
|
22
|
+
}
|
|
23
|
+
setTimeout(() => {
|
|
24
|
+
copyState.value = 'idle'
|
|
25
|
+
}, 1500)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/* Try it: closed on the server and at first paint. */
|
|
29
|
+
const open = shallowRef(false)
|
|
30
|
+
const mode = shallowRef<'mock' | 'server'>('mock')
|
|
31
|
+
const sending = shallowRef(false)
|
|
32
|
+
const output = shallowRef<{ status: string, body: string, error: boolean }>()
|
|
33
|
+
const request = props.data.tryIt?.request
|
|
34
|
+
const pathValues = reactive(Object.fromEntries((request?.pathParams ?? []).map(item => [item.name, item.value])))
|
|
35
|
+
const queryValues = reactive(Object.fromEntries((request?.query ?? []).map(item => [item.name, item.value])))
|
|
36
|
+
const token = shallowRef('')
|
|
37
|
+
const body = shallowRef(request?.body === undefined ? '' : typeof request.body === 'string' ? request.body : JSON.stringify(request.body, null, 2))
|
|
38
|
+
|
|
39
|
+
function resolvedPath(): string {
|
|
40
|
+
let path = request?.path ?? ''
|
|
41
|
+
for (const [name, value] of Object.entries(pathValues)) {
|
|
42
|
+
path = path.replaceAll(`{${name}}`, encodeURIComponent(value))
|
|
43
|
+
}
|
|
44
|
+
return path
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function send(): Promise<void> {
|
|
48
|
+
const tryIt = props.data.tryIt
|
|
49
|
+
if (!tryIt || !request) {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
let parsed: unknown
|
|
53
|
+
if (request.body !== undefined && typeof request.body !== 'string') {
|
|
54
|
+
try {
|
|
55
|
+
parsed = body.value.trim() ? JSON.parse(body.value) : undefined
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
output.value = { status: '400', body: props.data.text.invalidJson, error: true }
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (mode.value === 'mock') {
|
|
63
|
+
const errors = request.body !== undefined && typeof request.body !== 'string'
|
|
64
|
+
? validate(parsed, tryIt.rule as Rule | undefined, tryIt.messages)
|
|
65
|
+
: []
|
|
66
|
+
output.value = errors.length
|
|
67
|
+
? { status: `400 · ${props.data.text.mock}`, body: `${props.data.text.validationFailed}\n${errors.map(error => `- ${error}`).join('\n')}`, error: true }
|
|
68
|
+
: { status: `${tryIt.response?.status ?? '200'} · ${props.data.text.mock}`, body: tryIt.response?.body === undefined ? '' : JSON.stringify(tryIt.response.body, null, 2), error: false }
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
sending.value = true
|
|
72
|
+
try {
|
|
73
|
+
const search = new URLSearchParams(Object.entries(queryValues).filter(([, value]) => value !== ''))
|
|
74
|
+
const headers: Record<string, string> = {}
|
|
75
|
+
if (request.contentType) {
|
|
76
|
+
headers['Content-Type'] = request.contentType
|
|
77
|
+
}
|
|
78
|
+
if (request.auth && token.value) {
|
|
79
|
+
if (request.auth.kind === 'bearer') {
|
|
80
|
+
headers.Authorization = `Bearer ${token.value}`
|
|
81
|
+
}
|
|
82
|
+
else if (request.auth.kind === 'basic') {
|
|
83
|
+
headers.Authorization = `Basic ${token.value}`
|
|
84
|
+
}
|
|
85
|
+
else if (request.auth.kind === 'header') {
|
|
86
|
+
headers[request.auth.name] = token.value
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
search.set(request.auth.name, token.value)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const query = search.toString()
|
|
93
|
+
const response = await fetch(`${tryIt.server}${resolvedPath()}${query ? `?${query}` : ''}`, {
|
|
94
|
+
method: request.method,
|
|
95
|
+
headers,
|
|
96
|
+
...(request.body !== undefined ? { body: body.value } : {}),
|
|
97
|
+
})
|
|
98
|
+
const text = await response.text()
|
|
99
|
+
let pretty = text
|
|
100
|
+
try {
|
|
101
|
+
pretty = JSON.stringify(JSON.parse(text), null, 2)
|
|
102
|
+
}
|
|
103
|
+
catch {}
|
|
104
|
+
output.value = { status: `${response.status} ${response.statusText}`.trim(), body: pretty, error: !response.ok }
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
output.value = { status: 'Network error', body: String((error as Error).message ?? error), error: true }
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
sending.value = false
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
</script>
|
|
114
|
+
|
|
115
|
+
<template>
|
|
116
|
+
<div v-if="isSsgMarkdown" data-kawa-ssg-md-raw>
|
|
117
|
+
{{ `\n\n${data.markdown}\n\n` }}
|
|
118
|
+
</div>
|
|
119
|
+
<div v-else class="kawa-openapi-endpoint">
|
|
120
|
+
<div class="kawa-openapi-endpoint__bar">
|
|
121
|
+
<span :class="methodClass">{{ data.method }}</span>
|
|
122
|
+
<code class="kawa-openapi-endpoint__path">{{ data.path }}</code>
|
|
123
|
+
<span class="kawa-openapi-endpoint__spacer" />
|
|
124
|
+
<button v-if="data.curl" type="button" class="kawa-openapi-button" @click="copyCurl">
|
|
125
|
+
{{ copyState === 'copied' ? data.text.copied : copyState === 'failed' ? data.text.copyFailed : data.text.copyCurl }}
|
|
126
|
+
</button>
|
|
127
|
+
<button v-if="data.tryIt" type="button" class="kawa-openapi-button kawa-openapi-button--primary" :aria-expanded="open" @click="open = !open">
|
|
128
|
+
{{ data.text.tryIt }}
|
|
129
|
+
</button>
|
|
130
|
+
</div>
|
|
131
|
+
<div v-if="data.deprecated || data.text.since || data.security.length" class="kawa-openapi-endpoint__meta">
|
|
132
|
+
<span v-if="data.deprecated" class="kawa-openapi-badge kawa-openapi-badge--deprecated">{{ data.text.deprecated }}</span>
|
|
133
|
+
<span v-if="data.text.since" class="kawa-openapi-badge kawa-openapi-badge--new">{{ data.text.since }}</span>
|
|
134
|
+
<span v-if="data.security.length">{{ data.text.auth }}: <code v-for="name in data.security" :key="name">{{ name }}</code></span>
|
|
135
|
+
</div>
|
|
136
|
+
<div v-if="open && data.tryIt && request" class="kawa-openapi-try">
|
|
137
|
+
<div class="kawa-openapi-try__head">
|
|
138
|
+
<strong>{{ data.text.tryIt }}</strong>
|
|
139
|
+
<div v-if="data.tryIt.server" class="kawa-openapi-segmented" role="group">
|
|
140
|
+
<button type="button" :aria-pressed="mode === 'mock'" @click="mode = 'mock'">
|
|
141
|
+
{{ data.text.mock }}
|
|
142
|
+
</button>
|
|
143
|
+
<button type="button" :aria-pressed="mode === 'server'" @click="mode = 'server'">
|
|
144
|
+
{{ data.text.server }}
|
|
145
|
+
</button>
|
|
146
|
+
</div>
|
|
147
|
+
</div>
|
|
148
|
+
<p class="kawa-openapi-try__note">
|
|
149
|
+
{{ mode === 'mock' ? data.text.mockNote : data.text.serverNote }}
|
|
150
|
+
</p>
|
|
151
|
+
<div v-if="request.pathParams.length || request.query.length || (request.auth && mode === 'server')" class="kawa-openapi-try__inputs">
|
|
152
|
+
<label v-for="item in request.pathParams" :key="`path-${item.name}`">
|
|
153
|
+
<span>{{ item.name }}</span>
|
|
154
|
+
<input v-model="pathValues[item.name]" type="text" spellcheck="false">
|
|
155
|
+
</label>
|
|
156
|
+
<label v-for="item in request.query" :key="`query-${item.name}`">
|
|
157
|
+
<span>?{{ item.name }}</span>
|
|
158
|
+
<input v-model="queryValues[item.name]" type="text" spellcheck="false">
|
|
159
|
+
</label>
|
|
160
|
+
<label v-if="request.auth && mode === 'server'">
|
|
161
|
+
<span>{{ data.text.token }}</span>
|
|
162
|
+
<input v-model="token" type="password" autocomplete="off" spellcheck="false">
|
|
163
|
+
</label>
|
|
164
|
+
</div>
|
|
165
|
+
<label v-if="request.body !== undefined" class="kawa-openapi-try__body">
|
|
166
|
+
<span>{{ data.text.requestBody }}</span>
|
|
167
|
+
<textarea v-model="body" spellcheck="false" rows="10" />
|
|
168
|
+
</label>
|
|
169
|
+
<div class="kawa-openapi-try__actions">
|
|
170
|
+
<button type="button" class="kawa-openapi-button kawa-openapi-button--primary" :disabled="sending" @click="send">
|
|
171
|
+
{{ sending ? data.text.sending : data.text.send }}
|
|
172
|
+
</button>
|
|
173
|
+
<code class="kawa-openapi-try__target">{{ request.method }} {{ resolvedPath() }}</code>
|
|
174
|
+
</div>
|
|
175
|
+
<div class="kawa-openapi-try__output" :class="{ 'is-error': output?.error }" aria-live="polite">
|
|
176
|
+
<template v-if="output">
|
|
177
|
+
<div class="kawa-openapi-try__status">
|
|
178
|
+
{{ output.status }}
|
|
179
|
+
</div>
|
|
180
|
+
<pre v-if="output.body">{{ output.body }}</pre>
|
|
181
|
+
</template>
|
|
182
|
+
<span v-else class="kawa-openapi-try__empty">{{ data.text.notSent }}</span>
|
|
183
|
+
</div>
|
|
184
|
+
</div>
|
|
185
|
+
</div>
|
|
186
|
+
</template>
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { FieldRow } from '../data/body'
|
|
3
|
+
|
|
4
|
+
const props = defineProps<{
|
|
5
|
+
rows: FieldRow[]
|
|
6
|
+
text: { required: string, deprecated: string, since: string }
|
|
7
|
+
}>()
|
|
8
|
+
|
|
9
|
+
function since(version: string): string {
|
|
10
|
+
return props.text.since.replace('{n}', version)
|
|
11
|
+
}
|
|
12
|
+
</script>
|
|
13
|
+
|
|
14
|
+
<template>
|
|
15
|
+
<div class="kawa-openapi-fields">
|
|
16
|
+
<div
|
|
17
|
+
v-for="row in rows"
|
|
18
|
+
:key="row.path"
|
|
19
|
+
class="kawa-openapi-fields__row"
|
|
20
|
+
:class="{ 'is-deprecated': row.deprecated, 'is-nested': row.depth > 0 }"
|
|
21
|
+
:style="row.depth > 0 ? { marginInlineStart: `${row.depth * 0.75}rem` } : undefined"
|
|
22
|
+
>
|
|
23
|
+
<div class="kawa-openapi-fields__head">
|
|
24
|
+
<span class="kawa-openapi-fields__name">{{ row.path }}</span>
|
|
25
|
+
<a v-if="row.typeHref" class="kawa-openapi-fields__type" :href="row.typeHref">{{ row.type }}</a>
|
|
26
|
+
<span v-else class="kawa-openapi-fields__type">{{ row.type }}</span>
|
|
27
|
+
<span v-if="row.required" class="kawa-openapi-fields__required">{{ text.required }}</span>
|
|
28
|
+
<span v-for="constraint in row.constraints" :key="constraint" class="kawa-openapi-fields__constraint">{{ constraint }}</span>
|
|
29
|
+
<span v-if="row.since" class="kawa-openapi-badge kawa-openapi-badge--new">{{ since(row.since) }}</span>
|
|
30
|
+
<span v-if="row.deprecated" class="kawa-openapi-badge kawa-openapi-badge--deprecated">{{ text.deprecated }}</span>
|
|
31
|
+
</div>
|
|
32
|
+
<div v-if="row.descriptionHtml" class="kawa-openapi-fields__description" v-html="row.descriptionHtml" />
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
</template>
|