@northtech/bragi

Skeleton for Angular REST services

Usage no npm install needed!

<script type="module">
  import northtechBragi from 'https://cdn.skypack.dev/@northtech/bragi';
</script>

README

bragi

...is the god of skalds, giving the stories structure and form so they can be remembered and recited.

This package contains the generic skeleton for a REST service doing GET/PUT/POST/DELETE semantics against a URL and exposes the resulting data as Observable array.

It is an Angular tool, building upon the HttpClient, but it is not packaged and deployed as an Angular library. Instead, using code should extend the GenericRest to create project-specific, actual injectable data services.

Usage

At its simplest, the generic service can be used like this:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { GenericRest } from '@northtech/bragi';

@Injectable()
export class MyClassService extends GenericRest<MyClass> {
  constructor(httpClient: HttpClient, stringService: InterpolatedStringService) {
    super(httpClient, 'htt://url-to-rest-sercice', 'nameOfIdFieldInMyClass', {
      interpolatedStringService: stringService
    });
  }
}  

This will create an injectable MyClassService which has a data property of type Observable<MyClass[]>, and template code can simply do something like

<div *ngFor="let myClass of myClassService.data|async"></div>

At first glance, this does not look much different from simply using HttpClient and subscribing to the result, but the advantage of this service structure is that any changes, no matter how or where they are initiated, will be published from the service and reflected across the UI, in true reactive style.

The service skeleton also contains methods to refresh, save and delete objects, automatically publishing any such changes after the server accepts them.

The given URL will be registered in the InterpolatedStringService, making it easy to configure e.g. server addresses as placeholder variables in the string.

Additionally, the service also offers a fuzzy search, similarly exposed as an Observable.

While Bragi can be used by itself, it has a Java part which can auto-generate interfaces and data services from Java classes.

Remember to include the BragiModule in your app module.