API routes now return proper Responses and basic errors are handled

slight improvements in layouting
This commit is contained in:
2023-07-02 23:39:31 +02:00
parent ece3b3634c
commit 24ddd39f35
12 changed files with 349 additions and 78 deletions

View File

@ -3,23 +3,26 @@ import { Recipe } from '../../../models/Recipe';
import { dbConnect, dbDisconnect } from '../../../utils/db';
import type {RecipeModelType} from '../../../types/types';
import { BEARER_TOKEN } from '$env/static/private'
import { error } from '@sveltejs/kit';
// header: use for bearer token for now
// recipe json in body
export const POST: RequestHandler = async ({request}) => {
let message = await request.json()
const recipe_json = message.recipe
const bearer_token = message.headers.bearer
console.log("RECIPE:", recipe_json)
console.log("BEARER:", bearer_token)
if(bearer_token === BEARER_TOKEN){
console.log("PASSWORD CORRECT")
await dbConnect();
await Recipe.create(recipe_json);
try{
await Recipe.create(recipe_json);
} catch(e){
throw error(400, e)
}
await dbDisconnect();
return {status: 200} //TODO: cleanup error throwing
}
return new Response(JSON.stringify({msg: "Added recipe successfully"}),{
status: 200,
});
}
else{
console.log("PASSWORD INCORRECT")
return {status: 403}
}
throw error(403, "Password incorrect")
}
};

View File

@ -3,6 +3,7 @@ import { Recipe } from '../../../models/Recipe';
import { dbConnect, dbDisconnect } from '../../../utils/db';
import type {RecipeModelType} from '../../../types/types';
import { BEARER_TOKEN } from '$env/static/private'
import { error } from '@sveltejs/kit';
// header: use for bearer token for now
// recipe json in body
export const POST: RequestHandler = async ({request}) => {
@ -10,14 +11,14 @@ export const POST: RequestHandler = async ({request}) => {
const short_name = message.old_short_name
const bearer_token = message.headers.bearer
if(bearer_token === BEARER_TOKEN){
console.log("PASSWORD CORRECT")
await dbConnect();
await Recipe.findOneAndDelete({short_name: short_name});
await dbDisconnect();
return {status: 400} //TODO: cleanup error throwing
return new Response(JSON.stringify({msg: "Deleted recipe successfully"}),{
status: 200,
});
}
else{
console.log("PASSWORD INCORRECT")
return {status: 403}
throw error(403, "Password incorrect")
}
};
}

View File

@ -15,11 +15,12 @@ export const POST: RequestHandler = async ({request}) => {
await dbConnect();
await Recipe.findOneAndUpdate({short_name: message.old_short_name }, recipe_json);
await dbDisconnect();
const res = new Response(JSON.stringify({ message: "Updated Recipe successfully"}), { status: 200 })
return res
return new Response(JSON.stringify({msg: "Edited recipe successfully"}),{
status: 200,
});
}
else{
console.log("INCORRECT PASSWORD")
throw error(403, "Password incorrect")
}
};

View File

@ -6,7 +6,6 @@ import { error } from '@sveltejs/kit';
export const POST = (async ({ request }) => {
const data = await request.json();
console.log(data)
const filePath = path.join(
process.cwd(),
"static",
@ -14,13 +13,13 @@ export const POST = (async ({ request }) => {
data.filename as string
);
const file = data.image;
console.log(data.headers)
if(data.bearer === BEARER_TOKEN){
console.log("PASSWORD CORRECT")
writeFileSync(filePath, file, 'base64');
return new Response(JSON.stringify({msg: "Added image successfully"}),{
status: 200,
});
}
else{
console.log("PASSWORD INCORRECT")
throw error(403, "Password incorrect")
}

View File

@ -1,20 +0,0 @@
import path from 'path'
import fs from 'fs/promises'
import { fail, redirect } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export const POST = (async ({ request, url}) => {
try {
const data = Object.fromEntries(await request.formData())
const filePath = path.join(
process.cwd(),
"static",
"images",
data.filename as string
);
await fs.writeFile(filePath, Buffer.from(await (data.image as Blob).arrayBuffer()))
return new Response(String({status: 200}))
} catch (err) {
throw fail(500, { err: err })
}
}) satisfies RequestHandler;