ellipsesynergie / api-response Goto Github PK
View Code? Open in Web Editor NEWSimple package to handle response properly in your API.
License: MIT License
Simple package to handle response properly in your API.
License: MIT License
i expect the middleware to be registered as described in the readme
Tell us what should happen
middleware not recognise
Operating system:
**Web server:apache
**Database:php
**PHP version:7.2
**Browser:chrome
**Operating system:windows
Fatal error: Uncaught Error: Call to undefined method Illuminate\Foundation\Application::middleware() in C:\Users\SOLARSOFT\PhpstormProjects\lifetv\bootstrap\app.php:44
Stack trace:
#0 C:\Users\#####\PhpstormProjects\###\artisan(20): require_once()
#1 {main}
thrown in C:\Users\#########\PhpstormProjects\####\bootstrap\app.php on line 44
Can you provide an example of what that withArray method needs to return on the response class? It looks like the Laravel Response class you reference in the docs, uses the Response Contract from Laravel5, but that has quite a bit of non-vanilla php within it. Here is what I have, but it's not returning anything currently
use EllipseSynergie\ApiResponse\AbstractResponse as AbstractResponse;
class ApiResponse extends AbstractResponse{
/**
* @param array $array
* @param array $headers
* @return ResponseFactory
*/
public function withArray(array $array, array $headers = array())
{
return json_encode($array);
}
}
Is there a reason to stick to version 0.12 or can I update to latest 0.13?
$json = $this->actingAs($user, 'customer') ->json('GET','v2/customer', ['include'=>'cart']) ->assertJson([ 'data' => [ 'cart' => [ 'data' => [] ] ], ]);
Response should assert the 'cart' include, which works if I hit it with postman for example.
None of the includes, except the defaults are included
How to return request validation error messages?
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
Hi,
What is customKey for? Is customKey working?
Cuz I try to set customKey to something else but it doesnt work
Tried:
return $this->response->withItem($newUser, new UserTransformer, 'user');
and:
return $this->response->withItem($newUser, new UserTransformer, ['key' => 'user']);
````
The error codes are defined as constants, which is great since they can be overridden by creating a class that extends Response; however, due to the fact that the constants are referenced via self::CODE_NOT_FOUND instead of static::CODE_NOT_FOUND, the constants being referenced are always the ones defined in the original class despite the class that was initiated.
In the Laravel/Lumen example for the Master branch it says:
use EllipseSynergie\ApiResponse\Contracts\Response;
Should be:
use EllipseSynergie\ApiResponse\Laravel\Response;
Also
//Custom meta
$meta = [
'readers' = $book->readers
];
Should be:
//Custom meta
$meta = [
'readers' => $book->readers
];
/**
* @param array $array
* @param array $headers
* @return ResponseFactory
*/
public function withArray(array $array, array $headers = [],$options = 0)
{
return response()->json($array, $this->statusCode, $headers,$options);
}
I need use $options
as JSON_UNESCAPED_UNICODE
can you add it ?
Does this have the ability to use Serializers from Fractal?
I've tried adding in the $manager->setSerializer(new DataArraySerializer());
as well as the JsonApiSerializer()
, but I just receive errors as the third variable for the withItem method expects the meta array.
I'm building an api that will be consumed by Ember and I need to serialize the data so that the user reponse is 'user' instead of 'data'
Now that the api()
macro is deprecated, I want to know how to return a response from Laravel's exception handler (Exceptions/Handler).
In the past, I used something like this
// inside the render() method in Handler.php
if ($e instanceof ApiException) {
return response()->api()->withError($e->getMessage(), $e->getCode());
}
How can I avoid using api()
?
ps: sorry for not using the guidelines.
i try and use that "new BookTransformer" , but it has error .
so ,can you tell me how to use withCollection method?
$this->response->withCollection(
$users,
new UserTransformer
);
$array = json_decode($adminUserList);
It should return an array
It shows blank. I tested with json_last_error and showing error 4 which means bad syntax.
Ubuntu
Apache
Mysql
PHP 7.0
Chrome
E.g. if I get an QueryException or a ModelNotFoundException will this library also give a json response? Or do I still need to make my own handler and in there return the errorNotFound etc. Thanks.
Hello ,
We will need to add status code in to the response.
Like
public function withArray(array $array, array $headers = array()) {
// $code = $this->statusCode;
if ($this->statusCode == '200') {
$array = array('code' => $this->statusCode , 'success' => TRUE) + $array;
} else {
$array = array('code' => $this->statusCode, 'success' => FALSE) + $array;
}
return response()->json($array, $this->statusCode, $headers);
}
For now, we had modified library code on "ellipsesynergie/api-response/src/Laravel/Response.php".
But I feel. Its not good way.
Could you please suggest me how can i add this to all response without change library code ?
Or Is there any way to add the status code from controller for final response.
Actually, we will need final response like "http://screencast.com/t/nmWF7PYU".
Please help us on this.
Thanks,
Hi,
Would like to know how to add custom top level properties? Did not see at the docs how to do it.
the get param:include won't be analyzed
seems that when the code runs into the LumenServiceProvider
, Http/Request
class still not init yet.
it'll init in the code below where in the $app->run
method.(actual code is the traits Concerns/RoutesRequest.php
method parseIncomingRequest
)
so i fixed this by reparsing in my controller.
"barryvdh/laravel-cors": "^0.9.2
"ellipsesynergie/api-response": "^0.14.0"
bootstrap/app.php
file:$app->middleware([
'cors' => \Barryvdh\Cors\HandleCors::class,
\EllipseSynergie\ApiResponse\Laravel\Middleware\ParseInclude::class,
]);
$app->register(Barryvdh\Cors\ServiceProvider::class);
$app->register(EllipseSynergie\ApiResponse\Laravel\LumenServiceProvider::class);
use EllipseSynergie\ApiResponse\Contracts\Response;
$posts = Post::all();
return $this->response->withCollection($posts, new PostTransformer);
Must return the post model data.
The api-response package class \EllipseSynergie\ApiResponse\Laravel\Middleware\ParseInclude::class
is causing the error.
Specifically the handle methods returned response on line 41. return $next($next);
Change the return to return $next($request);
then the query responds correctly.
`Type error: Argument 1 passed to Barryvdh\Cors\CorsService::isCorsRequest() must be an instance of Symfony\Component\HttpFoundation\Request, instance of Closure given, called in /Users/waynegibson/Projects/spacecomx-ecosystem/firefly/vendor/barryvdh/laravel-cors/src/HandleCors.php on line 35`
Hello! I try to test my controller but I couldn't mock response's withPaginator method. I saw that method coming from Laravel's specific Response class. So I don't know how can I test that response line. Should I change Interface or is there any clean way to do?
Here's the code I would like to tests:
return $this->response->withPaginator(Test::paginate(20), new BarTransformer());
It's my test method:
public function testIfRequestHasNotSearchQuery()
{
$response = Mockery::mock('EllipseSynergie\ApiResponse\Contracts\Response');
$this->app->instance(\EllipseSynergie\ApiResponse\Contracts\Response::class, $response);
$paginator = new LengthAwarePaginator(['foo'], 1, 1, 1);
$barTransformer = new \Foo\Transformers\BarTransformer($paginator);
$response->shouldReceive('withPaginator')->with($paginator, $barTransformer)->andReturn('foo');
$response = $this->json('GET', '/api/v1/foo', [], [
'token' => 'foobarbaz'
]);
$this->assertResponseOk($response);
}
Runned test log:
[2016-12-02 08:29:47] testing.INFO: {}
[2016-12-02 08:30:13] testing.ERROR: Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_3_EllipseSynergie_ApiResponse_Contracts_Response::withPaginator(object(Illuminate\Pagination\LengthAwarePaginator), object(Foo\Transformers\BarTransformer)). Either the method was unexpected or its arguments matched no expected argument list for this method
Thanks for helping!
In some cases we just want to pass data without any transformer. Is it possible to make transformer object optional? like this
Response::api()->withItem($data);
Hi,
I would like to point out that, with error responses server defined headers are not sent.
For example, in my case I use nginx with this configuration (only headers part)
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
If I use errorNotFound()
(or similar error-reporting methods) the headers are not present, both testing from local development server and from online virtual machine.
I'll improve the documentation soon.
In my application using this package (latest), I have an automated test that uses Laravel's (5.3 latest) built-in helpers to test a URL like this:
$this->json('GET', "api/users/{$user->id}?include=posts");
It is supposed to give me back a list of users with their posts. When I test the URL in postman or chrome, it works just fine. However, when I run the test on the command line (by running "phpunit") and dump-and-die the output, it just includes the user's information without the "posts" include.
This is happening across the board for me in my automated tests for any endpoint that has a relationship. Based on what I've found by digging into Fractal and dump-and-dying all over the place, it looks like for some reason the "parseIncludes" function doesn't get set correctly when automated tests are run. But like I said, it's fine in a Postman test.
To be honest, I can't tell for sure if this is Laravel's problem, Fractal's problem, or this packages problem. I am continuing to dig into it.
Hi ellipsesynergie,
Would like to know how to return links
from
"pagination": {
"total": 25,
"count": 25,
"per_page": 80,
"current_page": 1,
"total_pages": 1,
"links": [ ]
}
to blank object
"pagination": {
"total": 25,
"count": 25,
"per_page": 80,
"current_page": 1,
"total_pages": 1,
"links": {}
}
When using Response::api() with any of the examples giving in the readme, the page throws the following error:
Call to undefined method EllipseSynergie\ApiResponse\Laravel\Response::api()
I have included this line in my controller use EllipseSynergie\ApiResponse\Laravel\Response;
along with all the proper League\Fractal.
When I use Fractal by itself everything works fine.
Hi,
Seems like the ->withPaginator()
now receives a paginator of type Illuminate\Pagination\LengthAwarePaginator
in L5 instead of the required Illuminate\Pagination\Paginator
Is there a workaround?
Error: "Class App\Http\Controllers\BookTransformer not found"
I am using the example code, after having added the "Book" class.
I spent a few hours trying to understand why I was hitting endpoint api/foo
but was getting response from endpoint api/bar
.
Turns out that when using Laravel's Form Request Validation (http://laravel.com/docs/5.1/validation#form-request-validation), if the request is not AJAX or does not contains the 'Accept:application/json' header, it gets redirected to the last endpoint.
I'm using Postman to make the requests so the adding the json header will tell Laravel to return a "JsonResponse" with the validation errors instead of redirecting which is good.
This is not a real package issue but if this package is handling api responses in general it will be really nice to handle form validation responses too (like Dingo API package does). What do you think?
I wonder if there is a nicer way to set includes for the response. I dont mean to parse the includes from include parameter, but set them "manually" for that response.
right now I am doing this (Laravel):
$this->response->getManager()->parseIncludes('team');
return $this->response->withItem($request->user(), new UserTransformer);
Maybe something like
return $this->response->parseIncludes('team')->withItem($request->user(), new UserTransformer);
would be shorter and nicer. I could prepare a pull request if that's something you would like to integrate.
Since package has dependency on:
"league/fractal": "~0.11"
This will load fractal v0.12, which is not supported in Laravel 4.
Shouldn't it be:
"league/fractal": "0.11.*"
Hi,
First of all thanks for this package, it's been very useful so far.
I'm using it with Lumen and I am facing a minor issue at the moment, as it is currently not possible to pass headers to the various shortcut methods for returning errors (errorForbidden
, errorNotFound
, etc).
In my case, authentification is handled using JWT, and the token is refreshed on every request and the new token is returned along with the response using a middleware.
When raising exceptions tho, the returned responses don't get through the middleware and the Authorization
header (the one carrying the token) is not set.
This is where I would need to set it manually but the shortcuts methods won't allow me to do so.
This my own specific use case of course, but I believe some others could exist and I don't see anything wrong in adding an argument to these methods' signatures, as such:
public function errorInternalError($message = 'Internal Error', $headers = [])
public function withError($message, $errorCode, $headers = [])
Especially as withArray
already accepts an array of headers as its second argument.
What do you think?
Thanks!
when i try to use this with api-guard it returns this:
Class 'App\Http\Controllers\BookTransformer' not found
Hi,
Please can you tag a stable version of the LengthAwarePaginator fix? I can't run my production app in stable because there is no tag for 0.9.*
How to use HTTP status constants(HTTP_UNAUTHORIZED etc) defined in Symfony\Component\HttpFoundation\Response without importing it in the controller as I am already using EllipseSynergie\ApiResponse\Contracts\Response?
I'd like to do something like this Response::HTTP_UNAUTHORIZED
with this package instead of 401? Could someone please guide if it is possible? Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.