swagger-fsd-gen 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.yarn/install-state.gz +0 -0
- package/.yarnrc.yml +1 -0
- package/LICENSE +21 -0
- package/README.md +205 -0
- package/USAGE.md +137 -0
- package/package.json +40 -0
- package/scripts/fetch-swagger.js +90 -0
- package/scripts/generate-all.js +341 -0
- package/swagger-fsd-gen-1.0.0.tgz +0 -0
- package/templates/api.ejs +77 -0
- package/templates/data-contracts.ejs +94 -0
- package/templates/dto-import.ejs +27 -0
- package/templates/procedure-call.ejs +103 -0
- package/templates/route-docs.ejs +29 -0
- package/templates/route-types.ejs +15 -0
- package/templates/tanstack-query/api.ejs +195 -0
- package/templates/tanstack-query/route-types.ejs +152 -0
- package/utils/fetch-swagger.js +46 -0
- package/utils/file.js +81 -0
- package/utils/parser.js +111 -0
- package/utils/string.js +33 -0
- package/utils/url.js +34 -0
|
Binary file
|
package/.yarnrc.yml
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nodeLinker: node-modules
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 swagger-client-autogen contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# Swagger Client Autogen
|
|
2
|
+
|
|
3
|
+
**ky + TanStack Query + FSD 패턴**을 위한 Swagger API 클라이언트 자동 생성 도구입니다.
|
|
4
|
+
|
|
5
|
+
## ✨ 주요 기능
|
|
6
|
+
|
|
7
|
+
- 🚀 **ky HTTP 클라이언트** 기반 API 클래스 자동 생성
|
|
8
|
+
- 🔄 **TanStack Query** 훅 자동 생성 (useQuery, useMutation)
|
|
9
|
+
- 📁 **FSD(Feature-Sliced Design)** 패턴 자동 적용
|
|
10
|
+
- 🔐 **HTTP Basic Authentication** 지원
|
|
11
|
+
- 📝 **TypeScript** 완전 지원 (타입 안전성)
|
|
12
|
+
- 🎨 **Prettier** 자동 포맷팅
|
|
13
|
+
|
|
14
|
+
## 📦 설치 방법
|
|
15
|
+
|
|
16
|
+
### 1. 저장소 클론
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
git clone <repository-url>
|
|
20
|
+
cd swagger-client-autogen
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### 2. 의존성 설치
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
yarn install
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### 3. 전역 설치 (선택사항)
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
yarn link
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## 🚀 사용 방법
|
|
36
|
+
|
|
37
|
+
### 1. Swagger 문서 다운로드
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# 기본 사용법
|
|
41
|
+
fetch-swagger --url https://api.example.com/swagger.json
|
|
42
|
+
|
|
43
|
+
# 인증이 필요한 경우
|
|
44
|
+
fetch-swagger --url https://api.example.com/swagger.json --username admin --password secret
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**결과**: `swagger/` 디렉토리에 YAML 파일로 저장됩니다.
|
|
48
|
+
|
|
49
|
+
### 2. API 클라이언트 생성
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# 원격 Swagger 문서에서 생성
|
|
53
|
+
generate-all --uri https://api.example.com/swagger.json
|
|
54
|
+
|
|
55
|
+
# 로컬 파일에서 생성
|
|
56
|
+
generate-all --uri ./swagger/my-api.yml
|
|
57
|
+
|
|
58
|
+
# 인증이 필요한 경우
|
|
59
|
+
generate-all --uri https://api.example.com/swagger.json --username admin --password secret
|
|
60
|
+
|
|
61
|
+
# 커스텀 출력 경로 지정
|
|
62
|
+
generate-all --uri ./swagger/my-api.yml \
|
|
63
|
+
--dto-output-path ./src/shared/api/dto.ts \
|
|
64
|
+
--api-output-path ./src/entities/{moduleName}/api/index.ts \
|
|
65
|
+
--query-output-path ./src/entities/{moduleName}/api/queries.ts \
|
|
66
|
+
--mutation-output-path ./src/entities/{moduleName}/api/mutations.ts
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## 📁 생성되는 파일 구조 (FSD 패턴)
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
src/
|
|
73
|
+
├── shared/
|
|
74
|
+
│ └── api/
|
|
75
|
+
│ └── dto.ts # 모든 DTO 타입 정의
|
|
76
|
+
└── entities/
|
|
77
|
+
└── {moduleName}/ # Swagger 태그별 모듈
|
|
78
|
+
└── api/
|
|
79
|
+
├── index.ts # API 클래스
|
|
80
|
+
├── instance.ts # API 인스턴스
|
|
81
|
+
├── queries.ts # TanStack Query 훅
|
|
82
|
+
└── mutations.ts # TanStack Mutation 훅
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## 💡 생성된 코드 사용 예시
|
|
86
|
+
|
|
87
|
+
### 1. API 인스턴스 설정
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
// src/app/providers/api.ts
|
|
91
|
+
import ky from "ky";
|
|
92
|
+
|
|
93
|
+
export const apiInstance = ky.create({
|
|
94
|
+
prefixUrl: "https://api.example.com",
|
|
95
|
+
headers: {
|
|
96
|
+
"Content-Type": "application/json",
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 2. API 클래스 사용
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
// src/entities/user/api/instance.ts (자동 생성됨)
|
|
105
|
+
import { UserApi } from "./index";
|
|
106
|
+
import { apiInstance } from "@/app/providers/api";
|
|
107
|
+
|
|
108
|
+
export const userApi = new UserApi(apiInstance);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 3. TanStack Query 훅 사용
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
// src/pages/user/ui/UserProfile.tsx
|
|
115
|
+
import { useGetUserByIdQuery } from "@/entities/user/api/queries";
|
|
116
|
+
import { useUpdateUserMutation } from "@/entities/user/api/mutations";
|
|
117
|
+
|
|
118
|
+
export const UserProfile = ({ userId }: { userId: number }) => {
|
|
119
|
+
// Query 사용
|
|
120
|
+
const { data: user, isLoading } = useGetUserByIdQuery(userId);
|
|
121
|
+
|
|
122
|
+
// Mutation 사용
|
|
123
|
+
const updateUserMutation = useUpdateUserMutation({
|
|
124
|
+
onSuccess: () => {
|
|
125
|
+
console.log("User updated successfully!");
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const handleUpdate = (userData: UpdateUserRequestDto) => {
|
|
130
|
+
updateUserMutation.mutate({
|
|
131
|
+
id: userId,
|
|
132
|
+
body: userData,
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
if (isLoading) return <div>Loading...</div>;
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
<div>
|
|
140
|
+
<h1>{user?.name}</h1>
|
|
141
|
+
<button onClick={() => handleUpdate({ name: "New Name" })}>
|
|
142
|
+
Update User
|
|
143
|
+
</button>
|
|
144
|
+
</div>
|
|
145
|
+
);
|
|
146
|
+
};
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## ⚙️ 설정 옵션
|
|
150
|
+
|
|
151
|
+
### 명령행 옵션
|
|
152
|
+
|
|
153
|
+
| 옵션 | 단축키 | 설명 | 기본값 |
|
|
154
|
+
| ------------------------ | ------ | ------------------------------- | -------------------------------------------- |
|
|
155
|
+
| `--uri` | `-u` | Swagger 문서 URL 또는 파일 경로 | 필수 |
|
|
156
|
+
| `--username` | `-un` | HTTP Basic Auth 사용자명 | - |
|
|
157
|
+
| `--password` | `-pw` | HTTP Basic Auth 비밀번호 | - |
|
|
158
|
+
| `--dto-output-path` | `-dp` | DTO 파일 출력 경로 | `src/shared/api/dto.ts` |
|
|
159
|
+
| `--api-output-path` | `-ap` | API 클래스 출력 경로 | `src/entities/{moduleName}/api/index.ts` |
|
|
160
|
+
| `--query-output-path` | `-qp` | Query 훅 출력 경로 | `src/entities/{moduleName}/api/queries.ts` |
|
|
161
|
+
| `--mutation-output-path` | `-mp` | Mutation 훅 출력 경로 | `src/entities/{moduleName}/api/mutations.ts` |
|
|
162
|
+
| `--project-template` | `-pt` | 커스텀 템플릿 경로 | - |
|
|
163
|
+
|
|
164
|
+
### 경로에서 `{moduleName}` 사용
|
|
165
|
+
|
|
166
|
+
`{moduleName}`을 포함한 경로는 Swagger 태그명으로 자동 대체됩니다.
|
|
167
|
+
|
|
168
|
+
예: `User` 태그 → `user` 모듈명으로 변환
|
|
169
|
+
|
|
170
|
+
## 🔧 커스텀 템플릿
|
|
171
|
+
|
|
172
|
+
기본 템플릿을 복사하여 프로젝트에 맞게 수정할 수 있습니다:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
# 템플릿 복사
|
|
176
|
+
cp -r templates/ ./my-templates/
|
|
177
|
+
|
|
178
|
+
# 커스텀 템플릿 사용
|
|
179
|
+
generate-all --uri ./swagger/my-api.yml --project-template ./my-templates/
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## 🛠️ 개발 환경
|
|
183
|
+
|
|
184
|
+
- **Node.js**: 18+
|
|
185
|
+
- **Package Manager**: Yarn 4.7.0
|
|
186
|
+
- **Type**: ES Module
|
|
187
|
+
|
|
188
|
+
## 📋 의존성
|
|
189
|
+
|
|
190
|
+
- `swagger-typescript-api`: Swagger 문서 파싱 및 코드 생성
|
|
191
|
+
- `minimist`: 명령행 인수 파싱
|
|
192
|
+
- `js-yaml`: YAML 파일 처리
|
|
193
|
+
- `node-fetch`: HTTP 요청
|
|
194
|
+
|
|
195
|
+
## 🤝 기여하기
|
|
196
|
+
|
|
197
|
+
1. Fork the repository
|
|
198
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
199
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
200
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
201
|
+
5. Open a Pull Request
|
|
202
|
+
|
|
203
|
+
## 📄 라이선스
|
|
204
|
+
|
|
205
|
+
이 프로젝트는 [MIT License](LICENSE) 하에 배포됩니다.
|
package/USAGE.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# 🚀 Quick Start Guide
|
|
2
|
+
|
|
3
|
+
## 📋 요구사항
|
|
4
|
+
|
|
5
|
+
- Node.js 18+
|
|
6
|
+
- Yarn 4.7.0+
|
|
7
|
+
|
|
8
|
+
## 🔧 설치
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
# 1. 저장소 클론
|
|
12
|
+
git clone <your-repo-url>
|
|
13
|
+
cd swagger-client-autogen
|
|
14
|
+
|
|
15
|
+
# 2. 의존성 설치
|
|
16
|
+
yarn install
|
|
17
|
+
|
|
18
|
+
# 3. 전역 설치 (선택사항)
|
|
19
|
+
yarn link
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 💡 사용법
|
|
23
|
+
|
|
24
|
+
### 1단계: Swagger 문서 다운로드 (선택사항)
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# 원격 Swagger 문서를 로컬에 저장
|
|
28
|
+
fetch-swagger --url https://api.example.com/swagger.json
|
|
29
|
+
|
|
30
|
+
# 인증이 필요한 경우
|
|
31
|
+
fetch-swagger --url https://api.example.com/swagger.json --username admin --password secret
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### 2단계: API 클라이언트 생성
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
# 원격 URL에서 직접 생성
|
|
38
|
+
generate-all --uri https://api.example.com/swagger.json
|
|
39
|
+
|
|
40
|
+
# 로컬 파일에서 생성
|
|
41
|
+
generate-all --uri ./swagger/my-api.yml
|
|
42
|
+
|
|
43
|
+
# 인증이 필요한 경우
|
|
44
|
+
generate-all --uri https://api.example.com/swagger.json --username admin --password secret
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 📁 생성되는 파일
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
src/
|
|
51
|
+
├── shared/
|
|
52
|
+
│ └── api/
|
|
53
|
+
│ └── dto.ts # 모든 DTO 타입
|
|
54
|
+
└── entities/
|
|
55
|
+
└── {moduleName}/ # 각 Swagger 태그별
|
|
56
|
+
└── api/
|
|
57
|
+
├── index.ts # API 클래스
|
|
58
|
+
├── instance.ts # API 인스턴스
|
|
59
|
+
├── queries.ts # Query 훅
|
|
60
|
+
└── mutations.ts # Mutation 훅
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## 🎯 프로젝트에 적용하기
|
|
64
|
+
|
|
65
|
+
### 1. 필요한 패키지 설치
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npm install ky @tanstack/react-query
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 2. API 인스턴스 설정
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// src/app/providers/api.ts
|
|
75
|
+
import ky from "ky";
|
|
76
|
+
|
|
77
|
+
export const apiInstance = ky.create({
|
|
78
|
+
prefixUrl: "https://api.example.com",
|
|
79
|
+
headers: {
|
|
80
|
+
"Content-Type": "application/json",
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 3. 생성된 코드 사용
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
// Query 사용
|
|
89
|
+
import { useGetUsersQuery } from "@/entities/user/api/queries";
|
|
90
|
+
|
|
91
|
+
const { data: users, isLoading } = useGetUsersQuery();
|
|
92
|
+
|
|
93
|
+
// Mutation 사용
|
|
94
|
+
import { useCreateUserMutation } from "@/entities/user/api/mutations";
|
|
95
|
+
|
|
96
|
+
const createUser = useCreateUserMutation({
|
|
97
|
+
onSuccess: () => console.log("User created!"),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
createUser.mutate({ body: { name: "John" } });
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## ⚙️ 커스텀 설정
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
# 출력 경로 커스터마이징
|
|
107
|
+
generate-all --uri ./swagger.json \
|
|
108
|
+
--dto-output-path ./src/types/api.ts \
|
|
109
|
+
--api-output-path ./src/api/{moduleName}/client.ts \
|
|
110
|
+
--query-output-path ./src/api/{moduleName}/queries.ts \
|
|
111
|
+
--mutation-output-path ./src/api/{moduleName}/mutations.ts
|
|
112
|
+
|
|
113
|
+
# 커스텀 템플릿 사용
|
|
114
|
+
generate-all --uri ./swagger.json --project-template ./my-templates/
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## 🔍 문제 해결
|
|
118
|
+
|
|
119
|
+
### 의존성 오류
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
yarn install
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 권한 오류 (전역 설치 시)
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
yarn unlink
|
|
129
|
+
yarn link
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 생성된 파일 확인
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
# 생성된 파일 목록 확인
|
|
136
|
+
find src -name "*.ts" -type f | grep -E "(api|dto)" | sort
|
|
137
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "swagger-fsd-gen",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
},
|
|
8
|
+
"keywords": [
|
|
9
|
+
"swagger",
|
|
10
|
+
"openapi",
|
|
11
|
+
"api-client",
|
|
12
|
+
"typescript",
|
|
13
|
+
"ky",
|
|
14
|
+
"tanstack-query",
|
|
15
|
+
"code-generator",
|
|
16
|
+
"fsd"
|
|
17
|
+
],
|
|
18
|
+
"author": "sen2y",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"bin": {
|
|
22
|
+
"fetch-swagger": "./scripts/fetch-swagger.js",
|
|
23
|
+
"generate-all": "./scripts/generate-all.js"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"js-yaml": "^4.1.0",
|
|
27
|
+
"minimist": "^1.2.8",
|
|
28
|
+
"node-fetch": "^3.3.2",
|
|
29
|
+
"swagger-typescript-api": "^13.0.22"
|
|
30
|
+
},
|
|
31
|
+
"packageManager": "yarn@4.7.0",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/sen-jik/swagger-autogen.git"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/sen-jik/swagger-autogen#readme",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/sen-jik/swagger-autogen/issues"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Swagger 문서 다운로드 도구
|
|
5
|
+
* - 원격 Swagger 문서를 가져와서 로컬에 YAML 파일로 저장
|
|
6
|
+
* - HTTP Basic Authentication 지원
|
|
7
|
+
* - 파일명은 Swagger 문서의 title을 kebab-case로 변환하여 생성
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import minimist from "minimist";
|
|
12
|
+
import yaml from "js-yaml";
|
|
13
|
+
import { fetchSwagger } from "../utils/fetch-swagger.js";
|
|
14
|
+
import { toKebabCase } from "../utils/string.js";
|
|
15
|
+
import { writeFileToPath } from "../utils/file.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 명령행 인수 파싱
|
|
19
|
+
*/
|
|
20
|
+
const argv = minimist(process.argv.slice(2), {
|
|
21
|
+
string: ["url", "username", "password"],
|
|
22
|
+
alias: {
|
|
23
|
+
u: "url",
|
|
24
|
+
un: "username",
|
|
25
|
+
pw: "password",
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const { url, username, password } = argv;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 사용법 출력
|
|
33
|
+
*/
|
|
34
|
+
const printUsage = () => {
|
|
35
|
+
console.error("❗️ Error: Please provide the Swagger URL");
|
|
36
|
+
console.error(
|
|
37
|
+
"Usage: fetch-swagger --url <swagger-url> [--username <username>] [--password <password>]"
|
|
38
|
+
);
|
|
39
|
+
console.error("\nExamples:");
|
|
40
|
+
console.error(" fetch-swagger --url https://api.example.com/swagger.json");
|
|
41
|
+
console.error(
|
|
42
|
+
" fetch-swagger --url https://api.example.com/swagger.json --username admin --password secret"
|
|
43
|
+
);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 메인 실행 함수
|
|
48
|
+
*/
|
|
49
|
+
const main = async () => {
|
|
50
|
+
console.log("📥 Starting Swagger document download...\n");
|
|
51
|
+
|
|
52
|
+
// URL 필수 체크
|
|
53
|
+
if (!url) {
|
|
54
|
+
printUsage();
|
|
55
|
+
process.exit(1);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
console.log(`🔄 Fetching Swagger document from: ${url}`);
|
|
60
|
+
|
|
61
|
+
// 인증 정보가 있는 경우 표시
|
|
62
|
+
if (username) {
|
|
63
|
+
console.log(`🔐 Using authentication: ${username}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Swagger 문서 가져오기
|
|
67
|
+
const swaggerData = await fetchSwagger(url, username, password);
|
|
68
|
+
|
|
69
|
+
// YAML 형식으로 변환
|
|
70
|
+
const yamlData = yaml.dump(swaggerData);
|
|
71
|
+
|
|
72
|
+
// 파일명 생성 (title을 kebab-case로 변환)
|
|
73
|
+
const fileName = toKebabCase(swaggerData.info.title);
|
|
74
|
+
const outputPath = path.resolve(process.cwd(), `swagger/${fileName}.yml`);
|
|
75
|
+
|
|
76
|
+
// 파일 저장
|
|
77
|
+
await writeFileToPath(outputPath, yamlData);
|
|
78
|
+
|
|
79
|
+
console.log(`✅ Successfully downloaded and saved Swagger document`);
|
|
80
|
+
console.log(`📁 File saved to: swagger/${fileName}.yml`);
|
|
81
|
+
console.log(`📋 API Title: ${swaggerData.info.title}`);
|
|
82
|
+
console.log(`📝 API Version: ${swaggerData.info.version}`);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.error("\n❌ Failed to download Swagger document:");
|
|
85
|
+
console.error(error.message);
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
main();
|