The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

WWW::Suffit::Plugin::CommonHelpers - Common helpers plugin for Suffit API servers

SYNOPSIS

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

DESCRIPTION

This plugin is a collection of common helpers for Suffit API 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/