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

Function applyRequestBody

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

Source from the content-addressed store, hash-verified

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

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