You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: MIGRATION.md
+97-78Lines changed: 97 additions & 78 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,95 +4,40 @@ This guide outlines the changes and steps needed to migrate your codebase to the
4
4
5
5
The main changes are that the SDK now relies on the [builtin Web fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) instead of `node-fetch` and has zero dependencies.
6
6
7
+
## Migration CLI
8
+
9
+
Most programs will only need minimal changes, but to assist there is a migration tool that will automatically update your code for the new version.
10
+
To use it, upgrade the `@anthropic-ai/sdk` package, then run `./node_modules/.bin/anthropic-ai-sdk migrate ./your/src/folders` to update your code.
11
+
To preview the changes without writing them to disk, run the tool with `--dry`.
12
+
7
13
## Environment requirements
8
14
9
15
The minimum supported runtime and tooling versions are now:
10
16
11
-
- Node.js 18.x last LTS (Required for built-in fetch support)
17
+
- Node.js 18.x last LTS (Required for builtin fetch support)
12
18
- This was previously documented as the minimum supported Node.js version but Node.js 16.x mostly worked at runtime; now it will not.
13
19
- TypeScript 4.9
14
20
- Jest 28
15
21
16
-
## Minimum types requirements
17
-
18
-
### DOM
19
-
20
-
`tsconfig.json`
21
-
22
-
```jsonc
23
-
{
24
-
"target":"ES2015", // note: we recommend ES2020 or higher
25
-
"lib": ["DOM", "DOM.Iterable", "ES2018"]
26
-
}
27
-
```
28
-
29
-
### Node.js
30
-
31
-
`tsconfig.json`
32
-
33
-
```jsonc
34
-
{
35
-
"target":"ES2015"// note: we recommend ES2020 or higher
36
-
}
37
-
```
38
-
39
-
`package.json`
40
-
41
-
```json
42
-
{
43
-
"devDependencies": {
44
-
"@types/node": ">= 18.18.7"
45
-
}
46
-
}
47
-
```
48
-
49
-
### Cloudflare Workers
50
-
51
-
`tsconfig.json`
52
-
53
-
```jsonc
54
-
{
55
-
"target":"ES2015", // note: we recommend ES2020 or higher
56
-
"lib": ["ES2020"], // <- needed by @cloudflare/workers-types
57
-
"types": ["@cloudflare/workers-types"]
58
-
}
59
-
```
60
-
61
-
`package.json`
62
-
63
-
```json
64
-
{
65
-
"devDependencies": {
66
-
"@cloudflare/workers-types": ">= 0.20221111.0"
67
-
}
68
-
}
69
-
```
70
-
71
-
### Bun
22
+
## Breaking changes
72
23
73
-
`tsconfig.json`
24
+
### Web types for `withResponse`, `asResponse`, and `APIError.headers`
74
25
75
-
```jsonc
76
-
{
77
-
"target":"ES2015"// note: we recommend ES2020 or higher
78
-
}
79
-
```
80
-
81
-
`package.json`
26
+
Because we now use the builtin Web fetch API on all platforms, if you wrote code that used `withResponse` or `asResponse` and then accessed `node-fetch`-specific properties on the result, you will need to switch to standardized alternatives.
27
+
For example, `body` is now a [Web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) rather than a [node `Readable`](https://nodejs.org/api/stream.html#readable-streams).
82
28
83
-
```json
84
-
{
85
-
"devDependencies": {
86
-
"@types/bun": ">= 1.2.0"
87
-
}
88
-
}
29
+
```ts
30
+
// Before:
31
+
const res =awaitclient.example.retrieve('string/with/slash').asResponse();
32
+
res.body.pipe(process.stdout);
33
+
34
+
// After:
35
+
import { Readable } from'node:stream';
36
+
const res =awaitclient.example.retrieve('string/with/slash').asResponse();
37
+
Readable.fromWeb(res.body).pipe(process.stdout);
89
38
```
90
39
91
-
### Deno
92
-
93
-
No config needed!
94
-
95
-
## Breaking changes
40
+
Additionally, the `headers` property on `APIError` objects is now an instance of the Web [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) class. It was previously defined as `Record<string, string | null | undefined>`.
96
41
97
42
### URI encoded path parameters
98
43
@@ -322,6 +267,80 @@ import Anthropic from '@anthropic-ai/sdk/src';
322
267
importAnthropicfrom'@anthropic-ai/sdk';
323
268
```
324
269
325
-
### Headers
270
+
## TypeScript troubleshooting
271
+
272
+
When referencing the library after updating, you may encounter new type errors related to JS features like private properties or fetch classes like Request, Response, and Headers.
273
+
To resolve these issues, configure your tsconfig.json and install the appropriate `@types` packages for your runtime environment using the guidelines below:
274
+
275
+
### Browsers
276
+
277
+
`tsconfig.json`
278
+
279
+
```jsonc
280
+
{
281
+
"target":"ES2018", // note: we recommend ES2020 or higher
282
+
"lib": ["DOM", "DOM.Iterable", "ES2018"]
283
+
}
284
+
```
285
+
286
+
### Node.js
326
287
327
-
The `headers` property on `APIError` objects is now an instance of the Web [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) class. It was previously just `Record<string, string | null | undefined>`.
288
+
`tsconfig.json`
289
+
290
+
```jsonc
291
+
{
292
+
"target":"ES2018"// note: we recommend ES2020 or higher
293
+
}
294
+
```
295
+
296
+
`package.json`
297
+
298
+
```json
299
+
{
300
+
"devDependencies": {
301
+
"@types/node": ">= 18.18.7"
302
+
}
303
+
}
304
+
```
305
+
306
+
### Cloudflare Workers
307
+
308
+
`tsconfig.json`
309
+
310
+
```jsonc
311
+
{
312
+
"target":"ES2018", // note: we recommend ES2020 or higher
313
+
"lib": ["ES2020"], // <- needed by @cloudflare/workers-types
314
+
"types": ["@cloudflare/workers-types"]
315
+
}
316
+
```
317
+
318
+
`package.json`
319
+
320
+
```json
321
+
{
322
+
"devDependencies": {
323
+
"@cloudflare/workers-types": ">= 0.20221111.0"
324
+
}
325
+
}
326
+
```
327
+
328
+
### Bun
329
+
330
+
`tsconfig.json`
331
+
332
+
```jsonc
333
+
{
334
+
"target":"ES2018"// note: we recommend ES2020 or higher
0 commit comments