diff --git a/lib/tapioca/dsl/compilers/grape_endpoints.rb b/lib/tapioca/dsl/compilers/grape_endpoints.rb index 3db37ae..7fb2dbf 100644 --- a/lib/tapioca/dsl/compilers/grape_endpoints.rb +++ b/lib/tapioca/dsl/compilers/grape_endpoints.rb @@ -67,8 +67,10 @@ class GrapeEndpoints < Tapioca::Dsl::Compiler sig { override.void } def decorate create_classes_and_includes - create_routing_methods + create_callbacks_methods + create_request_response_methods + create_routing_methods end class << self @@ -108,6 +110,14 @@ def callbacks_methods_module ) end + sig { returns(RBI::Scope) } + def request_response_methods_module + @request_response_methods_module ||= T.let( + api.create_module(RequestResponseMethodsModuleName), + T.nilable(RBI::Scope), + ) + end + sig { returns(RBI::Scope) } def routing_methods_module @routing_methods_module ||= T.let( @@ -119,6 +129,7 @@ def routing_methods_module sig { void } def create_classes_and_includes api.create_extend(CallbacksMethodsModuleName) + api.create_extend(RequestResponseMethodsModuleName) api.create_extend(RoutingMethodsModuleName) create_api_class create_endpoint_class @@ -159,6 +170,21 @@ def create_callbacks_methods end end + sig { void } + def create_request_response_methods + request_response_methods_module.create_method( + "rescue_from", + parameters: [ + create_rest_param("args", type: "T.untyped"), + create_block_param( + "block", + type: "T.nilable(T.proc.bind(#{EndpointClassName}).params(e: Exception).void)", + ), + ], + return_type: "void", + ) + end + sig { void } def create_routing_methods HTTP_VERB_METHODS.each do |verb| diff --git a/lib/tapioca/dsl/helpers/grape_constants_helper.rb b/lib/tapioca/dsl/helpers/grape_constants_helper.rb index f56f00d..7beedd7 100644 --- a/lib/tapioca/dsl/helpers/grape_constants_helper.rb +++ b/lib/tapioca/dsl/helpers/grape_constants_helper.rb @@ -8,6 +8,7 @@ module GrapeConstantsHelper extend T::Sig CallbacksMethodsModuleName = "GeneratedCallbacksMethods" + RequestResponseMethodsModuleName = "GeneratedRequestResponseMethods" RoutingMethodsModuleName = "GeneratedRoutingMethods" APIInstanceClassName = "PrivateAPIInstance" diff --git a/rbi/grape.rbi b/rbi/grape.rbi index a31e384..65207c5 100644 --- a/rbi/grape.rbi +++ b/rbi/grape.rbi @@ -31,7 +31,7 @@ module Grape module RequestResponse module ClassMethods - sig { params(args: T.untyped, block: T.proc.bind(Grape::Endpoint).void).void } + sig { params(args: T.untyped, block: T.proc.bind(Grape::Endpoint).params(e: Exception).void).void } def rescue_from(*args, &block); end end end diff --git a/spec/tapioca/dsl/compilers/grape_endpoints_spec.rb b/spec/tapioca/dsl/compilers/grape_endpoints_spec.rb index 07ef6fe..cf0c4a4 100644 --- a/spec/tapioca/dsl/compilers/grape_endpoints_spec.rb +++ b/spec/tapioca/dsl/compilers/grape_endpoints_spec.rb @@ -64,6 +64,7 @@ def authenticate! class TwitterAPI extend GeneratedCallbacksMethods + extend GeneratedRequestResponseMethods extend GeneratedRoutingMethods module GeneratedCallbacksMethods @@ -83,6 +84,11 @@ def before_validation(&block); end def finally(&block); end end + module GeneratedRequestResponseMethods + sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateEndpoint).params(e: Exception).void)).void } + def rescue_from(*args, &block); end + end + module GeneratedRoutingMethods sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateEndpoint).void)).void } def delete(*args, &block); end @@ -150,6 +156,7 @@ class TwitterAPI < Grape::API::Instance class TwitterAPI extend GeneratedCallbacksMethods + extend GeneratedRequestResponseMethods extend GeneratedRoutingMethods module GeneratedCallbacksMethods @@ -169,6 +176,11 @@ def before_validation(&block); end def finally(&block); end end + module GeneratedRequestResponseMethods + sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateEndpoint).params(e: Exception).void)).void } + def rescue_from(*args, &block); end + end + module GeneratedRoutingMethods sig { params(args: T.untyped, block: T.nilable(T.proc.bind(PrivateEndpoint).void)).void } def delete(*args, &block); end