11import { FastifyPluginAsync } from 'fastify' ;
22import { CollectionService } from '../services/collection.service' ;
33import { OpenApiService } from '../services/openapi.service' ;
4- import type { AuthConfig , BodyType , FormDataEntry } from '../models/proxy ' ;
4+ import type { SavedRequest } from '../models/collection ' ;
55import type { SyncApplyBody } from '../models/openapi-sync' ;
6+ import { createRequestPayloadSchema , updateRequestPayloadSchema } from '../dtos/collection.dto' ;
67
78interface Options {
89 collectionService : CollectionService ;
@@ -35,48 +36,43 @@ const collectionController: FastifyPluginAsync<Options> = async (server, opts) =
3536 async ( request , _reply ) => {
3637 const collection = await collectionService . update ( request . params . id , request . body ) ;
3738 return collection ;
38- } ,
39+ }
3940 ) ;
4041
4142 server . delete < { Params : { id : string } } > ( '/collections/:id' , async ( request , _reply ) => {
4243 await collectionService . delete ( request . params . id ) ;
4344 return { success : true } ;
4445 } ) ;
4546
46- server . post < { Params : { id : string } } > (
47- '/collections/:id/duplicate' ,
48- async ( request , reply ) => {
49- const collection = await collectionService . duplicateCollection ( request . params . id ) ;
50- return reply . code ( 201 ) . send ( collection ) ;
51- } ,
52- ) ;
47+ server . post < { Params : { id : string } } > ( '/collections/:id/duplicate' , async ( request , reply ) => {
48+ const collection = await collectionService . duplicateCollection ( request . params . id ) ;
49+ return reply . code ( 201 ) . send ( collection ) ;
50+ } ) ;
5351
5452 server . put < { Params : { id : string } ; Body : { targetOrder : number } } > (
5553 '/collections/:id/move' ,
5654 async ( request , _reply ) => {
5755 const { targetOrder } = request . body ;
5856 const collection = await collectionService . moveCollection ( request . params . id , targetOrder ) ;
5957 return collection ;
60- } ,
58+ }
6159 ) ;
6260
6361 server . post < {
6462 Params : { id : string } ;
65- Body : { name : string ; method : string ; url : string ; headers ?: Record < string , string > ; body ?: string ; bodyType ?: BodyType ; formDataEntries ?: FormDataEntry [ ] ; auth ?: AuthConfig ; folderId ?: string } ;
63+ Body : SavedRequest ;
6664 } > ( '/collections/:id/requests' , async ( request , reply ) => {
67- const { name, method, url, headers, body, bodyType, formDataEntries, auth, folderId } = request . body ;
68- if ( ! name || ! method || ! url ) {
69- return reply . code ( 400 ) . send ( { error : 'Name, method, and URL are required' } ) ;
70- }
71- const saved = await collectionService . addRequest ( request . params . id , { name, method, url, headers, body, bodyType, formDataEntries, auth, folderId } ) ;
65+ const data = createRequestPayloadSchema . parse ( request . body ) ;
66+ const saved = await collectionService . addRequest ( request . params . id , data ) ;
7267 return reply . code ( 201 ) . send ( saved ) ;
7368 } ) ;
7469
7570 server . put < {
7671 Params : { id : string ; requestId : string } ;
77- Body : { name ?: string ; method ?: string ; url ?: string ; headers ?: Record < string , string > ; body ?: string ; auth ?: AuthConfig ; folderId ?: string } ;
72+ Body : Partial < SavedRequest > ;
7873 } > ( '/collections/:id/requests/:requestId' , async ( request , _reply ) => {
79- const saved = await collectionService . updateRequest ( request . params . id , request . params . requestId , request . body ) ;
74+ const updates = updateRequestPayloadSchema . parse ( request . body ) ;
75+ const saved = await collectionService . updateRequest ( request . params . id , request . params . requestId , updates ) ;
8076 return saved ;
8177 } ) ;
8278
@@ -85,15 +81,15 @@ const collectionController: FastifyPluginAsync<Options> = async (server, opts) =
8581 async ( request , _reply ) => {
8682 await collectionService . deleteRequest ( request . params . id , request . params . requestId ) ;
8783 return { success : true } ;
88- } ,
84+ }
8985 ) ;
9086
9187 server . post < { Params : { id : string ; requestId : string } } > (
9288 '/collections/:id/requests/:requestId/duplicate' ,
9389 async ( request , reply ) => {
9490 const saved = await collectionService . duplicateRequest ( request . params . id , request . params . requestId ) ;
9591 return reply . code ( 201 ) . send ( saved ) ;
96- } ,
92+ }
9793 ) ;
9894
9995 server . post < { Params : { id : string } ; Body : { name : string ; parentId ?: string } } > (
@@ -105,23 +101,23 @@ const collectionController: FastifyPluginAsync<Options> = async (server, opts) =
105101 }
106102 const saved = await collectionService . addFolder ( request . params . id , { name, parentId } ) ;
107103 return reply . code ( 201 ) . send ( saved ) ;
108- } ,
104+ }
109105 ) ;
110106
111107 server . put < { Params : { id : string ; folderId : string } ; Body : { name ?: string ; parentId ?: string } } > (
112108 '/collections/:id/folders/:folderId' ,
113109 async ( request , _reply ) => {
114110 const saved = await collectionService . updateFolder ( request . params . id , request . params . folderId , request . body ) ;
115111 return saved ;
116- } ,
112+ }
117113 ) ;
118114
119115 server . delete < { Params : { id : string ; folderId : string } } > (
120116 '/collections/:id/folders/:folderId' ,
121117 async ( request , _reply ) => {
122118 await collectionService . deleteFolder ( request . params . id , request . params . folderId ) ;
123119 return { success : true } ;
124- } ,
120+ }
125121 ) ;
126122
127123 server . put < {
@@ -162,7 +158,7 @@ const collectionController: FastifyPluginAsync<Options> = async (server, opts) =
162158 }
163159 const result = await openApiService . importSpec ( source , { name, linkSpec } ) ;
164160 return reply . code ( 201 ) . send ( result ) ;
165- } ,
161+ }
166162 ) ;
167163
168164 server . get < { Params : { id : string } } > ( '/collections/:id/export' , async ( request , _reply ) => {
@@ -178,7 +174,7 @@ const collectionController: FastifyPluginAsync<Options> = async (server, opts) =
178174 '/collections/:id/sync-openapi/apply' ,
179175 async ( request , _reply ) => {
180176 return openApiService . applySync ( request . params . id , request . body ) ;
181- } ,
177+ }
182178 ) ;
183179
184180 server . delete < { Params : { id : string } } > ( '/collections/:id/openapi-link' , async ( request , _reply ) => {
0 commit comments