Skip to content

Commit

Permalink
fix(client): use explicit file extensions in _shims imports (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Sep 6, 2023
1 parent dafdf63 commit 16fe929
Show file tree
Hide file tree
Showing 29 changed files with 70 additions and 25 deletions.
1 change: 1 addition & 0 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rm -rf dist; mkdir dist
# Copy src to dist/src and build from dist/src into dist, so that
# the source map for index.js.map will refer to ./src/index.ts etc
cp -rp src README.md dist
rm dist/src/_shims/*-deno.*
for file in LICENSE CHANGELOG.md; do
if [ -e "${file}" ]; then cp "${file}" dist; fi
done
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
testEnvironment: 'node',
moduleNameMapper: {
'^openai$': '<rootDir>/src/index.ts',
'^openai/_shims/(.*)$': '<rootDir>/src/_shims/$1.node',
'^openai/_shims/(.*)$': '<rootDir>/src/_shims/$1-node',
'^openai/(.*)$': '<rootDir>/src/$1',
},
modulePathIgnorePatterns: ['<rootDir>/ecosystem-tests/', '<rootDir>/dist/', '<rootDir>/deno_tests/'],
Expand Down
36 changes: 27 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
"license": "Apache-2.0",
"private": false,
"exports": {
"./_shims/*": {
"./_shims/*.mjs": {
"deno": {
"types": "./dist/_shims/*.d.ts",
"require": "./dist/_shims/*.js",
"default": "./dist/_shims/*.mjs"
},
"bun": {
Expand All @@ -23,28 +22,47 @@
},
"browser": {
"types": "./dist/_shims/*.d.ts",
"require": "./dist/_shims/*.js",
"default": "./dist/_shims/*.mjs"
},
"worker": {
"types": "./dist/_shims/*.d.ts",
"require": "./dist/_shims/*.js",
"default": "./dist/_shims/*.mjs"
},
"workerd": {
"types": "./dist/_shims/*.d.ts",
"require": "./dist/_shims/*.js",
"default": "./dist/_shims/*.mjs"
},
"node": {
"types": "./dist/_shims/*.node.d.ts",
"require": "./dist/_shims/*.node.js",
"default": "./dist/_shims/*.node.mjs"
"types": "./dist/_shims/*-node.d.ts",
"default": "./dist/_shims/*-node.mjs"
},
"types": "./dist/_shims/*.d.ts",
"require": "./dist/_shims/*.js",
"default": "./dist/_shims/*.mjs"
},
"./_shims/*.js": {
"deno": {
"types": "./dist/_shims/*.d.ts",
"default": "./dist/_shims/*.js"
},
"browser": {
"types": "./dist/_shims/*.d.ts",
"default": "./dist/_shims/*.js"
},
"worker": {
"types": "./dist/_shims/*.d.ts",
"default": "./dist/_shims/*.js"
},
"workerd": {
"types": "./dist/_shims/*.d.ts",
"default": "./dist/_shims/*.js"
},
"node": {
"types": "./dist/_shims/*-node.d.ts",
"default": "./dist/_shims/*-node.js"
},
"types": "./dist/_shims/*.d.ts",
"default": "./dist/_shims/*.js"
},
".": {
"require": {
"types": "./dist/index.d.ts",
Expand Down
11 changes: 9 additions & 2 deletions scripts/replace-self-referencing-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
Object.defineProperty(exports, '__esModule', { value: true });

const path = require('path');
const distSrcDir = path.resolve(__dirname, '..', 'dist', 'src');
const distDir = path.resolve(__dirname, '..', 'dist');
const distSrcDir = path.join(distDir, 'src');

function replaceSelfReferencingImports({ orig, file, config }) {
// replace self-referencing imports in source files to reduce errors users will
// see if they go to definition
if (!file.startsWith(distSrcDir)) return orig;
if (!file.startsWith(distDir)) return orig;

return orig.replace(/['"]([^"'\r\n]+)['"]/, (match, importPath) => {
if (!importPath.startsWith('openai/')) return match;
if (!file.startsWith(distSrcDir)) {
const ext = file.endsWith('.d.ts') ? '' : path.extname(file);
const { dir, base } = path.parse(importPath);
return JSON.stringify(`${dir}/${base}${ext}`);
}
let relativePath = path.relative(
path.dirname(file),
path.join(distSrcDir, importPath.substring('openai/'.length)),
Expand Down
16 changes: 16 additions & 0 deletions scripts/resolve-full-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });

const path = require('path');

// tsc-alias --resolveFullPaths is buggy, it replaces 'formdata-node'
// with 'formdata-node.js' because we have a file with that name
function resolveFullPaths({ orig, file, config }) {
return orig.replace(/['"]([^"'\r\n]+)['"]/, (match, importPath) => {
if (!importPath.startsWith('.')) return match;
const { dir, name } = path.parse(importPath);
const ext = /\.mjs$/.test(file) ? '.mjs' : '.js';
return JSON.stringify(`${dir}/${name}${ext}`);
});
}
exports.default = resolveFullPaths;
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/_shims/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*
* This is a stub for non-node environments.
* In node environments, it gets replaced agent.node.ts by the package export map
* In node environments, it gets replaced agent-node.ts by the package export map
*/

export type Agent = any;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/_shims/fileFromPath.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*
* This is a stub that gets replaced by fileFromPath.node.js for node environments
* This is a stub that gets replaced by fileFromPath-node.js for node environments
* in the package export map
*/

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

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

Expand Down
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.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/

import { FormData } from './formdata.node';
import { FormData } from './form-data-node';
import type { RequestOptions } from '../core';
import { Readable } from 'node:stream';
import { FormDataEncoder } from 'form-data-encoder';
Expand Down
2 changes: 1 addition & 1 deletion src/_shims/getMultipartRequestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/

import { FormData } from './formdata';
import { FormData } from './form-data';
import type { RequestOptions } from '../core';
import { MultipartBody } from '../uploads';

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/uploads.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type RequestOptions } from './core';
import { type Readable } from 'openai/_shims/node-readable';
import { type BodyInit } from 'openai/_shims/fetch';
import { FormData, File, type Blob, type FilePropertyBag } from 'openai/_shims/formdata';
import { FormData, File, type Blob, type FilePropertyBag } from 'openai/_shims/form-data';
import { getMultipartRequestOptions } from 'openai/_shims/getMultipartRequestOptions';
import { fileFromPath } from 'openai/_shims/fileFromPath';
import { type FsReadStream, isFsReadStream } from 'openai/_shims/node-readable';
Expand Down
2 changes: 1 addition & 1 deletion tests/form.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { multipartFormRequestOptions, createForm } from 'openai/core';
import { Blob } from 'openai/_shims/formdata';
import { Blob } from 'openai/_shims/form-data';
import { toFile } from 'openai';

