Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

mini_api

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mini_api - npm Package Compare versions

Comparing version
0.1.3
to
0.1.4
+9
-2
lib/mini_api/default_responder.rb

@@ -18,7 +18,14 @@ # frozen_string_literal: true

body =
if success
{ data: data }
else
{ errors: data }
end
@controller.render(
json: {
success: success,
data: data,
message: @options[:message] || nil
message: @options[:message] || nil,
**body
},

@@ -25,0 +32,0 @@ status: @options[:status] || :ok

+26
-7

@@ -25,3 +25,3 @@ # frozen_string_literal: true

if resource_has_errors?
{ errors: @resource.errors.messages }.merge(body)
errors.merge(body)
else

@@ -46,2 +46,10 @@ { data: serialiable_body(@resource).as_json }.merge(body)

def errors
error_serializer = get_error_serializer(@resource)
return { errors: @resource.errors.messages } unless error_serializer
{ errors: error_serializer.new(@resource).as_json }
end
def status_code

@@ -62,8 +70,19 @@ return @options[:status] if @options[:status].present?

I18n.t(
kind,
scope: [:mini_api, :messages, :actions, @controller.action_name],
resource_name: @resource.class.model_name.human,
default: ''
)
model_path = "mini_api.messages.#{@resource.model_name.i18n_key}"
if I18n.exists? model_path
I18n.t(
kind,
scope: "#{model_path}.#{@controller.action_name}",
resource_name: @resource.class.model_name.human,
default: ''
)
else
I18n.t(
kind,
scope: [:mini_api, :messages, :actions, @controller.action_name],
resource_name: @resource.class.model_name.human,
default: ''
)
end
end

@@ -70,0 +89,0 @@

@@ -15,3 +15,9 @@ # frozen_string_literal: true

end
def get_error_serializer(resource)
serializer_class = @controller.get_serializer(resource)
"#{serializer_class.serializer}::Error".safe_constantize
end
end
end
# frozen_string_literal: true
module MiniApi
VERSION = '0.1.3'
VERSION = '0.1.4'
end

@@ -12,2 +12,4 @@ ![](https://github.com/leoncruz/api-responder/actions/workflows/tests.yml/badge.svg)

- [Success and failure actions](#success-and-failure-actions)
- [Errors](#errors)
- [Message](#message)
- [Transform keys](#transform-keys)

@@ -146,2 +148,46 @@ - [Overriding response](#overriding-response)

### Errors
To show errors of a model, by default will use the `errors.messages` method, but `MiniApi` adds an ability to `active_model_serializers` to create a error serializer
as a nested class in your serializer. Example:
```ruby
class UserSerializer < ActiveModel::Serializer
attributes :id, :first_name, :last_name
class Error < ActiveModel::Serializer
attributes :user
def user
{
first_name: object.errors[:first_name],
last_name: object.errors[:last_name],
}
end
end
end
```
The response will be like:
```json
{
"success": false,
"errors": {
"user": {
"first_name": "can't be blank",
"last_name": "can't be blank"
}
},
"message": "User could not be created."
}
```
You can create serializers for non `ActiveRecord` and add a nested `Error` class too
### Message
The `I18n` path for the key `message` is `mini_api.messages.actions`, the controller action name and the `notice` for success actions
or `alert` for failure actions. Example `mini_api.messages.actions.create.notice`
By default support the actions: `create`, `update` and `delete`, but you can add more actions to translate
You can add translation based on models, changing the `action` key for your model name. Example:
`mini_api.messages.model_name.create.alert`. With this, is more easy personalize messages
### Transform keys

@@ -148,0 +194,0 @@