Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add media type x-www-form-urlencoded #161

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions plugins/typescript/src/core/findCompatibleMediaType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ import {
*/
export const findCompatibleMediaType = (
requestBodyOrResponseObject: RequestBodyObject | ResponseObject
): MediaTypeObject | undefined => {
): (MediaTypeObject & { contentType: string }) | undefined => {
if (!requestBodyOrResponseObject.content) return;
for (let contentType of Object.keys(requestBodyOrResponseObject.content)) {
if (
contentType.startsWith("*/*") ||
contentType.startsWith("application/json") ||
contentType.startsWith("application/octet-stream") ||
contentType.startsWith("multipart/form-data")
contentType.startsWith("multipart/form-data") ||
contentType.startsWith("application/x-www-form-urlencoded")
) {
return requestBodyOrResponseObject.content[contentType];
return {
...requestBodyOrResponseObject.content[contentType],
contentType,
};
}
}
};
5 changes: 5 additions & 0 deletions plugins/typescript/src/core/getRequestBodyType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export const getRequestBodyType = ({
return f.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
}

// NOTE: Maybe not perfect to have this hardcoded here
if (mediaType.contentType === "application/x-www-form-urlencoded") {
return f.createTypeReferenceNode("URLSearchParams", undefined);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some unit test about this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, If would add it to getOperationTypes.test.ts right? What kind of test runner are you using?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the location of the test, I let you decide where this makes sense. The idea is to be sure this will not be broken later and to see that this is working as expected 😃

}

if (isReferenceObject(mediaType)) {
const [hash, topLevel, namespace, name] = mediaType.$ref.split("/");
if (hash !== "#" || topLevel !== "components") {
Expand Down
2 changes: 1 addition & 1 deletion plugins/typescript/src/templates/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function ${camel(prefix)}Fetch<
{
signal,
method: method.toUpperCase(),
body: body ? (body instanceof FormData ? body : JSON.stringify(body)) : undefined,
body: body ? ((body instanceof FormData || body instanceof URLSearchParams) ? body : JSON.stringify(body)) : undefined,
headers: requestHeaders
}
);
Expand Down