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

Function parseBody

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

Source from the content-addressed store, hash-verified

170}
171
172function parseBody(
173 rawBody: unknown,
174 source: string,
175 warnings: HttpImportWarning[],
176): Pick<HttpImportRequest, 'body' | 'bodyType' | 'formData'> {
177 if (!isRecord(rawBody)) {
178 return { body: null, bodyType: 'none', formData: [] }
179 }
180
181 const mode = asString(rawBody.mode)
182 if (mode === 'raw') {
183 const raw = asString(rawBody.raw)
184 const language
185 = isRecord(rawBody.options)
186 && isRecord(rawBody.options.raw)
187 && rawBody.options.raw.language === 'json'
188
189 return {
190 body: raw,
191 bodyType: language ? 'json' : 'text',
192 formData: [],
193 }
194 }
195
196 if (mode === 'urlencoded') {
197 const body = asArray(rawBody.urlencoded)
198 .filter(isRecord)
199 .filter(entry => entry.disabled !== true && asString(entry.key))
200 .map(entry => `${asString(entry.key)}=${asString(entry.value)}`)
201 .join('&')
202
203 return { body, bodyType: 'form-urlencoded', formData: [] }
204 }
205
206 if (mode === 'formdata') {
207 return {
208 body: null,
209 bodyType: 'multipart',
210 formData: asArray(rawBody.formdata)
211 .filter(isRecord)
212 .filter(entry => entry.disabled !== true && asString(entry.key))
213 .map(entry => ({
214 key: asString(entry.key),
215 type: entry.type === 'file' ? 'file' : 'text',
216 value: asString(entry.src || entry.value),
217 })),
218 }
219 }
220
221 if (mode === 'graphql') {
222 addWarning(warnings, source, 'GraphQL body imported as JSON')
223 return {
224 body: JSON.stringify(rawBody.graphql ?? {}, null, 2),
225 bodyType: 'json',
226 formData: [],
227 }
228 }
229

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