Class

RestService

RestService(adapter, clazzopt)

Constructor

# new RestService(adapter, clazzopt)

Provides a comprehensive implementation for interacting with REST APIs. This class implements CRUD operations for single and bulk operations, as well as the Observable pattern to notify observers of changes. It works with HTTP adapters to perform the actual API requests and handles model conversion.

Service class for REST API operations

Parameters:
Name Type Attributes Description
adapter A

The HTTP adapter instance

clazz Constructor.<M> <optional>

Optional constructor for the model class

View Source RestService.ts, line 39

Example
```typescript
// Create a service for User model with Axios adapter
const axiosAdapter = new AxiosAdapter({
  protocol: 'https',
  host: 'api.example.com'
});
const userService = new RestService(axiosAdapter, User);

// Create a new user
const user = new User({ name: 'John Doe', email: 'john@example.com' });
const createdUser = await userService.create(user);

// Update a user
createdUser.name = 'Jane Doe';
const updatedUser = await userService.update(createdUser);

// Delete a user
await userService.delete(updatedUser.id);
```

Classes

RestService

Provides a comprehensive implementation for interacting with REST APIs. This class implements CRUD operations for single and bulk operations, as well as the Observable pattern to notify observers of changes. It works with HTTP adapters to perform the actual API requests and handles model conversion.

RestService

Creates a new service instance with the specified adapter and optional model class. The constructor stores the adapter and model class for later use in CRUD operations.

Members

# adapter

Retrieves the HTTP adapter associated with this service. Throws an error if no adapter is found.

Gets the HTTP adapter

View Source RestService.ts, line 75

# class

Retrieves the model class constructor associated with this service. Throws an error if no class definition is found.

Gets the model class constructor

View Source RestService.ts, line 49

# pk

Retrieves the name of the primary key property for the model. If not already determined, it finds the primary key using the model class.

Gets the primary key property name

View Source RestService.ts, line 60

# tableName

Retrieves the table name associated with the model class. If not already determined, it gets the table name from the Repository utility.

Gets the table name for the model

View Source RestService.ts, line 86

Methods

# protected adapter() → {A}

Retrieves the HTTP adapter associated with this service. Throws an error if no adapter is found.

Gets the HTTP adapter

View Source RestService.ts, line 289

If no adapter is found

InternalError

The HTTP adapter instance

A

# class() → {Constructor.<M>}

Retrieves the model class constructor associated with this service. Throws an error if no class definition is found.

Gets the model class constructor

View Source RestService.ts, line 271

If no class definition is found

InternalError

The model class constructor

Constructor.<M>

# async create(model, …args) → {Promise.<M>}

Creates a new resource in the REST API using the provided model. The method prepares the model for the adapter, sends the create request, and then converts the response back to a model instance.

Creates a new resource

Parameters:
Name Type Attributes Description
model M

The model instance to create

args Array.<any> <repeatable>

Additional arguments to pass to the adapter

View Source RestService.ts, line 320

A promise that resolves with the created model instance

Promise.<M>

# async createAll(models, …args) → {Promise.<Array.<M>>}

Creates multiple resources in the REST API using the provided models. The method prepares each model for the adapter, sends a bulk create request, and then converts the responses back to model instances.

Creates multiple resources

Parameters:
Name Type Attributes Description
models Array.<M>

The model instances to create

args Array.<any> <repeatable>

Additional arguments to pass to the adapter

View Source RestService.ts, line 366

A promise that resolves with an array of created model instances

Promise.<Array.<M>>

# async delete(id, …args) → {Promise.<M>}

Removes a resource from the REST API using the provided ID. The method sends the delete request and converts the response to a model instance.

Deletes a resource by ID

Parameters:
Name Type Attributes Description
id string | number

The identifier of the resource to delete

args Array.<any> <repeatable>

Additional arguments to pass to the adapter

View Source RestService.ts, line 354

A promise that resolves with the deleted model instance

Promise.<M>

# async deleteAll(keys, …args) → {Promise.<Array.<M>>}

Removes multiple resources from the REST API using the provided IDs. The method sends a bulk delete request and converts the responses to model instances.

Deletes multiple resources by IDs

Parameters:
Name Type Attributes Description
keys Array.<string> | Array.<number>

The identifiers of the resources to delete

args Array.<any> <repeatable>

Additional arguments to pass to the adapter

View Source RestService.ts, line 377

A promise that resolves with an array of deleted model instances

Promise.<Array.<M>>

# observe(observer) → {void}

Adds an observer to the list of observers that will be notified of changes. Throws an error if the observer is already registered.

Registers an observer

Parameters:
Name Type Description
observer Observer

The observer to register

View Source RestService.ts, line 410

If the observer is already registered

InternalError
void

# pk()

Retrieves the name of the primary key property for the model. If not already determined, it finds the primary key using the model class.

Gets the primary key property name

View Source RestService.ts, line 279

The primary key property name

# async read(id, …args) → {Promise.<M>}

Fetches a resource from the REST API using the provided ID. The method sends the read request and converts the response to a model instance.

Retrieves a resource by ID

Parameters:
Name Type Attributes Description
id string | number

The identifier of the resource to retrieve

args Array.<any> <repeatable>

Additional arguments to pass to the adapter

View Source RestService.ts, line 331

A promise that resolves with the retrieved model instance

Promise.<M>

# async readAll(keys, …args) → {Promise.<Array.<M>>}

Fetches multiple resources from the REST API using the provided IDs. The method sends a bulk read request and converts the responses to model instances.

Retrieves multiple resources by IDs

Parameters:
Name Type Attributes Description
keys Array.<string> | Array.<number>

The identifiers of the resources to retrieve

args Array.<any> <repeatable>

Additional arguments to pass to the adapter

View Source RestService.ts, line 388

A promise that resolves with an array of retrieved model instances

Promise.<Array.<M>>

# protected tableName() → {string}

Retrieves the table name associated with the model class. If not already determined, it gets the table name from the Repository utility.

Gets the table name for the model

View Source RestService.ts, line 298

The table name

string

# unObserve(observer) → {void}

Removes an observer from the list of observers. Throws an error if the observer is not found.

Unregisters an observer

Parameters:
Name Type Description
observer Observer

The observer to unregister

View Source RestService.ts, line 420

If the observer is not found

InternalError
void

# async update(model, …args) → {Promise.<M>}

Updates an existing resource in the REST API using the provided model. The method prepares the model for the adapter, sends the update request, and then converts the response back to a model instance.

Updates an existing resource

Parameters:
Name Type Attributes Description
model M

The model instance with updated data

args Array.<any> <repeatable>

Additional arguments to pass to the adapter

View Source RestService.ts, line 343

A promise that resolves with the updated model instance

Promise.<M>

# async updateAll(models, …args) → {Promise.<Array.<M>>}

Updates multiple resources in the REST API using the provided models. The method prepares each model for the adapter, sends a bulk update request, and then converts the responses back to model instances.

Updates multiple resources

Parameters:
Name Type Attributes Description
models Array.<M>

The model instances with updated data

args Array.<any> <repeatable>

Additional arguments to pass to the adapter

View Source RestService.ts, line 400

A promise that resolves with an array of updated model instances

Promise.<Array.<M>>

# async updateObservers(…argsopt) → {Promise.<void>}

Calls the refresh method on all registered observers to update themselves. Any errors during observer refresh are logged as warnings but don't stop the process.

Notifies all registered observers

Parameters:
Name Type Attributes Description
args Array.<any> <optional>
<repeatable>

Optional arguments to pass to the observer refresh method

View Source RestService.ts, line 430

A promise that resolves when all observers have been updated

Promise.<void>