-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
154 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
title: JSON:API | ||
image: null | ||
--- | ||
|
||
# JSON:API | ||
|
||
Pagy can be `JSON:API` compliant by just setting the `:jsonapi` variable to true. When enabled, the query params used in the pagy URLs are nested under the `page` param, as specified by the [Query Parameter Family](https://jsonapi.org/format/#query-parameters-families) e.g. `https://example.com/products?page[page]=2&page[items]=30`. | ||
|
||
## Synopsis | ||
|
||
||| pagy.rb (initializer) | ||
```ruby | ||
require 'pagy/extras/jsonapi' | ||
# optionally disable it by default (opt-in) | ||
Pagy::DEFAULT[:jsonapi] = false | ||
# optional but useful extras | ||
require 'pagy/extras/items' | ||
require 'pagy/extras/metadata' | ||
``` | ||
||| | ||
|
||
||| Controller | ||
```ruby | ||
# enable/disable on single object | ||
@pagy, @items = pagy(collection, jsonapi: true) | ||
# optionl/custom setup | ||
@pagy, @items = pagy(collection, jsonapi: true, # enable the jsonapi specifications | ||
items_extra: true, # enable the items extra | ||
page_param: :number, # use page[number] param name instead of page[page] | ||
items_params: :size) # use page[size] param name instead of page[items] | ||
links_hash = pagy_jsonapi_links(@pagy) | ||
#=> {first: 'https://example.com/products?page[number]=1&page[size]=50&...', | ||
# last: 'https://example.com/products?page[number]=32&page[size]=50&...', | ||
# prev: 'https://example.com/products?page[number]=31&page[size]=50&...', | ||
# next: 'https://example.com/products?page[number]=33&page[size]=50&...'} | ||
``` | ||
||| | ||
|
||
## Interaction with extras | ||
|
||
If you want to allow your JSON:API app to allow the client to request a specific number of items per page and capping the request to a max number you can use the [items extra](/docs/extras/items.md). | ||
|
||
The [metadata extra]((/docs/extras/metadata.md)) implements also the `pagy_jsonapi_links` method returning the link hash (https://jsonapi.org/format/#fetching-pagination) | ||
|
||
An app that implements a JSON:API, if wants to allow the client to request a specific number of items per page, also capping the max number requested by the client. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.