Skip to content

Commit

Permalink
chore(internal): add internal helpers & improve build scripts (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Jan 22, 2024
1 parent d4db6b7 commit d12b655
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
6 changes: 5 additions & 1 deletion scripts/fix-index-exports.cjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion scripts/make-dist-package-json.cjs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
11 changes: 8 additions & 3 deletions scripts/postprocess-files.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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/bedrock-sdk/'

const distDir =
process.env['DIST_PATH'] ?
path.resolve(process.env['DIST_PATH'])
: path.resolve(__dirname, '..', 'dist');
const distSrcDir = path.join(distDir, 'src');

/**
Expand Down Expand Up @@ -105,11 +110,11 @@ async function postprocess() {

let transformed = mapModulePaths(code, (importPath) => {
if (file.startsWith(distSrcDir)) {
if (importPath.startsWith('@anthropic-ai/bedrock-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/bedrock-sdk/'.length)),
path.join(distSrcDir, importPath.substring(pkgImportPath.length)),
);
if (!relativePath.startsWith('.')) relativePath = `./${relativePath}`;
return relativePath;
Expand Down
11 changes: 11 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {}

/**
* Used as a callback for mutating the given `RequestInit` object.
*
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -1140,3 +1147,7 @@ export const toBase64 = (str: string | null | undefined): string => {

throw new AnthropicBedrockError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');
};

export function isObj(obj: unknown): obj is Record<string, unknown> {
return obj != null && typeof obj === 'object' && !Array.isArray(obj);
}

0 comments on commit d12b655

Please sign in to comment.