reliv / rcm-api-lib
Rcm Api Library
0.12.1
2019-04-16 16:14 UTC
Requires
Requires (Dev)
Suggests
- bower-asset/angular: Required for JS
- bower-asset/es6-promise-polyfill: ^1.2 for older browser support
- rcm/i18n: Required by default, can over-ride in config: Translate::class
- rwoverdijk/assetmanager: Required if using asset manager config
- zendframework/zend-http: Required if using ZF2 controllers
- zendframework/zend-mvc: Required if using ZF2 controllers
- zendframework/zend-stdlib: Required if using ZF2 controllers
- dev-master
- 0.12.1
- 0.12.0
- 0.11.2
- 0.11.1
- 0.11.0
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.2
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.9
- 0.5.8
- 0.5.7
- 0.5.6
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.1
- 0.4.0
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.1.12
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
This package is auto-updated.
Last update: 2024-12-17 04:46:29 UTC
README
API basic response library making use of Middleware (like Zend Expressive) OR ZF2's MVC framework
Makes it easy to quickly create a common API data format.
Include a JavaScript (Angular.JS) library for the client.
Several types of common error message types are supported:
- Array
- Exception
- HttpStatusCode
- InputFilter (Zend)
- String
- Custom types can also be injected
EXAMPLE: Middleware (like 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 ); } }
Returns something like:
{ "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'] ) ); }
EXAMPLE: Zend Framework
// 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'], );
Author:
James Jervis jjervis@relivinc.com Copyright (c) 2015, Reliv' International, Inc. https://github.com/reliv/rcm-api-lib