Service<Id, Data> constructor

Service<Id, Data>(
  1. {FutureOr<Data> readData(
    1. RequestContext,
    2. ResponseContext
    )}
)

An optional readData function can be passed to handle non-map/non-json bodies.

Implementation

Service({FutureOr<Data> Function(RequestContext, ResponseContext) readData}) {
  _readData = readData ??
      (req, res) {
        if (req.bodyAsObject is! Data) {
          throw AngelHttpException.badRequest(
              message:
                  'Invalid request body. Expected $Data; found ${req.bodyAsObject} instead.');
        } else {
          return req.bodyAsObject as Data;
        }
      };
}