Api Calls

The model has the capability to interact with your api. The exact behaviour depends on the ApiCaller implementation used. To facilitate some higher level methods like touch the model provides methods equivalent to the http methods.

Properties

endpoint

The endpoint property is a getter which should return a string that is the or part of the address which the model can interact with. If not setting this value, the endpoint will be guessed for you. This value will be appended to the baseEndPoint from the GlobalConfig if the baseEndPoint has been set.

loading

The loading property indicates whether there is an ongoing request on the model you're currently interacting with.

serverAttributeCasing

The serverAttributeCasing is a getter which similarly to attributeCasing casts the request keys recursively to the given casing on outgoing requests. The valid values are 'snake' or 'camel' with 'snake' being the default value.

_lastSyncedAt

The _lastSyncedAt or _last_synced_at (naming subject to attributeCasing) attribute is a getter attribute that is set only when the model data has been fetched, saved or refreshed. Its type subject to the datetime setting, with the value of when was the last time the data has been loaded from the backend.

Methods

TIP

All request methods on success will call the resetEndpoint and will reset all the query parameters.

get

async

The get method initiates a new GET request. It optionally accepts an object which are passed to the ApiCaller to transform into a query string. The method is also available statically. It returns a Model or ModelCollection.

import User from '@Models/User';

const user = new User;
user.get();
User.get();

post

async

The post method initiates a new POST request. It returns a Model if the endpoint returns data otherwise returns itself.

import User from '@Models/User';

const user = new User;
user.post({ attribute: 1 });

put

async

The put method initiates a new PUT request. It returns a Model if the endpoint returns data otherwise returns itself.

import User from '@Models/User';

const user = new User;
user.put({ attribute: 1 });

patch

async

The patch method initiates a new PATCH request. It returns a Model if the endpoint returns data otherwise returns itself.

import User from '@Models/User';

const user = new User;
user.patch({ attribute: 1 });

delete

async

The delete method initiates a new DELETE request. It returns a Model if the endpoint returns data otherwise returns itself.

import User from '@Models/User';

const user = new User;
user.delete({ attribute: 1 });

call

asyncadvanced

The call method is what powers the rest of the api calls on the model. It takes one argument and two optional arguments. The first argument is the method name which is one of 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT' | 'HEAD'. The second is the data to send along with the request. and the third is any custom headers in an object format to be sent along. After the request the resetEndpoint will be called and all query parameters will be reset.

import User from '@Models/User';

const user = new User();

await user.call('GET', { query: 'value' }); // GET your-api.com/users?query=value

Endpoint manipulation

There are couple utilities to change the endpoint for the next request.

setEndpoint

The setEndpoint method replaces the endpoint for the next request.

import User from '@Models/User';

const user = new User;
user.getEndpoint(); // 'users'
user.setEndpoint('/something').getEndpoint(); // '/something'

getEndpoint

The getEndpoint method returns the current endpoint.

import User from '@Models/User';

const user = new User;
user.getEndpoint(); // 'users'

resetEndpoint

The resetEndpoint method resets the endpoint to the original endpoint. If endpoint is not set, it will try to guess it based on the model name.

import User from '@Models/User';

const user = new User;
user.setEndpoint('/something').getEndpoint(); // '/something'
user.resetEndpoint().getEndpoint(); // 'users'

appendToEndpoint

The appendToEndpoint methods appends the given string to the current endpoint.

import User from '@Models/User';

const user = new User;
user.getEndpoint(); // 'users'
user.appendToEndpoint('/something').getEndpoint(); // 'users/something'

Miscellaneous

setLastSyncedAt

advanced

The setLastSyncedAt method sets the _lastSyncedAt attribute to the current Dateopen in new window. It optionally also accepts an argument for the value to be set as. The set value is subject to the date-time casting. Furthermore, it takes timestamps into consideration when enabled.

WARNING

This method should only really be used when mocking the model to look like it exists. Some possible use-cases are: - Testing a model. - Hydrating a model with known to existing record.

import User from '@Models/User';

const user = User.make({ id: 1 });
user.exists; // false
user.setLastSyncedAt().exists; // true

setModelEndpoint

The setModelEndpoint sets the endpoint on the model to the resource endpoint by appending the primary key.

import User from '@Models/User';

const user = User.make({ id: 1 });
user.getEndpoint(); // '/users'
user.setModelEndpoint().getEndpoint(); // '/users/1'