methodHandlers property

Map<String, Expression> get methodHandlers

Get all method expressions.

Implementation

Map<String, Expression> get methodHandlers {
  final Map<String, Expression> methods = {};

  for (final MapEntry<String, Segment> entry
      in segmentDocument.methodSegments.entries) {
    final Segment segment = entry.value;

    // If the handler is null, throw an error.
    if (segment.handler == null) {
      throw Exception('The handler for the method "${entry.key}" is null.');
    }
    final Builder builder = Builder(
      segmentDocument: segment,
      rootDirectory: rootDirectory,
    );

    Expression handler =
        refer('handler', relative(segment.handler!, from: rootDirectory));

    // Add middleware.
    if (builder.middleware != null) {
      handler = handler.property('use').call([builder.middleware!]);
    }

    // Add parameter middleware.
    for (final MapEntry<String, Reference> entry
        in builder.paramterMiddleware.entries) {
      handler = handler.property('param').call([
        literalString(entry.key, raw: true),
        entry.value,
      ]);
    }

    methods[entry.key] = handler;
  }

  return methods;
}