rcm/api-lib

0.12.1 2019-04-16 16:14 UTC

README

使用 Middleware (例如 Zend Expressive) 或 ZF2 的 MVC 框架的基本响应库

使其能够轻松快速地创建通用的 API 数据格式。

包含一个客户端 JavaScript (Angular.JS) 库。

支持多种常见的错误消息类型
  • 数组
  • 异常
  • Http 状态码
  • 输入过滤器 (Zend)
  • 字符串
  • 也可以注入自定义类型

示例:Middleware (例如 Zend Expressive)

// From a Middleware that extends Reliv\RcmApiLib\Middleware\AbstractJsonController

    /** EXAMPLE: InputFilter (Zend)  **/
    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface $response,
        callable $next
    ) {
        $inputFilter = new RcmGoogleAnalyticsFilter();

        $inputFilter->setData($data);

        if (!$inputFilter->isValid()) {
            return $this->getApiResponse(
                [],
                400,
                $inputFilter
            );
        }
    }
返回类似的内容
{
  "data": [],
  "messages": [
    {
      "type": "inputFilter",
      "source": "validation",
      "code": "error",
      "value": "Some information was missing or invalid on the form. Please check the form and try again.",
      "primary": true,
      "params": [],
      "key": "inputFilter.validation.error"
    },
    {
      "type": "validatorMessage",
      "source": "my-value",
      "code": "isEmpty",
      "value": "Value is required and can't be empty",
      "primary": null,
      "params": [],
      "key": "validatorMessage.my-value.isEmpty"
    }
  ]
} 
    /** EXAMPLE: General **/
    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface $response,
        callable $next
    ) {
        return $this->getApiResponse(
            ['my' => 'response'],
            400,
            new ApiMessage(
                'my-type',
                'my-message-value {my-param}',
                'my-source',
                'my-code',
                true,
                ['my-param' => 'my-value']
            )
        );
    }

示例:Zend 框架

// From a ZF2 Controller that extends \Reliv\RcmApiLib\Controller\AbstractRestfulJsonController
// @see \Reliv\RcmApiLib\Controller\ExampleRestfulJsonController

    // Add exception message
    $this->addApiMessage(
        new \Exception('Some exception')
    );

    // Add generic message as array
    $this->addApiMessage(
        [
            'key' => 'ArrayError',
            'value' => 'Some {param} Message',
            'primary' => true,
            'type' => 'Array',
            'code' => 'mycode',
            'params' => ['param' => 'array message']
        ]
    );

    // Add generic message as object
    $this->addApiMessage(
        new ApiMessage('MYKEY', 'Some Message')
    );
    
    // Add HTTP sttus message
    $this->addApiMessage(
        new HttpStatusCodeApiMessage(403)
    );

    // Add inputFilter message
    $inputFilter = new \Zend\InputFilter\InputFilter(); // Use you own inputFilter here
    $this->addApiMessage(
        $inputFilter
    );
    
    // Return the response
    return $this->getApiResponse(
        null,
        $statusCode = 200,
        $inputFilter,
        true
    );
    
    // Return the response with your data and no messages
    return $this->getApiResponse(
        ['myThing' => 'someThing'],
    );
    

作者

James Jervis jjervis@relivinc.com 版权所有 (c) 2015, Reliv' International, Inc. https://github.com/reliv/rcm-api-lib