|
1 | | -// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
2 | | - |
3 | | -import { type Grid } from './client'; |
4 | | - |
5 | | -import { type PromiseOrValue } from './internal/types'; |
6 | | -import { APIResponseProps, defaultParseResponse } from './internal/parse'; |
7 | | - |
8 | | -/** |
9 | | - * A subclass of `Promise` providing additional helper methods |
10 | | - * for interacting with the SDK. |
11 | | - */ |
12 | | -export class APIPromise<T> extends Promise<T> { |
13 | | - private parsedPromise: Promise<T> | undefined; |
14 | | - #client: Grid; |
15 | | - |
16 | | - constructor( |
17 | | - client: Grid, |
18 | | - private responsePromise: Promise<APIResponseProps>, |
19 | | - private parseResponse: ( |
20 | | - client: Grid, |
21 | | - props: APIResponseProps, |
22 | | - ) => PromiseOrValue<T> = defaultParseResponse, |
23 | | - ) { |
24 | | - super((resolve) => { |
25 | | - // this is maybe a bit weird but this has to be a no-op to not implicitly |
26 | | - // parse the response body; instead .then, .catch, .finally are overridden |
27 | | - // to parse the response |
28 | | - resolve(null as any); |
29 | | - }); |
30 | | - this.#client = client; |
31 | | - } |
32 | | - |
33 | | - _thenUnwrap<U>(transform: (data: T, props: APIResponseProps) => U): APIPromise<U> { |
34 | | - return new APIPromise(this.#client, this.responsePromise, async (client, props) => |
35 | | - transform(await this.parseResponse(client, props), props), |
36 | | - ); |
37 | | - } |
38 | | - |
39 | | - /** |
40 | | - * Gets the raw `Response` instance instead of parsing the response |
41 | | - * data. |
42 | | - * |
43 | | - * If you want to parse the response body but still get the `Response` |
44 | | - * instance, you can use {@link withResponse()}. |
45 | | - * |
46 | | - * 👋 Getting the wrong TypeScript type for `Response`? |
47 | | - * Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]` |
48 | | - * to your `tsconfig.json`. |
49 | | - */ |
50 | | - asResponse(): Promise<Response> { |
51 | | - return this.responsePromise.then((p) => p.response); |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * Gets the parsed response data and the raw `Response` instance. |
56 | | - * |
57 | | - * If you just want to get the raw `Response` instance without parsing it, |
58 | | - * you can use {@link asResponse()}. |
59 | | - * |
60 | | - * 👋 Getting the wrong TypeScript type for `Response`? |
61 | | - * Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]` |
62 | | - * to your `tsconfig.json`. |
63 | | - */ |
64 | | - async withResponse(): Promise<{ data: T; response: Response }> { |
65 | | - const [data, response] = await Promise.all([this.parse(), this.asResponse()]); |
66 | | - return { data, response }; |
67 | | - } |
68 | | - |
69 | | - private parse(): Promise<T> { |
70 | | - if (!this.parsedPromise) { |
71 | | - this.parsedPromise = this.responsePromise.then((data) => this.parseResponse(this.#client, data)); |
72 | | - } |
73 | | - return this.parsedPromise; |
74 | | - } |
75 | | - |
76 | | - override then<TResult1 = T, TResult2 = never>( |
77 | | - onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, |
78 | | - onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null, |
79 | | - ): Promise<TResult1 | TResult2> { |
80 | | - return this.parse().then(onfulfilled, onrejected); |
81 | | - } |
82 | | - |
83 | | - override catch<TResult = never>( |
84 | | - onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null, |
85 | | - ): Promise<T | TResult> { |
86 | | - return this.parse().catch(onrejected); |
87 | | - } |
88 | | - |
89 | | - override finally(onfinally?: (() => void) | undefined | null): Promise<T> { |
90 | | - return this.parse().finally(onfinally); |
91 | | - } |
92 | | -} |
| 1 | +/** @deprecated Import from ./core/api-promise instead */ |
| 2 | +export * from './core/api-promise'; |
0 commit comments