Skip to content

Commit dd6fe0a

Browse files
committed
feat: initial alpha changes
1 parent 19d1302 commit dd6fe0a

File tree

140 files changed

+3812
-4461
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+3812
-4461
lines changed

.eslintrc.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ module.exports = {
55
'no-unused-vars': 'off',
66
'prettier/prettier': 'error',
77
'unused-imports/no-unused-imports': 'error',
8+
'no-restricted-imports': [
9+
'error',
10+
{
11+
patterns: [
12+
{
13+
group: ['openai', 'openai/*'],
14+
message: 'Use a relative import, not a package import.',
15+
},
16+
],
17+
},
18+
],
819
},
20+
overrides: [
21+
{
22+
files: ['tests/**', 'examples/**'],
23+
rules: {
24+
'no-restricted-imports': 'off',
25+
},
26+
},
27+
],
928
root: true,
1029
};

README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ Request parameters that correspond to file uploads can be passed in many differe
303303

304304
```ts
305305
import fs from 'fs';
306-
import fetch from 'node-fetch';
307306
import OpenAI, { toFile } from 'openai';
308307

309308
const client = new OpenAI();
@@ -560,21 +559,23 @@ validate or strip extra properties from the response from the API.
560559

561560
### Customizing the fetch client
562561

563-
By default, this library uses `node-fetch` in Node, and expects a global `fetch` function in other environments.
562+
By default, this library expects a global `fetch` function is defined.
564563

565-
If you would prefer to use a global, web-standards-compliant `fetch` function even in a Node environment,
566-
(for example, if you are running Node with `--experimental-fetch` or using NextJS which polyfills with `undici`),
567-
add the following import before your first import `from "OpenAI"`:
564+
If you want to use a different `fetch` function, you can either polyfill the global:
568565

569566
```ts
570-
// Tell TypeScript and the package to use the global web fetch instead of node-fetch.
571-
// Note, despite the name, this does not add any polyfills, but expects them to be provided if needed.
572-
import 'openai/shims/web';
573-
import OpenAI from 'openai';
567+
import fetch from 'my-fetch';
568+
569+
globalThis.fetch = fetch;
574570
```
575571

576-
To do the inverse, add `import "openai/shims/node"` (which does import polyfills).
577-
This can also be useful if you are getting the wrong TypeScript types for `Response` ([more details](https://github.com/openai/openai-node/tree/master/src/_shims#readme)).
572+
Or pass it to the client:
573+
574+
```ts
575+
import fetch from 'my-fetch';
576+
577+
const client = new OpenAI({ fetch });
578+
```
578579

579580
### Logging and middleware
580581

@@ -620,6 +621,8 @@ await client.models.list({
620621
});
621622
```
622623

624+
## Frequently Asked Questions
625+
623626
## Semantic versioning
624627

625628
This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:
@@ -634,7 +637,7 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ope
634637

635638
## Requirements
636639

637-
TypeScript >= 4.5 is supported.
640+
TypeScript >= 4.9 is supported.
638641

639642
The following runtimes are supported:
640643

api.md

Lines changed: 44 additions & 45 deletions
Large diffs are not rendered by default.

