Releases: Kinto/kinto-http.js
v2.5.1
v2.5.0
v2.4.1
v2.4.0
v2.3.0
- Add
listHistory()
on buckets (#125)
Listing bucket history
client.bucket("blog").listHistory()
.then(({data}) => ...);
Sample result:
{
"data": [
{
"action": "update",
"collection_id": "articles",
"date": "2016-07-20T11:18:36.530281",
"id": "cb98ecd7-a66f-4f9d-82c5-73d06930f4f2",
"last_modified": 1469006316530,
"record_id": "b3b76c56-b6df-4195-8189-d79da4a128e1",
"resource_name": "record",
"target": {
"data": {
"id": "b3b76c56-b6df-4195-8189-d79da4a128e1",
"last_modified": 1469006316529,
"title": "Modified title"
},
"permissions": {
"write": [
"basicauth:43181ac0ae7581a23288c25a98786ef9db86433c62a04fd6071d11653ee69089"
]
}
},
"timestamp": 1469006098757,
"uri": "/buckets/blog/collections/articles/records/b3b76c56-b6df-4195-8189-d79da4a128e1",
"user_id": "basicauth:43181ac0ae7581a23288c25a98786ef9db86433c62a04fd6071d11653ee69089",
}
]
}
Options
headers
: Custom headers object to send along the HTTP request
This method accepts the generic parameters for sorting, filtering and paginating results.
v2.2.1
v2.2.0
New features
Listing all resource permissions
If the permissions_endpoint
capability is installed on the server, you can retrieve the list of all permissions set for the authenticated user using the listPermissions()
method:
client.listPermissions([options])
.then(result => ...);
Sample result:
{
"data": [
{
"bucket_id": "mybucket",
"id": "mybucket",
"permissions": [
"write",
"read",
"group:create",
"collection:create"
],
"resource_name": "bucket",
"uri": "/buckets/mybucket"
},
...
]
}
Generic options for list operations
Every list operations like listBuckets(), listCollections, listGroups() or listRecords() accept parameters to sort, filter and paginate the results:
sort
: The order field (default:-last_modified
);pages
: The number of result pages to retrieve (default:1
);limit
: The number of records to retrieve per page: unset by default, uses default server configuration;filters
: An object defining the filters to apply; read more about what's supported;since
: The ETag header value received from the last response from the server.
Sorting
By default, results are listed by last_modified
descending order. You can set the sort
option to order by another field:
client.bucket("blog").collection("posts")
.listRecords({sort: "title"})
.then(({data, next}) => {
Polling for changes
To retrieve the results modified since a given timestamp, use the since
option:
client.bucket("blog").collection("posts")
.listRecords({since: "1456183930780"})
.then(({data, next}) => {
Paginating results
By default, all results of the first page are retrieved, and the default configuration of the server defines no limit. To specify a max number of results to retrieve, you can use the limit
option:
client.bucket("blog").collection("posts")
.listRecords({limit: 20})
.then(({data, next}) => {
To retrieve the next page of results, just call next()
from the result object obtained. If no next page is available, next()
throws an error you can catch to exit the flow:
let getNextPage;
client.bucket("blog").collection("posts")
.listRecords({limit: 20})
.then(({data, next}) => {
console.log("Page 1", data);
getNextPage = next;
});
Later down the flow:
getNextPage()
.then(({data, next}) => {
console.log("Page 2", data);
})
.catch(_ => {
console.log("No more pages.");
});
Last, if you just want to retrieve and aggregate a given number of result pages, instead of dealing with calling next()
recursively you can simply specify the pages
option:
client.bucket("blog").collection("posts")
.listRecords({limit: 20, pages: 3})
.then(({data, next}) => ...); // A maximum of 60 results will be retrieved here
Notes
If you plan on fetching all the available pages, you can set the
pages
option toInfinity
. Be aware that for large datasets this strategy can possibly issue an important amount of HTTP requests.
v2.1.1
v2.1.0
- Added support for the Kinto attachments API. (#115)
Attachments
If the attachment capability is available from the Kinto server, you can attach files to records. Files must be passed as data urls, which can be generated using the FileReader API in the browser.
Adding an attachment to a record
client.bucket("blog").collection("posts")
.addAttachment(dataURL, {title: "First post"});
Options
headers
: Custom headers object to send along the HTTP request;safe
: Ensures operations won't override existing resources on the server if their associatedlast_modified
value or option are provided; otherwise ensures resources won't be overriden if they already exist on the server;last_modified
: Whensafe
is true, the last timestamp we know the resource has been updated on the server;permissions
: Permissions to be set on the record;filename
: Allows to specify the attachment filename, in case the data URI does not contain any, or if the file has to be renamed on upload;
Updating an attachment
client.bucket("blog").collection("posts")
.addAttachment(dataURL, {id: "22c1319e-7b09-46db-bec4-c240bdf4e3e9"});
Deleting an attachment
client.bucket("blog").collection("posts")
.removeAttachment("22c1319e-7b09-46db-bec4-c240bdf4e3e9");