Skip to content

Service

Pieter Verschaffelt edited this page Apr 21, 2019 · 2 revisions

The Service is very important in TypeScript REST Mapper and allows you to query REST API's directly. It supports reading from an API or storing and updating objects. The Service only requires a base URL for consuming API calls and automatically expands it to form the correct API endpoint. A Service follows these rules to compose endpoints automatically:

  • The class name is automatically pluralized and appended to the end of the base URL, if no endpoint was specified by the user. If the base URL is http://example.com, the Service<Post> will use the endpoint http://example.com/posts.
  • If the user specifies an endpoint explicitly, then this is the endpoint that will be appended to the base URL and that's used for requests.

Methods

constructor()

Parameters:

  • x: (new () => T): Type of the Entity for which a new Service needs to be instantiated.
  • baseURL: string: Base URL for the REST API that should be queried using this new Service. This corresponds to the root of the API in most cases. This URL will be supplemented with the correct endpoint path automatically, if you conform to the default implementation.

retrieve()

Retrieve one Entity of type T from the REST API. This method corresponds to performing a GET request on the associated endpoint of the API.

Parameters:

  • id: string = null: The id of the corresponding entity that should be retrieved from the REST API.
  • params: object = {}: Additional URL query parameters that should be appended to the URL before performing the GET request.
  • endPoint: string = null: The Service pluralizes the encapsulated Entity's name and appends it to the base URL by default. If you however decide to use a different name for your Entity than for your REST endpoint, you can override the default settings by passing the endpoint as this argument.

** This page is still under construction **

Clone this wiki locally