@specbook/codegen 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.md +6 -2
- package/package.json +54 -3
- package/src/browser.ts +98 -0
- package/src/build.ts +616 -0
- package/src/generate.ts +27 -0
- package/src/index.ts +10 -0
- package/src/js-yaml.d.ts +3 -0
- package/src/schema.ts +573 -0
- package/src/write.ts +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ajai Shankar
|
|
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.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @specbook/codegen
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Turns an OpenAPI or Swagger document into types, examples, schemas and metadata, in Node or in a browser.
|
|
4
|
+
|
|
5
|
+
Part of [Specbook](https://github.com/specbook-dev/specbook), Storybook for APIs. See the repository for documentation.
|
|
6
|
+
|
|
7
|
+
MIT licensed.
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@specbook/codegen",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Turns an OpenAPI or Swagger document into types, examples, schemas and metadata, in Node or in a browser.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"specbook",
|
|
7
|
+
"openapi",
|
|
8
|
+
"swagger",
|
|
9
|
+
"codegen",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"homepage": "https://ui.specbook.dev",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/specbook-dev/specbook/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/specbook-dev/specbook.git",
|
|
20
|
+
"directory": "packages/codegen"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./src/index.ts",
|
|
30
|
+
"default": "./src/index.ts"
|
|
31
|
+
},
|
|
32
|
+
"./browser": {
|
|
33
|
+
"types": "./src/browser.ts",
|
|
34
|
+
"default": "./src/browser.ts"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@apidevtools/swagger-parser": "10.1.1",
|
|
39
|
+
"js-yaml": "4.3.2",
|
|
40
|
+
"swagger2openapi": "7.0.8"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@specbook/core": "0.1.0",
|
|
44
|
+
"@types/node": "24.19.2",
|
|
45
|
+
"@types/swagger2openapi": "7.0.4",
|
|
46
|
+
"typescript": "7.0.2"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"src",
|
|
50
|
+
"!**/*.test.*"
|
|
51
|
+
],
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsc --noEmit",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
|
+
"test": "vitest run"
|
|
56
|
+
}
|
|
6
57
|
}
|
package/src/browser.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The generator for a browser or a worker: no file system, no Node-only parser. Hand it the text of an
|
|
3
|
+
* OpenAPI 3 or Swagger 2 document and get the same data files, manifest and operation metadata the CLI
|
|
4
|
+
* writes, without the TypeScript sources. Remote `$ref`s are never fetched.
|
|
5
|
+
*/
|
|
6
|
+
/// <reference path="./js-yaml.d.ts" />
|
|
7
|
+
import { load as parseYaml } from 'js-yaml'
|
|
8
|
+
import swagger2openapi from 'swagger2openapi'
|
|
9
|
+
import { type GenerateFlags, type GenerateOptions, generateFromDoc } from './build'
|
|
10
|
+
|
|
11
|
+
export type {
|
|
12
|
+
GeneratedFile,
|
|
13
|
+
GenerateFlags,
|
|
14
|
+
GenerateOptions,
|
|
15
|
+
ManifestOperation,
|
|
16
|
+
OperationMeta,
|
|
17
|
+
} from './build'
|
|
18
|
+
export { generateFromDoc, logoOf } from './build'
|
|
19
|
+
|
|
20
|
+
/** A document's text as an object: JSON first (fast), then YAML. */
|
|
21
|
+
export function parseSpecText(text: string): any {
|
|
22
|
+
const t = text.replace(/^/, '').trimStart()
|
|
23
|
+
if (t.startsWith('{')) {
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(t)
|
|
26
|
+
} catch {
|
|
27
|
+
/* fall through: it may be YAML flow style, or broken JSON the YAML parser reports better */
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const doc = parseYaml(t)
|
|
31
|
+
if (doc === null || typeof doc !== 'object')
|
|
32
|
+
throw new Error('The file is not an OpenAPI document')
|
|
33
|
+
return doc
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** `$ref`s that point outside the document: listed, not fetched. */
|
|
37
|
+
export function externalRefs(doc: unknown): string[] {
|
|
38
|
+
const out = new Set<string>()
|
|
39
|
+
const walk = (v: unknown, depth: number) => {
|
|
40
|
+
if (depth > 60 || v === null || typeof v !== 'object') return
|
|
41
|
+
if (Array.isArray(v)) {
|
|
42
|
+
for (const x of v) walk(x, depth + 1)
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
for (const [k, x] of Object.entries(v)) {
|
|
46
|
+
if (k === '$ref' && typeof x === 'string' && !x.startsWith('#')) out.add(x)
|
|
47
|
+
else walk(x, depth + 1)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
walk(doc, 0)
|
|
51
|
+
return [...out]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface OpenedSpec {
|
|
55
|
+
/** OpenAPI 3 document (a Swagger 2 input has been converted). */
|
|
56
|
+
doc: any
|
|
57
|
+
/** the input was Swagger 2 */
|
|
58
|
+
converted: boolean
|
|
59
|
+
warnings: string[]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Parse, and convert Swagger 2 to OpenAPI 3. Throws a readable error for anything that is not a spec. */
|
|
63
|
+
export async function openSpec(text: string): Promise<OpenedSpec> {
|
|
64
|
+
const parsed = parseSpecText(text)
|
|
65
|
+
const warnings: string[] = []
|
|
66
|
+
let doc = parsed
|
|
67
|
+
let converted = false
|
|
68
|
+
if (typeof parsed.swagger === 'string' && parsed.swagger.startsWith('2')) {
|
|
69
|
+
const res = await swagger2openapi.convertObj(parsed, {
|
|
70
|
+
patch: true,
|
|
71
|
+
warnOnly: true,
|
|
72
|
+
resolve: false,
|
|
73
|
+
direct: true,
|
|
74
|
+
} as never)
|
|
75
|
+
doc = typeof (res as any).openapi === 'object' ? (res as any).openapi : res
|
|
76
|
+
converted = true
|
|
77
|
+
} else if (typeof parsed.openapi !== 'string' || !parsed.openapi.startsWith('3')) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
'This is not an OpenAPI 3 or Swagger 2 document (no "openapi" or "swagger" field)',
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
if (!doc.paths || Object.keys(doc.paths).length === 0) warnings.push('The document has no paths.')
|
|
83
|
+
const ext = externalRefs(doc)
|
|
84
|
+
if (ext.length)
|
|
85
|
+
warnings.push(
|
|
86
|
+
`${ext.length} reference${ext.length > 1 ? 's' : ''} to other files were not loaded (for example ${ext[0]}).`,
|
|
87
|
+
)
|
|
88
|
+
return { doc, converted, warnings }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The data a UI reads for one spec: no TypeScript sources, and nothing written anywhere. */
|
|
92
|
+
export function generateData(
|
|
93
|
+
doc: any,
|
|
94
|
+
opts: Omit<GenerateOptions, 'spec'>,
|
|
95
|
+
flags: GenerateFlags = {},
|
|
96
|
+
) {
|
|
97
|
+
return generateFromDoc(doc, opts, { ...flags, dataOnly: true })
|
|
98
|
+
}
|