Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi Line #181

Open
AlexisHartford opened this issue Dec 14, 2023 · 1 comment
Open

Multi Line #181

AlexisHartford opened this issue Dec 14, 2023 · 1 comment

Comments

@AlexisHartford
Copy link

AlexisHartford commented Dec 14, 2023

We need a json Post Request to make Multi Line a bit better. an not use URL would make it a bit smoother.

@unilei
Copy link

unilei commented Dec 27, 2023

image


import { NextApiHandler } from "next";
import NextCors from "nextjs-cors";
import { getTranslationInfo, getTranslationText, getAudio, isValidCode, LanguageType, TranslationInfo } from "lingva-scraper";

type Data = {
    translation: string,
    info?: TranslationInfo
} | {
    audio: number[]
} | {
    error: string
};

const methods = ["POST"];

const handler: NextApiHandler<Data> = async (req, res) => {
    await NextCors(req, res, {
        methods,
        origin: "*",
        preflightContinue: true
    });
    const {
        body: { source,target,text },
        method
    } = req;

    if (!source || !target || !text)
        return res.status(404).json({ error: "Not Found" });

    if (!method || !methods.includes(method)) {
        res.setHeader("Allow", methods);
        return res.status(405).json({ error: "Method Not Allowed" });
    }

    if (!isValidCode(target, LanguageType.TARGET))
        return res.status(400).json({ error: "Invalid target language" });

    if (source === "audio") {
        const audio = await getAudio(target, text);
        return audio
            ? res.status(200).json({ audio })
            : res.status(500).json({ error: "An error occurred while retrieving the audio" });
    }

    if (!isValidCode(source, LanguageType.SOURCE))
        return res.status(400).json({ error: "Invalid source language" });

    const translation = await getTranslationText(source, target, text);

    if (!translation)
        return res.status(500).json({ error: "An error occurred while retrieving the translation" });

    const info = await getTranslationInfo(source, target, text);

    return info
        ? res.status(200).json({ translation, info })
        : res.status(200).json({ translation });
}

export default handler;


I add a post method , and works better .

the request link : https://your-url.com/api/v1/translate
requeset body : { source: source , target: target, text: text }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants