Skip to content

Commit

Permalink
✨ text translations
Browse files Browse the repository at this point in the history
  • Loading branch information
kalashshah committed Feb 3, 2023
1 parent 0c0b626 commit 9639f29
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .env.example
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=
9 changes: 9 additions & 0 deletions graphql/translate.graphql
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!
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"@nestjs/apollo": "^10.1.7",
"@nestjs/axios": "^0.1.0",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.3.0",
"@nestjs/core": "^9.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ApolloDriverConfig, ApolloDriver } from '@nestjs/apollo';
import { ConfigModule } from '@nestjs/config';
import { AuthenticationModule } from './authentication/authentication.module';
import { PrismaService } from 'prisma/prisma.service';
import { TranslateModule } from './translate/translate.module';

@Module({
imports: [
Expand All @@ -19,6 +20,7 @@ import { PrismaService } from 'prisma/prisma.service';
}),
ConfigModule.forRoot(),
AuthenticationModule,
TranslateModule,
],
controllers: [AppController],
providers: [AppService, PrismaService],
Expand Down
8 changes: 7 additions & 1 deletion src/constants/env.ts
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;
16 changes: 12 additions & 4 deletions src/graphql.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@
/* tslint:disable */
/* eslint-disable */

export class TranslateInput {
text: string;
source: string;
target: string;
}

export class SignupInput {
name: string;
email: string;
profilePicture: string;
language: string;
}

export abstract class IQuery {
abstract translate(input: TranslateInput): string | Promise<string>;

abstract getUser(): User | Promise<User>;
}

export class User {
id: string;
name: string;
Expand All @@ -25,10 +37,6 @@ export class User {
updatedAt: DateTime;
}

export abstract class IQuery {
abstract getUser(): User | Promise<User>;
}

export abstract class IMutation {
abstract signup(input: SignupInput): string | Promise<string>;
}
Expand Down
8 changes: 8 additions & 0 deletions src/translate/translate.module.ts
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 {}
13 changes: 13 additions & 0 deletions src/translate/translate.resolver.ts
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);
}
}
31 changes: 31 additions & 0 deletions src/translate/translate.service.ts
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.',
);
}
}
}
26 changes: 26 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 9639f29

Please sign in to comment.