1- const { Octokit } = require ( '@octokit/core' ) ;
2- const sharp = require ( 'sharp' ) ;
3- const crypto = require ( ' crypto') ;
1+ import { Octokit } from '@octokit/core' ;
2+ import sharp from 'sharp' ;
3+ import crypto from 'node: crypto';
44
5- // event. body expected to be:
5+ // Request JSON body expected to be:
66// {
77// title: "Something",
88// image: "base64string=",
@@ -38,28 +38,22 @@ async function validateImage(imageBase64) {
3838 }
3939}
4040
41- exports . handler = async function ( event , context ) {
42- console . log ( 'Sync handler called with' , event . body ) ;
41+ export default async ( request ) => {
42+ console . log ( 'Sync handler called' ) ;
4343
4444 if ( ! process . env . GITHUB_TOKEN ) {
4545 console . error ( 'GitHub Token not loaded' ) ;
46- return {
47- statusCode : 500 ,
48- body : JSON . stringify ( { } )
49- } ;
46+ return Response . json ( { } , { status : 500 } ) ;
5047 }
5148
5249 // parse payload
53- const postInfo = JSON . parse ( event . body ) ;
50+ const postInfo = await request . json ( ) ;
5451
5552 // validate image and extract its extension
5653 const { error, imageExtension } = await validateImage ( postInfo . image ) ;
5754 if ( error ) {
5855 console . error ( error ) ;
59- return {
60- statusCode : 400 ,
61- body : JSON . stringify ( { error } )
62- } ;
56+ return Response . json ( { error } , { status : 400 } ) ;
6357 }
6458
6559 // Shared properties
@@ -99,8 +93,8 @@ exports.handler = async function (event, context) {
9993 imageExtension
10094 } ;
10195 const signature = hmac . update ( JSON . stringify ( payload ) ) . digest ( 'hex' ) ;
102- await fetch (
103- event . rawUrl . replace ( ' submission-sync ', 'submission-background' ) ,
96+ const backgroundResponse = await fetch (
97+ new URL ( '/.netlify/functions/ submission-background ', request . url ) ,
10498 {
10599 method : 'POST' ,
106100 body : JSON . stringify ( payload ) ,
@@ -111,10 +105,13 @@ exports.handler = async function (event, context) {
111105 }
112106 ) ;
113107
114- return {
115- statusCode : 200 ,
116- body : JSON . stringify ( {
117- status : 'success'
118- } )
119- } ;
108+ if ( ! backgroundResponse . ok ) {
109+ console . error ( 'Background invocation failed:' , backgroundResponse . status ) ;
110+ return Response . json (
111+ { error : 'Could not start submission processing' } ,
112+ { status : 502 }
113+ ) ;
114+ }
115+
116+ return Response . json ( { status : 'success' } ) ;
120117} ;
0 commit comments