Default languages with env & langcodes typed (#94)
This commit is contained in:
@@ -2,7 +2,7 @@ import { ApolloServer, gql, IResolvers, ApolloError, UserInputError } from "apol
|
||||
import { NextApiHandler } from "next";
|
||||
import NextCors from "nextjs-cors";
|
||||
import { googleScrape, textToSpeechScrape } from "@utils/translate";
|
||||
import { retrieveFromType, getName } from "@utils/language";
|
||||
import { retrieveFromType, getName, isValid } from "@utils/language";
|
||||
|
||||
export const typeDefs = gql`
|
||||
enum LangType {
|
||||
@@ -33,6 +33,10 @@ export const resolvers: IResolvers = {
|
||||
Query: {
|
||||
translation(_, args) {
|
||||
const { source, target, query } = args;
|
||||
|
||||
if (!isValid(source) || !isValid(target))
|
||||
throw new UserInputError("Invalid language code");
|
||||
|
||||
return {
|
||||
source: {
|
||||
lang: {
|
||||
@@ -49,6 +53,10 @@ export const resolvers: IResolvers = {
|
||||
},
|
||||
audio(_, args) {
|
||||
const { lang, query } = args;
|
||||
|
||||
if (!isValid(lang))
|
||||
throw new UserInputError("Invalid language code");
|
||||
|
||||
return {
|
||||
lang: {
|
||||
code: lang
|
||||
@@ -87,13 +95,7 @@ export const resolvers: IResolvers = {
|
||||
Language: {
|
||||
name(parent) {
|
||||
const { code, name } = parent;
|
||||
if (name)
|
||||
return name;
|
||||
|
||||
const newName = getName(code);
|
||||
if (!newName)
|
||||
throw new UserInputError("Invalid language code");
|
||||
return newName;
|
||||
return name || getName(code);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { NextApiHandler } from "next";
|
||||
import NextCors from "nextjs-cors";
|
||||
import { googleScrape, textToSpeechScrape } from "@utils/translate";
|
||||
import { isValid } from "@utils/language";
|
||||
|
||||
type Data = {
|
||||
translation: string,
|
||||
@@ -34,6 +35,9 @@ const handler: NextApiHandler<Data> = async (req, res) => {
|
||||
|
||||
const [source, target, query] = slug;
|
||||
|
||||
if (!isValid(target))
|
||||
return res.status(400).json({ error: "Invalid target language" });
|
||||
|
||||
if (source === "audio") {
|
||||
const audio = await textToSpeechScrape(target, query);
|
||||
return audio
|
||||
@@ -41,6 +45,9 @@ const handler: NextApiHandler<Data> = async (req, res) => {
|
||||
: res.status(500).json({ error: "An error occurred while retrieving the audio" });
|
||||
}
|
||||
|
||||
if (!isValid(source))
|
||||
return res.status(400).json({ error: "Invalid source language" });
|
||||
|
||||
const textScrape = await googleScrape(source, target, query);
|
||||
|
||||
if ("errorMsg" in textScrape)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { NextApiHandler } from "next";
|
||||
import NextCors from "nextjs-cors";
|
||||
import { retrieveFromType } from "@utils/language";
|
||||
import { retrieveFromType, LangCode } from "@utils/language";
|
||||
|
||||
type Data = {
|
||||
languages: {
|
||||
code: string,
|
||||
code: LangCode,
|
||||
name: string
|
||||
}[]
|
||||
} | {
|
||||
|
||||
Reference in New Issue
Block a user