import { NextApiRequest , NextApiResponse } from 'next' ; import { PasteType } from 'types' ; import supabaseClient from 'utils/supabase' ; import filterBadWords from 'utils/filterBadWords' ; const handler = async ( req , res ) => { if ( req . method !== 'POST' ) { res . status ( 400 ) . json ( { message : 'Only POST requests allowed.' } ) ; return ; } let { code , language , title , description , pasteId , userId , _public , _private } = req . body ; if ( ! userId ) { return res . status ( 400 ) . json ( { message : 'Cannot delete anonymous paste' } ) ; } if ( code . trim ( ) === '' ) { return res . status ( 400 ) . json ( { message : 'Code cannot be blank.' } ) ; } if ( _public && _private ) { return res . status ( 400 ) . json ( { message : 'Paste cannot be public and private.' } ) ; } if ( _private && ! userId ) { return res . status ( 400 ) . json ( { message : 'Sign in to create a private paste.' } ) ; } if ( language . trim ( ) === '' ) { language = 'none' ; } const { data , error } = await supabaseClient . from < PasteType > ( 'Pastes' ) . update ( { code , language , title : filterBadWords ( title ) , description : filterBadWords ( description ) , public : _public , private : _private } ) . eq ( 'pasteId' , pasteId ) ; console . log ( data ) ; console . log ( error ) ; res . json ( { data , error } ) ; } ; export default handler ;