-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c0b626
commit 9639f29
Showing
10 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
PORT= | ||
|
||
MONGODB_URL= | ||
|
||
AZURE_OCP_APIM_SUBSCRIPTION_KEY= | ||
|
||
AZURE_OCP_APIM_REGION= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
input TranslateInput { | ||
text: String! | ||
source: String! | ||
target: String! | ||
} | ||
|
||
type Query { | ||
translate(input: TranslateInput!): String! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
import * as dotenv from 'dotenv'; | ||
dotenv.config(); | ||
|
||
export const { PORT, MONGODB_URL, JWT_SECRET_KEY } = process.env; | ||
export const { | ||
PORT, | ||
MONGODB_URL, | ||
JWT_SECRET_KEY, | ||
AZURE_OCP_APIM_SUBSCRIPTION_KEY, | ||
AZURE_OCP_APIM_REGION, | ||
} = process.env; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TranslateService } from './translate.service'; | ||
import { TranslateResolver } from './translate.resolver'; | ||
|
||
@Module({ | ||
providers: [TranslateResolver, TranslateService], | ||
}) | ||
export class TranslateModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Args, Query, Resolver } from '@nestjs/graphql'; | ||
import { TranslateInput } from 'src/graphql.types'; | ||
import { TranslateService } from './translate.service'; | ||
|
||
@Resolver() | ||
export class TranslateResolver { | ||
constructor(private readonly translateService: TranslateService) {} | ||
|
||
@Query('translate') | ||
async translate(@Args('input') input: TranslateInput) { | ||
return await this.translateService.translate(input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { BadGatewayException, Injectable } from '@nestjs/common'; | ||
import { | ||
AZURE_OCP_APIM_REGION, | ||
AZURE_OCP_APIM_SUBSCRIPTION_KEY, | ||
} from 'src/constants/env'; | ||
import { TranslateInput } from 'src/graphql.types'; | ||
import axios from 'axios'; | ||
|
||
@Injectable() | ||
export class TranslateService { | ||
async translate(input: TranslateInput): Promise<string> { | ||
try { | ||
const res = await axios.post( | ||
`https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=${input.source}&to=${input.target}`, | ||
JSON.stringify([{ text: input.text }]), | ||
{ | ||
headers: { | ||
'Ocp-Apim-Subscription-Key': AZURE_OCP_APIM_SUBSCRIPTION_KEY, | ||
'Ocp-Apim-Subscription-Region': AZURE_OCP_APIM_REGION, | ||
'Content-type': 'application/json', | ||
}, | ||
}, | ||
); | ||
return res.data[0].translations[0].text; | ||
} catch { | ||
throw new BadGatewayException( | ||
'An error occurred while translating. Please check language or try again later.', | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -882,6 +882,13 @@ | |
lodash.omit "4.5.0" | ||
tslib "2.4.1" | ||
|
||
"@nestjs/axios@^0.1.0": | ||
version "0.1.2" | ||
resolved "https://registry.yarnpkg.com/@nestjs/axios/-/axios-0.1.2.tgz#6bcfe23836d521739b4a80b0889a4a51561399bd" | ||
integrity sha512-KFW37K5ujSce3lukNp9Uym5T6/oKCDw8EQRYhAKdNiBveMtotkokJwKIHVKCc21qfXMyes4291eR2No1Sf/fow== | ||
dependencies: | ||
axios "1.2.1" | ||
|
||
"@nestjs/cli@^9.0.0": | ||
version "9.2.0" | ||
resolved "https://registry.yarnpkg.com/@nestjs/cli/-/cli-9.2.0.tgz#d174f54d7aaa6695b8e413093e3d18367bc8bec7" | ||
|
@@ -1866,6 +1873,15 @@ asynckit@^0.4.0: | |
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" | ||
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== | ||
|
||
[email protected]: | ||
version "1.2.1" | ||
resolved "https://registry.yarnpkg.com/axios/-/axios-1.2.1.tgz#44cf04a3c9f0c2252ebd85975361c026cb9f864a" | ||
integrity sha512-I88cFiGu9ryt/tfVEi4kX2SITsvDddTajXTOFmt2uK1ZVA8LytjtdeyefdQWEf5PU8w+4SSJDoYnggflB5tW4A== | ||
dependencies: | ||
follow-redirects "^1.15.0" | ||
form-data "^4.0.0" | ||
proxy-from-env "^1.1.0" | ||
|
||
babel-jest@^29.4.1: | ||
version "29.4.1" | ||
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-29.4.1.tgz#01fa167e27470b35c2d4a1b841d9586b1764da19" | ||
|
@@ -2898,6 +2914,11 @@ flatted@^3.1.0: | |
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787" | ||
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ== | ||
|
||
follow-redirects@^1.15.0: | ||
version "1.15.2" | ||
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13" | ||
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA== | ||
|
||
[email protected]: | ||
version "7.3.0" | ||
resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-7.3.0.tgz#a9c984a018493962360d7c7e77a67b44a2d5f3aa" | ||
|
@@ -4436,6 +4457,11 @@ proxy-addr@~2.0.7: | |
forwarded "0.2.0" | ||
ipaddr.js "1.9.1" | ||
|
||
proxy-from-env@^1.1.0: | ||
version "1.1.0" | ||
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" | ||
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== | ||
|
||
pump@^3.0.0: | ||
version "3.0.0" | ||
resolved "https://registry.yarnpkg.com/pump/-/pump-3.0.0.tgz#b4a2116815bde2f4e1ea602354e8c75565107a64" | ||
|