Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

va-ts-endpoint

2.0.2 • Public • Published

Api Endpoint

Installation

npm install va-ts-endpoint

Motivation

Writing data services with hard-coded endpoints that have parameters in the url string is not very comfortable, because whenever I want to interpolate them, I have to jump to the variable declaration that stores the string with the address. That's why I decided to create a very simple factory which helps to create typed endpoints.

Usage

export class ExampleDataService {
  readonly endpoints = {
    // Create simple endpoint.
    // In simple case ApiEndpoint is additional abstraction, but it's created to be consistent in every place.
    getItems: apiEndpointFactory(`${yourApiBaseUrl}/items`),
    // Create typed endpoint. Values with `:` will be interpolated.
    // Provide union type which corresponds to values which should be interpolated. 
    getItem: apiEndpointFactory<'exampleId' | 'itemId'>(`${yourApiBaseUrl}/examples/:exampleId/items/:itemId`, true)
  };
  
  constructor(private http: HttpClient) {}
  
  getItems(): Observable<Item[]> {
    return this.http.get(this.endpoints.getItems.url());
  }
  
  getItem(exampleId: string, itemId: string): Observable<Item> {
    return this.http.get(this.endpoints.getItem.url({exampleId, itemId}));
  }
}

Keywords

none

install

npm i va-ts-endpoint

Downloadsweekly downloads

102

version

2.0.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability