NAME

WWW::Suffit::Server::CommonHelpers - Common helpers plugin for Suffit servers

VERSION

Version 1.00

SYNOPSIS

# in your startup
$self->plugin('WWW::Suffit::Server::CommonHelpers');

DESCRIPTION

This plugin is a collection of common helpers for Suffit servers

HELPERS

This plugin implements the following helpers

base_url

my $url = $c->base_url;

Returns the base URL from request

client_ip

my $ip = $c->client_ip;
my $ip = $c->client_ip([ ..trusted_proxies ...]);

Returns the client IP address

remote_ip

See "client_ip"

reply.error

return $c->reply->error(); # 500, E0500, "Internal server error"
return $c->reply->error("Error message"); # 500, E0500, "Error message"
return $c->reply->error(501 => "Error message");
return $c->reply->error(501 => "Error code" => "Error message");

The method returns error in client request format

NOTE!: This method with HTML format requires the 'error' template

reply.json_error

return $c->reply->json_error(); # 500, E0500, "Internal server error"
return $c->reply->json_error("Error message"); # 500, E0500, "Error message"
return $c->reply->json_error(501 => "Error message");
return $c->reply->json_error(501 => "Error code" => "Error message");

{
  "code": "Error code",
  "message": "Error message",
  "status": false
}

The method returns API error as JSON response

reply.json_ok

return $c->reply->json_ok(); # 200, ""
return $c->reply->json_ok("Ok."); # 200, "Ok."
return $c->reply->json_ok(201 => "Ok."); # 201, "Ok."

{
  "code": "E0000",
  "message": "Ok.",
  "status": true
}

return $c->reply->json_ok({foo => "bar"}); # 200, {...}

{
  "code": "E0000",
  "foo": "bar",
  "status": true
}

return $c->reply->json_ok(201 => {foo => "bar"}); # 201, {...}

# 201
{
  "code": "E0000",
  "foo": "bar",
  "status": true
}

The method returns API success status as JSON response

reply.noapi

return $c->reply->noapi(
    status  => 501, # HTTP status code (default: 200)
    code    => "E0501", # The Suffit error code
    message => "Error message",
    data    => {...}, # Payload data
    html    => { template => "error" }, # HTML options
);

The method returns data in client request format

METHODS

Internal methods

register

Do not use directly. It is called by Mojolicious.

SEE ALSO

Mojolicious, Mojolicious::Plugin

AUTHOR

Serż Minus (Sergey Lepenkov) https://www.serzik.com <abalama@cpan.org>

COPYRIGHT

Copyright (C) 1998-2023 D&D Corporation. All Rights Reserved

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See LICENSE file and https://dev.perl.org/licenses/