Skip to content

Commit

Permalink
Fix issue with not found locales directory
Browse files Browse the repository at this point in the history
  • Loading branch information
horstenwillem committed Apr 27, 2018
1 parent af32bed commit bbed0c2
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/lib/errorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { errorConfig as errors } from './errorConfig';
import { errorDefaults } from './constants';
import i18n from './i18nConfig';

export function parseErrors(error: any, language?: string) {
export function parseErrors(error: any, language: string = 'en') {
const metaData: any = {};
let parsedError = new ApiError(errorDefaults.DEFAULT_HTTP_CODE, errorDefaults.DEFAULT_ERROR); // Default error

Expand All @@ -30,8 +30,8 @@ export function parseErrors(error: any, language?: string) {
i18n.setLocale(language);

let translatedMessage = i18n.__(error.i18n);
// if the translatedMessage equals the error code or is undefined
// no translation is found
// if the translatedMessage equals the error code
// OR is undefined because not found
// fallback to default error message from ErrorConfig
if (translatedMessage === error.i18n || translatedMessage === undefined) {
translatedMessage = error.message;
Expand Down
10 changes: 9 additions & 1 deletion src/lib/i18nConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import * as i18n from 'i18n';
import { exists, mkdir } from 'fs';

const dirName = __dirname + '/locales';
exists(dirName, (res) => {
if (!res) {
mkdir(dirName, () => Promise.resolve());
}
});

i18n.configure({
directory: __dirname + '/locales',
directory: dirName,
defaultLocale: 'en',
updateFiles: false,
});
Expand Down
10 changes: 8 additions & 2 deletions src/lib/importTranslations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import * as icappsTranslation from 'icapps-translations';
import { existsSync, mkdirSync } from 'fs';

const url = '';
const defaultOptions = {
destination: './lib/locales',
destination: __dirname + '/locales',
};

export function importTranslations(token: string, options?: TranslationOptions) {
export async function importTranslations(token: string, options?: TranslationOptions) {
const allOptions = Object.assign({}, defaultOptions, options);
// if locales location does not exists, make directory
if (!await existsSync(allOptions.destination)) {
await mkdirSync(allOptions.destination);
}

return icappsTranslation.import(url, token, allOptions);
}

Expand Down
Empty file removed src/lib/locales/.gitkeep
Empty file.
24 changes: 22 additions & 2 deletions tests/errorParser.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import * as httpStatus from 'http-status';
import { ValidationError } from 'express-validation';
import { parseErrors, ApiError, errors } from '../src';
import * as httpStatus from 'http-status';
import * as icappsTranslation from 'icapps-translations';
import { ApiError, errors, parseErrors, importTranslations } from '../src';
import { errorDefaults } from '../src/lib/constants';
import * as i18n from 'i18n';

describe('errorParser', () => {
const defaultError = new ApiError(errorDefaults.DEFAULT_HTTP_CODE, errorDefaults.DEFAULT_ERROR);
let i18nMock;
jest.spyOn(icappsTranslation, 'import').mockImplementation(() => { });

beforeAll(async () => {
await importTranslations('randomToken');
});

beforeEach(() => {
i18nMock = jest.spyOn(i18n, '__');
Expand Down Expand Up @@ -89,6 +95,20 @@ describe('errorParser', () => {
});
}
});
it('Should succesfully parse default ApiError without an i18n key', () => {
try {
throw new ApiError(httpStatus.BAD_REQUEST, errors.INVALID_INPUT);
} catch (err) {
const parsedError = parseErrors(err);
expect(parsedError).toMatchObject({
id: expect.any(String),
status: httpStatus.BAD_REQUEST,
code: errors.INVALID_INPUT.code,
title: errors.INVALID_INPUT.message,
detail: errors.INVALID_INPUT.message,
});
}
});
it('Should succesfully parse default ApiError with default message when language is not available', () => {
try {
throw new ApiError(httpStatus.BAD_REQUEST, errors.INVALID_INPUT);
Expand Down
21 changes: 18 additions & 3 deletions tests/translations.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import * as fs from 'fs';
import { importTranslations } from '../src';
import * as icappsTranslation from 'icapps-translations';
import { existsSync, rmdirSync } from 'fs';

describe('importTranslations', () => {
const icappsTranslationMock = jest.spyOn(icappsTranslation, 'import').mockImplementation(() => { });
afterAll(() => {
let icappsTranslationMock;

beforeEach(() => {
icappsTranslationMock = jest.spyOn(icappsTranslation, 'import').mockImplementation(() => { });
});
afterEach(() => {
jest.clearAllMocks();
});

it('should import the translations', async () => {
await importTranslations('randomToken');
expect(icappsTranslationMock).toHaveBeenCalledTimes(1);
});

it('should create the locales directory if not exists', async () => {
const newDestination = './tests/locales';

await importTranslations('randomToken', { destination: newDestination });
expect(icappsTranslationMock).toHaveBeenCalledTimes(1);
expect(await existsSync(newDestination)).toEqual(true);

// cleanup
await rmdirSync(newDestination);
});
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
version "0.2.30"
resolved "https://registry.npmjs.org/@types/http-status/-/http-status-0.2.30.tgz#b43a1e1673b6ed9b5a28e8647862b51b6473634d"

"@types/i18n@~0.8.3":
"@types/i18n@0.8.3":
version "0.8.3"
resolved "https://registry.npmjs.org/@types/i18n/-/i18n-0.8.3.tgz#f602164f2fae486ea87590f6be5d6dd5db1664e6"

Expand Down

0 comments on commit bbed0c2

Please sign in to comment.