Skip to content

Commit 431a0b5

Browse files
Claude Botclaude
andcommitted
Improve YAML documentation with Bun.YAML.stringify and TypeScript typing
- Add Bun.YAML.stringify() documentation with examples using (null, 2) for spacing - Document space parameter: 2 for readable format, 0 for compact flow style - Add TypeScript section in Module Import explaining manual .d.ts requirement - Clarify that unlike JSON, TypeScript doesn't automatically type YAML imports - Show comprehensive example with nested objects, arrays, and objects in arrays - Demonstrate YAML anchors & aliases with shared object references - Fix heading structure: parse examples under parse, stringify as separate section - Use actual tested Bun output for all examples 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 6f8138b commit 431a0b5

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

docs/runtime/yaml.mdx

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,44 @@ try {
121121
}
122122
```
123123

124+
### `Bun.YAML.stringify()`
125+
126+
Convert a JavaScript object into a YAML string.
127+
128+
```ts
129+
import { YAML } from "bun";
130+
131+
const obj = {
132+
name: "John Doe",
133+
age: 30,
134+
135+
hobbies: ["reading", "coding", "hiking"]
136+
};
137+
138+
const yaml = Bun.YAML.stringify(obj, null, 2);
139+
console.log(yaml);
140+
// name: John Doe
141+
// age: 30
142+
143+
// hobbies:
144+
// - reading
145+
// - coding
146+
// - hiking
147+
```
148+
149+
The signature is similar to `JSON.stringify()`:
150+
151+
```ts
152+
Bun.YAML.stringify(value, replacer?, space?)
153+
```
154+
155+
The `space` parameter controls indentation (default: 2). With `space: 0`, YAML outputs in compact flow style on a single line:
156+
157+
```ts
158+
const compact = Bun.YAML.stringify(obj, null, 0);
159+
// {name: John Doe,age: 30,email: [email protected],hobbies: [reading,coding,hiking]}
160+
```
161+
124162
---
125163
126164
## Module Import
@@ -193,6 +231,30 @@ const { database, redis } = require("./config.yaml");
193231
console.log(database.port); // 5432
194232
```
195233
234+
### TypeScript
235+
236+
Unlike JSON files, TypeScript doesn't automatically type YAML imports. Add type definitions by creating a `.d.ts` file with the same name as your YAML file:
237+
238+
```ts config.yaml.d.ts icon="/icons/typescript.svg"
239+
const contents: {
240+
database: {
241+
host: string;
242+
port: number;
243+
name: string;
244+
};
245+
server: {
246+
port: number;
247+
timeout: number;
248+
};
249+
features: {
250+
auth: boolean;
251+
rateLimit: boolean;
252+
};
253+
};
254+
255+
export = contents;
256+
```
257+
196258
---
197259
198260
## Hot Reloading with YAML
@@ -435,6 +497,76 @@ if (parseConfig(migrations).autoRun === "true") {
435497
}
436498
```
437499

500+
### Generating YAML Files
501+
502+
You can use `Bun.YAML.stringify()` to programmatically create YAML configuration files. It handles complex data structures including nested objects, arrays, and objects within arrays. When you reference the same object multiple times, YAML automatically creates anchors (`&`) and aliases (`*`) to avoid duplication:
503+
504+
<CodeGroup>
505+
```ts generate-config.ts icon="/icons/typescript.svg"
506+
import { YAML } from "bun";
507+
508+
const defaults = {
509+
timeout: 30000,
510+
retries: 3,
511+
};
512+
513+
const config = {
514+
defaults,
515+
development: {
516+
defaults,
517+
host: "localhost",
518+
port: 3000,
519+
},
520+
production: {
521+
defaults,
522+
host: "api.example.com",
523+
port: 443,
524+
},
525+
servers: [
526+
{ name: "server1", url: "https://api1.example.com" },
527+
{ name: "server2", url: "https://api2.example.com" },
528+
],
529+
features: {
530+
enabled: ["auth", "logging", "metrics"],
531+
disabled: ["experimental"],
532+
},
533+
};
534+
535+
// Generate readable YAML with 2-space indentation
536+
const yaml = Bun.YAML.stringify(config, null, 2);
537+
await Bun.write("config.yaml", yaml);
538+
```
539+
540+
```yaml output.yaml icon="file-code"
541+
defaults:
542+
&defaults
543+
timeout: 30000
544+
retries: 3
545+
development:
546+
defaults:
547+
*defaults
548+
host: localhost
549+
port: 3000
550+
production:
551+
defaults:
552+
*defaults
553+
host: api.example.com
554+
port: 443
555+
servers:
556+
- name: server1
557+
url: https://api1.example.com
558+
- name: server2
559+
url: https://api2.example.com
560+
features:
561+
enabled:
562+
- auth
563+
- logging
564+
- metrics
565+
disabled:
566+
- experimental
567+
```
568+
</CodeGroup>
569+
438570
### Bundler Integration
439571
440572
When you import YAML files in your application and bundle it with Bun, the YAML is parsed at build time and included as a JavaScript module:

0 commit comments

Comments
 (0)