Skip to content

Commit

Permalink
feat: type이 스키마 기반으로 참조가 걸려있는 부분 처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ww8007 committed Jul 16, 2024
1 parent f67c60f commit cd3d998
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/entities/swagger/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface SchemasProperties {
type: SwaggerType;
format?: SwaggerFormat;
};
$ref?: string;
}

export interface Schemas {
Expand Down
1 change: 1 addition & 0 deletions src/shared/util/typeConverter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const typeConverter = (type: string) => {
if (type === "integer") return "number";
if (!type) return "unknown";
return type;
};

Expand Down
39 changes: 31 additions & 8 deletions src/widgets/request-body/ui/api-body/RequestBody.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useSwaggerDocStore } from "@/entities/docs/model/store/document-store";
import { useGETDocs } from "@/entities/swagger/api/get-document";
import { Schemas } from "@/entities/swagger/types";
import { FormValues } from "@/features/request-api/module/hooks/useForm";
import { vars } from "@/shared/ui/styles/theme.css";
import { typeConverter } from "@/shared/util/typeConverter";
import React, { ChangeEvent, useState } from "react";
import { RequestArrayBody } from "../array-body/RequestArrayBody";
import { RequestNormalBody } from "../normal-body/RequestNormalBody";
Expand All @@ -26,6 +29,11 @@ export const RequestBody = ({
// 기존에 custom hook으로 관리하던 paramState와 달리 하나의 input만 담당
const [bodyValue, setBodyValue] = useState("");

const { pathInfo } = useSwaggerDocStore();

const { data: apiDocsData } = useGETDocs(pathInfo);
console.log(apiDocsData);

const onChangeBodyValue = (e: ChangeEvent<HTMLInputElement>) => {
// file type일 경우 직접 addArrayItem 호출
if (e.target.id === "file" && e.target.files) {
Expand All @@ -40,16 +48,29 @@ export const RequestBody = ({
setBodyValue("");
};

const isItemsTypeFile = (property: string) => {
let returnType = "text";
const isFileType = (property: string) => {
Object.keys(body.properties).map((property) => {
if (body.properties[property]?.format === "binary") {
returnType = "file";
return true;
}
});
// if (body.properties[property].type === "binary") return "file";
if (body.properties[property].items?.format === "binary") return "file";
return returnType;
if (body.properties[property].items?.format === "binary") return true;
return false;
};

const getType = (property: string) => {
const type = body.properties[property].type;
if (type) {
console.log("type", type);
return typeConverter(type);
}
const fullRef = body.properties[property].$ref;
console.log("fullRef", fullRef);
const ref = fullRef.split("/").pop();
console.log("ref", ref);
const schema = apiDocsData?.components?.schemas[ref];
console.log(schema);
return typeConverter(schema?.type);
};

return (
Expand All @@ -71,7 +92,8 @@ export const RequestBody = ({
property={property}
addArrayItem={onAddArrayItem}
removeArrayItem={removeArrayItem}
type={isItemsTypeFile(property)}
type={getType(property)}
isFileType={isFileType(property)}
/>
) : (
<RequestNormalBody
Expand All @@ -80,7 +102,8 @@ export const RequestBody = ({
handleChange={handleChange}
idx={idx}
property={property}
type={isItemsTypeFile(property)}
type={getType(property)}
isFileType={isFileType(property)}
/>
)}
</>
Expand Down
6 changes: 4 additions & 2 deletions src/widgets/request-body/ui/array-body/RequestArrayBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface RequestArrayBodyProps {
formValues: FormValues;
idx: number;
property: string;
isFileType: boolean;
type: string;
handleChange: (e: ChangeEvent<HTMLInputElement>) => void;
addArrayItem: (key: string) => void;
Expand All @@ -22,6 +23,7 @@ export const RequestArrayBody = ({
idx,
property,
type,
isFileType,
addArrayItem,
removeArrayItem,
handleChange,
Expand All @@ -35,7 +37,7 @@ export const RequestArrayBody = ({
{typeConverter(body.properties[property].type)}
</label>
<div className={requestArrayBodyStyles.rightWrapper}>
{type === "file" && (
{!!isFileType && (
<label
htmlFor="file"
className={requestArrayBodyStyles.inputLabel}
Expand All @@ -59,7 +61,7 @@ export const RequestArrayBody = ({
autoFocus={idx === 0}
onFocus={(e) => e.target.select()}
/>
{type !== "file" && (
{!isFileType && (
<button
onClick={() => addArrayItem(property)}
type="button"
Expand Down
10 changes: 5 additions & 5 deletions src/widgets/request-body/ui/normal-body/RequestNormalBody.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Schemas } from "@/entities/swagger/types";
import { FormValues } from "@/features/request-api/module/hooks/useForm";
import Input from "@/shared/ui/Input";
import { typeConverter } from "@/shared/util/typeConverter";
import { ChangeEvent } from "react";
import { requestNormalBodyStyles } from "./requestNormalBody.css";

Expand All @@ -10,6 +9,7 @@ interface RequestNormalBodyProps {
formValues: FormValues;
idx: number;
property: string;
isFileType: boolean;
type: string;
handleChange: (e: ChangeEvent<HTMLInputElement>) => void;
}
Expand All @@ -19,18 +19,18 @@ export const RequestNormalBody = ({
formValues,
idx,
property,
isFileType,
type,
handleChange,
}: RequestNormalBodyProps) => {
console.log(body);
return (
<>
<div className={requestNormalBodyStyles.inputWrapper} key={property}>
<div className={requestNormalBodyStyles.inputBox} key={property}>
<label className={requestNormalBodyStyles.label}>{property}</label>
<label className={requestNormalBodyStyles.type}>
{typeConverter(body.properties[property].type)}
</label>
{type === "file" && (
<label className={requestNormalBodyStyles.type}>{type}</label>
{!!isFileType && (
<label
htmlFor="file"
className={requestNormalBodyStyles.inputLabel}
Expand Down

0 comments on commit cd3d998

Please sign in to comment.