-
Notifications
You must be signed in to change notification settings - Fork 15
feature: create api-languages #340
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b24d049
chore: create api-languages
tataihono a62b237
feat: fetch data from arclight api
tataihono 7b897a9
chore: match translation object
tataihono 158838f
feat: implement language module
GeronimoJohn 46f5a6c
refactor: language and translation module
GeronimoJohn 2157021
fix: language getAll logic
GeronimoJohn 8a662ae
chore: add arclight api key env
tataihono 463e921
Merge branch '22-02-TN-chore-add-languages-api' of https://github.com…
tataihono 8347572
chore: refactor seed
tataihono e73dd6b
chore: add translation field decorator
tataihono 326e2b5
Merge branch 'main' into 22-02-TN-chore-add-languages-api
tataihono e172ad7
chore: change lint syntax
tataihono f531d6f
fix: nest decorator lint
tataihono 921e94c
fix: languageId should be id
tataihono 15740b2
fix: pr feedback
tataihono File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ testem.log | |
| Thumbs.db | ||
|
|
||
| #local .env files | ||
| .env | ||
| .env*local | ||
|
|
||
| # kube secrets | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,2 @@ | ||
| DATABASE_URL="arangodb://arangodb:8529" | ||
| ARCLIGHT_API_KEY= |
This file contains hidden or 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,21 @@ | ||
| { | ||
| "extends": ["../../.eslintrc.json"], | ||
| "ignorePatterns": ["!**/*"], | ||
| "overrides": [ | ||
| { | ||
| "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
| "rules": {} | ||
| }, | ||
| { | ||
| "files": ["*.ts", "*.tsx"], | ||
| "parserOptions": { | ||
| "project": ["apps/api-languages/tsconfig.*?.json"] | ||
| }, | ||
| "rules": {} | ||
| }, | ||
| { | ||
| "files": ["*.js", "*.jsx"], | ||
| "rules": {} | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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 @@ | ||
| FROM node:14-alpine | ||
| WORKDIR /app | ||
| COPY ./dist/apps/api-languages . | ||
| EXPOSE 4003 | ||
| # dependencies that nestjs needs | ||
| RUN npm install --production --silent | ||
| RUN npm install tslib apollo-server-express @nestjs/mapped-types | ||
| CMD node ./main.js |
This file contains hidden or 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 { Database } from 'arangojs' | ||
|
|
||
| export function ArangoDB(): Database { | ||
| let db: Database | ||
| if (process.env.DATABASE_DB != null) | ||
| db = new Database({ | ||
| url: process.env.DATABASE_URL, | ||
| databaseName: process.env.DATABASE_DB | ||
| }) | ||
| else db = new Database({ url: process.env.DATABASE_URL }) | ||
| db.useBasicAuth(process.env.DATABASE_USER, process.env.DATABASE_PASS) | ||
| return db | ||
| } |
This file contains hidden or 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,132 @@ | ||
| import { aql } from 'arangojs' | ||
| import { isEmpty } from 'lodash' | ||
| import fetch from 'node-fetch' | ||
| import { ArangoDB } from './db' | ||
|
|
||
| interface Language { | ||
| _key: string | ||
| name: Array<{ value: string; languageId: string; primary: boolean }> | ||
| bcp47?: string | ||
| iso3?: string | ||
| } | ||
|
|
||
| interface MediaLanguage { | ||
| languageId: number | ||
| bcp47?: string | ||
| iso3?: string | ||
| nameNative: string | ||
| metadataLanguageTag: string | ||
| name: string | ||
| } | ||
|
|
||
| const db = ArangoDB() | ||
|
|
||
| async function getLanguage(languageId: string): Promise<Language | undefined> { | ||
| const rst = await db.query(aql` | ||
| FOR item IN ${db.collection('languages')} | ||
| FILTER item._key == ${languageId} | ||
| LIMIT 1 | ||
| RETURN item`) | ||
| return await rst.next() | ||
| } | ||
|
|
||
| async function getLanguageByBcp47( | ||
| bcp47: string | ||
| ): Promise<Language | undefined> { | ||
| const rst = await db.query(aql` | ||
| FOR item IN ${db.collection('languages')} | ||
| FILTER item.bcp47 == ${bcp47} | ||
| LIMIT 1 | ||
| RETURN item`) | ||
| return await rst.next() | ||
| } | ||
|
|
||
| async function getMediaLanguages(): Promise<MediaLanguage[]> { | ||
| const response: { | ||
| _embedded: { mediaLanguages: MediaLanguage[] } | ||
| } = await ( | ||
| await fetch( | ||
| `https://api.arclight.org/v2/media-languages?limit=5000&apiKey=${ | ||
| process.env.ARCLIGHT_API_KEY ?? '' | ||
| }` | ||
| ) | ||
| ).json() | ||
| return response._embedded.mediaLanguages | ||
| } | ||
|
|
||
| async function digestMediaLanguage( | ||
| mediaLanguage: MediaLanguage | ||
| ): Promise<void> { | ||
| const { languageId, bcp47, iso3, nameNative } = mediaLanguage | ||
| const body: Omit<Language, '_key'> = { | ||
| bcp47: isEmpty(bcp47) ? undefined : bcp47, | ||
| iso3: isEmpty(iso3) ? undefined : iso3, | ||
| name: [ | ||
| { | ||
| value: nameNative, | ||
| languageId: languageId.toString(), | ||
| primary: true | ||
| } | ||
| ] | ||
| } | ||
| const language = await getLanguage(languageId.toString()) | ||
|
|
||
| if (language != null) { | ||
| await db.collection('languages').update(languageId.toString(), body) | ||
| } else { | ||
| await db | ||
| .collection('languages') | ||
| .save({ _key: languageId.toString(), ...body }) | ||
| } | ||
| } | ||
|
|
||
| async function digestMediaLanguageMetadata( | ||
| mediaLanguage: MediaLanguage | ||
| ): Promise<void> { | ||
| const { languageId, metadataLanguageTag, name } = mediaLanguage | ||
| const language = await getLanguage(languageId.toString()) | ||
| if (language == null) return | ||
|
|
||
| const metadataLanguage = await getLanguageByBcp47(metadataLanguageTag) | ||
| if (metadataLanguage == null) return | ||
|
|
||
| if ( | ||
| language.name.find( | ||
| ({ languageId }) => languageId === metadataLanguage._key | ||
| ) != null | ||
| ) | ||
| return | ||
|
|
||
| const body: Omit<Language, '_key'> = { | ||
| name: [ | ||
| ...language.name, | ||
| { | ||
| value: name, | ||
| languageId: metadataLanguage._key, | ||
| primary: false | ||
| } | ||
| ] | ||
| } | ||
| await db.collection('languages').update(languageId.toString(), body) | ||
| } | ||
|
|
||
| async function main(): Promise<void> { | ||
| try { | ||
| await db.createCollection('languages', { keyOptions: { type: 'uuid' } }) | ||
| } catch {} | ||
| const mediaLanguages = await getMediaLanguages() | ||
|
|
||
| for (const mediaLanguage of mediaLanguages) { | ||
| console.log('language:', mediaLanguage.languageId) | ||
| await digestMediaLanguage(mediaLanguage) | ||
| } | ||
|
|
||
| for (const mediaLanguage of mediaLanguages) { | ||
| await digestMediaLanguageMetadata(mediaLanguage) | ||
|
tataihono marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| main().catch((e) => { | ||
| console.error(e) | ||
| process.exit(1) | ||
| }) | ||
This file contains hidden or 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 @@ | ||
| export * from './src/app/__generated__/graphql' |
This file contains hidden or 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,15 @@ | ||
| module.exports = { | ||
| displayName: 'api-languages', | ||
| preset: '../../jest.preset.js', | ||
| globals: { | ||
| 'ts-jest': { | ||
| tsconfig: '<rootDir>/tsconfig.spec.json' | ||
| } | ||
| }, | ||
| testEnvironment: 'node', | ||
| transform: { | ||
| '^.+\\.[tj]s$': 'ts-jest' | ||
| }, | ||
| moduleFileExtensions: ['ts', 'js', 'html'], | ||
| coverageDirectory: '../../coverage/apps/api-languages' | ||
| } |
This file contains hidden or 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,90 @@ | ||
| { | ||
| "root": "apps/api-languages", | ||
| "sourceRoot": "apps/api-languages/src", | ||
| "projectType": "application", | ||
| "targets": { | ||
| "build": { | ||
| "executor": "@nrwl/node:build", | ||
| "outputs": ["{options.outputPath}"], | ||
| "options": { | ||
| "outputPath": "dist/apps/api-languages", | ||
| "main": "apps/api-languages/src/main.ts", | ||
| "tsConfig": "apps/api-languages/tsconfig.app.json", | ||
| "assets": [ | ||
| "apps/api-languages/src/assets", | ||
| { | ||
| "glob": "**/*.graphql", | ||
| "input": "apps/api-languages/src/app/", | ||
| "output": "./assets" | ||
| } | ||
| ], | ||
| "generatePackageJson": true | ||
| }, | ||
| "configurations": { | ||
| "production": { | ||
| "optimization": true, | ||
| "extractLicenses": true, | ||
| "inspect": false, | ||
| "fileReplacements": [ | ||
| { | ||
| "replace": "apps/api-languages/src/environments/environment.ts", | ||
| "with": "apps/api-languages/src/environments/environment.prod.ts" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| "seed": { | ||
| "executor": "@nrwl/workspace:run-commands", | ||
| "options": { | ||
| "command": "npx ts-node apps/api-languages/db/seed.ts" | ||
| } | ||
| }, | ||
| "serve-nowatch": { | ||
| "executor": "@nrwl/node:execute", | ||
| "options": { | ||
| "buildTarget": "api-languages:build" | ||
| } | ||
| }, | ||
| "serve": { | ||
| "executor": "@nrwl/workspace:run-commands", | ||
| "options": { | ||
| "commands": [ | ||
| { | ||
| "command": "nx serve-nowatch api-languages" | ||
| }, | ||
| { | ||
| "command": "npx ts-node apps/api-languages/src/generate-typings.ts" | ||
| } | ||
| ], | ||
| "parallel": true | ||
| } | ||
| }, | ||
| "lint": { | ||
| "executor": "@nrwl/linter:eslint", | ||
| "outputs": ["{options.outputFile}"], | ||
| "options": { | ||
| "lintFilePatterns": ["apps/api-languages/**/*.ts"] | ||
| } | ||
| }, | ||
| "test": { | ||
| "executor": "@nrwl/jest:jest", | ||
| "outputs": ["coverage/apps/api-languages"], | ||
| "options": { | ||
| "jestConfig": "apps/api-languages/jest.config.js", | ||
| "passWithNoTests": true | ||
| } | ||
| }, | ||
| "generate-graphql": { | ||
| "executor": "@nrwl/workspace:run-commands", | ||
| "options": { | ||
| "commands": [ | ||
| { | ||
| "command": "rover subgraph introspect http://localhost:4003/graphql > apps/api-languages/schema.graphql" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| "tags": [] | ||
| } |
This file contains hidden or 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,17 @@ | ||
| type Language @key(fields: "id") { | ||
| id: ID! | ||
| bcp47: String | ||
| iso3: String | ||
| name(languageId: ID, primary: Boolean): [Translation]! | ||
| } | ||
|
|
||
| type Translation { | ||
| value: String! | ||
| language: Language! | ||
| primary: Boolean! | ||
| } | ||
|
|
||
| extend type Query { | ||
| languages(page: Int, limit: Int): [Language!]! | ||
| language(id: ID!): Language | ||
| } |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.