MCPcopy Create free account
hub / github.com/openai/openai-node / buildBody

Method buildBody

src/client.ts:1189–1260  ·  view source on GitHub ↗
({ options }: { options: FinalRequestOptions })

Source from the content-addressed store, hash-verified

1187 }
1188
1189 private buildBody({ options }: { options: FinalRequestOptions }): {
1190 bodyHeaders: HeadersLike;
1191 body: BodyInit | undefined;
1192 isStreamingBody: boolean;
1193 } {
1194 const { body, headers: rawHeaders } = options;
1195 if (!body) {
1196 // A resource method always passes a `body` key when its operation defines a
1197 // request body, even if the caller omitted an optional body param. Keep the
1198 // content-type for those, and only elide it for operations with no body at
1199 // all (e.g. GET/DELETE).
1200 if (body === undefined && 'body' in options) {
1201 return { ...this.#encoder({ body, headers: buildHeaders([rawHeaders]) }), isStreamingBody: false };
1202 }
1203 return { bodyHeaders: undefined, body: undefined, isStreamingBody: false };
1204 }
1205 const headers = buildHeaders([rawHeaders]);
1206
1207 const isReadableStream =
1208 typeof (globalThis as any).ReadableStream !== 'undefined' &&
1209 body instanceof (globalThis as any).ReadableStream;
1210
1211 const isRetryableBody =
1212 !isReadableStream &&
1213 (typeof body === 'string' ||
1214 body instanceof ArrayBuffer ||
1215 ArrayBuffer.isView(body) ||
1216 (typeof (globalThis as any).Blob !== 'undefined' && body instanceof (globalThis as any).Blob) ||
1217 body instanceof URLSearchParams ||
1218 body instanceof FormData);
1219
1220 if (
1221 // Pass raw type verbatim
1222 ArrayBuffer.isView(body) ||
1223 body instanceof ArrayBuffer ||
1224 body instanceof DataView ||
1225 (typeof body === 'string' &&
1226 // Preserve legacy string encoding behavior for now
1227 headers.values.has('content-type')) ||
1228 // `Blob` is superset of `File`
1229 ((globalThis as any).Blob && body instanceof (globalThis as any).Blob) ||
1230 // `FormData` -> `multipart/form-data`
1231 body instanceof FormData ||
1232 // `URLSearchParams` -> `application/x-www-form-urlencoded`
1233 body instanceof URLSearchParams ||
1234 // Send chunked stream (each chunk has own `length`)
1235 isReadableStream
1236 ) {
1237 return { bodyHeaders: undefined, body: body as BodyInit, isStreamingBody: !isRetryableBody };
1238 } else if (
1239 typeof body === 'object' &&
1240 (Symbol.asyncIterator in body ||
1241 (Symbol.iterator in body && 'next' in body && typeof body.next === 'function'))
1242 ) {
1243 return {
1244 bodyHeaders: undefined,
1245 body: Shims.ReadableStreamFrom(body as AsyncIterable<Uint8Array>),
1246 isStreamingBody: true,

Callers 1

buildRequestMethod · 0.95

Calls 3

stringifyQueryMethod · 0.95
buildHeadersFunction · 0.90
getMethod · 0.80

Tested by

no test coverage detected