MCPcopy Create free account
hub / github.com/massCodeIO/massCode / buildBody

Function buildBody

src/main/ipc/handlers/http.ts:148–182  ·  view source on GitHub ↗
(
  bodyType: HttpBodyType,
  body: string | null,
  formData: HttpFormDataEntry[],
)

Source from the content-addressed store, hash-verified

146}
147
148export function buildBody(
149 bodyType: HttpBodyType,
150 body: string | null,
151 formData: HttpFormDataEntry[],
152): BuiltBody {
153 switch (bodyType) {
154 case 'none':
155 return { body: undefined }
156 case 'json':
157 return { body: body ?? '', contentType: 'application/json' }
158 case 'text':
159 return { body: body ?? '', contentType: 'text/plain' }
160 case 'form-urlencoded':
161 return {
162 body: body ?? '',
163 contentType: 'application/x-www-form-urlencoded',
164 }
165 case 'multipart': {
166 const fd = new FormData()
167 for (const entry of formData) {
168 if (!entry.key)
169 continue
170 if (entry.type === 'file' && entry.value) {
171 const buffer = readFileSync(entry.value)
172 const blob = new Blob([buffer])
173 fd.append(entry.key, blob, basename(entry.value))
174 }
175 else {
176 fd.append(entry.key, entry.value ?? '')
177 }
178 }
179 return { body: fd }
180 }
181 }
182}
183
184function detectBodyKind(contentType: string | undefined): HttpResponseBodyKind {
185 const ct = (contentType ?? '').toLowerCase()

Callers 1

executeHttpRequestFunction · 0.85

Calls 1

basenameFunction · 0.50

Tested by

no test coverage detected