Skip to content

Commit

Permalink
fix nested response type generate error #14.
Browse files Browse the repository at this point in the history
  • Loading branch information
teobler committed Feb 16, 2023
1 parent 0278309 commit 80ce578
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 14 deletions.
File renamed without changes.
7 changes: 7 additions & 0 deletions src/resolvers/__tests__/DefinitionsResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ const expectedDefinitions = {
"type?": "BookDetailVoType#EnumTypeSuffix",
},
"BookDetailVoType#EnumTypeSuffix": ["INTERVENTION_RUN", "CASE_CREATION_DATE"],
AuthenticationData: {
csrfToken: "string",
impersonationDetails: "Cat",
ssoUrl: "string",
user: "Dog",
},
BookVO: {
"address?": "string | null",
"price?": "string",
Expand Down Expand Up @@ -159,6 +165,7 @@ const expectedDefinitions = {

const expectedDeclarations = [
"export interface AttachmentBo {\n 'authorName'?: string;\n'createdDate'?: number;\n'fileName'?: string;\n'id'?: string;\n'mimeType'?: string;\n'path'?: string;\n }",
"export interface AuthenticationData {\n 'csrfToken': string;\n'impersonationDetails': Cat;\n'ssoUrl': string;\n'user': Dog;\n }",
"export interface BookDetailVo {\n 'CreatedDate'?: number;\n'attachment'?: ScheduleVo;\n'author_name'?: string;\n'filename'?: string;\n'id'?: string;\n'mimeType'?: string;\n'path'?: string;\n'type'?: BookDetailVoType;\n }",
'export enum BookDetailVoType {"INTERVENTION_RUN"="INTERVENTION_RUN","CASE_CREATION_DATE"="CASE_CREATION_DATE"}',
"export interface BookVo {\n 'address'?: string | null;\n'price'?: string;\n }",
Expand Down
22 changes: 22 additions & 0 deletions src/resolvers/__tests__/PathResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ describe("PathResolver", () => {
});

const expectedPathResolvedData = [
{
THeader: {},
TReqBody: {},
TReqCookie: {},
TReqPath: {},
TReqQuery: {},
TResp: {
"data?": "AuthenticationData[]",
"others?": "string",
},
cookieParams: [],
method: "get",
operationId: "userProfileInformation",
pathParams: [],
queryParams: [],
url: "/user/profile-information",
},
{
THeader: {
Authorities: "string",
Expand Down Expand Up @@ -224,6 +241,11 @@ const expectedRequest = [
'export const useUpdatePetsRequest = (mutationConfig?: SWRMutationConfig<UpdatePetsRequest, AxiosResponse<undefined>, IResponseError>, axiosConfig?: AxiosRequestConfig) => \n useMutationRequest<UpdatePetsRequest, AxiosResponse<undefined>, IResponseError>({\n url: `/pets`,\n method: "patch",headers: { "Content-Type": "application/json"},\n mutationConfig,\n axiosConfig});',
'export const useUploadAttachmentUsingPostRequest = ({authorities,userId,userName}:{\n \'authorities\': string;\n\'userId\': string;\n\'userName\': string;\n }, mutationConfig?: SWRMutationConfig<UploadAttachmentUsingPostRequest, AxiosResponse<AttachmentBo>, IResponseError>, axiosConfig?: AxiosRequestConfig) => \n useMutationRequest<UploadAttachmentUsingPostRequest, AxiosResponse<AttachmentBo>, IResponseError>({\n url: `/`,\n method: "post",headers: { "Authorities": authorities, "User-Id": userId, "User-Name": userName, "Content-Type": "multipart/form-data"},\n mutationConfig,\n axiosConfig});',
'export const useUploadDocumentUsingPostRequest = (mutationConfig?: SWRMutationConfig<UploadDocumentUsingPostRequest, AxiosResponse<undefined>, IResponseError>, axiosConfig?: AxiosRequestConfig) => \n useMutationRequest<UploadDocumentUsingPostRequest, AxiosResponse<undefined>, IResponseError>({\n url: `/documents`,\n method: "post",headers: { "Content-Type": "multipart/form-data"},\n mutationConfig,\n axiosConfig});',
`export const useUserProfileInformationRequest = (SWRConfig?: ISWRConfig<{data?:AuthenticationData[],others?:string}, IResponseError>, axiosConfig?: AxiosRequestConfig) =>
useGetRequest<{data?:AuthenticationData[],others?:string}, IResponseError>({
url: \`/user/profile-information\`,
method: "get",headers: { },
...axiosConfig}, SWRConfig);`,
"export interface UpdateBookJourneyUsingPostRequest {\n body: StatusFormData;\n \n }",
"export interface GetDocumentByIdUsingGetRequest {\n \n query: {\n 'from'?: FromFrom;\n }\n }",
"export interface GetScheduleDetailsByDateUsingGetRequest {\n \n query: {\n 'fruit': Fruit;\n'roleId'?: string;\n'scheduleDate': number;\n }\n }",
Expand Down
48 changes: 48 additions & 0 deletions src/resolvers/__tests__/mock-data/openAPI.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,36 @@
}
],
"paths": {
"/user/profile-information": {
"get": {
"tags": ["user"],
"summary": "Returns information about the current authentication status and user",
"operationId": "userProfileInformation",
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/components/schemas/AuthenticationData"
}
},
"others": {
"type": "string"
}
}
}
}
}
}
}
}
},
"/": {
"post": {
"tags": ["mock-controller"],
Expand Down Expand Up @@ -525,6 +555,24 @@
],
"components": {
"schemas": {
"AuthenticationData": {
"type": "object",
"properties": {
"csrfToken": {
"type": "string"
},
"user": {
"$ref": "#/components/schemas/Dog"
},
"ssoUrl": {
"type": "string"
},
"impersonationDetails": {
"$ref": "#/components/schemas/Cat"
}
},
"required": ["csrfToken", "user", "ssoUrl", "impersonationDetails"]
},
"Dog": {
"type": "object",
"properties": {
Expand Down
20 changes: 18 additions & 2 deletions src/utils/__tests__/formatters.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { arrayToObject, convertJsonStringToJson, toCapitalCase, toTypes } from "../formatters";
import {
arrayToObject,
convertJsonStringToJson,
convertResponseTypeObject,
toCapitalCase,
toTypes,
} from "../formatters";

describe("# formatters", () => {
describe("#toCapitalCase", () => {
Expand All @@ -16,7 +22,7 @@ describe("# formatters", () => {
[["a"], { a: "a" }],
[["a", "b"], { a: "a", b: "b" }],
])("should convert array to object", (input, result) => {
expect(arrayToObject(input)).toEqual(result);
expect(arrayToObject(input as any)).toEqual(result);
});
});

Expand Down Expand Up @@ -59,4 +65,14 @@ describe("# formatters", () => {
expect(mockPrint).toHaveBeenCalledWith("some error");
});
});

describe("# convertResponseTypeObject", () => {
it.each([
["", undefined],
[undefined, undefined],
[{ "data?": "SomeOtherSchema[]", key: "value" }, "{data?:SomeOtherSchema[],key:value}"],
])("should convert resolved responseType to ts format string", (responseType, result) => {
expect(convertResponseTypeObject(responseType)).toEqual(result);
});
});
});
12 changes: 12 additions & 0 deletions src/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,15 @@ export const convertJsonStringToJson = (
return;
}
};

export const convertResponseTypeObject = (responseType?: ResolvedSchema) => {
if (isObject(responseType)) {
return JSON.stringify(responseType).replace(/"/g, "");
}

if (responseType === "") {
return undefined;
}

return responseType;
};
22 changes: 10 additions & 12 deletions src/utils/generators.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { camelCase, compact, get, isEmpty, reduce, replace, some } from "lodash";
import { isNumber } from "./specifications";
import { IResolvedPath, IReqBody } from "../types";
import { IReqBody, IResolvedPath } from "../types";
import { ENUM_SUFFIX } from "../constants";
import { arrayToObject, toCapitalCase, toTypes } from "./formatters";
import { arrayToObject, convertResponseTypeObject, toCapitalCase, toTypes } from "./formatters";
import { RequestBodiesAndParams } from "src/resolvers/PathResolver";
import { ResolvedDefinitions, ResolvedSchema } from "src/resolvers/DefinitionsResolver";

Expand All @@ -23,12 +23,10 @@ export const generateEnums = (definitions: ResolvedDefinitions, key: string) =>
export const generateFunctionName = (operationId?: string) => `use${toCapitalCase(camelCase(operationId))}Request`;

export const generateGetClientName = (responseType?: ResolvedSchema) =>
`useGetRequest<${responseType === "" ? undefined : responseType}, IResponseError>`;
`useGetRequest<${convertResponseTypeObject(responseType)}, IResponseError>`;

export const generateMutationClientName = (responseType?: ResolvedSchema, requestBodyTypes?: string) =>
`useMutationRequest<${requestBodyTypes}, AxiosResponse<${
responseType === "" ? undefined : responseType
}>, IResponseError>`;
`useMutationRequest<${requestBodyTypes}, AxiosResponse<${convertResponseTypeObject(responseType)}>, IResponseError>`;

export const generateRequestBodyAndParams = (
requestBodyType?: IReqBody,
Expand Down Expand Up @@ -56,9 +54,9 @@ export const generateGetRequestArguments = (resolvedPath: IResolvedPath) => {
]).map((param) => camelCase(param));
const requestParams = requestParamList.length === 0 ? "" : `{${requestParamList.join(",")}}:${argumentTypes}`;

return `${requestParams ? requestParams + ", " : ""}SWRConfig?: ISWRConfig<${
resolvedPath.TResp || undefined
}, IResponseError>, axiosConfig?: AxiosRequestConfig`;
return `${requestParams ? requestParams + ", " : ""}SWRConfig?: ISWRConfig<${convertResponseTypeObject(
resolvedPath.TResp,
)}, IResponseError>, axiosConfig?: AxiosRequestConfig`;
};

export const generateMutationRequestArguments = (resolvedPath: IResolvedPath, requestBodyTypes?: string) => {
Expand All @@ -75,9 +73,9 @@ export const generateMutationRequestArguments = (resolvedPath: IResolvedPath, re

return `${
requestParams ? requestParams + ", " : ""
}mutationConfig?: SWRMutationConfig<${requestBodyTypes}, AxiosResponse<${
resolvedPath.TResp || undefined
}>, IResponseError>, axiosConfig?: AxiosRequestConfig`;
}mutationConfig?: SWRMutationConfig<${requestBodyTypes}, AxiosResponse<${convertResponseTypeObject(
resolvedPath.TResp,
)}>, IResponseError>, axiosConfig?: AxiosRequestConfig`;
};

export const generateHeader = (
Expand Down

0 comments on commit 80ce578

Please sign in to comment.