22 NOTE: /api/pastes/create -> endpoint for creating a paste
33*/
44
5- import type { NextApiRequest , NextApiResponse } from 'next' ;
6-
7- import { nanoid } from 'nanoid' ;
8-
9- import { PasteModel } from '@lib/models/paste' ;
10- import methodHandler from '@lib/middleware/methods' ;
11- import { getTokenAPI } from '@lib/hooks/useTokenAPI' ;
5+ import { errParseBody } from '@lib/body-parse' ;
126import { getCodeLanguage } from '@lib/code' ;
137import { getUser } from '@lib/hooks/getUser' ;
8+ import { getTokenAPI } from '@lib/hooks/useTokenAPI' ;
9+ import methodHandler from '@lib/middleware/methods' ;
10+ import { PasteModel } from '@lib/models/paste' ;
1411import { schemaValidate } from '@lib/validate' ;
15-
16- import { errParseBody } from '@lib/body-parse' ;
12+ import { ApiCreatePasteBody , Paste } from '@utils/interfaces/paste' ;
1713import { CreatePasteQuery , QueryErrorResponse } from '@utils/interfaces/query' ;
1814import { ApiCreateBodySchema } from '@utils/schema/createBody' ;
19- import { ApiCreatePasteBody , Paste } from '@utils/interfaces/paste' ;
15+ import { nanoid } from 'nanoid' ;
16+ import type { NextApiRequest , NextApiResponse } from 'next' ;
2017
2118export type ApiCreatePasteResponse = CreatePasteQuery ;
2219type ValidateCreateProps = { rdata : ApiCreatePasteBody ; ok : boolean ; err ?: QueryErrorResponse } ;
@@ -31,7 +28,7 @@ const createPaste = async (req: NextApiRequest, res: NextApiResponse<ApiCreatePa
3128 }
3229
3330 const token = getTokenAPI ( req , res ) ;
34- const { isUser, name } = await getUser ( req , res ) ;
31+ const { isUser, name } = await handleUser ( req , res ) ;
3532
3633 const data : Paste = {
3734 createdDate : new Date ( ) . toISOString ( ) ,
@@ -50,6 +47,19 @@ const createPaste = async (req: NextApiRequest, res: NextApiResponse<ApiCreatePa
5047 res . status ( q . code ) . json ( q ) ;
5148} ;
5249
50+ const handleUser = async ( req : NextApiRequest , res : NextApiResponse ) : Promise < { isUser : boolean ; name : string } > => {
51+ const { isAnonymous } = req . query ;
52+ if ( isAnonymous === 'true' ) {
53+ return {
54+ isUser : false ,
55+ name : 'anonymous'
56+ } ;
57+ }
58+
59+ const { isUser, name } = await getUser ( req , res ) ;
60+ return { isUser, name } ;
61+ } ;
62+
5363// Getter and Validator for req.body
5464const getPostCreateData = async ( req : NextApiRequest ) : Promise < ValidateCreateProps > => {
5565 const d : ApiCreatePasteBody = req . body ;
0 commit comments