jest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ const config: JestConfigWithTsJest = {
88
},
99
moduleNameMapper: {
1010
'^openai$': '<rootDir>/src/index.ts',
11-
'^openai/_shims/auto/(.*)$': '<rootDir>/src/_shims/auto/$1-node',
1211
'^openai/(.*)$': '<rootDir>/src/$1',
1312
},
13+
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
1414
modulePathIgnorePatterns: [
1515
'<rootDir>/ecosystem-tests/',
1616
'<rootDir>/dist/',

package.json

Lines changed: 10 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,105 +23,48 @@
2323
"lint": "./scripts/lint",
2424
"fix": "./scripts/format"
2525
},
26-
"dependencies": {
27-
"@types/node": "^18.11.18",
28-
"@types/node-fetch": "^2.6.4",
29-
"abort-controller": "^3.0.0",
30-
"agentkeepalive": "^4.2.1",
31-
"form-data-encoder": "1.7.2",
32-
"formdata-node": "^4.3.2",
33-
"node-fetch": "^2.6.7"
34-
},
26+
"dependencies": {},
3527
"devDependencies": {
28+
"@arethetypeswrong/cli": "^0.17.0",
3629
"@swc/core": "^1.3.102",
3730
"@swc/jest": "^0.2.29",
3831
"@types/jest": "^29.4.0",
32+
"@types/node": "^20.17.6",
3933
"@typescript-eslint/eslint-plugin": "^6.7.0",
40-
"@typescript-eslint/parser": "^6.7.0",
34+
"@typescript-eslint/parser": "^6.0.0",
4135
"eslint": "^8.49.0",
4236
"eslint-plugin-prettier": "^5.0.1",
4337
"eslint-plugin-unused-imports": "^3.0.0",
4438
"fast-check": "^3.22.0",
4539
"iconv-lite": "^0.6.3",
4640
"jest": "^29.4.0",
4741
"prettier": "^3.0.0",
48-
"prettier-2": "npm:prettier@^2",
49-
"segfault-handler": "^1.3.0",
42+
"publint": "^0.2.12",
5043
"ts-jest": "^29.1.0",
5144
"ts-node": "^10.5.0",
52-
"tsc-multi": "^1.1.0",
45+
"tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.3/tsc-multi.tgz",
5346
"tsconfig-paths": "^4.0.0",
5447
"typescript": "^4.8.2",
5548
"zod": "^3.23.8"
5649
},
57-
"sideEffects": [
58-
"./_shims/index.js",
59-
"./_shims/index.mjs",
60-
"./shims/node.js",
61-
"./shims/node.mjs",
62-
"./shims/web.js",
63-
"./shims/web.mjs"
64-
],
6550
"imports": {
6651
"openai": ".",
6752
"openai/*": "./src/*"
6853
},
6954
"exports": {
70-
"./_shims/auto/*": {
71-
"deno": {
72-
"types": "./dist/_shims/auto/*.d.ts",
73-
"require": "./dist/_shims/auto/*.js",
74-
"default": "./dist/_shims/auto/*.mjs"
75-
},
76-
"bun": {
77-
"types": "./dist/_shims/auto/*.d.ts",
78-
"require": "./dist/_shims/auto/*-bun.js",
79-
"default": "./dist/_shims/auto/*-bun.mjs"
80-
},
81-
"browser": {
82-
"types": "./dist/_shims/auto/*.d.ts",
83-
"require": "./dist/_shims/auto/*.js",
84-
"default": "./dist/_shims/auto/*.mjs"
85-
},
86-
"worker": {
87-
"types": "./dist/_shims/auto/*.d.ts",
88-
"require": "./dist/_shims/auto/*.js",
89-
"default": "./dist/_shims/auto/*.mjs"
90-
},
91-
"workerd": {
92-
"types": "./dist/_shims/auto/*.d.ts",
93-
"require": "./dist/_shims/auto/*.js",
94-
"default": "./dist/_shims/auto/*.mjs"
95-
},
96-
"node": {
97-
"types": "./dist/_shims/auto/*-node.d.ts",
98-
"require": "./dist/_shims/auto/*-node.js",
99-
"default": "./dist/_shims/auto/*-node.mjs"
100-
},
101-
"types": "./dist/_shims/auto/*.d.ts",
102-
"require": "./dist/_shims/auto/*.js",
103-
"default": "./dist/_shims/auto/*.mjs"
104-
},
10555
".": {
106-
"require": {
107-
"types": "./dist/index.d.ts",
108-
"default": "./dist/index.js"
109-
},
110-
"types": "./dist/index.d.mts",
111-
"default": "./dist/index.mjs"
56+
"import": "./dist/index.mjs",
57+
"require": "./dist/index.js"
11258
},
11359
"./*.mjs": {
114-
"types": "./dist/*.d.ts",
11560
"default": "./dist/*.mjs"
11661
},
11762
"./*.js": {
118-
"types": "./dist/*.d.ts",
11963
"default": "./dist/*.js"
12064
},
12165
"./*": {
122-
"types": "./dist/*.d.ts",
123-
"require": "./dist/*.js",
124-
"default": "./dist/*.mjs"
66+
"import": "./dist/*.mjs",
67+
"require": "./dist/*.js"
12568
}
12669
},
12770
"bin": "./bin/cli",

