Skip to content

Commit 16fe929

Browse files
committed
fix(client): use explicit file extensions in _shims imports (openai#276)
1 parent dafdf63 commit 16fe929

29 files changed

+70
-25
lines changed

build

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ rm -rf dist; mkdir dist
1212
# Copy src to dist/src and build from dist/src into dist, so that
1313
# the source map for index.js.map will refer to ./src/index.ts etc
1414
cp -rp src README.md dist
15+
rm dist/src/_shims/*-deno.*
1516
for file in LICENSE CHANGELOG.md; do
1617
if [ -e "${file}" ]; then cp "${file}" dist; fi
1718
done

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
testEnvironment: 'node',
55
moduleNameMapper: {
66
'^openai$': '<rootDir>/src/index.ts',
7-
'^openai/_shims/(.*)$': '<rootDir>/src/_shims/$1.node',
7+
'^openai/_shims/(.*)$': '<rootDir>/src/_shims/$1-node',
88
'^openai/(.*)$': '<rootDir>/src/$1',
99
},
1010
modulePathIgnorePatterns: ['<rootDir>/ecosystem-tests/', '<rootDir>/dist/', '<rootDir>/deno_tests/'],

package.json

+27-9
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
"license": "Apache-2.0",
1111
"private": false,
1212
"exports": {
13-
"./_shims/*": {
13+
"./_shims/*.mjs": {
1414
"deno": {
1515
"types": "./dist/_shims/*.d.ts",
16-
"require": "./dist/_shims/*.js",
1716
"default": "./dist/_shims/*.mjs"
1817
},
1918
"bun": {
@@ -23,28 +22,47 @@
2322
},
2423
"browser": {
2524
"types": "./dist/_shims/*.d.ts",
26-
"require": "./dist/_shims/*.js",
2725
"default": "./dist/_shims/*.mjs"
2826
},
2927
"worker": {
3028
"types": "./dist/_shims/*.d.ts",
31-
"require": "./dist/_shims/*.js",
3229
"default": "./dist/_shims/*.mjs"
3330
},
3431
"workerd": {
3532
"types": "./dist/_shims/*.d.ts",
36-
"require": "./dist/_shims/*.js",
3733
"default": "./dist/_shims/*.mjs"
3834
},
3935
"node": {
40-
"types": "./dist/_shims/*.node.d.ts",
41-
"require": "./dist/_shims/*.node.js",
42-
"default": "./dist/_shims/*.node.mjs"
36+
"types": "./dist/_shims/*-node.d.ts",
37+
"default": "./dist/_shims/*-node.mjs"
4338
},
4439
"types": "./dist/_shims/*.d.ts",
45-
"require": "./dist/_shims/*.js",
4640
"default": "./dist/_shims/*.mjs"
4741
},
42+
"./_shims/*.js": {
43+
"deno": {
44+
"types": "./dist/_shims/*.d.ts",
45+
"default": "./dist/_shims/*.js"
46+
},
47+
"browser": {
48+
"types": "./dist/_shims/*.d.ts",
49+
"default": "./dist/_shims/*.js"
50+
},
51+
"worker": {
52+
"types": "./dist/_shims/*.d.ts",
53+
"default": "./dist/_shims/*.js"
54+
},
55+
"workerd": {
56+
"types": "./dist/_shims/*.d.ts",
57+
"default": "./dist/_shims/*.js"
58+
},
59+
"node": {
60+
"types": "./dist/_shims/*-node.d.ts",
61+
"default": "./dist/_shims/*-node.js"
62+
},
63+
"types": "./dist/_shims/*.d.ts",
64+
"default": "./dist/_shims/*.js"
65+
},
4866
".": {
4967
"require": {
5068
"types": "./dist/index.d.ts",

scripts/replace-self-referencing-imports.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
Object.defineProperty(exports, '__esModule', { value: true });
33

44
const path = require('path');
5-
const distSrcDir = path.resolve(__dirname, '..', 'dist', 'src');
5+
const distDir = path.resolve(__dirname, '..', 'dist');
6+
const distSrcDir = path.join(distDir, 'src');
67

78
function replaceSelfReferencingImports({ orig, file, config }) {
89
// replace self-referencing imports in source files to reduce errors users will
910
// see if they go to definition
10-
if (!file.startsWith(distSrcDir)) return orig;
11+
if (!file.startsWith(distDir)) return orig;
12+
1113
return orig.replace(/['"]([^"'\r\n]+)['"]/, (match, importPath) => {
1214
if (!importPath.startsWith('openai/')) return match;
15+
if (!file.startsWith(distSrcDir)) {
16+
const ext = file.endsWith('.d.ts') ? '' : path.extname(file);
17+
const { dir, base } = path.parse(importPath);
18+
return JSON.stringify(`${dir}/${base}${ext}`);
19+
}
1320
let relativePath = path.relative(
1421
path.dirname(file),
1522
path.join(distSrcDir, importPath.substring('openai/'.length)),

scripts/resolve-full-paths.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
Object.defineProperty(exports, '__esModule', { value: true });
3+
4+
const path = require('path');
5+
6+
// tsc-alias --resolveFullPaths is buggy, it replaces 'formdata-node'
7+
// with 'formdata-node.js' because we have a file with that name
8+
function resolveFullPaths({ orig, file, config }) {
9+
return orig.replace(/['"]([^"'\r\n]+)['"]/, (match, importPath) => {
10+
if (!importPath.startsWith('.')) return match;
11+
const { dir, name } = path.parse(importPath);
12+
const ext = /\.mjs$/.test(file) ? '.mjs' : '.js';
13+
return JSON.stringify(`${dir}/${name}${ext}`);
14+
});
15+
}
16+
exports.default = resolveFullPaths;
File renamed without changes.
File renamed without changes.

src/_shims/agent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
33
*
44
* This is a stub for non-node environments.
5-
* In node environments, it gets replaced agent.node.ts by the package export map
5+
* In node environments, it gets replaced agent-node.ts by the package export map
66
*/
77

88
export type Agent = any;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/_shims/fileFromPath.node.ts src/_shims/fileFromPath-node.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import { fileFromPath as _fileFromPath } from 'formdata-node/file-from-path';
6-
import type { File, FilePropertyBag } from './formdata.node';
6+
import type { File, FilePropertyBag } from './form-data-node';
77

88
export type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>;
99

src/_shims/fileFromPath.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
22
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
33
*
4-
* This is a stub that gets replaced by fileFromPath.node.js for node environments
4+
* This is a stub that gets replaced by fileFromPath-node.js for node environments
55
* in the package export map
66
*/
77

8-
import type { FilePropertyBag, File } from './formdata';
8+
import type { FilePropertyBag, File } from './form-data';
99

1010
export type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>;
1111

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/_shims/getMultipartRequestOptions.node.ts src/_shims/getMultipartRequestOptions-node.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
33
*/
44

5-
import { FormData } from './formdata.node';
5+
import { FormData } from './form-data-node';
66
import type { RequestOptions } from '../core';
77
import { Readable } from 'node:stream';
88
import { FormDataEncoder } from 'form-data-encoder';

src/_shims/getMultipartRequestOptions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
33
*/
44

5-
import { FormData } from './formdata';
5+
import { FormData } from './form-data';
66
import type { RequestOptions } from '../core';
77
import { MultipartBody } from '../uploads';
88

File renamed without changes.

src/uploads.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type RequestOptions } from './core';
22
import { type Readable } from 'openai/_shims/node-readable';
33
import { type BodyInit } from 'openai/_shims/fetch';
4-
import { FormData, File, type Blob, type FilePropertyBag } from 'openai/_shims/formdata';
4+
import { FormData, File, type Blob, type FilePropertyBag } from 'openai/_shims/form-data';
55
import { getMultipartRequestOptions } from 'openai/_shims/getMultipartRequestOptions';
66
import { fileFromPath } from 'openai/_shims/fileFromPath';
77
import { type FsReadStream, isFsReadStream } from 'openai/_shims/node-readable';

