MCPcopy Index your code
hub / github.com/massCodeIO/massCode / parseBody

Function parseBody

src/main/http/import/opencollection.ts:240–300  ·  view source on GitHub ↗
(
  rawBody: unknown,
  source: string,
  warnings: HttpImportWarning[],
)

Source from the content-addressed store, hash-verified

238}
239
240function parseBody(
241 rawBody: unknown,
242 source: string,
243 warnings: HttpImportWarning[],
244): Pick<HttpImportRequest, 'body' | 'bodyType' | 'formData'> {
245 if (!isRecord(rawBody)) {
246 return { body: null, bodyType: 'none', formData: [] }
247 }
248
249 const type = asString(rawBody.type)
250 if (type === 'json') {
251 return { body: asString(rawBody.data), bodyType: 'json', formData: [] }
252 }
253
254 if (type === 'text' || type === 'xml') {
255 return { body: asString(rawBody.data), bodyType: 'text', formData: [] }
256 }
257
258 if (type === 'form-urlencoded') {
259 const body = asArray(rawBody.data)
260 .filter(isRecord)
261 .filter(
262 entry => entry.disabled !== true && asString(entry.name ?? entry.key),
263 )
264 .map(
265 entry =>
266 `${asString(entry.name ?? entry.key)}=${asString(entry.value)}`,
267 )
268 .join('&')
269 return { body, bodyType: 'form-urlencoded', formData: [] }
270 }
271
272 if (type === 'multipart-form') {
273 return {
274 body: null,
275 bodyType: 'multipart',
276 formData: asArray(rawBody.data)
277 .filter(isRecord)
278 .filter(
279 entry =>
280 entry.disabled !== true && asString(entry.name ?? entry.key),
281 )
282 .map(entry => ({
283 key: asString(entry.name ?? entry.key),
284 type: asString(entry.type) === 'file' ? 'file' : 'text',
285 value: asString(entry.value),
286 })),
287 }
288 }
289
290 if (type === 'graphql') {
291 addWarning(warnings, source, 'GraphQL body imported as JSON')
292 return {
293 body: JSON.stringify(rawBody.data ?? {}, null, 2),
294 bodyType: 'json',
295 formData: [],
296 }
297 }

Callers 1

parseRequestFunction · 0.70

Calls 4

addWarningFunction · 0.90
isRecordFunction · 0.70
asStringFunction · 0.70
asArrayFunction · 0.70

Tested by

no test coverage detected