addResourceTemplate method

void addResourceTemplate(
  1. ResourceTemplate template,
  2. ReadResourceHandler handler
)

Adds the ResourceTemplate template with handler.

When reading resources, first regular resources added by addResource are prioritized. Then, we call the handler for each template, in the order they were added (using this method), and the first one to return a non-null response wins. This package does not automatically handle matching of templates and handlers must accept URIs in any form.

Throws a StateError if there is already a template registered with the same uri template.

Implementation

void addResourceTemplate(
  ResourceTemplate template,
  ReadResourceHandler handler,
) {
  if (_resourceTemplates.any(
    (t) => t.template.uriTemplate == template.uriTemplate,
  )) {
    throw StateError(
      'Failed to add resource template ${template.name}, there is '
      'already a resource template with the same uri pattern '
      '${template.uriTemplate}.',
    );
  }
  _resourceTemplates.add((template: template, handler: handler));
}