diff --git a/scripts/fix-index-exports.cjs b/scripts/fix-index-exports.cjs index 0909af7c..b61b2ea3 100644 --- a/scripts/fix-index-exports.cjs +++ b/scripts/fix-index-exports.cjs @@ -1,7 +1,11 @@ const fs = require('fs'); const path = require('path'); -const indexJs = path.resolve(__dirname, '..', 'dist', 'index.js'); +const indexJs = + process.env['DIST_PATH'] ? + path.resolve(process.env['DIST_PATH'], 'index.js') + : path.resolve(__dirname, '..', 'dist', 'index.js'); + let before = fs.readFileSync(indexJs, 'utf8'); let after = before.replace( /^\s*exports\.default\s*=\s*(\w+)/m, diff --git a/scripts/make-dist-package-json.cjs b/scripts/make-dist-package-json.cjs index def768ed..d4a0a69b 100644 --- a/scripts/make-dist-package-json.cjs +++ b/scripts/make-dist-package-json.cjs @@ -1,4 +1,4 @@ -const pkgJson = require('../package.json'); +const pkgJson = require(process.env['PKG_JSON_PATH'] || '../package.json'); function processExportMap(m) { for (const key in m) { diff --git a/scripts/postprocess-files.cjs b/scripts/postprocess-files.cjs index f990dbc5..cbe266dc 100644 --- a/scripts/postprocess-files.cjs +++ b/scripts/postprocess-files.cjs @@ -2,7 +2,12 @@ const fs = require('fs'); const path = require('path'); const { parse } = require('@typescript-eslint/parser'); -const distDir = path.resolve(__dirname, '..', 'dist'); +const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? '@anthropic-ai/sdk/' + +const distDir = + process.env['DIST_PATH'] ? + path.resolve(process.env['DIST_PATH']) + : path.resolve(__dirname, '..', 'dist'); const distSrcDir = path.join(distDir, 'src'); /** @@ -105,11 +110,11 @@ async function postprocess() { let transformed = mapModulePaths(code, (importPath) => { if (file.startsWith(distSrcDir)) { - if (importPath.startsWith('@anthropic-ai/sdk/')) { + if (importPath.startsWith(pkgImportPath)) { // convert self-references in dist/src to relative paths let relativePath = path.relative( path.dirname(file), - path.join(distSrcDir, importPath.substring('@anthropic-ai/sdk/'.length)), + path.join(distSrcDir, importPath.substring(pkgImportPath.length)), ); if (!relativePath.startsWith('.')) relativePath = `./${relativePath}`; return relativePath; diff --git a/src/core.ts b/src/core.ts index 756d7bc3..404e9b01 100644 --- a/src/core.ts +++ b/src/core.ts @@ -342,6 +342,11 @@ export abstract class APIClient { return reqHeaders; } + /** + * Used as a callback for mutating the given `FinalRequestOptions` object. + */ + protected async prepareOptions(options: FinalRequestOptions): Promise {} + /** * Used as a callback for mutating the given `RequestInit` object. * @@ -387,6 +392,8 @@ export abstract class APIClient { retriesRemaining = options.maxRetries ?? this.maxRetries; } + await this.prepareOptions(options); + const { req, url, timeout } = this.buildRequest(options); await this.prepareRequest(req, { url, options }); @@ -1139,3 +1146,7 @@ export const toBase64 = (str: string | null | undefined): string => { throw new AnthropicError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined'); }; + +export function isObj(obj: unknown): obj is Record { + return obj != null && typeof obj === 'object' && !Array.isArray(obj); +}