Skip to content

Commit d882dd1

Browse files
cubehouseclaude
andcommitted
fix(docs): make examples runnable as plain JS
Cookbook and README code blocks were labeled \`ts\` but mixed TS-only syntax (type guards, generics, non-null assertions) into runnable examples. A user copy-pasting into \`test.mjs\` and running with node hit SyntaxErrors. Also Recipe 5's generic-alternative block referenced MAGIC_KINGDOM without declaring it. - Rename runnable blocks from \`ts\` to \`js\` and strip TS-only syntax. - Keep the single TypeScript example (Cache adapter with \`implements\`) labeled \`ts\` with a note on how to translate. - Add a preamble in both docs explaining the js vs ts file-extension swap so users know which runtime to use. - Add the missing MAGIC_KINGDOM constant in the generic-alternative block. - Replace \`wdw!.id\` (TS non-null assertion) with a real entity id in the low-level escape-hatch example. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7b82762 commit d882dd1

2 files changed

Lines changed: 30 additions & 19 deletions

File tree

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ Runs on Node 18+, evergreen browsers, Deno, Bun, and Cloudflare Workers. Zero ru
1414

1515
## Print live wait times
1616

17-
```ts
17+
Every example in this README works as plain JavaScript — save as `.mjs` and
18+
run with `node`. To use them as TypeScript, rename to `.ts` and run with
19+
`npx tsx`; the SDK ships full `.d.ts` types so TypeScript infers everything.
20+
21+
```js
1822
import { ThemeParks, currentWaitTime } from 'themeparks';
1923

2024
const MAGIC_KINGDOM = '75ea578a-adc8-4116-a54d-dccb60765ef9';
@@ -56,7 +60,7 @@ Jungle Cruise 40 min
5660
5761
Example:
5862
59-
```ts
63+
```js
6064
const tp = new ThemeParks({
6165
userAgent: 'my-app/1.2.3 (+https://example.com)',
6266
timeoutMs: 15_000,
@@ -81,7 +85,7 @@ Each variant is exposed as a key on `entry.queue`. All are optional — `undefin
8185
8286
### Direct access
8387
84-
```ts
88+
```js
8589
import { ThemeParks } from 'themeparks';
8690

