Home Reference Source Test
import KintoClientBase from 'kinto-http/lib/base.js'
public class | source

KintoClientBase

Direct Subclass:

KintoClient

High level HTTP client for the Kinto API.

Example:

const client = new KintoClient("https://kinto.dev.mozaws.net/v1");
client.bucket("default")
   .collection("my-blog")
   .createRecord({title: "First article"})
  .then(console.log.bind(console))
  .catch(console.error.bind(console));

Constructor Summary

Public Constructor
public

constructor(remote: String, options: Object)

Constructor.

Member Summary

Public Members
public get

backoff: Number

Backoff remaining time, in milliseconds.

public get

remote: String

The remote endpoint base URL.

public get

version: String

The current server protocol version, eg.

Private Members
private
private
private
private

_remote: *

private

_requests: *[]

private

_retry: *

private

_safe: *

private

Method Summary

Public Methods
public

bucket(name: String, options: Object): Bucket

Retrieve a bucket object to perform operations on it.

public

async createBucket(id: String | null, options: Object): Promise<Object, Error>

Creates a new bucket on the server.

public

async fetchHTTPApiVersion(options: Object): Promise<Object, Error>

Retrieve authenticated user information.

public

async fetchServerCapabilities(options: Object): Promise<Object, Error>

Retrieve server capabilities information.

public

async fetchServerInfo(options: Object): Promise<Object, Error>

Retrieves server information and persist them locally.

public

async fetchServerSettings(options: Object): Promise<Object, Error>

Retrieves Kinto server settings.

public

async fetchUser(options: Object): Promise<Object, Error>

Retrieve authenticated user information.

public

async listBuckets(options: Object): Promise<Object[], Error>

Retrieves the list of buckets.

public

async listPermissions(options: Object): Promise<Object[], Error>

Lists all permissions.

public

setHeaders(headers: Object)

Set client "headers" for every request, updating previous headers (if any).

Private Methods
private

async _batchRequests(requests: Array, options: Object): Promise<Object, Error>

Process batch requests, chunking them according to the batch_max_requests server setting when needed.

private

_getHeaders(options: Object): Object

Get the value of "headers" for a given request, merging the per-request headers with our own "default" headers.

private

async _getHello(options: Object): Promise<Object, Error>

Retrieves the server's "hello" endpoint. This endpoint reveals server capabilities and settings as well as telling the client "who they are" according to their given authorization headers.

private

_getRetry(options: *): *

As _getSafe, but for "retry".

private

_getSafe(options: Object): Boolean

Get the value of "safe" for a given request, using the per-request option if present or falling back to our default otherwise.

private

Registers HTTP events.

private

async execute(request: Object, options: Object): Promise<Object, Error>

Executes an atomic HTTP request.

private

async paginatedList(path: String, params: Object, options: Object): *

Fetch some pages from a paginated list, following the next-page header automatically until we have fetched the requested number of pages.

Public Constructors

public constructor(remote: String, options: Object) source

Constructor.

Params:

NameTypeAttributeDescription
remote String

The remote URL.

options Object
  • optional
  • default: {}

The options object.

options.safe Boolean
  • optional
  • default: true

Adds concurrency headers to every requests.

options.events EventEmitter
  • optional
  • default: EventEmitter

The events handler instance.

options.headers Object
  • optional
  • default: {}

The key-value headers to pass to each request.

options.retry Object
  • optional
  • default: 0

Number of retries when request fails (default: 0)

options.bucket String
  • optional
  • default: "default"

The default bucket to use.

options.requestMode String
  • optional
  • default: "cors"

The HTTP request mode (from ES6 fetch spec).

options.timeout Number
  • optional
  • default: null

The request timeout in ms, if any.

Public Members

public get backoff: Number source

Backoff remaining time, in milliseconds. Defaults to zero if no backoff is ongoing.

Test:

public get remote: String source

The remote endpoint base URL. Setting the value will also extract and validate the version.

