diff --git a/.github/workflows/open-release-prs.yml b/.github/workflows/open-release-prs.yml deleted file mode 100644 index d088bc86..00000000 --- a/.github/workflows/open-release-prs.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Open release PRs -on: - push: - branches: - - next - -jobs: - release: - name: release - if: github.ref == 'refs/heads/next' && github.repository == 'anthropics/anthropic-sdk-typescript' - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - - uses: stainless-api/trigger-release-please@v1 - id: release - with: - repo: ${{ github.event.repository.full_name }} - stainless-api-key: ${{ secrets.STAINLESS_API_KEY }} - branch-with-changes: next diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 7e23a4f8..eb5abf78 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.6.3" + ".": "0.6.4" } diff --git a/CHANGELOG.md b/CHANGELOG.md index 5599e881..9723fe35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,32 @@ # Changelog +## 0.6.4 (2023-09-08) + +Full Changelog: [v0.6.3...v0.6.4](https://github.com/anthropics/anthropic-sdk-typescript/compare/v0.6.3...v0.6.4) + +### Features + +* **package:** add Bun export map ([#139](https://github.com/anthropics/anthropic-sdk-typescript/issues/139)) ([ba3310d](https://github.com/anthropics/anthropic-sdk-typescript/commit/ba3310d903cd5fda91168266335f4e445e60cad4)) + + +### Bug Fixes + +* **client:** fix TS errors that appear when users Go to Source in VSCode ([#142](https://github.com/anthropics/anthropic-sdk-typescript/issues/142)) ([f7bfbea](https://github.com/anthropics/anthropic-sdk-typescript/commit/f7bfbeaa54d364201bbe5cddf3132875ae2a3ccf)) +* **client:** handle case where the client is instantiated with a undefined baseURL ([#143](https://github.com/anthropics/anthropic-sdk-typescript/issues/143)) ([10e5203](https://github.com/anthropics/anthropic-sdk-typescript/commit/10e52034990d90dcdaf26672ea384545b88ddf35)) +* **client:** use explicit file extensions in _shims imports ([#141](https://github.com/anthropics/anthropic-sdk-typescript/issues/141)) ([10fd687](https://github.com/anthropics/anthropic-sdk-typescript/commit/10fd68742a202c5c0a8b520db190c239dce9b676)) +* fix module not found errors in Vercel edge ([#148](https://github.com/anthropics/anthropic-sdk-typescript/issues/148)) ([72e51a1](https://github.com/anthropics/anthropic-sdk-typescript/commit/72e51a170855281a8d099b00c6fb1e9ccb276212)) +* **readme:** update link to api.md to use the correct branch ([#145](https://github.com/anthropics/anthropic-sdk-typescript/issues/145)) ([5db78ed](https://github.com/anthropics/anthropic-sdk-typescript/commit/5db78edec4826f86b2fc21ee3f470b49a4987029)) + + +### Chores + +* **internal:** export helper from core ([#147](https://github.com/anthropics/anthropic-sdk-typescript/issues/147)) ([7e79de1](https://github.com/anthropics/anthropic-sdk-typescript/commit/7e79de14edeab8110d740e996653e9f9cc2299a4)) + + +### Documentation + +* **readme:** add link to api.md ([#144](https://github.com/anthropics/anthropic-sdk-typescript/issues/144)) ([716c9f0](https://github.com/anthropics/anthropic-sdk-typescript/commit/716c9f0714c0e9c26cb6cdcb007457aff1284cf4)) + ## 0.6.3 (2023-08-28) Full Changelog: [v0.6.2...v0.6.3](https://github.com/anthropics/anthropic-sdk-typescript/compare/v0.6.2...v0.6.3) diff --git a/README.md b/README.md index eeb35339..826401be 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,8 @@ yarn add @anthropic-ai/sdk ## Usage +The full API of this library can be found in [api.md](https://www.github.com/anthropics/anthropic-sdk-typescript/blob/main/api.md). + ```js import Anthropic from '@anthropic-ai/sdk'; @@ -330,6 +332,8 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ant ## Requirements +TypeScript >= 4.5 is supported. + The following runtimes are supported: - Node.js 16 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions. diff --git a/build b/build index 4c9c9608..acfd0492 100755 --- a/build +++ b/build @@ -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 @@ -37,14 +38,14 @@ node scripts/fix-index-exports.cjs # index.d.mts the default import will work (even though both files have # the same export default statement) cp dist/index.d.ts dist/index.d.mts - -SED=(sed -i) -if [[ "$OSTYPE" == "darwin"* ]]; then SED=(sed -i ''); fi +cp tsconfig.dist-src.json dist/src/tsconfig.json # strip out lib="dom" and types="node" references; these are needed at build time, # but would pollute the user's TS environment -REFERENCE_SUBS='s/^ *\/\/\/ */src/index.ts', - '^@anthropic-ai/sdk/_shims/(.*)$': '/src/_shims/$1.node', + '^@anthropic-ai/sdk/_shims/(.*)$': '/src/_shims/$1-node', '^@anthropic-ai/sdk/(.*)$': '/src/$1', }, modulePathIgnorePatterns: ['/ecosystem-tests/', '/dist/', '/deno_tests/'], diff --git a/package.json b/package.json index 85752216..184a1a34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@anthropic-ai/sdk", - "version": "0.6.3", + "version": "0.6.4", "description": "Client library for the Anthropic API", "author": "Anthropic ", "types": "dist/index.d.ts", @@ -16,6 +16,11 @@ "require": "./dist/_shims/*.js", "default": "./dist/_shims/*.mjs" }, + "bun": { + "types": "./dist/_shims/*.d.ts", + "require": "./dist/_shims/*.js", + "default": "./dist/_shims/*.mjs" + }, "browser": { "types": "./dist/_shims/*.d.ts", "require": "./dist/_shims/*.js", @@ -32,9 +37,9 @@ "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", + "require": "./dist/_shims/*-node.js", + "default": "./dist/_shims/*-node.mjs" }, "types": "./dist/_shims/*.d.ts", "require": "./dist/_shims/*.js", diff --git a/scripts/remove-triple-slash-references.js b/scripts/remove-triple-slash-references.js new file mode 100644 index 00000000..d47d45b7 --- /dev/null +++ b/scripts/remove-triple-slash-references.js @@ -0,0 +1,11 @@ +// strip out lib="dom" and types="node" references; these are needed at build time, +// but would pollute the user's TS environment +const fs = require('fs'); +for (const file of process.argv.slice(2)) { + const before = fs.readFileSync(file, 'utf8'); + const after = before.replace(/^ *\/\/\/ * { if (!importPath.startsWith('@anthropic-ai/sdk/')) return match; + if (!file.startsWith(distSrcDir)) return match; let relativePath = path.relative( path.dirname(file), path.join(distSrcDir, importPath.substring('@anthropic-ai/sdk/'.length)), diff --git a/scripts/replace-shim-guards.js b/scripts/replace-shim-guards.js new file mode 100644 index 00000000..17bd9fb5 --- /dev/null +++ b/scripts/replace-shim-guards.js @@ -0,0 +1,14 @@ +// strip out `unknown extends RequestInit ? never :` from dist/src/_shims; +// these cause problems when viewing the .ts source files in go to definition +const fs = require('fs'); +for (const file of process.argv.slice(2)) { + const before = fs.readFileSync(file, 'utf8'); + const after = before.replace( + new RegExp('unknown extends (typeof )?\\S+ \\? \\S+ :\\s*'.replace(/\s+/, '\\s+'), 'gm'), + '', + ); + if (after !== before) { + fs.writeFileSync(file, after, 'utf8'); + console.error('wrote', file); + } +} diff --git a/scripts/resolve-full-paths.js b/scripts/resolve-full-paths.js new file mode 100644 index 00000000..f8f979ad --- /dev/null +++ b/scripts/resolve-full-paths.js @@ -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; diff --git a/src/_shims/ReadableStream.node.ts b/src/_shims/ReadableStream-node.ts similarity index 100% rename from src/_shims/ReadableStream.node.ts rename to src/_shims/ReadableStream-node.ts diff --git a/src/_shims/agent.node.ts b/src/_shims/agent-node.ts similarity index 100% rename from src/_shims/agent.node.ts rename to src/_shims/agent-node.ts diff --git a/src/_shims/agent.ts b/src/_shims/agent.ts index c39d1cfb..24e5bf7c 100644 --- a/src/_shims/agent.ts +++ b/src/_shims/agent.ts @@ -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; diff --git a/src/_shims/fetch.deno.ts b/src/_shims/fetch-deno.ts similarity index 84% rename from src/_shims/fetch.deno.ts rename to src/_shims/fetch-deno.ts index fbc2a8dd..c7ef4ffc 100644 --- a/src/_shims/fetch.deno.ts +++ b/src/_shims/fetch-deno.ts @@ -1,11 +1,15 @@ const _fetch = fetch; +type _fetch = typeof fetch; const _Request = Request; +type _Request = Request; type _RequestInfo = RequestInfo; type _RequestInit = RequestInit; const _Response = Response; +type _Response = Response; type _ResponseInit = ResponseInit; type _BodyInit = BodyInit; const _Headers = Headers; +type _Headers = Headers; type _HeadersInit = HeadersInit; export const isPolyfilled = false; diff --git a/src/_shims/fetch.node.d.ts b/src/_shims/fetch-node.d.ts similarity index 100% rename from src/_shims/fetch.node.d.ts rename to src/_shims/fetch-node.d.ts diff --git a/src/_shims/fetch.node.js b/src/_shims/fetch-node.js similarity index 100% rename from src/_shims/fetch.node.js rename to src/_shims/fetch-node.js diff --git a/src/_shims/fetch.node.mjs b/src/_shims/fetch-node.mjs similarity index 100% rename from src/_shims/fetch.node.mjs rename to src/_shims/fetch-node.mjs diff --git a/src/_shims/fileFromPath.node.ts b/src/_shims/fileFromPath-node.ts similarity index 93% rename from src/_shims/fileFromPath.node.ts rename to src/_shims/fileFromPath-node.ts index 676d2e2d..065fd7ec 100644 --- a/src/_shims/fileFromPath.node.ts +++ b/src/_shims/fileFromPath-node.ts @@ -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; diff --git a/src/_shims/fileFromPath.ts b/src/_shims/fileFromPath.ts index 57692354..0ec8a23c 100644 --- a/src/_shims/fileFromPath.ts +++ b/src/_shims/fileFromPath.ts @@ -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; diff --git a/src/_shims/formdata.deno.ts b/src/_shims/form-data-deno.ts similarity index 84% rename from src/_shims/formdata.deno.ts rename to src/_shims/form-data-deno.ts index 56e31b30..ec0349eb 100644 --- a/src/_shims/formdata.deno.ts +++ b/src/_shims/form-data-deno.ts @@ -2,8 +2,11 @@ type _BlobPropertyBag = BlobPropertyBag; type _FilePropertyBag = FilePropertyBag; const _FormData = FormData; +type _FormData = FormData; const _File = File; +type _File = File; const _Blob = Blob; +type _Blob = Blob; export const isPolyfilled = false; diff --git a/src/_shims/formdata.node.d.ts b/src/_shims/form-data-node.d.ts similarity index 100% rename from src/_shims/formdata.node.d.ts rename to src/_shims/form-data-node.d.ts diff --git a/src/_shims/formdata.node.js b/src/_shims/form-data-node.js similarity index 100% rename from src/_shims/formdata.node.js rename to src/_shims/form-data-node.js diff --git a/src/_shims/formdata.node.mjs b/src/_shims/form-data-node.mjs similarity index 100% rename from src/_shims/formdata.node.mjs rename to src/_shims/form-data-node.mjs diff --git a/src/_shims/formdata.d.ts b/src/_shims/form-data.d.ts similarity index 100% rename from src/_shims/formdata.d.ts rename to src/_shims/form-data.d.ts diff --git a/src/_shims/formdata.js b/src/_shims/form-data.js similarity index 100% rename from src/_shims/formdata.js rename to src/_shims/form-data.js diff --git a/src/_shims/formdata.mjs b/src/_shims/form-data.mjs similarity index 100% rename from src/_shims/formdata.mjs rename to src/_shims/form-data.mjs diff --git a/src/_shims/getMultipartRequestOptions.node.ts b/src/_shims/getMultipartRequestOptions-node.ts similarity index 94% rename from src/_shims/getMultipartRequestOptions.node.ts rename to src/_shims/getMultipartRequestOptions-node.ts index 3d690749..ac13a075 100644 --- a/src/_shims/getMultipartRequestOptions.node.ts +++ b/src/_shims/getMultipartRequestOptions-node.ts @@ -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'; diff --git a/src/_shims/getMultipartRequestOptions.ts b/src/_shims/getMultipartRequestOptions.ts index b9abe643..f74227d0 100644 --- a/src/_shims/getMultipartRequestOptions.ts +++ b/src/_shims/getMultipartRequestOptions.ts @@ -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'; diff --git a/src/_shims/node-readable.node.ts b/src/_shims/node-readable-node.ts similarity index 100% rename from src/_shims/node-readable.node.ts rename to src/_shims/node-readable-node.ts diff --git a/src/core.ts b/src/core.ts index 9415f257..8e3a9930 100644 --- a/src/core.ts +++ b/src/core.ts @@ -52,7 +52,7 @@ async function defaultParseResponse(props: APIResponseProps): Promise { // TODO handle blob, arraybuffer, other content types, etc. const text = await response.text(); debug('response', response.status, response.url, response.headers, text); - return text as T; + return text as any as T; } /** @@ -313,7 +313,8 @@ export abstract class APIClient { protected parseHeaders(headers: HeadersInit | null | undefined): Record { return ( !headers ? {} - : Symbol.iterator in headers ? Object.fromEntries(Array.from(headers).map((header) => [...header])) + : Symbol.iterator in headers ? + Object.fromEntries(Array.from(headers as Iterable).map((header) => [...header])) : { ...headers } ); } @@ -397,7 +398,7 @@ export abstract class APIClient { return new PagePromise(this, request, Page); } - buildURL(path: string, query: Req | undefined): string { + buildURL>(path: string, query: Req | null | undefined): string { const url = isAbsoluteURL(path) ? new URL(path) @@ -900,7 +901,7 @@ const isAbsoluteURL = (url: string): boolean => { return startsWithSchemeRegexp.test(url); }; -const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); +export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); const validatePositiveInteger = (name: string, n: unknown): number => { if (typeof n !== 'number' || !Number.isInteger(n)) { diff --git a/src/error.ts b/src/error.ts index ebb56bff..7cc10ab4 100644 --- a/src/error.ts +++ b/src/error.ts @@ -97,8 +97,8 @@ export class APIConnectionError extends APIError { } export class APIConnectionTimeoutError extends APIConnectionError { - constructor() { - super({ message: 'Request timed out.' }); + constructor({ message }: { message?: string } = {}) { + super({ message: message ?? 'Request timed out.' }); } } diff --git a/src/index.ts b/src/index.ts index 9fbde896..65ec88c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -97,8 +97,8 @@ export class Anthropic extends Core.APIClient { const options: ClientOptions = { apiKey, authToken, - baseURL: `https://api.anthropic.com`, ...opts, + baseURL: opts.baseURL ?? `https://api.anthropic.com`, }; super({ diff --git a/src/uploads.ts b/src/uploads.ts index 055878e3..fd16f68e 100644 --- a/src/uploads.ts +++ b/src/uploads.ts @@ -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'; diff --git a/src/version.ts b/src/version.ts index ae4dccc6..67bde7c2 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export const VERSION = '0.6.3'; // x-release-please-version +export const VERSION = '0.6.4'; // x-release-please-version diff --git a/tests/form.test.ts b/tests/form.test.ts index 4e8056f2..7628ddbd 100644 --- a/tests/form.test.ts +++ b/tests/form.test.ts @@ -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', () => { diff --git a/tests/uploads.test.ts b/tests/uploads.test.ts index a52764f4..85f8bef6 100644 --- a/tests/uploads.test.ts +++ b/tests/uploads.test.ts @@ -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'; diff --git a/tsconfig.build.json b/tsconfig.build.json index 9c9d8244..a83edac9 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -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"] @@ -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" } } } diff --git a/tsconfig.dist-src.json b/tsconfig.dist-src.json new file mode 100644 index 00000000..e9f2d70b --- /dev/null +++ b/tsconfig.dist-src.json @@ -0,0 +1,11 @@ +{ + // this config is included in the published src directory to prevent TS errors + // from appearing when users go to source, and VSCode opens the source .ts file + // via declaration maps + "include": ["index.ts"], + "compilerOptions": { + "target": "es2015", + "lib": ["DOM"], + "moduleResolution": "node" + } +} diff --git a/tsconfig.json b/tsconfig.json index 6ec26e6a..70bb78ed 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"]