Skip to content

Commit 50b67e4

Browse files
committed
feat: update pnpm lock and workspace configurations; rename operatePipelineGenerator to generate in CLI and generator modules; enhance JSON source reading in pipeline
1 parent a9e105d commit 50b67e4

10 files changed

Lines changed: 53 additions & 16 deletions

File tree

docs/.config/docs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ url: 'https://genapi.hairyf.com'
88
lang: en
99
themeColor: yellow
1010
landing:
11-
heroCode: npx genapi init
1211
contributors: true
1312
features:
1413
- title: Multiple HTTP Clients
0 Bytes
Binary file not shown.

docs/.docs/components/CodeHighlighter.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div
33
v-if="highlighted"
44
ref="highlightRef"
5-
class="p-4 font-mono text-sm leading-relaxed pointer-events-none [&_pre]:!p-3 [&_pre]:!m-0 [&_pre]:!rounded [&_pre]:!bg-transparent [&_pre]:!leading-3"
5+
class="p-4 font-mono text-sm leading-relaxed pointer-events-none [&_pre]:!p-3 [&_pre]:!m-0 [&_pre]:!rounded [&_pre]:!bg-transparent [&_pre]:!leading-3 [&_pre]:!whitespace-pre-wrap [&_pre]:break-words"
66
v-html="highlighted"
77
/>
88
<div

packages/core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Create `genapi.config.ts` or `genapi.config.js` in your project root, then run t
2525

2626
## Core
2727

28-
### `operatePipelineGenerator(config)`
28+
### `generate(config)`
2929

3030
Runs the GenAPI pipeline for one or more configs. Resolves pipeline by name (e.g. `swag-axios-ts`), then runs config → original → parser → compiler → generate → dest.
3131

packages/core/src/cli/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import process from 'node:process'
33
import { merge } from '@hairy/utils'
44
import { loadConfig } from 'c12'
55
import { defineCommand, runMain } from 'citty'
6-
import { operatePipelineGenerator } from '../generator'
6+
import { generate } from '../generator'
77
import { initCommand } from './init'
88
import * as parser from './parser'
99

@@ -19,7 +19,7 @@ const main = defineCommand({
1919
description: 'The compilation preset used supports npm package (add the prefix genapi -) | local path',
2020
},
2121
input: {
22-
type: 'string',
22+
type: 'positional',
2323
description: 'The incoming string resolves to a uri or json path.',
2424
},
2525
outfile: {
@@ -42,6 +42,7 @@ const main = defineCommand({
4242
if (isInitCommand) {
4343
return
4444
}
45+
4546
const options = {
4647
preset: args.preset,
4748
input: args.input,
@@ -65,7 +66,7 @@ const main = defineCommand({
6566
}
6667

6768
const servers = parser.servers(config)
68-
await operatePipelineGenerator(servers)
69+
await generate(servers)
6970
},
7071
})
7172

packages/core/src/generator/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { inPipeline } from '../internal'
66
/**
77
* Runs the GenAPI pipeline for one or more configs.
88
* Resolves pipeline by name (e.g. `swag-axios-ts`), then runs config → original → parser → compiler → generate → dest.
9-
*
9+
*·
1010
* @param config - Single config or array of configs (e.g. from `servers`)
1111
* @group Core
1212
*/
13-
export async function operatePipelineGenerator(config: ApiPipeline.Config | ApiPipeline.Config[]) {
13+
export async function generate(config: ApiPipeline.Config | ApiPipeline.Config[]) {
1414
const configs: ApiPipeline.Config[] = Array.isArray(config) ? config : [config]
1515
const s = spinner()
1616
s.start('Generate API File...')

packages/pipeline/src/original/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ export async function original(configRead: ApiPipeline.ConfigRead) {
6666
return configRead
6767
}
6868

69-
function readJsonSource(json: string | Record<string, any>) {
69+
function readJsonSource(json: string | Record<string, any>): Promise<Record<string, any> | undefined> {
7070
if (!json)
71-
return
71+
return Promise.resolve(undefined)
7272
if (typeof json === 'object')
73-
return json
74-
else
75-
return import(json).then(mod => mod.default)
73+
return Promise.resolve(json)
74+
const trimmed = String(json).trim()
75+
// 内联 JSON 字符串(例如 API 传入的 spec 内容)与文件路径区分:以 { 或 [ 开头则解析为 JSON
76+
if (trimmed.startsWith('{') || trimmed.startsWith('['))
77+
return Promise.resolve(JSON.parse(json as string) as Record<string, any>)
78+
return import(json as string).then(mod => mod.default)
7679
}

packages/pipeline/test/original/index.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,33 @@ describe('original', () => {
217217
expect(ofetch).not.toHaveBeenCalled()
218218
})
219219

220+
it('reads source from inline json string input', async () => {
221+
const jsonSource = {
222+
swagger: '2.0',
223+
info: { title: 'Example API', version: '1.0.0' },
224+
paths: {},
225+
}
226+
const configRead: ApiPipeline.ConfigRead = {
227+
config: { input: '' } as ApiPipeline.Config,
228+
inputs: { json: JSON.stringify(jsonSource) },
229+
outputs: [],
230+
graphs: {
231+
comments: [],
232+
functions: [],
233+
imports: [],
234+
interfaces: [],
235+
typings: [],
236+
variables: [],
237+
response: {},
238+
},
239+
}
240+
241+
const result = await original(configRead)
242+
243+
expect(result.source).toEqual(jsonSource)
244+
expect(ofetch).not.toHaveBeenCalled()
245+
})
246+
220247
it('reads source from json string path input', async () => {
221248
// This test is skipped because dynamic import() requires actual file system
222249
// and cannot be easily mocked in the test environment

pnpm-lock.yaml

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ onlyBuiltDependencies:
7070
- esbuild
7171
- simple-git-hooks
7272
- vue-demi
73+
packageExtensions:
74+
undocs:
75+
dependencies:
76+
ufo: '*'

0 commit comments

Comments
 (0)