Skip to content

Commit 1039ff1

Browse files
feat: add support for json:api content type (#1187)
* feat: add support for json:api content type * test: update snapshots to add json:api content type * test: add jsonapi-media-type test * style: fix format for JSON API spec * chore(changeset): add support for json:api content type * chore: update .changeset/plenty-symbols-fail.md Co-authored-by: Sora Morimoto <[email protected]> --------- Co-authored-by: Sora Morimoto <[email protected]>
1 parent a013686 commit 1039ff1

File tree

37 files changed

+1119
-0
lines changed

37 files changed

+1119
-0
lines changed

.changeset/plenty-symbols-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"swagger-typescript-api": minor
3+
---
4+
5+
Add support for json:api content type.

src/schema-routes/schema-routes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { SpecificArgNameResolver } from "./util/specific-arg-name-resolver.js";
2222

2323
const CONTENT_KIND = {
2424
JSON: "JSON",
25+
JSON_API: "JSON_API",
2526
URL_ENCODED: "URL_ENCODED",
2627
FORM_DATA: "FORM_DATA",
2728
IMAGE: "IMAGE",
@@ -280,6 +281,10 @@ export class SchemaRoutes {
280281
);
281282

282283
getContentKind = (contentTypes) => {
284+
if (contentTypes.includes("application/vnd.api+json")) {
285+
return CONTENT_KIND.JSON_API;
286+
}
287+
283288
if (
284289
contentTypes.some((contentType) =>
285290
contentType.startsWith("application/json"),

templates/base/http-clients/axios-http-client.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequest
3232

3333
export enum ContentType {
3434
Json = "application/json",
35+
JsonApi = "application/vnd.api+json",
3536
FormData = "multipart/form-data",
3637
UrlEncoded = "application/x-www-form-urlencoded",
3738
Text = "text/plain",

templates/base/http-clients/fetch-http-client.ejs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type CancelToken = Symbol | string | number;
4343

4444
export enum ContentType {
4545
Json = "application/json",
46+
JsonApi = "application/vnd.api+json",
4647
FormData = "multipart/form-data",
4748
UrlEncoded = "application/x-www-form-urlencoded",
4849
Text = "text/plain",
@@ -103,6 +104,7 @@ export class HttpClient<SecurityDataType = unknown> {
103104

104105
private contentFormatters: Record<ContentType, (input: any) => any> = {
105106
[ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
107+
[ContentType.JsonApi]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
106108
[ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
107109
[ContentType.FormData]: (input: any) =>
108110
Object.keys(input || {}).reduce((formData, key) => {

templates/default/procedure-call.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const wrapperArgs = _
4848
// RequestParams["type"]
4949
const requestContentKind = {
5050
"JSON": "ContentType.Json",
51+
"JSON_API": "ContentType.JsonApi",
5152
"URL_ENCODED": "ContentType.UrlEncoded",
5253
"FORM_DATA": "ContentType.FormData",
5354
"TEXT": "ContentType.Text",

templates/modular/procedure-call.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const wrapperArgs = _
4848
// RequestParams["type"]
4949
const requestContentKind = {
5050
"JSON": "ContentType.Json",
51+
"JSON_API": "ContentType.JsonApi",
5152
"URL_ENCODED": "ContentType.UrlEncoded",
5253
"FORM_DATA": "ContentType.FormData",
5354
"TEXT": "ContentType.Text",

0 commit comments

Comments
 (0)