Skip to content

Q: Query parameter name customisation #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PrimozRome opened this issue Nov 14, 2018 · 7 comments
Closed

Q: Query parameter name customisation #32

PrimozRome opened this issue Nov 14, 2018 · 7 comments

Comments

@PrimozRome
Copy link

PrimozRome commented Nov 14, 2018

Maybe I have missed it somehow but reading trough your docs I haven't found that you can customise how the parameters are named when sent to the back-end. For example if not working with Spatie ORM package.

Here are my two questions.

  1. Can I customise how vue-query-api generates URL and names parameters? From your docs
// GET /posts?filter[status]=ACTIVE&include=user,category&append=likes&orderBy=-created_at,category_id

let posts = await Post
  .where('status', 'ACTIVE')
  .include('user', 'category')
  .append('likes')
  .orderBy('-created_at', 'category_id')  
  .get()

For example if I would like that filter[status] is named filter_by[status] for example.

  1. Can I customise how array parameters are encoded in the URL? For example here is filter by array which in your documentation says its send like this:
//GET /posts?filter[status]=ACTIVE,ARCHIVED

let posts = await Post
  .whereIn('status', ['ACTIVE', 'ARCHIVED'])
  .get()

I wonder if it's possible to send like this:

//GET /posts?filter[status][]=ACTIVE&filter[status][]=ARCHIVED

Thank you.

@robsontenorio
Copy link
Owner

1. Can I customise how vue-query-api generates URL and names parameters?

Yes, you can name params and values as you wish

// GET /posts?filter[my_fancy_status]=ACTIVE_ONE

let posts = await Post
  .where('my_fancy_status', 'ACTIVE_ONE')
  .get()

2. Can I customise how array parameters are encoded in the URL? For example here is filter by array which in your documentation says its send like this:

According to spatie/laravel-query-builder docs, you can perform a "AND" or "OR". Both are supported by vue-api-query.
So if you intend to use vue-api-query and spatie/laravel-query-builder you must stick to the convention.

You can also pass in an array of filters to the allowedFilters() method.

// GET /users?filter[name]=john&filter[email]=gmail
$users = QueryBuilder::for(User::class)
    ->allowedFilters(['name', 'email'])
    ->get();
// $users will contain all users with "john" in their name AND "gmail" in their email address
// GET /users?filter[name]=seb,freek
$users = QueryBuilder::for(User::class)
    ->allowedFilters('name')
    ->get();
// $users will contain all users that contain "seb" OR "freek" in their name

@PrimozRome
Copy link
Author

PrimozRome commented Nov 14, 2018

@robsontenorio you didn't get my answer right for the point 1.

I want to customise the filter in filter[status] not the status field name... So insted of generating this URL

// GET /posts?filter[status]=ACTIVE_ONE

i would like to have

// GET /posts?filter_field[status]=ACTIVE_ONE

@robsontenorio
Copy link
Owner

@PrimozRome I'm sorry, but right now it is not possible, because is sticked to the convention

@PrimozRome
Copy link
Author

PrimozRome commented Nov 14, 2018

@robsontenorio I think it would be an useful add-on, having some-kind of configuration possibility to rename default key names. Really not a big deal I guess.

@PrimozRome
Copy link
Author

PrimozRome commented Nov 16, 2018

UPDATE:

spatie/laravel-query-builder package also allows to change these parameters so I think this is needed to reflect the configuration on the server side.

Excellent package by-the-way!

return [

    /*
     * By default the package will use the `include`, `filter`, `sort` and `fields` query parameters.
     *
     * Here you can customize those names.
     */
    'parameters' => [
        'include' => 'include',

        'filter' => 'filter',

        'sort' => 'sort',

        'fields' => 'fields',
    ],

];

@robsontenorio
Copy link
Owner

For now, lets stick to default. If you can, please submit a PR.

@robsontenorio
Copy link
Owner

Now that is possible
#42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants