Skip to content

Commit 102bc8c

Browse files
stainless-botRobertCraigie
authored andcommitted
chore(internal): add internal helpers & improve build scripts (#261)
1 parent 86d728e commit 102bc8c

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

scripts/fix-index-exports.cjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const fs = require('fs');
22
const path = require('path');
33

4-
const indexJs = path.resolve(__dirname, '..', 'dist', 'index.js');
4+
const indexJs =
5+
process.env['DIST_PATH'] ?
6+
path.resolve(process.env['DIST_PATH'], 'index.js')
7+
: path.resolve(__dirname, '..', 'dist', 'index.js');
8+
59
let before = fs.readFileSync(indexJs, 'utf8');
610
let after = before.replace(
711
/^\s*exports\.default\s*=\s*(\w+)/m,

scripts/make-dist-package-json.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const pkgJson = require('../package.json');
1+
const pkgJson = require(process.env['PKG_JSON_PATH'] || '../package.json');
22

33
function processExportMap(m) {
44
for (const key in m) {

scripts/postprocess-files.cjs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ const fs = require('fs');
22
const path = require('path');
33
const { parse } = require('@typescript-eslint/parser');
44

5-
const distDir = path.resolve(__dirname, '..', 'dist');
5+
const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? '@anthropic-ai/sdk/'
6+
7+
const distDir =
8+
process.env['DIST_PATH'] ?
9+
path.resolve(process.env['DIST_PATH'])
10+
: path.resolve(__dirname, '..', 'dist');
611
const distSrcDir = path.join(distDir, 'src');
712

813
/**
@@ -105,11 +110,11 @@ async function postprocess() {
105110

106111
let transformed = mapModulePaths(code, (importPath) => {
107112
if (file.startsWith(distSrcDir)) {
108-
if (importPath.startsWith('@anthropic-ai/sdk/')) {
113+
if (importPath.startsWith(pkgImportPath)) {
109114
// convert self-references in dist/src to relative paths
110115
let relativePath = path.relative(
111116
path.dirname(file),
112-
path.join(distSrcDir, importPath.substring('@anthropic-ai/sdk/'.length)),
117+
path.join(distSrcDir, importPath.substring(pkgImportPath.length)),
113118
);
114119
if (!relativePath.startsWith('.')) relativePath = `./${relativePath}`;
115120
return relativePath;

src/core.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ export abstract class APIClient {
342342
return reqHeaders;
343343
}
344344

345+
/**
346+
* Used as a callback for mutating the given `FinalRequestOptions` object.
347+
*/
348+
protected async prepareOptions(options: FinalRequestOptions): Promise<void> {}
349+
345350
/**
346351
* Used as a callback for mutating the given `RequestInit` object.
347352
*
@@ -387,6 +392,8 @@ export abstract class APIClient {
387392
retriesRemaining = options.maxRetries ?? this.maxRetries;
388393
}
389394

395+
await this.prepareOptions(options);
396+
390397
const { req, url, timeout } = this.buildRequest(options);
391398

392399
await this.prepareRequest(req, { url, options });
@@ -1139,3 +1146,7 @@ export const toBase64 = (str: string | null | undefined): string => {
11391146

11401147
throw new AnthropicError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');
11411148
};
1149+
1150+
export function isObj(obj: unknown): obj is Record<string, unknown> {
1151+
return obj != null && typeof obj === 'object' && !Array.isArray(obj);
1152+
}

0 commit comments

Comments
 (0)