Class CommonMiddleware
java.lang.Object
com.google.genkit.core.middleware.CommonMiddleware
CommonMiddleware provides factory methods for creating commonly-used
middleware functions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <I,O> Middleware <I, O> beforeAfter(BiConsumer<I, ActionContext> before, BiConsumer<O, ActionContext> after) Creates a before/after middleware that runs callbacks before and after execution.static <I,O> Middleware <I, O> cache(MiddlewareCache<O> cache, Function<I, String> keyExtractor) Creates a caching middleware that caches results based on a key.static <I,O> Middleware <I, O> conditional(BiPredicate<I, ActionContext> predicate, Middleware<I, O> middleware) Creates a conditional middleware that only applies if the predicate is true.static <I,O> Middleware <I, O> errorHandler(Function<GenkitException, O> errorHandler) Creates an error handling middleware that catches and transforms exceptions.static <I,O> Middleware <I, O> Creates a logging middleware that logs requests and responses.static <I,O> Middleware <I, O> Creates a logging middleware with a custom logger.static <I,O> Middleware <I, O> rateLimit(int maxRequests, long windowMs) Creates a rate limiting middleware (simple token bucket implementation).static <I,O> Middleware <I, O> retry(int maxRetries, long initialDelayMs) Creates a retry middleware with exponential backoff.static <I,O> Middleware <I, O> retry(int maxRetries, long initialDelayMs, Function<GenkitException, Boolean> shouldRetry) Creates a retry middleware with exponential backoff and custom retry predicate.static <I,O> Middleware <I, O> timeout(long timeoutMs) Creates a timeout middleware that throws an exception if execution takes too long.static <I,O> Middleware <I, O> Creates a timing middleware that measures execution time.static <I,O> Middleware <I, O> transformRequest(Function<I, I> transformer) Creates a transformation middleware that transforms the request before processing.static <I,O> Middleware <I, O> transformResponse(Function<O, O> transformer) Creates a transformation middleware that transforms the response after processing.static <I,O> Middleware <I, O> Creates a validation middleware that validates the request before processing.
-
Method Details
-
logging
Creates a logging middleware that logs requests and responses.- Type Parameters:
I- input typeO- output type- Parameters:
name- the name to use in log messages- Returns:
- a logging middleware
-
logging
Creates a logging middleware with a custom logger.- Type Parameters:
I- input typeO- output type- Parameters:
name- the name to use in log messagescustomLogger- the logger to use- Returns:
- a logging middleware
-
timing
Creates a timing middleware that measures execution time.- Type Parameters:
I- input typeO- output type- Parameters:
callback- callback to receive timing information (duration in milliseconds)- Returns:
- a timing middleware
-
retry
Creates a retry middleware with exponential backoff.- Type Parameters:
I- input typeO- output type- Parameters:
maxRetries- maximum number of retry attemptsinitialDelayMs- initial delay between retries in milliseconds- Returns:
- a retry middleware
-
retry
public static <I,O> Middleware<I,O> retry(int maxRetries, long initialDelayMs, Function<GenkitException, Boolean> shouldRetry) Creates a retry middleware with exponential backoff and custom retry predicate.- Type Parameters:
I- input typeO- output type- Parameters:
maxRetries- maximum number of retry attemptsinitialDelayMs- initial delay between retries in millisecondsshouldRetry- predicate to determine if an exception should trigger a retry- Returns:
- a retry middleware
-
validate
Creates a validation middleware that validates the request before processing.- Type Parameters:
I- input typeO- output type- Parameters:
validator- the validation function (throws GenkitException on invalid input)- Returns:
- a validation middleware
-
transformRequest
Creates a transformation middleware that transforms the request before processing.- Type Parameters:
I- input typeO- output type- Parameters:
transformer- the transformation function- Returns:
- a transformation middleware
-
transformResponse
Creates a transformation middleware that transforms the response after processing.- Type Parameters:
I- input typeO- output type- Parameters:
transformer- the transformation function- Returns:
- a transformation middleware
-
cache
public static <I,O> Middleware<I,O> cache(MiddlewareCache<O> cache, Function<I, String> keyExtractor) Creates a caching middleware that caches results based on a key.- Type Parameters:
I- input typeO- output type- Parameters:
cache- the cache implementationkeyExtractor- function to extract cache key from request- Returns:
- a caching middleware
-
errorHandler
Creates an error handling middleware that catches and transforms exceptions.- Type Parameters:
I- input typeO- output type- Parameters:
errorHandler- the error handler function- Returns:
- an error handling middleware
-
conditional
public static <I,O> Middleware<I,O> conditional(BiPredicate<I, ActionContext> predicate, Middleware<I, O> middleware) Creates a conditional middleware that only applies if the predicate is true.- Type Parameters:
I- input typeO- output type- Parameters:
predicate- the condition to checkmiddleware- the middleware to apply if condition is true- Returns:
- a conditional middleware
-
beforeAfter
public static <I,O> Middleware<I,O> beforeAfter(BiConsumer<I, ActionContext> before, BiConsumer<O, ActionContext> after) Creates a before/after middleware that runs callbacks before and after execution.- Type Parameters:
I- input typeO- output type- Parameters:
before- callback to run before executionafter- callback to run after execution- Returns:
- a before/after middleware
-
rateLimit
Creates a rate limiting middleware (simple token bucket implementation).- Type Parameters:
I- input typeO- output type- Parameters:
maxRequests- maximum requests allowed in the time windowwindowMs- time window in milliseconds- Returns:
- a rate limiting middleware
-
timeout
Creates a timeout middleware that throws an exception if execution takes too long.- Type Parameters:
I- input typeO- output type- Parameters:
timeoutMs- timeout in milliseconds- Returns:
- a timeout middleware
-