Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Object.keys(refs).forEach((key) => {
data.relationships = data.relationships || { };
const refIds = getRefId(model, key);
let rel: IDefinition|Array|undefined;
if (refIds instanceof Array || isObservableArray(refIds)) {
rel = (refIds as Array).map((id, index) => {
const type = getModelType(model[key][index] ? model[key][index] : refs[key].model).toString();
if (!type) {
throw error(`The model type can't be retrieved for the reference ${key}`);
}
return { id, type };
});
} else {
const type: string = getModelType(model[key] ? model[key] : refs[key].model).toString();
if (!type) {
throw error(`The model type can't be retrieved for the reference ${key}`);
export function saveRelationship(
model: T,
ref: string,
options?: IRequestOptions,
): Promise {
const collection = getModelCollection(model) as IJsonapiCollection;
const link = getLink(model, ref, 'self');
const href: string = typeof link === 'object' ? link.href : link;
const ids = getRefId(model, ref);
const type = getModelType(getModelMetaKey(model, 'refs')[ref].model);
type ID = IDefinition|Array;
const data: ID = mapItems(ids, (id) => ({ id, type })) as ID;
return update(href, { data }, collection, options && options.headers)
.then(handleResponse(model, ref));
}