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")
}
};