public get version: String source

The current server protocol version, eg. v1.

Private Members

private _backoffReleaseTime: * source

private _headers: * source

private _isBatch: * source

private _remote: * source

private _requests: *[] source

private _retry: * source

private _safe: * source

private _version: * source

Public Methods

public bucket(name: String, options: Object): Bucket source

Retrieve a bucket object to perform operations on it.

Params:

NameTypeAttributeDescription
name String

The bucket name.

options Object
  • optional
  • default: {}

The request options.

options.safe Boolean
  • optional

The resulting safe option.

options.retry Number
  • optional

The resulting retry option.

options.headers Object
  • optional

The extended headers object option.

Return:

Bucket

Test:

public async createBucket(id: String | null, options: Object): Promise<Object, Error> source

Creates a new bucket on the server.

Params:

NameTypeAttributeDescription
id String | null

The bucket name (optional).

options Object
  • optional
  • default: {}

The options object.

options.data Boolean
  • optional

The bucket data option.

options.safe Boolean
  • optional

The safe option.

options.headers Object
  • optional

The headers object option.

options.retry Number
  • optional
  • default: 0

Number of retries to make when faced with transient errors.

Return:

Promise<Object, Error>

Test:

public async fetchHTTPApiVersion(options: Object): Promise<Object, Error> source

Retrieve authenticated user information.

Params:

NameTypeAttributeDescription
options Object
  • optional
  • default: {}

The request options.

options.retry Number
  • optional
  • default: 0

Number of retries to make when faced with transient errors.

Return:

Promise<Object, Error>

Decorators:

  • nobatch("This operation is not supported within a batch operation.")

Test:

public async fetchServerCapabilities(options: Object): Promise<Object, Error> source

Retrieve server capabilities information.

Params:

NameTypeAttributeDescription
options Object
  • optional
  • default: {}

The request options.

options.retry Number
  • optional
  • default: 0

Number of retries to make when faced with transient errors.

Return:

Promise<Object, Error>

Decorators:

  • nobatch("This operation is not supported within a batch operation.")

Test:

public async fetchServerInfo(options: Object): Promise<Object, Error> source

Retrieves server information and persist them locally. This operation is usually performed a single time during the instance lifecycle.

Params:

NameTypeAttributeDescription
options Object
  • optional
  • default: {}

The request options.

options.retry Number
  • optional
  • default: 0

Number of retries to make when faced with transient errors.

Return:

Promise<Object, Error>

Test:

public async fetchServerSettings(options: Object): Promise<Object, Error> source

Retrieves Kinto server settings.

Params:

NameTypeAttributeDescription
options Object
  • optional
  • default: {}

The request options.

options.retry Number
  • optional
  • default: 0

Number of retries to make when faced with transient errors.

Return:

Promise<Object, Error>

Decorators:

  • nobatch("This operation is not supported within a batch operation.")

Test:

public async fetchUser(options: Object): Promise<Object, Error> source

Retrieve authenticated user information.

Params:

NameTypeAttributeDescription
options Object
  • optional
  • default: {}

The request options.

options.headers Object
  • optional
  • default: {}

Headers to use when making this request.

options.retry Number
  • optional
  • default: 0

Number of retries to make when faced with transient errors.

Return:

Promise<Object, Error>

Decorators:

  • nobatch("This operation is not supported within a batch operation.")

Test:

public async listBuckets(options: Object): Promise<Object[], Error> source

Retrieves the list of buckets.

Params:

NameTypeAttributeDescription
options Object
  • optional
  • default: {}

The options object.

options.headers Object
  • optional
  • default: {}

Headers to use when making this request.

options.retry Number
  • optional
  • default: 0

Number of retries to make when faced with transient errors.

Return:

Promise<Object[], Error>

Test:

public async listPermissions(options: Object): Promise<Object[], Error> source

Lists all permissions.

Params:

