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

Function applyRequestBody

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

Source from the content-addressed store, hash-verified

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

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