src/services/TemporaryIdService.js
import JsonApiService from './JsonApiService';
import {
hasAttribute,
hasToken,
isRequired,
validate,
} from '../utilities/ValidationUtility';
/**
* Service handling TemporaryId calls with specific endpoints.
*/
class TemporaryIdService extends JsonApiService {
/**
* Update type to be temporaryIds
*/
constructor() {
super('temporary_identifiers',
{
title: 'TemporaryIdService',
obj: 'temporaryId',
tokenRequired: ['get', 'insert']
});
}
/**
* @override
* Call a GET request expecting JSON API information.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @param {Object} [temporaryId] - Parameters used to get information from server (such as token)
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<TemporaryId>} - Promise object that returns one TemporaryId
*
* @example
* import { temporaryIdService } from 'clinical6';
*
* // You will be able to access the temporary identifier data and associated user using the `get` method.
* temporaryIdService.get().then(temporaryId => console.log(temporaryId));
*
* // You can retrieve the temporary identifier data and associated user using `get`
* // method, using the temporary identifier token as a parameter.
* temporaryIdService.get(temporaryId).then(temporaryId => console.log(temporaryId));
*/
async get(temporaryId, cacheMode = undefined) {
validate('TemporaryIdService.get',
hasToken(),
isRequired({ temporaryId }),
hasAttribute({ temporaryId }, 'token'));
const url = `/v3/temporary_identifiers/${temporaryId.token}`;
return super.get(temporaryId, { url, cacheMode });
}
/**
* @override
* Call a POST request on the main obj.type expecting JSON API information.
*
* @param {Object} [temporaryId] - Parameters used to get information from server (such as id)
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<TemporaryId>} - Inserted temporaryId
*
* @example
* import { TemporaryId, temporaryIdService } from 'clinical6';
* const temporaryId = new TemporaryId({...});
*
* // you can insert a temporaryId using the `insert` method.
* temporaryIdService.insert(temporaryId).then(temporaryId => console.log(temporaryId));
*
* // you could also just call `save` on the temporaryId if it doesn't have an id, which will also
* // invoke the `insert` method
* temporaryId.save();
*/
async insert(temporaryId, cacheMode = undefined) {
validate('TemporaryIdService.insert',
hasToken(),
isRequired({ temporaryId }),
hasAttribute({ temporaryId }, 'type'));
return super.insert(temporaryId, { cacheMode });
}
}
export default TemporaryIdService;