Skip to content

feat: add support for json:api content type #1187

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

Open
wants to merge 4 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
5 changes: 5 additions & 0 deletions src/schema-routes/schema-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { SpecificArgNameResolver } from "./util/specific-arg-name-resolver.js";

const CONTENT_KIND = {
JSON: "JSON",
JSON_API: "JSON_API",
URL_ENCODED: "URL_ENCODED",
FORM_DATA: "FORM_DATA",
IMAGE: "IMAGE",
Expand Down Expand Up @@ -280,6 +281,10 @@ export class SchemaRoutes {
);

getContentKind = (contentTypes) => {
if (contentTypes.includes("application/vnd.api+json")) {
return CONTENT_KIND.JSON_API;
}

if (
contentTypes.some((contentType) =>
contentType.startsWith("application/json"),
Expand Down
1 change: 1 addition & 0 deletions templates/base/http-clients/axios-http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequest

export enum ContentType {
Json = "application/json",
JsonApi = "application/vnd.api+json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
Expand Down
2 changes: 2 additions & 0 deletions templates/base/http-clients/fetch-http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type CancelToken = Symbol | string | number;

export enum ContentType {
Json = "application/json",
JsonApi = "application/vnd.api+json",
FormData = "multipart/form-data",
UrlEncoded = "application/x-www-form-urlencoded",
Text = "text/plain",
Expand Down Expand Up @@ -103,6 +104,7 @@ export class HttpClient<SecurityDataType = unknown> {

private contentFormatters: Record<ContentType, (input: any) => any> = {
[ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
[ContentType.JsonApi]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
[ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
[ContentType.FormData]: (input: any) =>
Object.keys(input || {}).reduce((formData, key) => {
Expand Down
1 change: 1 addition & 0 deletions templates/default/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const wrapperArgs = _
// RequestParams["type"]
const requestContentKind = {
"JSON": "ContentType.Json",
"JSON_API": "ContentType.JsonApi",
"URL_ENCODED": "ContentType.UrlEncoded",
"FORM_DATA": "ContentType.FormData",
"TEXT": "ContentType.Text",
Expand Down
1 change: 1 addition & 0 deletions templates/modular/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const wrapperArgs = _
// RequestParams["type"]
const requestContentKind = {
"JSON": "ContentType.Json",
"JSON_API": "ContentType.JsonApi",
"URL_ENCODED": "ContentType.UrlEncoded",
"FORM_DATA": "ContentType.FormData",
"TEXT": "ContentType.Text",
Expand Down
Loading