( request: HttpClientRequest.HttpClientRequest, contentType: string, bodyValue: unknown, encoding: Record<string, EncodingObject> | undefined, )
| 827 | | { readonly ok: false; readonly invalidFileField: string }; |
| 828 | |
| 829 | const applyRequestBody = ( |
| 830 | request: HttpClientRequest.HttpClientRequest, |
| 831 | contentType: string, |
| 832 | bodyValue: unknown, |
| 833 | encoding: Record<string, EncodingObject> | undefined, |
| 834 | ): AppliedRequestBody => { |
| 835 | const sent = (req: HttpClientRequest.HttpClientRequest): AppliedRequestBody => ({ |
| 836 | ok: true, |
| 837 | request: req, |
| 838 | }); |
| 839 | |
| 840 | if (isJsonContentType(contentType)) { |
| 841 | // Pre-serialized JSON strings pass through with the declared media |
| 842 | // type preserved (important for `application/vnd.foo+json` etc.). |
| 843 | if (typeof bodyValue === "string") { |
| 844 | return sent(HttpClientRequest.bodyText(request, bodyValue, contentType)); |
| 845 | } |
| 846 | return sent(HttpClientRequest.bodyJsonUnsafe(request, bodyValue)); |
| 847 | } |
| 848 | |
| 849 | if (isFormUrlEncoded(contentType)) { |
| 850 | if (typeof bodyValue === "string") { |
| 851 | return sent(HttpClientRequest.bodyText(request, bodyValue, contentType)); |
| 852 | } |
| 853 | if (typeof bodyValue === "object" && bodyValue !== null && !Array.isArray(bodyValue)) { |
| 854 | // Serialize ourselves so OAS3 encoding (style/explode/deepObject) |
| 855 | // is honored. bodyUrlParams doesn't know about per-field style. |
| 856 | const serialized = serializeFormUrlEncoded(bodyValue as Record<string, unknown>, encoding); |
| 857 | return sent(HttpClientRequest.bodyText(request, serialized, contentType)); |
| 858 | } |
| 859 | // Non-object body — fall back to platform helper (handles URLSearchParams). |
| 860 | return sent( |
| 861 | HttpClientRequest.bodyUrlParams( |
| 862 | request, |
| 863 | bodyValue as Parameters<typeof HttpClientRequest.bodyUrlParams>[1], |
| 864 | ), |
| 865 | ); |
| 866 | } |
| 867 | |
| 868 | if (isMultipartFormData(contentType)) { |
| 869 | if (bodyValue instanceof FormData) { |
| 870 | return sent(HttpClientRequest.bodyFormData(request, bodyValue)); |
| 871 | } |
| 872 | if (typeof bodyValue === "object" && bodyValue !== null) { |
| 873 | const coerced = coerceFormDataRecord(bodyValue as Record<string, unknown>, encoding); |
| 874 | if (!coerced.ok) return { ok: false, invalidFileField: coerced.field }; |
| 875 | return sent(HttpClientRequest.bodyFormDataRecord(request, coerced.record)); |
| 876 | } |
| 877 | // String / primitive under multipart is almost certainly wrong on the |
| 878 | // caller's end — send it as text with their declared content type and |
| 879 | // let the server produce a useful error. |
| 880 | return sent(HttpClientRequest.bodyText(request, String(bodyValue), contentType)); |
| 881 | } |
| 882 | |
| 883 | if (isOctetStream(contentType)) { |
| 884 | const bytes = toUint8Array(bodyValue); |
| 885 | if (bytes) return sent(HttpClientRequest.bodyUint8Array(request, bytes, contentType)); |
| 886 | if (typeof bodyValue === "string") { |
no test coverage detected