Skip to content

Commit c86b059

Browse files
stainless-botrattrayalex
authored andcommitted
feat(client): add support for accessing the raw response object (#105)
1 parent 9af1527 commit c86b059

File tree

14 files changed

+576
-322
lines changed

14 files changed

+576
-322
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,32 @@ On timeout, an `APIConnectionTimeoutError` is thrown.
263263

264264
Note that requests which time out will be [retried twice by default](#retries).
265265

266+
## Advanced Usage
267+
268+
### Accessing raw Response data (e.g., headers)
269+
270+
The "raw" `Response` returned by `fetch()` can be accessed through the `.asResponse()` method on the `APIPromise` type that all methods return.
271+
272+
You can also use the `.withResponse()` method to get the raw `Response` along with the parsed data.
273+
274+
```ts
275+
const anthropic = new Anthropic();
276+
277+
const response = await anthropic.completions
278+
.create({
279+
prompt: `${Anthropic.HUMAN_PROMPT} Can you help me effectively ask for a raise at work? ${Anthropic.AI_PROMPT}`,
280+
max_tokens_to_sample: 300,
281+
model: 'claude-2',
282+
})
283+
.asResponse();
284+
console.log(response.headers.get('X-My-Header'));
285+
console.log(response.raw.statusText); // access the underlying Response object
286+
287+
// parses the response body, returning an object if the API responds with JSON
288+
const completion: Completions.Completion = await response.parse();
289+
console.log(completion.completion);
290+
```
291+
266292
## Configuring an HTTP(S) Agent (e.g., for proxies)
267293

268294
By default, this library uses a stable agent for all http/https requests to reuse TCP connections, eliminating many TCP & TLS handshakes and shaving around 100ms off most requests.

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ module.exports = {
77
'^@anthropic-ai/sdk/_shims/(.*)$': '<rootDir>/src/_shims/$1.node',
88
'^@anthropic-ai/sdk/(.*)$': '<rootDir>/src/$1',
99
},
10-
modulePathIgnorePatterns: ['<rootDir>/ecosystem-tests/', '<rootDir>/dist/'],
10+
modulePathIgnorePatterns: ['<rootDir>/ecosystem-tests/', '<rootDir>/dist/', '<rootDir>/deno_tests/'],
1111
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"ts-node": "^10.5.0",
9696
"tsc-alias": "^1.8.6",
9797
"tsc-multi": "^1.1.0",
98-
"tsconfig-paths": "^3.12.0",
98+
"tsconfig-paths": "^4.0.0",
9999
"typescript": "^4.8.2"
100100
}
101101
}

src/_shims/ReadableStream.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
/**
6+
* >>> Confused? <<<
7+
*
8+
* If you're getting errors from these types, try adding "lib": ["DOM"]
9+
* to your tsconfig.json, or otherwise configure the appropriate builtin
10+
* `ReadableStream` type for your environment.
11+
*/
12+
13+
// @ts-ignore
14+
type _ReadableStream<R = any> = unknown extends ReadableStream ? never : ReadableStream<R>;
15+
declare const _ReadableStream: {
16+
prototype: _ReadableStream;
17+
new (
18+
underlyingSource: _UnderlyingByteSource,
19+
strategy?: { highWaterMark?: number },
20+
): _ReadableStream<Uint8Array>;
21+
new <R = any>(
22+
underlyingSource: _UnderlyingDefaultSource<R>,
23+
strategy?: _QueuingStrategy<R>,
24+
): _ReadableStream<R>;
25+
new <R = any>(underlyingSource?: _UnderlyingSource<R>, strategy?: _QueuingStrategy<R>): _ReadableStream<R>;
26+
};
27+
28+
// @ts-ignore
29+
type _UnderlyingSource<R = any> = unknown extends UnderlyingSource ? never : UnderlyingSource<R>;
30+
// @ts-ignore
31+
type _UnderlyingByteSource = unknown extends UnderlyingByteSource ? never : UnderlyingByteSource;
32+
type _UnderlyingDefaultSource<R = any> =
33+
// @ts-ignore
34+
unknown extends UnderlyingDefaultSource ? never : UnderlyingDefaultSource<R>;
35+
// @ts-ignore
36+
type _QueuingStrategy<R = any> = unknown extends QueuingStrategy ? never : QueuingStrategy<R>;
37+
38+
export { _ReadableStream as ReadableStream };

src/_shims/ReadableStream.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
exports.ReadableStream = ReadableStream;

src/_shims/ReadableStream.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
const _ReadableStream = ReadableStream;
6+
7+
export { _ReadableStream as ReadableStream };

src/_shims/ReadableStream.node.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
import { ReadableStream } from 'web-streams-polyfill';
5+
6+
export { ReadableStream };

src/_shims/fetch.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type _Response = unknown extends Response ? never : Response;
2626
// @ts-ignore
2727
type _ResponseInit = unknown extends ResponseInit ? never : ResponseInit;
2828
// @ts-ignore
29+
type _ResponseType = unknown extends ResponseType ? never : ResponseType;
30+
// @ts-ignore
2931
type _BodyInit = unknown extends BodyInit ? never : BodyInit;
3032
// @ts-ignore
3133
type _Headers = unknown extends Headers ? never : Headers;
@@ -49,4 +51,9 @@ declare const _Headers: {
4951
export const isPolyfilled = false;
5052

5153
export { _fetch as fetch, _Request as Request, _Response as Response, _Headers as Headers };
52-
export type { _RequestInit as RequestInit, _RequestInfo as RequestInfo, _BodyInit as BodyInit };
54+
export type {
55+
_RequestInit as RequestInit,
56+
_RequestInfo as RequestInfo,
57+
_ResponseType as ResponseType,
58+
_BodyInit as BodyInit,
59+
};

src/_shims/fetch.node.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ type _RequestInit = unknown extends RequestInit ? nf.RequestInit : RequestInit;
2626
type _Response = unknown extends Response ? nf.Response : Response;
2727
// @ts-ignore
2828
type _ResponseInit = unknown extends ResponseInit ? nf.ResponseInit : ResponseInit;
29+
type _ResponseType =
30+
// @ts-ignore
31+
unknown extends ResponseType ? 'basic' | 'cors' | 'default' | 'error' | 'opaque' | 'opaqueredirect'
32+
: // @ts-ignore
33+
ResponseType;
2934
// @ts-ignore
3035
type _BodyInit = unknown extends BodyInit ? nf.BodyInit : BodyInit;
3136
// @ts-ignore
@@ -50,4 +55,9 @@ declare const _Headers: {
5055
export const isPolyfilled = false;
5156

5257
export { _fetch as fetch, _Request as Request, _Response as Response, _Headers as Headers };
53-
export type { _RequestInit as RequestInit, _RequestInfo as RequestInfo, _BodyInit as BodyInit };
58+
export type {
59+
_RequestInit as RequestInit,
60+
_RequestInfo as RequestInfo,
61+
_ResponseType as ResponseType,
62+
_BodyInit as BodyInit,
63+
};

0 commit comments

Comments
 (0)