8791
const tp = new ThemeParks();
@@ -116,7 +120,7 @@ for (const entry of live.liveData ?? []) {
116120
117121
If branching on every variant is too verbose, `narrowQueues(queue)` flattens all populated variants into a typed discriminated union:
118122
119-
```ts
123+
```js
120124
import { ThemeParks, narrowQueues } from 'themeparks';
121125

122126
const tp = new ThemeParks();
@@ -135,7 +139,7 @@ for (const entry of live.liveData ?? []) {
135139
136140
## Ergonomic helpers
137141
138-
```ts
142+
```js
139143
import { ThemeParks } from 'themeparks';
140144

141145
const tp = new ThemeParks();
@@ -159,17 +163,17 @@ console.log(`${entries.length} schedule entries`);
159163
160164
Every ergonomic helper is built on top of `tp.raw`, which is a thin, typed 1:1 wrapper over the OpenAPI operations. Use it directly when you want the raw response shape:
161165
162-
```ts
166+
```js
163167
const live = await tp.raw.getEntityLive('75ea578a-adc8-4116-a54d-dccb60765ef9');
164168
const dests = await tp.raw.getDestinations();
165-
const children = await tp.raw.getEntityChildren(wdw!.id);
169+
const children = await tp.raw.getEntityChildren('e957da41-3552-4cf6-b636-5babc5cbc4e5');
166170
```
167171
168172
## Error handling
169173
170174
All SDK errors inherit from `ThemeParksError`. The ones you'll typically catch:
171175
172-
```ts
176+
```js
173177
import { ThemeParks, ApiError, RateLimitError, NetworkError, TimeoutError } from 'themeparks';
174178

175179
const tp = new ThemeParks();
@@ -196,7 +200,7 @@ try {
196200
197201
There's no built-in HTTP logger to flip on (we run directly on platform `fetch`). The clean idiom is to pass a wrapping `fetch` implementation:
198202
199-
```ts
203+
```js
200204
import { ThemeParks } from 'themeparks';
201205

202206
const tp = new ThemeParks({
@@ -226,13 +230,13 @@ The default client caches `GET` responses in-memory (LRU) with sensible per-endp
226230
227231
### Disable caching
228232
229-
```ts
233+
```js
230234
const tp = new ThemeParks({ cache: false });
231235
```
232236
233237
### Plug in your own adapter
234238
235-
`Cache` is a structural interface — any object implementing `get`, `set`, and `delete` works. Redis, filesystem, IndexedDB, etc.:
239+
`Cache` is a structural interface — any object implementing `get`, `set`, and `delete` works. Redis, filesystem, IndexedDB, etc. (TypeScript shown; drop the annotations for plain JS):
236240
237241
```ts
238242
import { ThemeParks, type Cache } from 'themeparks';

docs/cookbook.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ Entity IDs used throughout:
88
- Magic Kingdom: `75ea578a-adc8-4116-a54d-dccb60765ef9`
99
- Walt Disney World Resort: `e957da41-3552-4cf6-b636-5babc5cbc4e5`
1010

11+
Every example below is written as plain JavaScript with ES modules. Save any
12+
block as `test.mjs` and run it with `node test.mjs` (Node ≥18). For
13+
TypeScript just rename to `.ts` and run with `npx tsx test.ts` — the SDK
14+
ships full `.d.ts` types so TypeScript will infer the shapes automatically.
15+
1116
## Recipe 1 — Wait times in order (longest → shortest)
1217

1318
Pull the live data for Magic Kingdom, keep only the rides that are reporting
1419
a wait time, and print them sorted from longest to shortest queue.
1520

16-
```ts
21+
```js
1722
import { ThemeParks, currentWaitTime } from 'themeparks';
1823

1924
const MAGIC_KINGDOM = '75ea578a-adc8-4116-a54d-dccb60765ef9';
@@ -23,7 +28,7 @@ const live = await tp.entity(MAGIC_KINGDOM).live();
2328

2429
const waits = (live.liveData ?? [])
2530
.map((entry) => ({ name: entry.name, wait: currentWaitTime(entry) }))
26-
.filter((w): w is { name: string; wait: number } => w.wait !== null)
31+
.filter((w) => w.wait !== null)
2732
.sort((a, b) => b.wait - a.wait);
2833

2934
for (const { name, wait } of waits) {
@@ -56,7 +61,7 @@ EXTRA_HOURS (early entry / extended evening), and TICKETED_EVENT (after-hours
5661
events). `range()` returns them all in chronological order across any month
5762
boundaries.
5863

59-
```ts
64+
```js
6065
import { ThemeParks } from 'themeparks';
6166
6267
const MAGIC_KINGDOM = '75ea578a-adc8-4116-a54d-dccb60765ef9';
@@ -98,14 +103,14 @@ coordinates by `entityType`:
98103
> tree in a single response, so even for the largest destinations this is
99104
> one HTTP request.
100105

101-
```ts
106+
```js
102107
import { ThemeParks } from 'themeparks';
103108
104109
const WALT_DISNEY_WORLD = 'e957da41-3552-4cf6-b636-5babc5cbc4e5';
105110
106111
const tp = new ThemeParks();
107112
108-
const byType = new Map<string, Array<{ name: string; lat: number; lng: number }>>();
113+
const byType = new Map();
109114
110115
for await (const child of tp.entity(WALT_DISNEY_WORLD).walk()) {
111116
const loc = child.location;
@@ -154,7 +159,7 @@ RESTAURANT (218)
154159
There's no built-in HTTP logger to flip on (the SDK runs directly on platform
155160
`fetch`). The clean idiom is to pass a wrapping `fetch` implementation:
156161
157-
```ts
162+
```js
158163
import { ThemeParks } from 'themeparks';
159164
160165
const tp = new ThemeParks({
@@ -202,7 +207,7 @@ Lane) plus SINGLE_RIDER. The full set of variants is:
202207

203208
Print all populated queue variants for every attraction at Magic Kingdom:
204209

205-
```ts
210+
```js
206211
import { ThemeParks } from 'themeparks';
207212
208213
const MAGIC_KINGDOM = '75ea578a-adc8-4116-a54d-dccb60765ef9';
@@ -259,9 +264,11 @@ TRON Lightcycle / Run LIGHTNING LANE $20.00, return 2026-04-
259264
If branching on every variant is too verbose, `narrowQueues(queue)` flattens
260265
all populated variants into a typed discriminated union you can switch over:
261266

262-
```ts
267+
```js
263268
import { ThemeParks, narrowQueues } from 'themeparks';
264269

270+
const MAGIC_KINGDOM = '75ea578a-adc8-4116-a54d-dccb60765ef9';
271+
265272
const tp = new ThemeParks();
266273
const live = await tp.entity(MAGIC_KINGDOM).live();
267274

0 commit comments

Comments
 (0)