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
sequenceDiagram
participant Client
participant Service as RestService
participant Adapter as HttpAdapter
participant API
Client->>Service: create(model)
Service->>Adapter: prepare(model, pk)
Service->>Adapter: create(table, id, record)
Adapter->>API: HTTP POST
API-->>Adapter: 201 Created
Adapter-->>Service: record
Service-->>Client: revert(record)
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
adapter |
A
|
The HTTP adapter instance |
|
clazz |
Constructor.<M>
|
<optional> |
Optional constructor for the model class |
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
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.
- 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.