Skip to content

Commit cf1cbc3

Browse files
committed
feat: type이 스키마 기반으로 참조가 걸려있는 부분 처리 추가
1 parent 2d5cce2 commit cf1cbc3

5 files changed

Lines changed: 42 additions & 15 deletions

File tree

‎src/entities/swagger/types/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface SchemasProperties {
4949
type: SwaggerType;
5050
format?: SwaggerFormat;
5151
};
52+
$ref?: string;
5253
}
5354

5455
export interface Schemas {

‎src/shared/util/typeConverter.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const typeConverter = (type: string) => {
22
if (type === "integer") return "number";
3+
if (!type) return "unknown";
34
return type;
45
};
56

‎src/widgets/request-body/ui/api-body/RequestBody.tsx‎

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

32+
const { pathInfo } = useSwaggerDocStore();
33+
34+
const { data: apiDocsData } = useGETDocs(pathInfo);
35+
console.log(apiDocsData);
36+
2937
const onChangeBodyValue = (e: ChangeEvent<HTMLInputElement>) => {
3038
// file type일 경우 직접 addArrayItem 호출
3139
if (e.target.id === "file" && e.target.files) {
@@ -40,16 +48,29 @@ export const RequestBody = ({
4048
setBodyValue("");
4149
};
4250

43-
const isItemsTypeFile = (property: string) => {
44-
let returnType = "text";
51+
const isFileType = (property: string) => {
4552
Object.keys(body.properties).map((property) => {
4653
if (body.properties[property]?.format === "binary") {
47-
returnType = "file";
54+
return true;
4855
}
4956
});
50-
// if (body.properties[property].type === "binary") return "file";
51-
if (body.properties[property].items?.format === "binary") return "file";
52-
return returnType;
57+
if (body.properties[property].items?.format === "binary") return true;
58+
return false;
59+
};
60+
61+
const getType = (property: string) => {
62+
const type = body.properties[property].type;
63+
if (type) {
64+
console.log("type", type);
65+
return typeConverter(type);
66+
}
67+
const fullRef = body.properties[property].$ref;
68+
console.log("fullRef", fullRef);
69+
const ref = fullRef.split("/").pop();
70+
console.log("ref", ref);
71+
const schema = apiDocsData?.components?.schemas[ref];
72+
console.log(schema);
73+
return typeConverter(schema?.type);
5374
};
5475

5576
return (
@@ -71,7 +92,8 @@ export const RequestBody = ({
7192
property={property}
7293
addArrayItem={onAddArrayItem}
7394
removeArrayItem={removeArrayItem}
74-
type={isItemsTypeFile(property)}
95+
type={getType(property)}
96+
isFileType={isFileType(property)}
7597
/>
7698
) : (
7799
<RequestNormalBody
@@ -80,7 +102,8 @@ export const RequestBody = ({
80102
handleChange={handleChange}
81103
idx={idx}
82104
property={property}
83-
type={isItemsTypeFile(property)}
105+
type={getType(property)}
106+
isFileType={isFileType(property)}
84107
/>
85108
)}
86109
</>

‎src/widgets/request-body/ui/array-body/RequestArrayBody.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface RequestArrayBodyProps {
1010
formValues: FormValues;
1111
idx: number;
1212
property: string;
13+
isFileType: boolean;
1314
type: string;
1415
handleChange: (e: ChangeEvent<HTMLInputElement>) => void;
1516
addArrayItem: (key: string) => void;
@@ -22,6 +23,7 @@ export const RequestArrayBody = ({
2223
idx,
2324
property,
2425
type,
26+
isFileType,
2527
addArrayItem,
2628
removeArrayItem,
2729
handleChange,
@@ -35,7 +37,7 @@ export const RequestArrayBody = ({
3537
{typeConverter(body.properties[property].type)}
3638
</label>
3739
<div className={requestArrayBodyStyles.rightWrapper}>
38-
{type === "file" && (
40+
{!!isFileType && (
3941
<label
4042
htmlFor="file"
4143
className={requestArrayBodyStyles.inputLabel}
@@ -59,7 +61,7 @@ export const RequestArrayBody = ({
5961
autoFocus={idx === 0}
6062
onFocus={(e) => e.target.select()}
6163
/>
62-
{type !== "file" && (
64+
{!isFileType && (
6365
<button
6466
onClick={() => addArrayItem(property)}
6567
type="button"

‎src/widgets/request-body/ui/normal-body/RequestNormalBody.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Schemas } from "@/entities/swagger/types";
22
import { FormValues } from "@/features/request-api/module/hooks/useForm";
33
import Input from "@/shared/ui/Input";
4-
import { typeConverter } from "@/shared/util/typeConverter";
54
import { ChangeEvent } from "react";
65
import { requestNormalBodyStyles } from "./requestNormalBody.css";
76

@@ -10,6 +9,7 @@ interface RequestNormalBodyProps {
109
formValues: FormValues;
1110
idx: number;
1211
property: string;
12+
isFileType: boolean;
1313
type: string;
1414
handleChange: (e: ChangeEvent<HTMLInputElement>) => void;
1515
}
@@ -19,18 +19,18 @@ export const RequestNormalBody = ({
1919
formValues,
2020
idx,
2121
property,
22+
isFileType,
2223
type,
2324
handleChange,
2425
}: RequestNormalBodyProps) => {
26+
console.log(body);
2527
return (
2628
<>
2729
<div className={requestNormalBodyStyles.inputWrapper} key={property}>
2830
<div className={requestNormalBodyStyles.inputBox} key={property}>
2931
<label className={requestNormalBodyStyles.label}>{property}</label>
30-
<label className={requestNormalBodyStyles.type}>
31-
{typeConverter(body.properties[property].type)}
32-
</label>
33-
{type === "file" && (
32+
<label className={requestNormalBodyStyles.type}>{type}</label>
33+
{!!isFileType && (
3434
<label
3535
htmlFor="file"
3636
className={requestNormalBodyStyles.inputLabel}

0 commit comments

Comments
 (0)