scripts/build

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ rm -rf dist; mkdir dist
1515
# Copy src to dist/src and build from dist/src into dist, so that
1616
# the source map for index.js.map will refer to ./src/index.ts etc
1717
cp -rp src README.md dist
18-
rm dist/src/_shims/*-deno.ts dist/src/_shims/auto/*-deno.ts
1918
for file in LICENSE CHANGELOG.md; do
2019
if [ -e "${file}" ]; then cp "${file}" dist; fi
2120
done
@@ -29,9 +28,6 @@ node scripts/utils/make-dist-package-json.cjs > dist/package.json
2928

3029
# build to .js/.mjs/.d.ts files
3130
npm exec tsc-multi
32-
# copy over handwritten .js/.mjs/.d.ts files
33-
cp src/_shims/*.{d.ts,js,mjs,md} dist/_shims
34-
cp src/_shims/auto/*.{d.ts,js,mjs} dist/_shims/auto
3531
# we need to add exports = module.exports = OpenAI to index.js;
3632
# No way to get that from index.ts because it would cause compile errors
3733
# when building .mjs
@@ -42,6 +38,10 @@ node scripts/utils/fix-index-exports.cjs
4238
# the same export default statement)
4339
cp dist/index.d.ts dist/index.d.mts
4440
cp tsconfig.dist-src.json dist/src/tsconfig.json
41+
cp src/internal/shim-types.d.ts dist/internal/shim-types.d.ts
42+
cp src/internal/shim-types.d.ts dist/internal/shim-types.d.mts
43+
mkdir -p dist/internal/polyfill
44+
cp src/internal/polyfill/*.{mjs,js,d.ts} dist/internal/polyfill
4545

4646
node scripts/utils/postprocess-files.cjs
4747

scripts/build-deno

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,6 @@ cd "$(dirname "$0")/.."
77
rm -rf dist-deno; mkdir dist-deno
88
cp -rp src/* jsr.json dist-deno
99

10-
rm -rf dist-deno/shims
11-
12-
rm dist-deno/_shims/node*.{js,mjs,ts}
13-
rm dist-deno/_shims/manual*.{js,mjs,ts}
14-
rm dist-deno/_shims/index.{d.ts,js,mjs}
15-
for file in dist-deno/_shims/*-deno.ts; do
16-
mv -- "$file" "${file%-deno.ts}.ts"
17-
done
18-
19-
rm dist-deno/_shims/auto/*-node.ts
20-
rm dist-deno/_shims/auto/*.{d.ts,js,mjs}
21-
for file in dist-deno/_shims/auto/*-deno.ts; do
22-
mv -- "$file" "${file%-deno.ts}.ts"
23-
done
24-
2510
for file in README.md LICENSE CHANGELOG.md; do
2611
if [ -e "${file}" ]; then cp "${file}" dist-deno; fi
2712
done

scripts/lint

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@ cd "$(dirname "$0")/.."
77
echo "==> Running eslint"
88
ESLINT_USE_FLAT_CONFIG="false" ./node_modules/.bin/eslint --ext ts,js .
99

10-
echo "==> Running tsc"
11-
./node_modules/.bin/tsc --noEmit
10+
echo "==> Building"
11+
./scripts/build # also checks types
12+
13+
echo "==> Running Are The Types Wrong?"
14+
./node_modules/.bin/attw --pack dist -f json >.attw.json || true
15+
node scripts/utils/attw-report.cjs
16+
17+
echo "==> Running publint"
18+
./node_modules/.bin/publint dist

scripts/utils/attw-report.cjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const fs = require('fs');
2+
const problems = Object.values(JSON.parse(fs.readFileSync('.attw.json', 'utf-8')).problems)
3+
.flat()
4+
.filter(
5+
(problem) =>
6+
!(
7+
// This is intentional, if the user specifies .mjs they get ESM.
8+
(
9+
(problem.kind === 'CJSResolvesToESM' && problem.entrypoint.endsWith('.mjs')) ||
10+
// This is intentional for backwards compat reasons.
11+
(problem.kind === 'MissingExportEquals' && problem.implementationFileName.endsWith('/index.js'))
12+
)
13+
),
14+
);
15+
fs.unlinkSync('.attw.json');
16+
if (problems.length) {
17+
process.stdout.write('The types are wrong!\n' + JSON.stringify(problems, null, 2) + '\n');
18+
process.exitCode = 1;
19+
} else {
20+
process.stdout.write('Types ok!\n');
21+
}

scripts/utils/fix-index-exports.cjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const indexJs =
88

99
let before = fs.readFileSync(indexJs, 'utf8');
1010
let after = before.replace(
11-
/^\s*exports\.default\s*=\s*(\w+)/m,
12-
'exports = module.exports = $1;\nmodule.exports.AzureOpenAI = AzureOpenAI;\nexports.default = $1',
11+
/^(\s*Object\.defineProperty\s*\(exports,\s*["']__esModule["'].+)$/m,
12+
`exports = module.exports = function (...args) {
13+
return new exports.default(...args)
14+
}
15+
$1`.replace(/^ /gm, ''),
1316
);
1417
fs.writeFileSync(indexJs, after, 'utf8');

0 commit comments

Comments
 (0)