@redocly/cli 2.47.0 → 2.48.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/lib/chunks/{TEQA5OM7.js → 2GJPOM5H.js} +1 -1
- package/lib/chunks/{W2NJCZOS.js → 3CR6IHZV.js} +1 -1
- package/lib/chunks/3JJEI7CJ.js +456 -0
- package/lib/chunks/{QRD3XQNU.js → 3M6ARSHY.js} +1 -1
- package/lib/chunks/3X52NOC5.js +7 -0
- package/lib/chunks/46OWEERC.js +19 -0
- package/lib/chunks/4IA3ZTGK.js +20 -0
- package/lib/chunks/{TO2KJ36N.js → 5E3L4HJN.js} +1 -1
- package/lib/chunks/5EG7SNNU.js +177 -0
- package/lib/chunks/5L3FH5WQ.js +16 -0
- package/lib/chunks/5L5WOOWA.js +7 -0
- package/lib/chunks/5ZEWVWOC.js +14 -0
- package/lib/chunks/6O3ZHZZ2.js +7 -0
- package/lib/chunks/{KEUH3PST.js → AJZAID3R.js} +1 -1
- package/lib/chunks/BD5JG7YZ.js +7 -0
- package/lib/chunks/{B6HS7CVY.js → BEOYUN4L.js} +48 -48
- package/lib/chunks/CF5YCHNA.js +7 -0
- package/lib/chunks/D27SC2KR.js +90 -0
- package/lib/chunks/{SN2EC2ZR.js → DYAVWTVD.js} +1 -1
- package/lib/chunks/E35Q6YJN.js +7 -0
- package/lib/chunks/EB2V4VMP.js +7 -0
- package/lib/chunks/{QVV2EKMT.js → FL3CA53Z.js} +1 -1
- package/lib/chunks/FVBSRIWC.js +18 -0
- package/lib/chunks/G2XR6TRU.js +67 -0
- package/lib/chunks/G3YJARF7.js +57 -0
- package/lib/chunks/GA6WYGKY.js +7 -0
- package/lib/chunks/GBRES7WG.js +5400 -0
- package/lib/chunks/GHPAE4DZ.js +20 -0
- package/lib/chunks/{SL6AH3N2.js → GJRJZYC4.js} +1 -1
- package/lib/chunks/{REBHK2JR.js → GOJNDGZT.js} +1 -1
- package/lib/chunks/HJYFWIVE.js +9 -0
- package/lib/chunks/HLZCPNF7.js +16 -0
- package/lib/chunks/KIQI3UBS.js +177 -0
- package/lib/chunks/KNPQJVYA.js +14 -0
- package/lib/chunks/L3TZNNJJ.js +13 -0
- package/lib/chunks/{34J2ZJDK.js → LUC2DWUK.js} +1 -1
- package/lib/chunks/MBHFDTYI.js +78 -0
- package/lib/chunks/NB54XRRU.js +10 -0
- package/lib/chunks/NSIQWNYL.js +7 -0
- package/lib/chunks/NWMTC4AN.js +7 -0
- package/lib/chunks/PFYPTG4G.js +7 -0
- package/lib/chunks/{5NR4DJW4.js → PVM7EVWV.js} +1 -1
- package/lib/chunks/Q5FNL2RM.js +22 -0
- package/lib/chunks/QW5MTOU2.js +22 -0
- package/lib/chunks/REFX2EKS.js +13 -0
- package/lib/chunks/S4GQKQ5G.js +7 -0
- package/lib/chunks/SGBB4RNM.js +11 -0
- package/lib/chunks/{YQW463Z7.js → SPPRUUK3.js} +1 -1
- package/lib/chunks/SQVG3CEM.js +8 -0
- package/lib/chunks/TSB6PODJ.js +13 -0
- package/lib/chunks/U2A6GEAN.js +42 -0
- package/lib/chunks/UDBINQ2S.js +42 -0
- package/lib/chunks/UKSQ7B5H.js +2268 -0
- package/lib/chunks/{QL4V2A4A.js → V3JMSQFP.js} +1 -1
- package/lib/chunks/VWSPU27A.js +30 -0
- package/lib/chunks/W62VKXGM.js +26 -0
- package/lib/chunks/WIZYCYZG.js +7 -0
- package/lib/chunks/XDK4JK3V.js +9 -0
- package/lib/chunks/{5PXRDPHV.js → YJGI7CZ2.js} +1 -1
- package/lib/chunks/YQS3OX36.js +11 -0
- package/lib/chunks/{S4MX7JPH.js → Z2AXUXMK.js} +1 -1
- package/lib/chunks/ZBL4IVEW.js +11 -0
- package/lib/chunks/ZGMTMVBB.js +19 -0
- package/lib/chunks/{RIFZSMUA.js → ZVUMPMHU.js} +1 -1
- package/lib/eject-assets/AGENTS.md +137 -0
- package/lib/eject-assets/generators/cli/docs.ts +224 -0
- package/lib/eject-assets/generators/cli/engine-source.ts +21 -0
- package/lib/eject-assets/generators/cli/index.ts +90 -0
- package/lib/eject-assets/generators/cli/render.ts +348 -0
- package/lib/eject-assets/generators/go/client.ts +57 -0
- package/lib/eject-assets/generators/go/descriptor.ts +41 -0
- package/lib/eject-assets/generators/go/index.ts +336 -0
- package/lib/eject-assets/generators/go/models.ts +179 -0
- package/lib/eject-assets/generators/go/naming.ts +49 -0
- package/lib/eject-assets/generators/go/operations.ts +296 -0
- package/lib/eject-assets/generators/go/pagination.ts +194 -0
- package/lib/eject-assets/generators/go/types.ts +60 -0
- package/lib/eject-assets/generators/mock/faker.ts +214 -0
- package/lib/eject-assets/generators/mock/index.ts +35 -0
- package/lib/eject-assets/generators/mock/render.ts +282 -0
- package/lib/eject-assets/generators/mock/sample.ts +316 -0
- package/lib/eject-assets/generators/mock/values.ts +63 -0
- package/lib/eject-assets/generators/php/client.ts +63 -0
- package/lib/eject-assets/generators/php/descriptor.ts +58 -0
- package/lib/eject-assets/generators/php/index.ts +237 -0
- package/lib/eject-assets/generators/php/models.ts +275 -0
- package/lib/eject-assets/generators/php/naming.ts +51 -0
- package/lib/eject-assets/generators/php/operations.ts +232 -0
- package/lib/eject-assets/generators/php/pagination.ts +133 -0
- package/lib/eject-assets/generators/php/types.ts +148 -0
- package/lib/eject-assets/generators/python/client.ts +123 -0
- package/lib/eject-assets/generators/python/descriptor.ts +52 -0
- package/lib/eject-assets/generators/python/index.ts +251 -0
- package/lib/eject-assets/generators/python/models.ts +242 -0
- package/lib/eject-assets/generators/python/naming.ts +46 -0
- package/lib/eject-assets/generators/python/operations.ts +147 -0
- package/lib/eject-assets/generators/python/pagination.ts +128 -0
- package/lib/eject-assets/generators/python/types.ts +54 -0
- package/lib/eject-assets/generators/swr/index.ts +37 -0
- package/lib/eject-assets/generators/swr/render.ts +78 -0
- package/lib/eject-assets/generators/tanstack-query/index.ts +48 -0
- package/lib/eject-assets/generators/tanstack-query/render.ts +346 -0
- package/lib/eject-assets/generators/transformers/index.ts +46 -0
- package/lib/eject-assets/generators/transformers/render.ts +506 -0
- package/lib/eject-assets/generators/typescript/banner.ts +35 -0
- package/lib/eject-assets/generators/typescript/client.ts +254 -0
- package/lib/eject-assets/generators/typescript/descriptor.ts +137 -0
- package/lib/eject-assets/generators/typescript/index.ts +101 -0
- package/lib/eject-assets/generators/typescript/inline-runtime.ts +135 -0
- package/lib/eject-assets/generators/typescript/operation-signature.ts +62 -0
- package/lib/eject-assets/generators/typescript/operation-types.ts +17 -0
- package/lib/eject-assets/generators/typescript/operations.ts +524 -0
- package/lib/eject-assets/generators/typescript/response-headers.ts +74 -0
- package/lib/eject-assets/generators/typescript/type-guards.ts +159 -0
- package/lib/eject-assets/generators/typescript/types.ts +172 -0
- package/lib/eject-assets/generators/zod/index.ts +32 -0
- package/lib/eject-assets/generators/zod/schemas.ts +456 -0
- package/lib/eject-assets/skills/cli-generator/SKILL.md +121 -0
- package/lib/eject-assets/skills/client-generators/SKILL.md +142 -0
- package/lib/eject-assets/skills/go-generator/SKILL.md +96 -0
- package/lib/eject-assets/skills/mock-generator/SKILL.md +50 -0
- package/lib/eject-assets/skills/php-generator/SKILL.md +112 -0
- package/lib/eject-assets/skills/python-generator/SKILL.md +110 -0
- package/lib/eject-assets/skills/swr-generator/SKILL.md +50 -0
- package/lib/eject-assets/skills/tanstack-query-generator/SKILL.md +55 -0
- package/lib/eject-assets/skills/transformers-generator/SKILL.md +47 -0
- package/lib/eject-assets/skills/typescript-generator/SKILL.md +95 -0
- package/lib/eject-assets/skills/zod-generator/SKILL.md +54 -0
- package/lib/index.js +272 -193
- package/package.json +1 -1
- package/lib/chunks/PIJLRHNN.js +0 -8
- package/lib/chunks/Y5HLCNL5.js +0 -2542
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# Writing custom client generators
|
|
2
|
+
|
|
3
|
+
A generator is a plain module: `(input) => GeneratedFile[]`. It receives the
|
|
4
|
+
language-agnostic API model and returns files — in ANY output language. It runs
|
|
5
|
+
in the same pass as the built-ins; select it by path in `redocly.yaml`:
|
|
6
|
+
|
|
7
|
+
```yaml
|
|
8
|
+
client:
|
|
9
|
+
generators: [typescript, ./generators/my-generator.mjs]
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## The contract
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
/** @type {import('@redocly/client-generator').CustomGenerator} */
|
|
16
|
+
export default {
|
|
17
|
+
name: 'my-generator',
|
|
18
|
+
run({ model, output, outputMode, emit }) {
|
|
19
|
+
return [{ path: output.path.replace(/\.ts$/, '.mine.txt'), content: '…' }];
|
|
20
|
+
},
|
|
21
|
+
// Optional: one idiomatic call snippet per operation for docs (x-codeSamples),
|
|
22
|
+
// collected into an overlay file when `client.codeSamples: true` is set.
|
|
23
|
+
sample(operation, { model, emit }) {
|
|
24
|
+
return { lang: 'python', source: '…' };
|
|
25
|
+
},
|
|
26
|
+
// Optional: the reference page for what `run` emits, written when `client.docs` (or
|
|
27
|
+
// --docs) is on. Same `{ path, content }` shape as `run`; `renderReferencePage` gives
|
|
28
|
+
// the standard layout and takes `sample` for its snippets. A generator documents itself.
|
|
29
|
+
docs({ model, output, emit }) {
|
|
30
|
+
return [{ path: output.path.replace(/\.ts$/, '.mine.md'), content: '…' }];
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Declaring options
|
|
36
|
+
|
|
37
|
+
A generator that needs configuration declares it as a schema; `run` then receives
|
|
38
|
+
`options` already validated, with defaults applied:
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
export default {
|
|
42
|
+
name: 'permissions-matrix',
|
|
43
|
+
options: {
|
|
44
|
+
type: 'object',
|
|
45
|
+
properties: { groupBy: { enum: ['tag', 'path'], default: 'tag' } },
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
},
|
|
48
|
+
run({ model, output, options }) {
|
|
49
|
+
return [
|
|
50
|
+
{ path: output.path.replace(/\.ts$/, '.permissions.md'), content: render(options.groupBy) },
|
|
51
|
+
];
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Users set them per generator name:
|
|
57
|
+
|
|
58
|
+
```yaml
|
|
59
|
+
client:
|
|
60
|
+
generators: [typescript, ./generators/permissions-matrix.mjs]
|
|
61
|
+
options:
|
|
62
|
+
permissions-matrix:
|
|
63
|
+
groupBy: path
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The supported subset is a top-level `type: 'object'` with `properties`, `required`, and
|
|
67
|
+
`additionalProperties`; each property is a scalar (`string`/`number`/`boolean`), an
|
|
68
|
+
`enum`, or an array of scalars, and may carry a `default` and a `description`. Don't
|
|
69
|
+
validate options inside `run` — an unknown key, a wrong type, a value outside an `enum`,
|
|
70
|
+
or a missing `required` key already fails generation before `run` is called.
|
|
71
|
+
|
|
72
|
+
Rules: output is deterministic (same description → same bytes); never add
|
|
73
|
+
dependencies to the generated client; **never hand-edit generated output** —
|
|
74
|
+
edit this generator and regenerate. Emitted file paths must stay inside the
|
|
75
|
+
`--output` directory (subdirectories are fine) — escapes are rejected.
|
|
76
|
+
Optionally declare `requiresGenerator` — the `@redocly/client-generator` version
|
|
77
|
+
range you wrote this against (`'^1.2.0'`, `'~1.2.0'`, `'>=1.2.0'`, or an exact
|
|
78
|
+
version). A CLI outside the range then fails with the fix path instead of feeding
|
|
79
|
+
your generator an unexpected model shape. Ejected generators carry it
|
|
80
|
+
automatically; hand-written ones without it are taken as current.
|
|
81
|
+
|
|
82
|
+
## The model (IR)
|
|
83
|
+
|
|
84
|
+
`model.services[].operations[]` — each operation carries `name`, `specName`,
|
|
85
|
+
`method`, `path`, `tags`, `pathParams`/`queryParams`/`headerParams`/`cookieParams`,
|
|
86
|
+
`requestBody`, `successResponses`/`errorResponses` (each with a `schema`), and
|
|
87
|
+
`security`. `model.schemas` holds the named schemas. Every schema is a
|
|
88
|
+
discriminated union on `kind`: `scalar`, `array`, `object`, `record`, `ref`,
|
|
89
|
+
`literal`, `enum`, `union` (optionally with a discriminator), `intersection`
|
|
90
|
+
(allOf), `null`, `unknown`, `omit`.
|
|
91
|
+
|
|
92
|
+
## Helpers (import from '@redocly/client-generator')
|
|
93
|
+
|
|
94
|
+
| Helper | Use |
|
|
95
|
+
| ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
96
|
+
| `flattenAllOf(schema, model)` | The merged property view of allOf compositions — languages without intersection types render this. |
|
|
97
|
+
| `deref(schema, model)` | Follow a `ref` chain to the schema it names (cycle-guarded). |
|
|
98
|
+
| `jsonSuccessSchema(op)` / `sseResponse(op)` | The primary JSON success schema; the `text/event-stream` response when the operation streams. |
|
|
99
|
+
| `isMultipartBody(op)` | Whether the request body is multipart. |
|
|
100
|
+
| `serverUrlParts(server)` | A server-URL template as literal/variable parts, ready for any concatenation syntax. |
|
|
101
|
+
| `securityRequirements(op, model)` | The operation's security as OR-alternatives of AND-sets, denormalized against the declared schemes. |
|
|
102
|
+
| `paginationItemSchema(pageSchema, itemsPointer, model)` | The raw element schema behind a pagination rule's `items` pointer — a `ref` element keeps its name. |
|
|
103
|
+
| `discriminatorCases(schema, model)` | `{ property, cases }` dispatch table for discriminated unions. |
|
|
104
|
+
| `isNullable(schema)` / `unwrapNullable(schema)` | Detect and strip `null` union members (`Optional[T]`, pointers, `Option<T>`). |
|
|
105
|
+
| `enumValues(schema)` | Values plus SCREAMING_SNAKE member-name suggestions. |
|
|
106
|
+
| `headerCoerceType(schema, model)` | Response-header coerce hint (`integer`/`number`/`boolean`/`string`) through refs, nullables, and allOf wrappers. |
|
|
107
|
+
| `casing` / `identifierFor(name, { style, reserved })` | camel/pascal/snake/screaming; keyword-safe identifiers (`RESERVED_WORDS.python/go/typescript` shipped). |
|
|
108
|
+
| `uniqueIdentifiers(names, { style, reserved, taken })` | The same, made unique among themselves and among names you already took — for a signature that takes one argument per parameter. |
|
|
109
|
+
| `Printer` | Indentation-aware text builder — no manual whitespace bookkeeping. |
|
|
110
|
+
| `docText(description)` | Description as trimmed lines for any comment syntax. |
|
|
111
|
+
| `schemaAtPointer(schema, pointer, model)` | Resolve an RFC 6901 JSON pointer over a schema (through refs and allOf) — e.g. a pagination `items` pointer to its element type. |
|
|
112
|
+
| `paginationRuleFor(op, config)` | The pagination rule that applies to an operation (per-op config > extension > fitting convention), normalized. |
|
|
113
|
+
| `renderReferencePage(model, options)` | The Markdown reference page a generator's `docs` hook returns — your `sample` hook supplies its call snippets. |
|
|
114
|
+
| `NotSupportedError` | Throw it to reject an option the generator can't honor — the CLI prints the message as a user error, not a crash. |
|
|
115
|
+
| `AUTHORING_HELPER_NAMES` | The list of the above (introspection). |
|
|
116
|
+
|
|
117
|
+
Worked example: the built-in `python` generator
|
|
118
|
+
(`packages/client-generator/src/generators/python/index.ts` in the Redocly CLI repo) is
|
|
119
|
+
authored with exactly this toolkit and nothing else — models via `flattenAllOf`/
|
|
120
|
+
`enumValues`/`discriminatorCases`, all code through `Printer`, every name through
|
|
121
|
+
`identifierFor(..., RESERVED_WORDS.python)`.
|
|
122
|
+
|
|
123
|
+
A generator that emits TypeScript may additionally use the source-text renderers from
|
|
124
|
+
`@redocly/client-generator/generate` — `tsType` (schema → type), `tsJsdoc`, `codeLiteral`,
|
|
125
|
+
`operationSignature`, `pascalCase`, `safeIdent`. There is no AST toolkit and no
|
|
126
|
+
`typescript` dependency: every generator prints text through `Printer`.
|
|
127
|
+
|
|
128
|
+
## The loop
|
|
129
|
+
|
|
130
|
+
1. Edit the generator.
|
|
131
|
+
2. Run `redocly generate-client`.
|
|
132
|
+
3. Inspect `git diff` of the generated output.
|
|
133
|
+
4. Repeat. Generated files are never hand-edited.
|
|
134
|
+
|
|
135
|
+
If you had to work around a **missing helper** or a wrong default, tell the user
|
|
136
|
+
and suggest filing an issue at https://github.com/Redocly/redocly-cli/issues —
|
|
137
|
+
include the generator snippet and the helper you expected to exist.
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// Ejected from @redocly/client-generator@0.4.0 — the built-in "cli" generator.
|
|
2
|
+
// This file is yours: edit freely; the generated client stays machine-owned and is
|
|
3
|
+
// rebuilt by `redocly generate-client`. Newer generator versions merge in with
|
|
4
|
+
// `redocly eject-generator cli --update`.
|
|
5
|
+
// The cli-docs emitter: renders the Markdown reference for the generated CLI from the
|
|
6
|
+
// SAME command table `runCli` dispatches on, and the same `groupSlug`/`constantCase` the
|
|
7
|
+
// runtime addresses groups and reads credentials with. A second model would drift from
|
|
8
|
+
// the tool the first time either side changed.
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
type CliCommand,
|
|
12
|
+
type CliFlag,
|
|
13
|
+
constantCase,
|
|
14
|
+
groupSlug,
|
|
15
|
+
Printer,
|
|
16
|
+
} from '@redocly/client-generator';
|
|
17
|
+
|
|
18
|
+
export type CliDocsOptions = {
|
|
19
|
+
/** Page heading. */
|
|
20
|
+
title: string;
|
|
21
|
+
/** Emit YAML front matter carrying the title, for docs sites that expect it. */
|
|
22
|
+
frontmatter: boolean;
|
|
23
|
+
/** The generated file's stem: what the page calls the command, and what its credential
|
|
24
|
+
* variables derive from. A reader who installs it under another bin name renames only
|
|
25
|
+
* the command — the variables are fixed at generation. */
|
|
26
|
+
name: string;
|
|
27
|
+
/** Auth schemes the description declares, in the order the CLI resolves them. */
|
|
28
|
+
schemes: Array<{ key: string; kind: 'bearer' | 'basic' | 'apiKey' }>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Table-cell-safe text: one line, and pipes/backslashes escaped so they don't alter columns/escaping. */
|
|
32
|
+
function cell(text: string | undefined): string {
|
|
33
|
+
return (text ?? '').replace(/\s+/g, ' ').trim().replace(/\\/g, '\\\\').replace(/\|/g, '\\|');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** How a command is typed at the prompt: `<group-slug> <name>`, or just `<name>`. */
|
|
37
|
+
function address(command: CliCommand): string {
|
|
38
|
+
return [command.group === undefined ? undefined : groupSlug(command.group), command.name]
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
.join(' ');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function usageLine(name: string, command: CliCommand): string {
|
|
44
|
+
const words = [
|
|
45
|
+
name,
|
|
46
|
+
address(command),
|
|
47
|
+
...command.positionals.map((positional) => `<${positional.name}>`),
|
|
48
|
+
...command.flags.filter((flag) => flag.required).map((flag) => `--${flag.name} <${flag.type}>`),
|
|
49
|
+
...(command.body ? [command.body.required ? "--json '<json>'" : "[--json '<json>']"] : []),
|
|
50
|
+
];
|
|
51
|
+
return words.filter((word) => word !== '').join(' ');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function writeFlagTable(printer: Printer, flags: CliFlag[]): void {
|
|
55
|
+
printer.line('| Flag | Type | Required | Description |');
|
|
56
|
+
printer.line('| ---- | ---- | -------- | ----------- |');
|
|
57
|
+
for (const flag of flags) {
|
|
58
|
+
const description = [
|
|
59
|
+
cell(flag.description),
|
|
60
|
+
flag.enum === undefined
|
|
61
|
+
? ''
|
|
62
|
+
: `One of ${flag.enum.map((value) => `\`${value}\``).join(', ')}.`,
|
|
63
|
+
flag.type === 'array' ? 'Repeat the flag for multiple values.' : '',
|
|
64
|
+
]
|
|
65
|
+
.filter((part) => part !== '')
|
|
66
|
+
.join(' ');
|
|
67
|
+
printer.line(
|
|
68
|
+
`| \`--${flag.name}\` | ${flag.type} | ${flag.required ? 'yes' : 'no'} | ${description} |`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
printer.blank();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function writeCommand(printer: Printer, command: CliCommand, options: CliDocsOptions): void {
|
|
75
|
+
printer.line(`### \`${address(command)}\``);
|
|
76
|
+
printer.blank();
|
|
77
|
+
if (command.summary !== undefined) {
|
|
78
|
+
printer.line(cell(command.summary));
|
|
79
|
+
printer.blank();
|
|
80
|
+
}
|
|
81
|
+
printer.line(`\`${command.method} ${command.path}\``);
|
|
82
|
+
printer.blank();
|
|
83
|
+
printer.line('```sh');
|
|
84
|
+
printer.line(usageLine(options.name, command));
|
|
85
|
+
printer.line('```');
|
|
86
|
+
printer.blank();
|
|
87
|
+
if (command.positionals.length > 0) {
|
|
88
|
+
printer.line('| Argument | Description |');
|
|
89
|
+
printer.line('| -------- | ----------- |');
|
|
90
|
+
for (const positional of command.positionals) {
|
|
91
|
+
printer.line(`| \`<${positional.name}>\` | ${cell(positional.description)} |`);
|
|
92
|
+
}
|
|
93
|
+
printer.blank();
|
|
94
|
+
}
|
|
95
|
+
if (command.flags.length > 0) writeFlagTable(printer, command.flags);
|
|
96
|
+
const notes = [
|
|
97
|
+
command.body === undefined
|
|
98
|
+
? ''
|
|
99
|
+
: `Takes a JSON body${command.body.required ? ' (required)' : ''}: \`--json '<json>'\`, \`--json @file.json\`, or \`--json @-\` for stdin.`,
|
|
100
|
+
command.unsupportedBody === undefined
|
|
101
|
+
? ''
|
|
102
|
+
: `Takes a \`${command.unsupportedBody}\` body, which the CLI cannot build — call this operation through the generated client instead.`,
|
|
103
|
+
command.paginated === true
|
|
104
|
+
? 'Paginated: `--page-all` follows every page, printing one JSON page per line.'
|
|
105
|
+
: '',
|
|
106
|
+
command.sse === true ? 'Streams server-sent events as one JSON object per line.' : '',
|
|
107
|
+
command.blob === true ? 'Returns binary content, so `--output <path>` is required.' : '',
|
|
108
|
+
].filter((note) => note !== '');
|
|
109
|
+
for (const note of notes) printer.line(note);
|
|
110
|
+
if (notes.length > 0) printer.blank();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The whole page: heading, global flags, credentials, exit codes, then every command. */
|
|
114
|
+
export function renderCliDocs(commands: CliCommand[], options: CliDocsOptions): string {
|
|
115
|
+
const printer = new Printer();
|
|
116
|
+
if (options.frontmatter) {
|
|
117
|
+
printer.line('---');
|
|
118
|
+
printer.line(`title: ${options.title}`);
|
|
119
|
+
printer.line('---');
|
|
120
|
+
printer.blank();
|
|
121
|
+
}
|
|
122
|
+
printer.line(`# ${options.title}`);
|
|
123
|
+
printer.blank();
|
|
124
|
+
printer.line(
|
|
125
|
+
`Generated command-line reference for \`${options.name}\`, produced from the API description by \`redocly generate-client\`.`
|
|
126
|
+
);
|
|
127
|
+
printer.line('Re-run generation to update it — this file is not hand-edited.');
|
|
128
|
+
printer.blank();
|
|
129
|
+
|
|
130
|
+
printer.line('## Usage');
|
|
131
|
+
printer.blank();
|
|
132
|
+
printer.line('```sh');
|
|
133
|
+
printer.line(`${options.name} <command> [flags]`);
|
|
134
|
+
printer.line(`${options.name} --help`);
|
|
135
|
+
printer.line(`${options.name} schema <command> # request/response schemas`);
|
|
136
|
+
printer.line('```');
|
|
137
|
+
printer.blank();
|
|
138
|
+
printer.line(
|
|
139
|
+
'Install the file under any `bin` name: the command takes that name, and the credential variables below do not change.'
|
|
140
|
+
);
|
|
141
|
+
printer.blank();
|
|
142
|
+
|
|
143
|
+
printer.line('## Global flags');
|
|
144
|
+
printer.blank();
|
|
145
|
+
printer.line('| Flag | Description |');
|
|
146
|
+
printer.line('| ---- | ----------- |');
|
|
147
|
+
// `--token` mirrors the CLI itself: without a bearer scheme the tool rejects the flag,
|
|
148
|
+
// so the reference must not list it.
|
|
149
|
+
const hasBearer = options.schemes.some((scheme) => scheme.kind === 'bearer');
|
|
150
|
+
for (const [flag, description] of [
|
|
151
|
+
['--server-url <url>', 'Override the server URL included in the client.'],
|
|
152
|
+
['--format <json\\|ndjson>', 'Output format.'],
|
|
153
|
+
['--dry-run', 'Print the prepared request, credentials redacted, without sending it.'],
|
|
154
|
+
['--page-all', 'Follow pagination, printing one JSON page per line.'],
|
|
155
|
+
['--output <path>', 'Write the response body to a file. Required for binary responses.'],
|
|
156
|
+
...(hasBearer ? [['--token <token>', 'Bearer token, overriding the environment.']] : []),
|
|
157
|
+
['--json <json\\|@file\\|@->', 'Request body, inline or from a file or stdin.'],
|
|
158
|
+
] as const) {
|
|
159
|
+
printer.line(`| \`${flag}\` | ${description} |`);
|
|
160
|
+
}
|
|
161
|
+
printer.blank();
|
|
162
|
+
|
|
163
|
+
const prefix = constantCase(options.name);
|
|
164
|
+
printer.line('## Credentials');
|
|
165
|
+
printer.blank();
|
|
166
|
+
if (options.schemes.length === 0) {
|
|
167
|
+
printer.line('The description declares no security schemes, so no credentials are read.');
|
|
168
|
+
} else {
|
|
169
|
+
printer.line('Credentials come from the environment:');
|
|
170
|
+
printer.blank();
|
|
171
|
+
printer.line('| Scheme | Variable |');
|
|
172
|
+
printer.line('| ------ | -------- |');
|
|
173
|
+
for (const scheme of options.schemes) {
|
|
174
|
+
const variable =
|
|
175
|
+
scheme.kind === 'bearer'
|
|
176
|
+
? `\`${prefix}_TOKEN\` (or \`--token\`)`
|
|
177
|
+
: scheme.kind === 'basic'
|
|
178
|
+
? `\`${prefix}_USERNAME\` and \`${prefix}_PASSWORD\``
|
|
179
|
+
: `\`${prefix}_API_KEY_${constantCase(scheme.key)}\``;
|
|
180
|
+
printer.line(`| ${scheme.kind} (\`${scheme.key}\`) | ${variable} |`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
printer.blank();
|
|
184
|
+
|
|
185
|
+
printer.line('## Exit codes');
|
|
186
|
+
printer.blank();
|
|
187
|
+
printer.line('| Code | Meaning |');
|
|
188
|
+
printer.line('| ---- | ------- |');
|
|
189
|
+
for (const [code, meaning] of [
|
|
190
|
+
[0, 'success'],
|
|
191
|
+
[1, 'API error (status other than 401 or 403)'],
|
|
192
|
+
[2, 'auth error (401 or 403)'],
|
|
193
|
+
[3, 'validation error'],
|
|
194
|
+
[4, 'usage error (unknown command or flag, bad `--json`)'],
|
|
195
|
+
] as const) {
|
|
196
|
+
printer.line(`| ${code} | ${meaning} |`);
|
|
197
|
+
}
|
|
198
|
+
printer.blank();
|
|
199
|
+
printer.line('Errors print one JSON object to stderr, so stdout stays clean for piping.');
|
|
200
|
+
printer.blank();
|
|
201
|
+
|
|
202
|
+
// One section per tag, in the order the description declares them, then the untagged
|
|
203
|
+
// commands — the same order `--help` lists them in.
|
|
204
|
+
const groups = [...new Set(commands.map((command) => command.group))];
|
|
205
|
+
for (const group of groups) {
|
|
206
|
+
const inGroup = commands.filter((command) => command.group === group);
|
|
207
|
+
if (group === undefined) {
|
|
208
|
+
printer.line('## Commands');
|
|
209
|
+
printer.blank();
|
|
210
|
+
} else {
|
|
211
|
+
printer.line(`## ${group}`);
|
|
212
|
+
printer.blank();
|
|
213
|
+
printer.line(`Addressed as \`${options.name} ${groupSlug(group)} <command>\`.`);
|
|
214
|
+
printer.blank();
|
|
215
|
+
}
|
|
216
|
+
for (const command of inGroup) writeCommand(printer, command, options);
|
|
217
|
+
}
|
|
218
|
+
return (
|
|
219
|
+
printer
|
|
220
|
+
.toString()
|
|
221
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
222
|
+
.trimEnd() + '\n'
|
|
223
|
+
);
|
|
224
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Ejected from @redocly/client-generator@0.4.0 — the built-in "cli" generator.
|
|
2
|
+
// This file is yours: edit freely; the generated client stays machine-owned and is
|
|
3
|
+
// rebuilt by `redocly generate-client`. Newer generator versions merge in with
|
|
4
|
+
// `redocly eject-generator cli --update`.
|
|
5
|
+
// The cli engine's embeddable source, snapshotted at prepare time (see
|
|
6
|
+
// scripts/generate-runtime-sources.mjs).
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
RUNTIME_SOURCES,
|
|
10
|
+
RUNTIME_SOURCES_STRIPPED,
|
|
11
|
+
} from '@redocly/client-generator/runtime-sources';
|
|
12
|
+
|
|
13
|
+
/** The cli engine (`runCli` + types) stripped for embedding into `<stem>.cli.ts`. */
|
|
14
|
+
export function embedCliRuntime(): string {
|
|
15
|
+
return RUNTIME_SOURCES_STRIPPED['cli.ts'];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The cli engine RAW, for `runtime: 'module'` (written as `runtime/cli.ts`). */
|
|
19
|
+
export function cliRuntimeSource(): string {
|
|
20
|
+
return RUNTIME_SOURCES['cli.ts'];
|
|
21
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Ejected from @redocly/client-generator@0.4.0 — the built-in "cli" generator.
|
|
2
|
+
// This file is yours: edit freely; the generated client stays machine-owned and is
|
|
3
|
+
// rebuilt by `redocly generate-client`. Newer generator versions merge in with
|
|
4
|
+
// `redocly eject-generator cli --update`.
|
|
5
|
+
import {
|
|
6
|
+
type CodeSample,
|
|
7
|
+
type Generator,
|
|
8
|
+
groupSlug,
|
|
9
|
+
type OperationModel,
|
|
10
|
+
type SampleContext,
|
|
11
|
+
} from '@redocly/client-generator';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
|
|
14
|
+
import { renderCliDocs } from './docs.ts';
|
|
15
|
+
import { cliRuntimeSource } from './engine-source.ts';
|
|
16
|
+
import { cliAuthSchemes, commandData, renderCliModule } from './render.ts';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The cli generator: a bin-ready `<stem>.cli.ts` — a zero-dependency, typed
|
|
20
|
+
* command-line interface over the sibling client (typed flags, `--json`
|
|
21
|
+
* bodies, env auth, `--page-all`, SSE/blob output, a documented exit-code
|
|
22
|
+
* contract). Requires `typescript` (throw mode); wires zod validation when co-selected.
|
|
23
|
+
*/
|
|
24
|
+
export const cliGenerator: Generator = ({ model, output, banner, emit, selected, pagination }) => {
|
|
25
|
+
const content = renderCliModule(model, {
|
|
26
|
+
stem: output.stem,
|
|
27
|
+
importExt: emit.importExt ?? 'js',
|
|
28
|
+
zodSelected: selected?.includes('zod') ?? false,
|
|
29
|
+
pagination,
|
|
30
|
+
argsStyle: emit.argsStyle ?? 'grouped',
|
|
31
|
+
runtime: emit.runtime ?? 'inline',
|
|
32
|
+
});
|
|
33
|
+
const entry = { path: join(output.dir, `${output.stem}.cli.ts`), content };
|
|
34
|
+
if (emit.runtime !== 'module') return [entry];
|
|
35
|
+
const header = banner.map((line) => `// ${line}`).join('\n');
|
|
36
|
+
return [
|
|
37
|
+
entry,
|
|
38
|
+
{
|
|
39
|
+
path: join(output.dir, 'runtime', 'cli.ts'),
|
|
40
|
+
content: `${header}\n\n${cliRuntimeSource().trim()}\n`,
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The CLI's own reference page, written when `client.docs` is on: the usage line, the
|
|
47
|
+
* global flags, the credential variables, the exit codes, and one section per command.
|
|
48
|
+
* It renders from `commandData` — the same table `runCli` dispatches on — so the page
|
|
49
|
+
* cannot describe a tool other than the one beside it.
|
|
50
|
+
*/
|
|
51
|
+
export const cliDocs: Generator = ({ model, output, emit, pagination }) => {
|
|
52
|
+
const content = renderCliDocs(commandData(model, { pagination }), {
|
|
53
|
+
title: `${model.title} command-line reference`,
|
|
54
|
+
frontmatter: emit.docsFrontmatter === true,
|
|
55
|
+
name: output.stem,
|
|
56
|
+
schemes: cliAuthSchemes(model),
|
|
57
|
+
});
|
|
58
|
+
return [{ path: join(output.dir, `${output.stem}.cli.md`), content }];
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** One shell invocation per operation — feeds `x-codeSamples` for docs. */
|
|
62
|
+
export function cliSample(op: OperationModel, ctx: SampleContext): CodeSample | undefined {
|
|
63
|
+
const command = commandData(ctx.model, { pagination: ctx.pagination }).find(
|
|
64
|
+
(candidate) => candidate.name === op.name
|
|
65
|
+
);
|
|
66
|
+
if (command === undefined) return undefined;
|
|
67
|
+
const words = [
|
|
68
|
+
'client',
|
|
69
|
+
...(command.group ? [groupSlug(command.group)] : []),
|
|
70
|
+
command.name,
|
|
71
|
+
...command.positionals.map((positional) => `<${positional.name}>`),
|
|
72
|
+
...command.flags.filter((flag) => flag.required).map((flag) => `--${flag.name} <${flag.type}>`),
|
|
73
|
+
...(command.body ? ["--json '<json>'"] : []),
|
|
74
|
+
];
|
|
75
|
+
return {
|
|
76
|
+
lang: 'shell',
|
|
77
|
+
label: 'CLI',
|
|
78
|
+
source: `npx tsx client.cli.ts ${words.slice(1).join(' ')}\n`,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export default {
|
|
83
|
+
name: 'cli',
|
|
84
|
+
run: cliGenerator,
|
|
85
|
+
sample: cliSample,
|
|
86
|
+
docs: cliDocs,
|
|
87
|
+
requires: ["typescript","zod"],
|
|
88
|
+
errorModes: ["throw"],
|
|
89
|
+
requiresGenerator: '^0.4.0',
|
|
90
|
+
};
|