MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / applyRequestBody

Function applyRequestBody

packages/plugins/openapi/src/sdk/invoke.ts:829–911  ·  view source on GitHub ↗
(
  request: HttpClientRequest.HttpClientRequest,
  contentType: string,
  bodyValue: unknown,
  encoding: Record<string, EncodingObject> | undefined,
)

Source from the content-addressed store, hash-verified

827 | { readonly ok: false; readonly invalidFileField: string };
828
829const 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") {

Callers 1

invoke.tsFile · 0.85

Calls 10

sentFunction · 0.85
isFormUrlEncodedFunction · 0.85
serializeFormUrlEncodedFunction · 0.85
isMultipartFormDataFunction · 0.85
coerceFormDataRecordFunction · 0.85
isOctetStreamFunction · 0.85
toUint8ArrayFunction · 0.85
isXmlContentTypeFunction · 0.85
isTextContentTypeFunction · 0.85
isJsonContentTypeFunction · 0.70

Tested by

no test coverage detected