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

Function applyRequestBody

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

Source from the content-addressed store, hash-verified

774// ---------------------------------------------------------------------------
775
776const applyRequestBody = (
777 request: HttpClientRequest.HttpClientRequest,
778 contentType: string,
779 bodyValue: unknown,
780 encoding: Record<string, EncodingObject> | undefined,
781): HttpClientRequest.HttpClientRequest => {
782 if (isJsonContentType(contentType)) {
783 // Pre-serialized JSON strings pass through with the declared media
784 // type preserved (important for `application/vnd.foo+json` etc.).
785 if (typeof bodyValue === "string") {
786 return HttpClientRequest.bodyText(request, bodyValue, contentType);
787 }
788 return HttpClientRequest.bodyJsonUnsafe(request, bodyValue);
789 }
790
791 if (isFormUrlEncoded(contentType)) {
792 if (typeof bodyValue === "string") {
793 return HttpClientRequest.bodyText(request, bodyValue, contentType);
794 }
795 if (typeof bodyValue === "object" && bodyValue !== null && !Array.isArray(bodyValue)) {
796 // Serialize ourselves so OAS3 encoding (style/explode/deepObject)
797 // is honored. bodyUrlParams doesn't know about per-field style.
798 const serialized = serializeFormUrlEncoded(bodyValue as Record<string, unknown>, encoding);
799 return HttpClientRequest.bodyText(request, serialized, contentType);
800 }
801 // Non-object body — fall back to platform helper (handles URLSearchParams).
802 return HttpClientRequest.bodyUrlParams(
803 request,
804 bodyValue as Parameters<typeof HttpClientRequest.bodyUrlParams>[1],
805 );
806 }
807
808 if (isMultipartFormData(contentType)) {
809 if (bodyValue instanceof FormData) {
810 return HttpClientRequest.bodyFormData(request, bodyValue);
811 }
812 if (typeof bodyValue === "object" && bodyValue !== null) {
813 return HttpClientRequest.bodyFormDataRecord(
814 request,
815 coerceFormDataRecord(bodyValue as Record<string, unknown>, encoding),
816 );
817 }
818 // String / primitive under multipart is almost certainly wrong on the
819 // caller's end — send it as text with their declared content type and
820 // let the server produce a useful error.
821 return HttpClientRequest.bodyText(request, String(bodyValue), contentType);
822 }
823
824 if (isOctetStream(contentType)) {
825 const bytes = toUint8Array(bodyValue);
826 if (bytes) return HttpClientRequest.bodyUint8Array(request, bytes, contentType);
827 if (typeof bodyValue === "string") {
828 return HttpClientRequest.bodyText(request, bodyValue, contentType);
829 }
830 // Unknown shape — serialize as JSON so at least the payload is visible.
831 return HttpClientRequest.bodyText(request, JSON.stringify(bodyValue), contentType);
832 }
833

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