Home Identifier Source Repository
public class | source

Base

A base class for API resources. This class is meant to be subclassed and have it's apiMethods method overridden to include all methods that should be made available to consumers.

Example:

import Base from "./Base";

class Thing extends Base {
  constructor() {
    super("thing")
  }

  get apiMethods() {
    return super.apiMethods.concat(["fetchAllTheThings"]);
  }

  fetchAllTheThings() {
    return this.fetch("/admin/things.json");
  }
}

Constructor Summary

Public Constructor
public

constructor(resourceName: string, options: object)

Using the supplied resourceName, this constructor will generate the singular and plural names for this resource

Member Summary

Public Members
public get

Returns an object that supplies the clear, count and merge actions for this resource

public get

An array of methods to be exposed via APIProxy

Method Summary

Public Methods
public

clear()

Dispatches the clear action for this resource.

public

create(resource: object, options: object): RequestPromise

Performs a POST request, sending the resource along as the request body

public

Destroys the resource

public

Performs a GET request for the specified query.

public

Performs a GET request for the resource's count endpoint

public

update(id: number | string, resource: object, options: object): RequestPromise

Updates the given resource

Protected Methods
protected

Public Constructors

public constructor(resourceName: string, options: object) source

Using the supplied resourceName, this constructor will generate the singular and plural names for this resource which will be used to infer the merge, count, and clear actions to be dispatched.

Params:

NameTypeAttributeDescription
resourceName string

The singular name for the resource (e.g. product)

options object
  • optional

Options for this resource

options.countable boolean

Whether or not this resource is countable (has a /count.json endpoint)

options.createable boolean

Whether or not this resource can be created

options.destroyable boolean

Whether or not this resource can be deleted

Public Members

public get actions: Actions: * source

Returns an object that supplies the clear, count and merge actions for this resource

Return:

Actions

public get apiMethods: Array<string>: * source

An array of methods to be exposed via APIProxy

Return:

Array<string>

The names of all methods to be made available

Public Methods

public clear() source

Dispatches the clear action for this resource. The action will be handled by the appropriate store.

public create(resource: object, options: object): RequestPromise source

Performs a POST request, sending the resource along as the request body

Params:

NameTypeAttributeDescription
resource object

The resource to be created

options object
  • optional

Options to be passed along to the request

Return:

RequestPromise

public destroy(id: number | string, options: object): RequestPromise source

Destroys the resource

Params:

NameTypeAttributeDescription
id number | string

The id of the resource

options object
  • optional

Options to be passed along with the request

Return:

RequestPromise

public fetch(query: ResourceQuery): RequestPromise source

Performs a GET request for the specified query.

Params:

NameTypeAttributeDescription
query ResourceQuery
  • optional

Return:

RequestPromise

public fetchCount(query: ResourceQuery): RequestPromise source

Performs a GET request for the resource's count endpoint

Params:

NameTypeAttributeDescription
query ResourceQuery
  • optional

Return:

RequestPromise

public update(id: number | string, resource: object, options: object): RequestPromise source

Updates the given resource

Params:

NameTypeAttributeDescription
id number | string

The id of the resource

resource object

The resource to be updated

options object
  • optional

Options to be passed along with the request

Return:

RequestPromise

Protected Methods

protected urlFor(query: ResourceQuery, params: ResourceQuery): string source

Params:

NameTypeAttributeDescription
query ResourceQuery
  • optional
params ResourceQuery
  • optional

Query string parameters (only used when query is a number or string)

Return:

string

The resource URL for the specified query

Example:

import Base from "./Base";

let instance = new Base("product");

instance.urlFor();                            // `/admin/products.json`
instance.urlFor(1);                           // `/admin/products/1.json`
instance.urlFor("1");                         // `/admin/products/1.json`
instance.urlFor(1, { type: "test" })          // `/admin/products/1.json?type=test
instance.urlFor({ fields: ["id", "title"] }); // `/admin/products.json?fields=id,title