Base
Direct Subclass:
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 |
Member Summary
Public Members | ||
public get |
Returns an object that supplies the clear, count and merge actions for this resource |
|
public get |
apiMethods: Array<string>: * 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 |
destroy(id: number | string, options: object): RequestPromise Destroys the resource |
|
public |
fetch(query: ResourceQuery): RequestPromise Performs a GET request for the specified query. |
|
public |
fetchCount(query: ResourceQuery): RequestPromise Performs a GET request for the resource's count endpoint |
|
public |
Updates the given resource |
Protected Methods | ||
protected |
urlFor(query: ResourceQuery, params: ResourceQuery): string |
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:
Name | Type | Attribute | Description |
resourceName | string | The singular name for the resource (e.g. |
|
options | object |
|
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 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
public fetch(query: ResourceQuery): RequestPromise source
Performs a GET request for the specified query.
Params:
Name | Type | Attribute | Description |
query | ResourceQuery |
|
public fetchCount(query: ResourceQuery): RequestPromise source
Performs a GET request for the resource's count endpoint
Params:
Name | Type | Attribute | Description |
query | ResourceQuery |
|
public update(id: number | string, resource: object, options: object): RequestPromise source
Updates the given resource
Protected Methods
protected urlFor(query: ResourceQuery, params: ResourceQuery): string source
Params:
Name | Type | Attribute | Description |
query | ResourceQuery |
|
|
params | ResourceQuery |
|
Query string parameters (only used when query is a number or string) |
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