tests/form.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { multipartFormRequestOptions, createForm } from 'openai/core';
2-
import { Blob } from 'openai/_shims/formdata';
2+
import { Blob } from 'openai/_shims/form-data';
33
import { toFile } from 'openai';
44

55
describe('form data validation', () => {

tests/uploads.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs';
22
import { toFile, type ResponseLike } from 'openai/uploads';
3-
import { File } from 'openai/_shims/formdata';
3+
import { File } from 'openai/_shims/form-data';
44

55
class MyClass {
66
name: string = 'foo';

tsconfig.build.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"compilerOptions": {
66
"rootDir": "./dist/src",
77
"paths": {
8-
"openai/_shims/*": ["dist/src/_shims/*.node"],
8+
"openai/_shims/*": ["dist/src/_shims/*-node"],
99
"openai": ["dist/src/index.ts"],
1010
"openai/*": ["dist/src/*"],
1111
"digest-fetch": ["./typings/digest-fetch"]
@@ -18,14 +18,17 @@
1818
"sourceMap": true
1919
},
2020
"tsc-alias": {
21-
"resolveFullPaths": true,
2221
"fileExtensions": {
2322
"inputGlob": "{mjs,cjs,js,jsx,mts,cts,ts,tsx}"
2423
},
2524
"replacers": {
26-
"replace-absolute-imports": {
25+
"replace-self-referencing-imports": {
2726
"enabled": true,
2827
"file": "./scripts/replace-self-referencing-imports.js"
28+
},
29+
"resolve-full-paths": {
30+
"enabled": true,
31+
"file": "./scripts/resolve-full-paths.js"
2932
}
3033
}
3134
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"esModuleInterop": true,
1010
"baseUrl": "./",
1111
"paths": {
12-
"openai/_shims/*": ["src/_shims/*.node"],
12+
"openai/_shims/*": ["src/_shims/*-node"],
1313
"openai": ["src/index.ts"],
1414
"openai/*": ["src/*"],
1515
"digest-fetch": ["./typings/digest-fetch"]

0 commit comments

Comments
 (0)