summaryrefslogtreecommitdiffhomepage
path: root/packages/sdk/src/internal/parse.ts
diff options
context:
space:
mode:
authoradamdotdevin <[email protected]>2025-07-22 11:50:51 -0500
committeradamdotdevin <[email protected]>2025-07-22 11:50:51 -0500
commit10c8b495907069461f5dc464f6285321290c8b14 (patch)
tree9fed07910dd7d99688c0261a364d810a500e21d3 /packages/sdk/src/internal/parse.ts
parent500cea5ce7fa635a924cd9abea63aaf672f7645d (diff)
downloadopencode-10c8b495907069461f5dc464f6285321290c8b14.tar.gz
opencode-10c8b495907069461f5dc464f6285321290c8b14.zip
chore: generate sdk into packages/sdk
Diffstat (limited to 'packages/sdk/src/internal/parse.ts')
-rw-r--r--packages/sdk/src/internal/parse.ts64
1 files changed, 64 insertions, 0 deletions
diff --git a/packages/sdk/src/internal/parse.ts b/packages/sdk/src/internal/parse.ts
new file mode 100644
index 000000000..dcb4026e3
--- /dev/null
+++ b/packages/sdk/src/internal/parse.ts
@@ -0,0 +1,64 @@
+// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
+
+import type { FinalRequestOptions } from './request-options';
+import { Stream } from '../core/streaming';
+import { type Opencode } from '../client';
+import { formatRequestDetails, loggerFor } from './utils/log';
+
+export type APIResponseProps = {
+ response: Response;
+ options: FinalRequestOptions;
+ controller: AbortController;
+ requestLogID: string;
+ retryOfRequestLogID: string | undefined;
+ startTime: number;
+};
+
+export async function defaultParseResponse<T>(client: Opencode, props: APIResponseProps): Promise<T> {
+ const { response, requestLogID, retryOfRequestLogID, startTime } = props;
+ const body = await (async () => {
+ if (props.options.stream) {
+ loggerFor(client).debug('response', response.status, response.url, response.headers, response.body);
+
+ // Note: there is an invariant here that isn't represented in the type system
+ // that if you set `stream: true` the response type must also be `Stream<T>`
+
+ if (props.options.__streamClass) {
+ return props.options.__streamClass.fromSSEResponse(response, props.controller, client) as any;
+ }
+
+ return Stream.fromSSEResponse(response, props.controller, client) as any;
+ }
+
+ // fetch refuses to read the body when the status code is 204.
+ if (response.status === 204) {
+ return null as T;
+ }
+
+ if (props.options.__binaryResponse) {
+ return response as unknown as T;
+ }
+
+ const contentType = response.headers.get('content-type');
+ const mediaType = contentType?.split(';')[0]?.trim();
+ const isJSON = mediaType?.includes('application/json') || mediaType?.endsWith('+json');
+ if (isJSON) {
+ const json = await response.json();
+ return json as T;
+ }
+
+ const text = await response.text();
+ return text as unknown as T;
+ })();
+ loggerFor(client).debug(
+ `[${requestLogID}] response parsed`,
+ formatRequestDetails({
+ retryOfRequestLogID,
+ url: response.url,
+ status: response.status,
+ body,
+ durationMs: Date.now() - startTime,
+ }),
+ );
+ return body;
+}