Created
November 15, 2018 21:59
-
-
Save kasper573/9260d65fc2e1f2aac7ac4ad9d7e6ebfc to your computer and use it in GitHub Desktop.
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
import {FormTransportErrors, FormTransportResponse} from 'lib/FormTransport'; | |
import {Model} from './models/Model'; | |
export async function failsafeUpdate<T extends Model<any, any>> ( | |
instance: T, | |
changes: Partial<T>, | |
send: (updatedInstance: T) => Promise<FormTransportResponse<T, keyof T>>, | |
handleError?: (error: FormTransportErrors<keyof T>) => any | |
) { | |
const updated = new (instance.constructor as Class<T>)().update(instance).update(changes); | |
const response = await send(updated); | |
if (response.error) { | |
if (handleError) { | |
handleError(response.error); | |
} | |
} else { | |
instance.update(changes); | |
} | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment