diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d479cfa --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +## 0.0.2 (2024-06-05) + +- Added support for callbacks (`before`, `after`, etc.) + +## 0.0.1 (2024-06-04) + +- First release diff --git a/lib/tapioca/dsl/compilers/grape_endpoints.rb b/lib/tapioca/dsl/compilers/grape_endpoints.rb index e5adbf3..ec9aa96 100644 --- a/lib/tapioca/dsl/compilers/grape_endpoints.rb +++ b/lib/tapioca/dsl/compilers/grape_endpoints.rb @@ -68,6 +68,7 @@ class GrapeEndpoints < Tapioca::Dsl::Compiler def decorate create_classes_and_includes create_routing_methods + create_callbacks_methods end class << self @@ -84,6 +85,11 @@ def gather_constants T::Array[Symbol], ) + CALLBACKS_METHODS = T.let( + [:before, :before_validation, :after_validation, :after, :finally].freeze, + T::Array[Symbol], + ) + private sig { returns(RBI::Scope) } @@ -158,6 +164,20 @@ def create_routing_methods return_type: "void", ) end + + sig { void } + def create_callbacks_methods + CALLBACKS_METHODS.each do |callback| + routing_methods_module.create_method( + callback.to_s, + parameters: [ + create_rest_param("args", type: "T.untyped"), + create_block_param("blk", type: "T.nilable(T.proc.bind(#{EndpointClassName}).void)"), + ], + return_type: "void", + ) + end + end end end end diff --git a/spec/tapioca/dsl/compilers/grape_endpoints_spec.rb b/spec/tapioca/dsl/compilers/grape_endpoints_spec.rb index b52a1fb..50cd513 100644 --- a/spec/tapioca/dsl/compilers/grape_endpoints_spec.rb +++ b/spec/tapioca/dsl/compilers/grape_endpoints_spec.rb @@ -66,9 +66,24 @@ class TwitterAPI extend GeneratedRoutingMethods module GeneratedRoutingMethods + sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void } + def after(*args, &blk); end + + sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void } + def after_validation(*args, &blk); end + + sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void } + def before(*args, &blk); end + + sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void } + def before_validation(*args, &blk); end + sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void } def delete(*args, &blk); end + sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void } + def finally(*args, &blk); end + sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void } def get(*args, &blk); end