describe('form data validation', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/uploads.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import { toFile, type ResponseLike } from 'openai/uploads';
import { File } from 'openai/_shims/formdata';
import { File } from 'openai/_shims/form-data';

class MyClass {
name: string = 'foo';
Expand Down
9 changes: 6 additions & 3 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compilerOptions": {
"rootDir": "./dist/src",
"paths": {
"openai/_shims/*": ["dist/src/_shims/*.node"],
"openai/_shims/*": ["dist/src/_shims/*-node"],
"openai": ["dist/src/index.ts"],
"openai/*": ["dist/src/*"],
"digest-fetch": ["./typings/digest-fetch"]
Expand All @@ -18,14 +18,17 @@
"sourceMap": true
},
"tsc-alias": {
"resolveFullPaths": true,
"fileExtensions": {
"inputGlob": "{mjs,cjs,js,jsx,mts,cts,ts,tsx}"
},
"replacers": {
"replace-absolute-imports": {
"replace-self-referencing-imports": {
"enabled": true,
"file": "./scripts/replace-self-referencing-imports.js"
},
"resolve-full-paths": {
"enabled": true,
"file": "./scripts/resolve-full-paths.js"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"esModuleInterop": true,
"baseUrl": "./",
"paths": {
"openai/_shims/*": ["src/_shims/*.node"],
"openai/_shims/*": ["src/_shims/*-node"],
"openai": ["src/index.ts"],
"openai/*": ["src/*"],
"digest-fetch": ["./typings/digest-fetch"]
Expand Down

0 comments on commit 16fe929

Please sign in to comment.