|
1 | 1 | import fs from 'fs/promises';
|
2 | 2 | import path from 'path';
|
3 | 3 | import { NextResponse } from 'next/server';
|
4 |
| -import { corsMiddleware } from '../middleware'; |
5 |
| -const testDataPath = path.join(process.cwd(), 'dataBuild.json'); |
6 | 4 |
|
| 5 | +const testDataPath = path.join(process.cwd(), 'dataBuild.json'); |
7 | 6 |
|
8 |
| -export async function GET(request) { |
9 |
| - try { |
10 |
| - const corsResponse = await corsMiddleware(request); |
11 |
| - console.log('get request made to /dashboard/api/build'); |
12 |
| - const data = JSON.parse(await fs.readFile(testDataPath, 'utf8')); |
13 |
| - console.log('build time data:', data); |
14 |
| - //original version |
15 |
| - // return NextResponse.json(data, { headers: corsResponse.headers }); //does this need the new keyword? |
16 |
| - //new version - Combine CORS headers with JSON response |
17 |
| - const response = NextResponse.json(data); |
18 |
| - corsResponse.headers.forEach((value, key) => { |
19 |
| - response.headers.set(key, value); |
20 |
| - }); |
21 |
| - return response; |
22 |
| - } catch (error) { |
23 |
| - console.error(error); |
24 |
| - return new NextResponse(`Internal Server Error: ${error.message}`, { status: 500 }); //does this need the new keyword? |
25 |
| - } |
| 7 | +export async function GET() { |
| 8 | + try { |
| 9 | + const data = JSON.parse(await fs.readFile(testDataPath, 'utf8')); |
| 10 | + return NextResponse.json(data); |
| 11 | + } catch (error) { |
| 12 | + console.error(error); |
| 13 | + return NextResponse.json({ error: error.message }, { status: 500 }); |
| 14 | + } |
26 | 15 | }
|
27 | 16 |
|
28 | 17 | export async function POST(request) {
|
29 |
| - try { |
30 |
| - const corsResponse = await corsMiddleware(request); // Apply CORS middleware |
31 |
| - console.log('post request made to /dashboard/api/build'); |
32 |
| - const body = await request.json(); |
33 |
| - const apiKey = request.headers.get('api-key'); |
34 |
| - const newData = { |
35 |
| - "buildTime": body.buildTime, //do the keys need to be strings? |
36 |
| - "apiKey": apiKey, |
37 |
| - }; |
38 |
| - const currentData = JSON.parse(await fs.readFile(testDataPath, 'utf8')); |
39 |
| - currentData.testData.push(newData); |
40 |
| - await fs.writeFile(testDataPath, JSON.stringify(currentData)); |
41 |
| - |
42 |
| - //original version |
43 |
| - // return NextResponse(JSON.stringify(newData), { //does this need the new keyword? |
44 |
| - // headers: corsResponse.headers, |
45 |
| - // status: 201, |
46 |
| - // }); |
47 |
| - |
48 |
| - //new version - Combine CORS headers with JSON response |
49 |
| - const response = new NextResponse(JSON.stringify(newData), { status: 201 }); |
50 |
| - corsResponse.headers.forEach((value, key) => { |
51 |
| - response.headers.set(key, value); |
52 |
| - }); |
53 |
| - return response; |
54 |
| - |
55 |
| - } catch (error) { |
56 |
| - console.error(error); |
57 |
| - return new NextResponse('Internal Server Error', { status: 500 }); //does this need the new keyword? |
58 |
| - } |
| 18 | + try { |
| 19 | + const body = await request.json(); |
| 20 | + const apiKey = request.headers.get('Authorization'); |
| 21 | + const newData = { |
| 22 | + buildTime: body.buildTime, |
| 23 | + apiKey: apiKey, |
| 24 | + }; |
| 25 | + const currentData = JSON.parse(await fs.readFile(testDataPath, 'utf8')); |
| 26 | + currentData.testData.push(newData); |
| 27 | + await fs.writeFile(testDataPath, JSON.stringify(currentData)); |
| 28 | + |
| 29 | + return NextResponse.json(newData, { status: 201 }); |
| 30 | + } catch (error) { |
| 31 | + console.error(error); |
| 32 | + return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }); |
| 33 | + } |
59 | 34 | }
|
60 | 35 |
|
| 36 | + |
| 37 | +// import fs from 'fs/promises'; |
| 38 | +// import path from 'path'; |
| 39 | +// import { NextResponse } from 'next/server'; |
| 40 | +// import { corsMiddleware } from '../middleware'; |
| 41 | +// const testDataPath = path.join(process.cwd(), 'dataBuild.json'); |
| 42 | + |
| 43 | + |
| 44 | +// export async function GET(request) { |
| 45 | +// try { |
| 46 | +// const corsResponse = await corsMiddleware(request); |
| 47 | +// console.log('get request made to /dashboard/api/build'); |
| 48 | +// const data = JSON.parse(await fs.readFile(testDataPath, 'utf8')); |
| 49 | +// console.log('build time data:', data); |
| 50 | +// //original version |
| 51 | +// // return NextResponse.json(data, { headers: corsResponse.headers }); //does this need the new keyword? |
| 52 | +// //new version - Combine CORS headers with JSON response |
| 53 | +// const response = NextResponse.json(data); |
| 54 | +// corsResponse.headers.forEach((value, key) => { |
| 55 | +// response.headers.set(key, value); |
| 56 | +// }); |
| 57 | +// return response; |
| 58 | +// } catch (error) { |
| 59 | +// console.error(error); |
| 60 | +// return new NextResponse(`Internal Server Error: ${error.message}`, { status: 500 }); //does this need the new keyword? |
| 61 | +// } |
| 62 | +// } |
| 63 | + |
| 64 | +// export async function POST(request) { |
| 65 | +// try { |
| 66 | +// const corsResponse = await corsMiddleware(request); // Apply CORS middleware |
| 67 | +// console.log('post request made to /dashboard/api/build'); |
| 68 | +// const body = await request.json(); |
| 69 | +// const apiKey = request.headers.get('Authorization'); |
| 70 | +// const newData = { |
| 71 | +// "buildTime": body.buildTime, //do the keys need to be strings? |
| 72 | +// "apiKey": apiKey, |
| 73 | +// }; |
| 74 | +// const currentData = JSON.parse(await fs.readFile(testDataPath, 'utf8')); |
| 75 | +// currentData.testData.push(newData); |
| 76 | +// await fs.writeFile(testDataPath, JSON.stringify(currentData)); |
| 77 | + |
| 78 | +// //original version |
| 79 | +// // return NextResponse(JSON.stringify(newData), { //does this need the new keyword? |
| 80 | +// // headers: corsResponse.headers, |
| 81 | +// // status: 201, |
| 82 | +// // }); |
| 83 | + |
| 84 | +// //new version - Combine CORS headers with JSON response |
| 85 | +// const response = new NextResponse(JSON.stringify(newData), { status: 201 }); |
| 86 | +// corsResponse.headers.forEach((value, key) => { |
| 87 | +// response.headers.set(key, value); |
| 88 | +// }); |
| 89 | +// return response; |
| 90 | + |
| 91 | +// } catch (error) { |
| 92 | +// console.error(error); |
| 93 | +// return new NextResponse('Internal Server Error', { status: 500 }); //does this need the new keyword? |
| 94 | +// } |
| 95 | +// } |
| 96 | + |
0 commit comments