Giter Club home page Giter Club logo

api-response's People

Contributors

aannecchiarico avatar andrew-miller-rakuten avatar bryant1410 avatar earnaway avatar emileber avatar fer-ri avatar gholol avatar ikari7789 avatar maximebeaudoin avatar mindwingx avatar qwertyuu avatar saverio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

api-response's Issues

Possibility to pass headers to error responses

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!

withCollection method gives bad json output. I can't convert it to array.

Steps to reproduce

$this->response->withCollection(
$users,
new UserTransformer
);
$array = json_decode($adminUserList);

Expected behavior

It should return an array

Actual behavior

It shows blank. I tested with json_last_error and showing error 4 which means bad syntax.

Server configuration

Ubuntu

Apache

Mysql

PHP 7.0

Client configuration

Chrome

Automated test responses don't include relationships

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.

Example of Response Class which does not include Laravel Response Factory

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);

    }

}

Server headers blocked

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.

Response::api() returns fatalErrorException

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.

Call to undefined method Illuminate\Foundation\Application::middleware()

Steps to reproduce

  1. include the middleware in app/http/kernel.php

Expected behaviour

i expect the middleware to be registered as described in the readme

Tell us what should happen

Actual behaviour

middleware not recognise

Server configuration

Operating system:

**Web server:apache

**Database:php

**PHP version:7.2

Client configuration

**Browser:chrome

**Operating system:windows

Logs

Web server error log


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


Laravel 5 paginator class changed

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?

Wondering how to return null / object on links json output

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": {}
}

Make Transformer object optional

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);

Tag stable

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 defined in Symfony\Component\HttpFoundation\Response?

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!

Changing customKey

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']);
````

Handle Form Request Validation

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?

Feature testing with ->get or ->json doesn't pass include string to Manager

Steps to reproduce

  1. When Testing endpoint, request a response with include
    $json = $this->actingAs($user, 'customer') ->json('GET','v2/customer', ['include'=>'cart']) ->assertJson([ 'data' => [ 'cart' => [ 'data' => [] ] ], ]);

Expected behaviour

Response should assert the 'cart' include, which works if I hit it with postman for example.

Actual behaviour

None of the includes, except the defaults are included

Error code references as "self" prevent extensibility

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.

Testing withPaginator method

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!

ParseInclude middleware error

Steps to reproduce

  1. Installed Lumen 5.4
  2. composer required "barryvdh/laravel-cors": "^0.9.2
  3. composer require "ellipsesynergie/api-response": "^0.14.0"
  4. Added middleware and service providers to the 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);
  1. Added the following to the lumen controller:

use EllipseSynergie\ApiResponse\Contracts\Response;

$posts = Post::all();
return $this->response->withCollection($posts, new PostTransformer);

Expected behaviour

Must return the post model data.

Actual behaviour

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.

Browser log

`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`

Using Serializers from Fractal

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'

Retrun validation errors

How to return request validation error messages?

$this->validate($request, [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

Lumen5.4 not parse include

Steps to reproduce

  1. using api-response in lumen 5.4

Expected behaviour

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.

How to add HTTP Status code and status text in to response ?

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,

Documentation Error

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
];

Return response from Laravel's Handler (now that api() macro is deprecated)

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.

Class *Transformer not found

Error: "Class App\Http\Controllers\BookTransformer not found"
I am using the example code, after having added the "Book" class.

Does this handle exceptions?

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.

set includes for response

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.

return json need $options

/**
     * @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 ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.