1
1
import { randomBytes } from "node:crypto" ;
2
- import type { CodemodListResponse } from "@codemod-com/api-types" ;
2
+ import {
3
+ type ApiResponse ,
4
+ BAD_REQUEST ,
5
+ type CodemodListResponse ,
6
+ INTERNAL_SERVER_ERROR ,
7
+ } from "@codemod-com/api-types" ;
3
8
import { getAuthPlugin } from "@codemod-com/auth" ;
4
9
import { prisma } from "@codemod-com/database" ;
5
10
import { decryptWithIv , encryptWithIv } from "@codemod-com/utilities" ;
@@ -10,6 +15,7 @@ import Fastify, {
10
15
type FastifyPluginCallback ,
11
16
type FastifyRequest ,
12
17
} from "fastify" ;
18
+ import type { InferOutput } from "valibot" ;
13
19
import {
14
20
type GetCodemodDownloadLinkResponse ,
15
21
getCodemodDownloadLink ,
@@ -25,6 +31,8 @@ import {
25
31
publishHandler ,
26
32
} from "./publishHandler.js" ;
27
33
import {
34
+ type beforeAfterDiffSchema ,
35
+ parseBeforeAfterDiffArray ,
28
36
parseCreateIssueBody ,
29
37
parseCreateIssueParams ,
30
38
parseDiffCreationBody ,
@@ -189,48 +197,46 @@ const routes: FastifyPluginCallback = (instance, _opts, done) => {
189
197
getCodemodsListHandler ,
190
198
) ;
191
199
192
- instance . get < { Reply : { before : string ; after : string } } > (
193
- "/ diffs/:id" ,
194
- async ( request , reply ) => {
195
- const { id } = parseGetCodeDiffParams ( request . params ) ;
196
- const { iv : ivStr } = parseIv ( request . query ) ;
200
+ instance . get < {
201
+ Reply : ApiResponse < { diffs : InferOutput < typeof beforeAfterDiffSchema > [ ] } > ;
202
+ } > ( "/diffs/:id" , async ( request , reply ) => {
203
+ const { id } = parseGetCodeDiffParams ( request . params ) ;
204
+ const { iv : ivStr } = parseIv ( request . query ) ;
197
205
198
- const key = Buffer . from ( environment . ENCRYPTION_KEY , "base64url" ) ;
199
- const iv = Buffer . from ( ivStr , "base64url" ) ;
206
+ const key = Buffer . from ( environment . ENCRYPTION_KEY , "base64url" ) ;
207
+ const iv = Buffer . from ( ivStr , "base64url" ) ;
200
208
201
- const codeDiff = await prisma . codeDiff . findUnique ( {
202
- where : { id } ,
203
- } ) ;
209
+ const codeDiff = await prisma . codeDiff . findUnique ( {
210
+ where : { id } ,
211
+ } ) ;
204
212
205
- if ( ! codeDiff ) {
206
- reply . code ( 400 ) . send ( ) ;
207
- return ;
208
- }
213
+ if ( ! codeDiff ) {
214
+ return reply
215
+ . code ( 400 )
216
+ . send ( { errorText : "Code diff not found" , error : BAD_REQUEST } ) ;
217
+ }
209
218
210
- let before : string ;
211
- let after : string ;
212
- try {
213
- before = decryptWithIv (
214
- "aes-256-cbc" ,
215
- { key, iv } ,
216
- Buffer . from ( codeDiff . before , "base64url" ) ,
217
- ) . toString ( ) ;
218
- after = decryptWithIv (
219
- "aes-256-cbc" ,
220
- { key, iv } ,
221
- Buffer . from ( codeDiff . after , "base64url" ) ,
222
- ) . toString ( ) ;
223
- } catch ( err ) {
224
- reply . code ( 400 ) . send ( ) ;
225
- return ;
226
- }
219
+ try {
220
+ const diffs = parseBeforeAfterDiffArray (
221
+ JSON . parse (
222
+ decryptWithIv (
223
+ "aes-256-cbc" ,
224
+ { key, iv } ,
225
+ Buffer . from ( codeDiff . diffs , "base64url" ) ,
226
+ ) . toString ( ) ,
227
+ ) ,
228
+ ) ;
227
229
228
- reply . type ( "application/json" ) . code ( 200 ) ;
229
- return { before, after } ;
230
- } ,
231
- ) ;
230
+ reply . type ( "application/json" ) . code ( 200 ) . send ( { diffs } ) ;
231
+ } catch ( err ) {
232
+ return reply . code ( 400 ) . send ( {
233
+ errorText : `Failed to decrypt the diffs array: ${ ( err as Error ) . message } ` ,
234
+ error : INTERNAL_SERVER_ERROR ,
235
+ } ) ;
236
+ }
237
+ } ) ;
232
238
233
- instance . post < { Reply : { id : string ; iv : string } } > (
239
+ instance . post < { Reply : ApiResponse < { id : string ; iv : string } > } > (
234
240
"/diffs" ,
235
241
async ( request , reply ) => {
236
242
const body = parseDiffCreationBody ( request . body ) ;
@@ -242,21 +248,18 @@ const routes: FastifyPluginCallback = (instance, _opts, done) => {
242
248
data : {
243
249
name : body . name ,
244
250
source : body . source ,
245
- before : encryptWithIv (
251
+ diffs : encryptWithIv (
246
252
"aes-256-cbc" ,
247
253
{ key, iv } ,
248
- Buffer . from ( body . before ) ,
249
- ) . toString ( "base64url" ) ,
250
- after : encryptWithIv (
251
- "aes-256-cbc" ,
252
- { key, iv } ,
253
- Buffer . from ( body . after ) ,
254
+ Buffer . from ( JSON . stringify ( body . diffs ) ) ,
254
255
) . toString ( "base64url" ) ,
255
256
} ,
256
257
} ) ;
257
258
258
- reply . type ( "application/json" ) . code ( 200 ) ;
259
- return { id : codeDiff . id , iv : iv . toString ( "base64url" ) } ;
259
+ return reply
260
+ . type ( "application/json" )
261
+ . code ( 200 )
262
+ . send ( { id : codeDiff . id , iv : iv . toString ( "base64url" ) } ) ;
260
263
} ,
261
264
) ;
262
265
0 commit comments