src/services/LanguageService.js
import Client from '../Client';
import JsonApiService from './JsonApiService';
import Language from '../helpers/Language';
import { arrayToObject } from '../utilities/ArrayUtility';
import {
hasAttribute,
isA,
isRequired,
validate,
} from '../utilities/ValidationUtility';
/**
* Service handling Language calls with specific endpoints.
*
* Clinical6 is available in multiple different languages.
*/
class LanguageService extends JsonApiService {
/**
* Update type to be languages
*/
constructor() {
super();
/** @type {String} - The type of the default data */
this.type = 'languages';
this.options.obj = 'language';
this.options.title = 'LanguageService';
this.options.tokenRequired = ['delete', 'insert', 'update'];
}
/**
* @override
* Call a PATCH request on the main obj.type expecting JSON API information.
*
* @param {Language} language - Language object in which to update
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise} - Message
*
* @example
* import { languageService } from 'clinical6';
*
* // You will be able to delete a language using `delete`.
* languageService.delete({ id: 23 });
* // This will also clear the local storage of this type and id
*/
delete(language, cacheMode = undefined) {
validate('LanguageService.delete',
isRequired({ language }, 'id'),
isA({ id: language.id }, 'number'));
return super.delete(language, { cacheMode });
}
/**
* @override
* Call a GET request expecting JSON API information.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @param {Object} [params] - Parameters used to get information from server (such as id)
* @param {Number} [params.id] - Id of the language
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Language[] | Language>}
* - Promise object that returns one language or a key:language hashtable
*
* @example
* import { languageService } from 'clinical6';
*
* // You will be able to access these languages using the `get` method.
* languageService.get().then(languages => console.log(languages));
*
* // Additionally, you can retrieve the subcategories for a specific language with the `get`
* // method, using the ID of the desired language as a parameter.
* languageService.get({ id: 23 }).then(language => console.log(language));
*/
get(params = {}, cacheMode = undefined) {
return super.get(params, { cacheMode });
}
/**
* Returns the list of available languages for an application.
*
* Users can get the list of available languages for an application using the `getLanguages`
* method.
*
* @throws {Clinical6Error} - If missing token
* @param {Boolean} [getTranslations] - If true, then populate the Language translation key/val
* @return {Promise<{ [id: String] : Language } | Language>}
* - Promise object that returns success or failure
*
* @example
* import { languageService } from 'clinical6';
* languageService.getLanguages();
*
* @example <caption>Success Response</caption>
* [
* {
* "name": "English",
* "icon": {
* "original": "/assets/default/default-4832093.png",
* "thumb": "/assets/default/thumb_default-5238439.png"
* },
* "iso": "en"
* },
* {
* "name": "non",
* "icon": {
* "original": "/assets/default/default-4832093.png",
* "thumb": "/assets/default/thumb_default-5238439.png"
* },
* "iso": "dummy_752"
* },
* {
* "name": "non",
* "icon": {
* "original": "/assets/default/default-4832093.png",
* "thumb": "/assets/default/thumb_default-5238439.png"
* },
* "iso": "dummy_753"
* }
* ]
*/
getLanguages(getTranslations = false) {
return Client.instance.request('languages',
Client.instance.fetch('/api/languages/').then(response => arrayToObject(response.map(
obj => new Language(obj, getTranslations)
), { key: 'iso' })),
{ mode: this.cacheMode, key: 'iso', asArray: false });
}
/**
* Returns the translations for a given language.
*
* Users can get the translations for a given language using the `getTranslations` method. This
* method requires the ISO value of a language.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @param {!String} iso - ISO of the language for which the translations are needed.
* @return {Promise} - Promise object that returns success or failure
*
* @example
* import { languageService } from 'clinical6';
*
* // Returns the translations for a given language.
* languageService.getTranslations(id);
*/
getTranslations(iso) {
validate('Clinical6 getTranslations',
isRequired({ iso }),
isA({ iso }, 'string'));
return new Promise((resolve, reject) => {
Client.instance.fetch(`/api/languages/${iso}`).then(
response => resolve(response.translations),
err => reject(err)
);
});
}
/**
* @override
* Call a POST request on the main obj.type expecting JSON API information.
*
* @param {Object} [language] - Parameters used to get information from server (such as id)
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Language>} - Inserted language
*
* @example
* import { Language, languageService } from 'clinical6';
* const language = new Language({...});
*
* // you can insert a language using the `insert` method.
* languageService.insert(language).then(language => console.log(language));
*
* // you could also just call `save` on the language if it doesn't have an id, which will also
* // invoke the `insert` method
* language.save();
*/
insert(language, cacheMode = undefined) {
validate('LanguageService.insert',
hasAttribute({ language }, 'iso'),
hasAttribute({ language }, 'name'));
return super.insert(language, { cacheMode });
}
/**
* @override
* Call a PATCH request on the main obj.type expecting JSON API information.
*
* @param {Language} language - Language object in which to update
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Language>} - Updated language
*
* @example
* import { Language, languageService } from 'clinical6';
* const language = new Language({...});
*
* // you can update a language using the `update` method.
* languageService.update(language).then(language => console.log(language));
*
* // you could also just call `save` on the language if it has an id, which will also
* // invoke the `update` method
* language.save();
*/
update(language, cacheMode = undefined) {
validate('LanguageService.update',
hasAttribute({ language }, 'iso'),
hasAttribute({ language }, 'name'));
return super.update(language, { cacheMode });
}
}
export default LanguageService;