1- import { NextRequest , NextResponse } from "next/server"
2- import { session , userGitHubClient } from "@/composition"
1+ import { NextRequest , NextResponse } from "next/server"
2+ import { session } from "@/composition"
33import { makeUnauthenticatedAPIErrorResponse } from "@/common"
4- import { execSync } from "child_process "
4+ import { diffCalculator } from "@/composition "
55
66interface GetDiffParams {
77 owner : string
@@ -17,38 +17,30 @@ export async function GET(req: NextRequest, { params }: { params: Promise<GetDif
1717
1818 const { path : paramsPath , owner, repository } = await params
1919 const path = paramsPath . join ( "/" )
20-
21- const fromRef = req . nextUrl . searchParams . get ( "from" )
20+
2221 const toRef = req . nextUrl . searchParams . get ( "to" )
23-
24- if ( ! fromRef || ! toRef ) {
25- return NextResponse . json ( { error : "Missing from/to parameters" } , { status : 400 } )
22+ const baseRefOid = req . nextUrl . searchParams . get ( "baseRefOid" )
23+
24+ if ( ! toRef ) {
25+ return NextResponse . json ( { error : "Missing 'to' parameter" } , { status : 400 } )
26+ }
27+
28+ if ( ! baseRefOid ) {
29+ return NextResponse . json ( { error : "Missing 'baseRefOid' parameter" } , { status : 400 } )
2630 }
2731
28- const fullRepositoryName = repository + "-openapi"
29-
30- const spec1 = await userGitHubClient . getRepositoryContent ( {
31- repositoryOwner : owner ,
32- repositoryName : fullRepositoryName ,
33- path : path ,
34- ref : fromRef
35- } )
36-
37- const spec2 = await userGitHubClient . getRepositoryContent ( {
38- repositoryOwner : owner ,
39- repositoryName : fullRepositoryName ,
40- path : path ,
41- ref : toRef
42- } )
43-
44- const result = execSync ( `oasdiff changelog --format json "${ spec1 . downloadURL } " "${ spec2 . downloadURL } "` , { encoding : 'utf8' } )
45-
46-
47- const diffData = JSON . parse ( result )
48-
49- return NextResponse . json ( {
50- from : fromRef ,
51- to : toRef ,
52- changes : diffData
53- } )
54- }
32+ try {
33+ const diff = await diffCalculator . calculateDiff (
34+ owner ,
35+ repository ,
36+ path ,
37+ baseRefOid ,
38+ toRef
39+ )
40+
41+ return NextResponse . json ( diff )
42+ } catch ( error ) {
43+ const message = error instanceof Error ? error . message : "Unknown error while calculating diff"
44+ return NextResponse . json ( { error : message } , { status : 500 } )
45+ }
46+ }
0 commit comments