Skip to content

Commit

Permalink
Fix for Nutlope#18
Browse files Browse the repository at this point in the history
  • Loading branch information
mwelwankuta committed Jan 31, 2023
1 parent 3f5cc66 commit 09c03d4
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pages/api/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ interface ExtendedNextApiRequest extends NextApiRequest {
};
}

interface EndPointSuccess {
pass: string | number | null;
fail: string | number | null;
}
// Create a new ratelimiter, that allows 3 requests per 60 seconds
const ratelimit = redis
? new Ratelimit({
Expand All @@ -18,6 +22,14 @@ const ratelimit = redis
})
: undefined;

const endpointResponse = (restoredImage: string | null, endpointSuccess: EndPointSuccess) => {
const {pass, fail} = endpointSuccess;
if (restoredImage) {
return pass;
}
return fail;
}

export default async function handler(
req: ExtendedNextApiRequest,
res: NextApiResponse<Data>
Expand Down Expand Up @@ -79,7 +91,9 @@ export default async function handler(
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
const status = endpointResponse(restoredImage, {pass: 200, fail: 500})
const json = endpointResponse(restoredImage, {pass: restoredImage, fail: "Failed to restore image"})
res
.status(200)
.json(restoredImage ? restoredImage : "Failed to restore image");
.status(status)
.json(json);
}

0 comments on commit 09c03d4

Please sign in to comment.