It is the main class of the package and provides an abstraction for an REST Api resource.
Creates a new instance of the ApiEntityService
for the entity T
.
constructor( { api, basePath, parser, creator, errorParser, paths } ): ApiEntityService<T>
Name | Type | Description | Required | Default |
---|---|---|---|---|
api | IApi | The api client to use to connect with the remote API | yes | - |
basePath | string |
The base path for the resource without the leading and ending slash. | yes | - |
parser | IResponseParser | An object in charge of parsing the API responses | yes | - |
creator | IEntityCreator | An object in charge of creating the fetched entities in the correct stores | yes | - |
errorParser | IErrorParser | An object in charge of parsing the error responses | no | NullErrorParser |
paths | ICustomPaths | A hash indicating custom paths for the CRUD methods | no | {} |
Returns a new instance of the ApiEntityService
that will:
- Use the
api
to make the requests to the remote REST API. - Use the
parser
to parse theapi
responses before sending them to thecreator
. - Use the
creator
to create the entities including in the parsed response in their corresponding entities stores. - Use the
errorParser
to parse theapi
errors before throwing them.
Creates a new entity in the remote API.
create( attributes, params, config ): Promise<SingleEntityResponse<T>>
Name | Type | Description | Required | Default |
---|---|---|---|---|
attributes | Attributes | The attributes for the new entity to create | yes | - |
params | Params | Additional query parameters to send with the request to the remote API | no | {} |
config | RequestWithBodyConfig | Additional configuration for the request to the remote API | no | {} |
Returns a promise that resolves to a response object containing the created entity if the request to the API is successful.
Updates an existing entity in the remote API.
update( id, attributes, params, config ): Promise<SingleEntityResponse<T>>
Name | Type | Description | Required | Default |
---|---|---|---|---|
id | EntityID | The ID of the entity to update | yes | - |
attributes | Attributes | The attributes to update | yes | - |
params | Params | Additional query parameters to send with the request to the remote API | no | {} |
config | RequestWithBodyConfig | Additional configuration for the request to the remote API | no | {} |
Returns a promise that resolves to a response object containing the update entity if the request to the API is successful.
Fetches an existing entity in the remote API.
fetch( id, params ): Promise<SingleEntityResponse<T>>
Name | Type | Description | Required | Default |
---|---|---|---|---|
id | EntityID | The ID of the entity to fetch | yes | - |
params | Params | Additional query parameters to send with the request to the remote API | no | {} |
Returns a promise that resolves to a response object containing the fetched entity if the request to the API is successful.
Fetches a list of entities from the remote API.
fetchAll( params ): Promise<MultiEntityResponse<T>>
Name | Type | Description | Required | Default |
---|---|---|---|---|
params | Params | Additional query parameters to send with the request to the remote API | no | {} |
Returns a promise that resolves to a response object containing the fetched entities if the request to the API is successful.
Deletes an existing entity in the remote API.
delete( id, params ): Promise<SingleEntityResponse<T>>
Name | Type | Description | Required | Default |
---|---|---|---|---|
id | EntityID | The ID of the entity to delete | yes | - |
params | Params | Additional query parameters to send with the request to the remote API | no | {} |
Returns a promise that resolves to null
if the request to the API is successful.
Makes a custom request to the remote API. It comes handy when you have to make a custom request that not respects the traditional CRUD methods.
request( { method, url, attributes, params, config } ): Promise<EntityResponse<T> | null>
Name | Type | Description | Required | Default |
---|---|---|---|---|
method | HTTPMethod | The HTTP method to use in the request. | no | HTTPMethod.GET |
url | string |
The api path to call. Note that this method does not use the base path to make the request so if you want a URL relative to the base path you have to do it yourself and pass it here. | no | HTTPMethod.GET |
attributes | Attributes | Attributes to send as the body of the request. | no | null |
params | Params | Query parameters to send with the request to the remote API. | no | {} |
config | RequestWithBodyConfig | Additional configuration for the request to the remote API. | no | {} |
Returns a promise that resolves to a response object containing the fetched entities if the request to the API is successful.