Simon
Simon

Reputation: 51

Using MirageJS with NextJS API route

I want to call an external API from the api folder from Next.js. I want to call it on server side because the client should not see what URL is called. In the dev environment I use a Mirage server to mock the external API. Is it possible to catch this request with MirageJS?

Some more details:

This is the first call from the client to the API server side.

async function userExists(email: string, callback: (arg: any) => void)  {
    await axios.get('/api/lcm/users/exists/' + email)
        .then(response => {
            logger.info(response)
            callback(response.status)
        })
        .catch(error => {
            logger.error("an error occured in lcmServices, userExists()", error)
    //do something with errors
            callback(error)
            throw error
        });
}

This snippet called from above.

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
    const  mail = req.query.email;

    await axios.get(process.env.NEXT_PUBLIC_LCM_FIRSTRUNNER_URL + '/service/exist/mail/' + mail)
        .then(response => {
            res.status(response.status).send(response.data);
        })
        .catch(error => {
            console.log(error)
            //do something with errors
            throw error
        });

    return res.end(); 
}

Then I have a route in MirageJS which should catch this call above process.env.NEXT_PUBLIC_LCM_FIRSTRUNNER_URL + '/service/exist/mail/' + mail, but it throws an error:

Error: Request failed with status code 404.

Is there a workaround to catch this call?

Upvotes: 2

Views: 484

Answers (0)

Related Questions