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

Function applyRequestBody

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

Source from the content-addressed store, hash-verified

733// ---------------------------------------------------------------------------
734
735const applyRequestBody = (
736 request: HttpClientRequest.HttpClientRequest,
737 contentType: string,
738 bodyValue: unknown,
739 encoding: Record<string, EncodingObject> | undefined,
740): HttpClientRequest.HttpClientRequest => {
741 if (isJsonContentType(contentType)) {
742 // Pre-serialized JSON strings pass through with the declared media
743 // type preserved (important for `application/vnd.foo+json` etc.).
744 if (typeof bodyValue === "string") {
745 return HttpClientRequest.bodyText(request, bodyValue, contentType);
746 }
747 return HttpClientRequest.bodyJsonUnsafe(request, bodyValue);
748 }
749
750 if (isFormUrlEncoded(contentType)) {
751 if (typeof bodyValue === "string") {
752 return HttpClientRequest.bodyText(request, bodyValue, contentType);
753 }
754 if (typeof bodyValue === "object" && bodyValue !== null && !Array.isArray(bodyValue)) {
755 // Serialize ourselves so OAS3 encoding (style/explode/deepObject)
756 // is honored. bodyUrlParams doesn't know about per-field style.
757 const serialized = serializeFormUrlEncoded(bodyValue as Record<string, unknown>, encoding);
758 return HttpClientRequest.bodyText(request, serialized, contentType);
759 }
760 // Non-object body — fall back to platform helper (handles URLSearchParams).
761 return HttpClientRequest.bodyUrlParams(
762 request,
763 bodyValue as Parameters<typeof HttpClientRequest.bodyUrlParams>[1],
764 );
765 }
766
767 if (isMultipartFormData(contentType)) {
768 if (bodyValue instanceof FormData) {
769 return HttpClientRequest.bodyFormData(request, bodyValue);
770 }
771 if (typeof bodyValue === "object" && bodyValue !== null) {
772 return HttpClientRequest.bodyFormDataRecord(
773 request,
774 coerceFormDataRecord(bodyValue as Record<string, unknown>, encoding),
775 );
776 }
777 // String / primitive under multipart is almost certainly wrong on the
778 // caller's end — send it as text with their declared content type and
779 // let the server produce a useful error.
780 return HttpClientRequest.bodyText(request, String(bodyValue), contentType);
781 }
782
783 if (isOctetStream(contentType)) {
784 const bytes = toUint8Array(bodyValue);
785 if (bytes) return HttpClientRequest.bodyUint8Array(request, bytes, contentType);
786 if (typeof bodyValue === "string") {
787 return HttpClientRequest.bodyText(request, bodyValue, contentType);
788 }
789 // Unknown shape — serialize as JSON so at least the payload is visible.
790 return HttpClientRequest.bodyText(request, JSON.stringify(bodyValue), contentType);
791 }
792

Callers 1

invoke.tsFile · 0.85

Calls 9

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