@cubicweb/data-provider
Version:
CubicWeb data provider implementation
37 lines (34 loc) • 843 B
text/typescript
import {
ApiErrorResponse,
Client,
Transaction,
TransactionResult,
} from "@cubicweb/client";
import HttpError from "../HttpError.js";
/**
* Converts a CubicWeb error response to a react-admin compatible error object
*
* @param error The CubicWeb error response payload
* @returns
*/
export const apiErrorToReactAdmin = (error: ApiErrorResponse) => {
return new HttpError(`${error.title}: ${error.message}`, error.status, error);
};
export const executeApiTransaction = (
client: Client,
transaction: Transaction
) => {
return new Promise(
(
resolve: (value: TransactionResult) => void,
reject: (reason: HttpError) => void
) => {
client
.executeTransaction(transaction)
.then(resolve)
.catch((e) => {
reject(apiErrorToReactAdmin(e));
});
}
);
};