Skip to content

Commit 9ce30ba

Browse files
stainless-app[bot]RobertCraigie
authored andcommitted
chore(internal): fix format script
1 parent 6f8fce9 commit 9ce30ba

File tree

9 files changed

+173
-85
lines changed

9 files changed

+173
-85
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
"postCreateCommand": "yarn install",
1010
"customizations": {
1111
"vscode": {
12-
"extensions": [
13-
"esbenp.prettier-vscode"
14-
]
12+
"extensions": ["esbenp.prettier-vscode"]
1513
}
1614
}
1715
}

MIGRATION.md

Lines changed: 97 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,40 @@ This guide outlines the changes and steps needed to migrate your codebase to the
44

55
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.
66

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+
713
## Environment requirements
814

915
The minimum supported runtime and tooling versions are now:
1016

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)
1218
- This was previously documented as the minimum supported Node.js version but Node.js 16.x mostly worked at runtime; now it will not.
1319
- TypeScript 4.9
1420
- Jest 28
1521

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
7223

73-
`tsconfig.json`
24+
### Web types for `withResponse`, `asResponse`, and `APIError.headers`
7425

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).
8228

83-
```json
84-
{
85-
"devDependencies": {
86-
"@types/bun": ">= 1.2.0"
87-
}
88-
}
29+
```ts
30+
// Before:
31+
const res = await client.example.retrieve('string/with/slash').asResponse();
32+
res.body.pipe(process.stdout);
33+
34+
// After:
35+
import { Readable } from 'node:stream';
36+
const res = await client.example.retrieve('string/with/slash').asResponse();
37+
Readable.fromWeb(res.body).pipe(process.stdout);
8938
```
9039

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>`.
9641

9742
### URI encoded path parameters
9843

@@ -322,6 +267,80 @@ import Anthropic from '@anthropic-ai/sdk/src';
322267
import Anthropic from '@anthropic-ai/sdk';
323268
```
324269

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
326287

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
335+
}
336+
```
337+
338+
`package.json`
339+
340+
```json
341+
{
342+
"devDependencies": {
343+
"@types/bun": ">= 1.2.0"
344+
}
345+
}
346+
```

bin/cli

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env node
2+
3+
const { spawnSync } = require('child_process');
4+
5+
const commands = {
6+
migrate: {
7+
description:
8+
'Run migrations to update your code using @anthropic-ai/[email protected] to be compatible with @anthropic-ai/[email protected]',
9+
fn: () => {
10+
const result = spawnSync(
11+
'npx',
12+
[
13+
'-y',
14+
'https://github.com/stainless-api/migrate-ts/releases/download/0.0.2/stainless-api-migrate-0.0.2-6.tgz',
15+
'--migrationConfig',
16+
require.resolve('./migration-config.json'),
17+
...process.argv.slice(3),
18+
],
19+
{ stdio: 'inherit' },
20+
);
21+
if (result.status !== 0) {
22+
process.exit(result.status);
23+
}
24+
},
25+
},
26+
};
27+
28+
function exitWithHelp() {
29+
console.log(`Usage: anthropic-ai-sdk <subcommand>`);
30+
console.log();
31+
console.log('Subcommands:');
32+
33+
for (const [name, info] of Object.entries(commands)) {
34+
console.log(` ${name} ${info.description}`);
35+
}
36+
37+
console.log();
38+
process.exit(1);
39+
}
40+
41+
if (process.argv.length < 3) {
42+
exitWithHelp();
43+
}
44+
45+
const commandName = process.argv[2];
46+
47+
const command = commands[commandName];
48+
if (!command) {
49+
console.log(`Unknown subcommand ${commandName}.`);
50+
exitWithHelp();
51+
}
52+
53+
command.fn();

bin/migration-config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"pkg": "@anthropic-ai/sdk",
3+
"githubRepo": "https://github.com/stainless-sdks/anthropic-typescript",
4+
"clientClass": "Anthropic",
5+
"baseClientClass": "BaseAnthropic",
6+
"methods": []
7+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"test": "./scripts/test",
1818
"build": "./scripts/build-all",
1919
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
20-
"format": "prettier --write --cache --cache-strategy metadata . !dist",
20+
"format": "./scripts/format",
2121
"prepare": "if ./scripts/utils/check-is-in-git-install.sh; then ./scripts/build && ./scripts/utils/git-swap.sh; fi",
2222
"tsn": "ts-node -r tsconfig-paths/register",
2323
"lint": "./scripts/lint",
@@ -57,6 +57,9 @@
5757
"@anthropic-ai/sdk": ".",
5858
"@anthropic-ai/sdk/*": "./src/*"
5959
},
60+
"bin": {
61+
"anthropic-ai-sdk": "bin/cli"
62+
},
6063
"exports": {
6164
".": {
6265
"import": "./dist/index.mjs",

scripts/build

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ for file in LICENSE CHANGELOG.md; do
1919
if [ -e "${file}" ]; then cp "${file}" dist; fi
2020
done
2121
if [ -e "bin/cli" ]; then
22-
mkdir dist/bin
22+
mkdir -p dist/bin
2323
cp -p "bin/cli" dist/bin/;
2424
fi
25+
if [ -e "bin/migration-config.json" ]; then
26+
mkdir -p dist/bin
27+
cp -p "bin/migration-config.json" dist/bin/;
28+
fi
2529
# this converts the export map paths for the dist directory
2630
# and does a few other minor things
2731
node scripts/utils/make-dist-package-json.cjs > dist/package.json

scripts/format

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ cd "$(dirname "$0")/.."
66

77
echo "==> Running eslint --fix"
88
./node_modules/.bin/eslint --fix .
9+
10+
echo "==> Running prettier --write"
11+
# format things eslint didn't
12+
./node_modules/.bin/prettier --write --cache --cache-strategy metadata . '!**/dist' '!**/*.ts' '!**/*.mts' '!**/*.cts' '!**/*.js' '!**/*.mjs' '!**/*.cjs'

tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"rootDir": "./dist/src",
77
"paths": {
88
"@anthropic-ai/sdk/*": ["dist/src/*"],
9-
"@anthropic-ai/sdk": ["dist/src/index.ts"],
9+
"@anthropic-ai/sdk": ["dist/src/index.ts"]
1010
},
1111
"noEmit": false,
1212
"declaration": true,

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"noUncheckedIndexedAccess": true,
3232
"noImplicitOverride": true,
3333
"noPropertyAccessFromIndexSignature": true,
34-
"isolatedModules": false,
34+
"isolatedModules": false,
3535

3636
"skipLibCheck": true
3737
}

0 commit comments

Comments
 (0)