Skip to content

Commit 1095c83

Browse files
chore(internal): fix format script
1 parent 6d5aa86 commit 1095c83

File tree

9 files changed

+944
-94
lines changed

9 files changed

+944
-94
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 `openai` package, then run `./node_modules/.bin/openai 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
### Named path parameters
9843

@@ -409,6 +354,80 @@ import OpenAI from 'openai/src';
409354
import OpenAI from 'openai';
410355
```
411356

412-
### Headers
357+
## TypeScript troubleshooting
358+
359+
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.
360+
To resolve these issues, configure your tsconfig.json and install the appropriate `@types` packages for your runtime environment using the guidelines below:
361+
362+
### Browsers
363+
364+
`tsconfig.json`
365+
366+
```jsonc
367+
{
368+
"target": "ES2018", // note: we recommend ES2020 or higher
369+
"lib": ["DOM", "DOM.Iterable", "ES2018"]
370+
}
371+
```
372+
373+
### Node.js
413374

414-
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>`.
375+
`tsconfig.json`
376+
377+
```jsonc
378+
{
379+
"target": "ES2018" // note: we recommend ES2020 or higher
380+
}
381+
```
382+
383+
`package.json`
384+
385+
```json
386+
{
387+
"devDependencies": {
388+
"@types/node": ">= 18.18.7"
389+
}
390+
}
391+
```
392+
393+
### Cloudflare Workers
394+
395+
`tsconfig.json`
396+
397+
```jsonc
398+
{
399+
"target": "ES2018", // note: we recommend ES2020 or higher
400+
"lib": ["ES2020"], // <- needed by @cloudflare/workers-types
401+
"types": ["@cloudflare/workers-types"]
402+
}
403+
```
404+
405+
`package.json`
406+
407+
```json
408+
{
409+
"devDependencies": {
410+
"@cloudflare/workers-types": ">= 0.20221111.0"
411+
}
412+
}
413+
```
414+
415+
### Bun
416+
417+
`tsconfig.json`
418+
419+
```jsonc
420+
{
421+
"target": "ES2018" // note: we recommend ES2020 or higher
422+
}
423+
```
424+
425+
`package.json`
426+
427+
```json
428+
{
429+
"devDependencies": {
430+
"@types/bun": ">= 1.2.0"
431+
}
432+
}
433+
```

bin/cli

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,28 @@ const { spawnSync } = require('child_process');
44

55
const commands = {
66
migrate: {
7-
description: 'Run migrations to update from openai v3 to v4',
7+
description: 'Run migrations to update to the latest major SDK version',
88
fn: () => {
9-
console.log('This automatic code migration is provided by grit.io');
10-
console.log('Visit https://app.grit.io/studio?preset=openai_v4 for more details.')
11-
129
const result = spawnSync(
1310
'npx',
14-
['-y', '@getgrit/launcher', 'apply', 'openai_v4', ...process.argv.slice(3)],
11+
[
12+
'-y',
13+
'https://github.com/stainless-api/migrate-ts/releases/download/0.0.2/stainless-api-migrate-0.0.2-6.tgz',
14+
'--migrationConfig',
15+
require.resolve('./migration-config.json'),
16+
...process.argv.slice(3),
17+
],
1518
{ stdio: 'inherit' },
1619
);
1720
if (result.status !== 0) {
1821
process.exit(result.status);
1922
}
20-
}
21-
}
22-
}
23+
},
24+
},
25+
};
2326

2427
function exitWithHelp() {
25-
console.log("Usage: $0 <subcommand>");
28+
console.log(`Usage: openai <subcommand>`);
2629
console.log();
2730
console.log('Subcommands:');
2831

0 commit comments

Comments
 (0)