|
1
|
import { NextResponse } from "next/server" |
|
2
|
import getSchemaContent from "../../utils/get-schema-content" |
|
3
|
import { getPathForEnv } from "../../utils/get-path-for-env" |
|
4
|
|
|
5
|
export async function GET(request: Request) { |
|
6
|
const { searchParams } = new URL(request.url) |
|
7
|
let name = searchParams.get("name") |
|
8
|
const area = searchParams.get("area") |
|
9
|
|
|
10
|
if (!name) { |
|
11
|
return NextResponse.json( |
|
12
|
{ |
|
13
|
success: false, |
|
14
|
message: `Name is required.`, |
|
15
|
}, |
|
16
|
{ |
|
17
|
status: 400, |
|
18
|
} |
|
19
|
) |
|
20
|
} |
|
21
|
|
|
22
|
if (area !== "admin" && area !== "store") { |
|
23
|
return NextResponse.json( |
|
24
|
{ |
|
25
|
success: false, |
|
26
|
message: `area ${area} is not allowed`, |
|
27
|
}, |
|
28
|
{ |
|
29
|
status: 400, |
|
30
|
} |
|
31
|
) |
|
32
|
} |
|
33
|
|
|
34
|
name = name |
|
35
|
.replace("#/components/schemas/", "") |
|
36
|
.replaceAll("./components/schemas/", "") |
|
37
|
|
|
38
|
const r2Base = process.env.SPECS_R2_BASE_URL |
|
39
|
|
|
40
|
const baseSchemasUrl = getPathForEnv( |
|
41
|
r2Base || process.cwd(), |
|
42
|
"specs", |
|
43
|
area, |
|
44
|
"components", |
|
45
|
"schemas" |
|
46
|
) |
|
47
|
const schemaUrl = getPathForEnv(baseSchemasUrl, name) |
|
48
|
|
|
49
|
try { |
|
50
|
const { dereferencedDocument, originalSchema: schema } = |
|
51
|
await getSchemaContent(schemaUrl, baseSchemasUrl) |
|
52
|
|
|
53
|
return NextResponse.json( |
|
54
|
{ |
|
55
|
schema: dereferencedDocument.components?.schemas |
|
56
|
? Object.values(dereferencedDocument.components?.schemas)[0] |
|
57
|
: schema, |
|
58
|
}, |
|
59
|
{ |
|
60
|
status: 200, |
|
61
|
} |
|
62
|
) |
|
63
|
} catch (error) { |
|
64
|
return NextResponse.json( |
|
65
|
{ |
|
66
|
success: false, |
|
67
|
message: `Failed to fetch schema: ${error}`, |
|
68
|
}, |
|
69
|
{ |
|
70
|
status: 500, |
|
71
|
} |
|
72
|
) |
|
73
|
} |
|
74
|
} |