NameTypeAttributeDescription
options Object
  • optional
  • default: {}

The options object.

options.headers Object
  • optional
  • default: {}

Headers to use when making this request.

options.retry Number
  • optional
  • default: 0

Number of retries to make when faced with transient errors.

Return:

Promise<Object[], Error>

Decorators:

Test:

public setHeaders(headers: Object) source

Set client "headers" for every request, updating previous headers (if any).

Params:

NameTypeAttributeDescription
headers Object

The headers to merge with existing ones.

Test:

Private Methods

private async _batchRequests(requests: Array, options: Object): Promise<Object, Error> source

Process batch requests, chunking them according to the batch_max_requests server setting when needed.

Params:

NameTypeAttributeDescription
requests Array

The list of batch subrequests to perform.

options Object
  • optional
  • default: {}

The options object.

Return:

Promise<Object, Error>

private _getHeaders(options: Object): Object source

Get the value of "headers" for a given request, merging the per-request headers with our own "default" headers.

Note that unlike other options, headers aren't overridden, but merged instead.

Params:

NameTypeAttributeDescription
options Object

The options for a request.

Return:

Object

private async _getHello(options: Object): Promise<Object, Error> source

Retrieves the server's "hello" endpoint. This endpoint reveals server capabilities and settings as well as telling the client "who they are" according to their given authorization headers.

Params:

NameTypeAttributeDescription
options Object
  • optional
  • default: {}

The request options.

options.headers Object
  • optional
  • default: {}

Headers to use when making this request.

options.retry Number
  • optional
  • default: 0

Number of retries to make when faced with transient errors.

Return:

Promise<Object, Error>

private _getRetry(options: *): * source

As _getSafe, but for "retry".

Params:

NameTypeAttributeDescription
options *

Return:

*

private _getSafe(options: Object): Boolean source

Get the value of "safe" for a given request, using the per-request option if present or falling back to our default otherwise.

Params:

NameTypeAttributeDescription
options Object

The options for a request.

Return:

Boolean

private _registerHTTPEvents() source

Registers HTTP events.

private async execute(request: Object, options: Object): Promise<Object, Error> source

Executes an atomic HTTP request.

Params:

NameTypeAttributeDescription
request Object

The request object.

request.path String

The path to fetch, relative to the Kinto server root.

request.method String
  • optional
  • default: "GET"

The method to use in the request.

request.body Body
  • optional

The request body.

request.headers Object
  • optional
  • default: {}

The request headers.

options Object
  • optional
  • default: {}

The options object.

options.raw Boolean
  • optional
  • default: false

If true, resolve with full response

options.stringify Boolean
  • optional
  • default: true

If true, serialize body data to

options.retry Number
  • optional
  • default: 0

The number of times to retry a request if the server responds with Retry-After. JSON.

Return:

Promise<Object, Error>

Test:

private async paginatedList(path: String, params: Object, options: Object): * source

Fetch some pages from a paginated list, following the next-page header automatically until we have fetched the requested number of pages. Return a response with a .next() method that can be called to fetch more results.

Params:

NameTypeAttributeDescription
path String

The path to make the request to.

params Object

The parameters to use when making the request.

params.sort String
  • optional
  • default: "-last_modified"

The sorting order to use when fetching.

params.filters Object
  • optional
  • default: {}

The filters to send in the request.

params.limit Number
  • optional
  • default: undefined

The limit to send in the request. Undefined means no limit.

params.pages Number
  • optional
  • default: undefined

The number of pages to fetch. Undefined means one page. Pass Infinity to fetch everything.

params.since String
  • optional
  • default: undefined

The ETag from which to start fetching.

options Object
  • optional
  • default: {}

Additional request-level parameters to use in all requests.

options.headers Object
  • optional
  • default: {}

Headers to use during all requests.

options.retry Number
  • optional
  • default: 0

Number of times to retry each request if the server responds with Retry-After.

Return:

*

Test: