diff --git a/src/commands/gen-api-models/__tests__/render.test.ts b/src/commands/gen-api-models/__tests__/render.test.ts index ac4d5a4..83f9390 100644 --- a/src/commands/gen-api-models/__tests__/render.test.ts +++ b/src/commands/gen-api-models/__tests__/render.test.ts @@ -6,6 +6,8 @@ import * as SwaggerParser from "swagger-parser"; import { renderDefinitionCode, renderAllOperations } from "../render"; import { getDefinitionOrFail, getParser } from "./utils/parser.utils"; +import { getfirstSuccessType } from "../render"; + let spec: OpenAPIV2.Document; describe.each` @@ -50,5 +52,33 @@ describe.each` const code = renderAllOperations([operationInfo1], true) expect(code).toMatchSnapshot(); }) - }); + +describe("getfirstSuccessType", () => { + it("should return the first successful response", () => { + const responses = [ + { e1: "200", e2: "SuccessType", e3: [] }, + { e1: "301", e2: "RedirectType", e3: [] } + ]; + const result = getfirstSuccessType(responses); + expect(result).toEqual({ e1: "200", e2: "SuccessType", e3: [] }); + }); + + it("should return the first redirection response if no successful response is found", () => { + const responses = [ + { e1: "301", e2: "RedirectType", e3: [] }, + { e1: "302", e2: "AnotherRedirectType", e3: [] } + ]; + const result = getfirstSuccessType(responses); + expect(result).toEqual({ e1: "301", e2: "RedirectType", e3: [] }); + }); + + it("should return undefined if no successful or redirection responses are found", () => { + const responses = [ + { e1: "400", e2: "ClientErrorType", e3: [] }, + { e1: "500", e2: "ServerErrorType", e3: [] } + ]; + const result = getfirstSuccessType(responses); + expect(result).toBeUndefined(); + }); +}); \ No newline at end of file diff --git a/src/commands/gen-api-models/render.ts b/src/commands/gen-api-models/render.ts index 31d237c..40325e1 100644 --- a/src/commands/gen-api-models/render.ts +++ b/src/commands/gen-api-models/render.ts @@ -196,6 +196,36 @@ export const renderOperation = ( ${requestTypeDefinition}${responsesDecoderCode}`; }; +/** + * Retrieves the first successful response type from a list of operation responses. + * + * This function iterates through the provided responses and checks the status code. + * If a response has a status code starting with "2" (indicating success), it returns that response. + * If no such response is found, it checks for responses with status codes starting with "3" (indicating redirection). + * If any redirection responses are found, it returns the first one. + * If neither successful nor redirection responses are found, it returns `undefined`. + * + * @param responses - An array of operation responses to evaluate. + * @returns The first successful response, the first redirection response if no successful response is found, or `undefined` if neither are found. + */ +export const getfirstSuccessType = ( + responses: IOperationInfo["responses"] +): IOperationInfo["responses"][number] | undefined => { + const redirectResponses = []; + for (const response of responses) { + if (response.e1.length === 3) { + if (response.e1[0] === "2") { + return response; + } + if (response.e1[0] === "3") { + // eslint-disable-next-line functional/immutable-data + redirectResponses.push(response); + } + } + } + return redirectResponses.length > 0 ? redirectResponses[0] : undefined; +}; + /** * Compose the code for response decoder of an operation * @@ -206,9 +236,7 @@ export const renderOperation = ( // eslint-disable-next-line @typescript-eslint/explicit-function-return-type, prefer-arrow/prefer-arrow-functions export function renderDecoderCode({ responses, operationId }: IOperationInfo) { // use the first 2xx type as "success type" that we allow to be overridden - const firstSuccessType = responses.find( - ({ e1 }) => e1.length === 3 && e1[0] === "2" - ); + const firstSuccessType = getfirstSuccessType(responses); if (!firstSuccessType) { return ""; }