Hapi call one route from another and pass payload

16 views Asked by At

I am currently using hapi.js and have been interacting with an API that contains specific logic. Now, I aim to leverage the same logic by invoking that route from another API.

This is my route where I have written all my logic

const routeConfig = {
    method: 'POST',
    path: "/first-route",
    config: {
        auth: { strategy: "jwt", mode: "optional" },
        handler
    }
}

Here I want to call above api and want to pass payload

const handler = async (request, reply) => {
  try {
    const userId = Helpers.extractUserId(request);
    const { markerId, streamId, ...rest } = request.payload;

    const stream = await Stream.findOne({ _id: streamId });
    if (!stream) throw new Error("Stream not found");

    const { title, start_time, end_time } = rest;
    const payload = {
      title,
      start_time: start_time,
      end_time: end_time,
    };
    await Clip.create(payload);
    const clipCreationPayload = {
      streamId: stream.streamId,
      start_time: start_time,
      input_video_url: stream.url,
    };

    // here I want to call the above api with `clipCreationPayload` payload
    return reply({
      status: true,
      message: "Clip has been sent.",
    });
  } catch (error) {
    logger.error(error);
    return reply({
      status: false,
      message: error.message,
    });
  }
};

const routeConfig = {
  method: "POST",
  path: "/second-route",
  config: {
    auth: "jwt",
    handler,
  },
};
0

There are 0 answers