Skip to content

Commit

Permalink
refactor(http): refactor http client (#3482)
Browse files Browse the repository at this point in the history
Refs #3481
  • Loading branch information
glowcloud committed Apr 24, 2024
1 parent df80732 commit c4198da
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 433 deletions.
7 changes: 3 additions & 4 deletions src/execute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { isPlainObject } from 'is-plain-object';
import { url } from '@swagger-api/apidom-reference/configuration/empty';

import { DEFAULT_BASE_URL, DEFAULT_OPENAPI_3_SERVER } from '../constants.js';
import stockHttp, { mergeInQueryOrForm } from '../http/index.js';
import stockHttp from '../http/index.js';
import { serializeRequest } from '../http/serializers/request/index.js';
import createError from '../specmap/lib/create-error.js';
import SWAGGER2_PARAMETER_BUILDERS from './swagger2/parameter-builders.js';
import * as OAS3_PARAMETER_BUILDERS from './oas3/parameter-builders.js';
Expand Down Expand Up @@ -305,9 +306,7 @@ export function buildRequest(options) {

// Will add the query object into the URL, if it exists
// ... will also create a FormData instance, if multipart/form-data (eg: a file)
mergeInQueryOrForm(req);

return req;
return serializeRequest(req);
}

const stripNonAlpha = (str) => (str ? str.replace(/\W/g, '') : null);
Expand Down
23 changes: 22 additions & 1 deletion src/execute/oas3/build-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,32 @@ export default function buildRequest(options, req) {

req.form = {};
Object.keys(requestBody).forEach((k) => {
let value;
try {
value = JSON.parse(requestBody[k]);
} catch {
value = requestBody[k];
}
req.form[k] = {
value: requestBody[k],
value,
encoding: encoding[k] || {},
};
});
} else if (typeof requestBody === 'string') {
const encoding = requestBodyDef.content[requestContentType]?.encoding ?? {};

try {
req.form = {};
const form = JSON.parse(requestBody);
Object.entries(form).forEach(([key, value]) => {
req.form[key] = {
value,
encoding: encoding[key] || {},
};
});
} catch {
req.form = requestBody;
}
} else {
req.form = requestBody;
}
Expand Down
Loading

0 comments on commit c4198da

Please sign in to comment.