Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {
'^@anthropic-ai/sdk$': '<rootDir>/src/index.ts',
'^@anthropic-ai/sdk/_shims/(.*)$': '<rootDir>/src/_shims/$1.node',
'^@anthropic-ai/sdk/_shims/(.*)$': '<rootDir>/src/_shims/$1-node',
'^@anthropic-ai/sdk/(.*)$': '<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": "MIT",
"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('@anthropic-ai/sdk/')) 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('@anthropic-ai/sdk/'.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 '@anthropic-ai/sdk/_shims/node-readable';
import { type BodyInit } from '@anthropic-ai/sdk/_shims/fetch';
import { FormData, File, type Blob, type FilePropertyBag } from '@anthropic-ai/sdk/_shims/formdata';
import { FormData, File, type Blob, type FilePropertyBag } from '@anthropic-ai/sdk/_shims/form-data';
import { getMultipartRequestOptions } from '@anthropic-ai/sdk/_shims/getMultipartRequestOptions';
import { fileFromPath } from '@anthropic-ai/sdk/_shims/fileFromPath';
import { type FsReadStream, isFsReadStream } from '@anthropic-ai/sdk/_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 '@anthropic-ai/sdk/core';
import { Blob } from '@anthropic-ai/sdk/_shims/formdata';
import { Blob } from '@anthropic-ai/sdk/_shims/form-data';
import { toFile } from '@anthropic-ai/sdk';

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 '@anthropic-ai/sdk/uploads';
import { File } from '@anthropic-ai/sdk/_shims/formdata';
import { File } from '@anthropic-ai/sdk/_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": {
"@anthropic-ai/sdk/_shims/*": ["dist/src/_shims/*.node"],
"@anthropic-ai/sdk/_shims/*": ["dist/src/_shims/*-node"],
"@anthropic-ai/sdk": ["dist/src/index.ts"],
"@anthropic-ai/sdk/*": ["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": {
"@anthropic-ai/sdk/_shims/*": ["src/_shims/*.node"],
"@anthropic-ai/sdk/_shims/*": ["src/_shims/*-node"],
"@anthropic-ai/sdk": ["src/index.ts"],
"@anthropic-ai/sdk/*": ["src/*"],
"digest-fetch": ["./typings/digest-fetch"]
Expand Down