npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lysis-restangular-services-generator

v0.0.23

Published

Generate Restangular services for JSON LD backend

Downloads

53

Readme

Lysis Restangular services generator

Overview

This generator creates Restangular services from JSON-LD to handle CRUD actions.

Samples

Backend service generic class

The backend-service.ts file is created in the services directory.
It uses the Restangular service.

It provides many methods:

  • get(id: any): Observable<T>
  • getAll(pageNumber?: number, criterias: Object = {}): Observable<T[]>
  • add(item: T): Observable<T>
  • update(item: T): Observable<T>
  • remove(item: T): Observable<any>

Where <T> is the result class, maybe from TypeScript classes generator

Two helper methods can be called directly, or used in inherited services:

  • getAllBy(field: string, value: any, pageNumber?: number, criterias: Object = {}, alias: string = this.resource): Observable<T[]>
  • getAllByFilter(filter: string, value: any, pageNumber?: number, criterias: Object = {}): Observable<T[]>

Rest services

A service class is created for each resource.

Example of service to handle books, in the generated file Books.service.ts:

import { Injectable } from '@angular/core';
import { BackendService } from './backend.service';
import { Books } from '../classes/Books';

@Injectable()
export class BooksService extends BackendService<Books> {
  protected get resource() { return 'books'; }
  protected get class() { return Books; }

}

Basic usage

In a controller, to get books:

import { Component, OnInit } from '@angular/core';

import { BooksService } from '../backend/services';
import { Book } from '../backend/classes';

@Component({
  selector: 'app-books-list',
  templateUrl: './books-list.component.html',
  styleUrls: ['./books-list.component.css']
})

export class BooksListComponent implements OnInit {
  books: Array<Book> = [];

  constructor(
    private booksService : BooksService
  ) {}

  ngOnInit() {
    this.booksService.getAll().subscribe(books => {
      this.books = this.books.concat(books);
    });
  }
}

Extend services

As services are simple, and do contain nothing more than the resource class and name, it is not overwritten in further generations.

This allows you to update services to add helper methods.

For example, this method extends the generated ReviewService to add a method to easily get reviews per book.

import { Injectable } from '@angular/core';
import { BackendService } from './backend.service';
import { Reviews } from '../classes/Reviews';

@Injectable()
export class ReviewsService extends BackendService<Reviews> {
  protected get resource() { return 'reviews'; }
  protected get class() { return Reviews; }

  getAllByBookId(id: number, pageNumber?: number, criterias: Object = {}): Observable<Reviews[]> {
    return this.getAllByFilter('book.id', id, pageNumber, criterias);
  }
}

The request URI is for example: http://my-backend/reviews?book.id=5.

However, the same result can be reached, using getAllByFilter directly in the controller, without additional method:

this.reviewsService.getAllByFilter('book.id', 5);

Index file

The index file is the one to include in the application controller, services, ... to use services.

It should not be modified, as it is overwritten during further generations.

Use

Prerequisites

If it is not already done, install api-lysis globally and as dev dependency:

npm install api-lysis -g
npm install api-lysis --save-dev

### Install this generator

Install this generator:

npm install lysis-restangular-services-generator --save-dev

### Install ngx-restangular

As the generated services work with ngx-restangular, it must be included in the project:

npm install ngx-restangular --save

Restangular configuration

A Restangular configuration function is provided with generated services.

Main purpose is to ease JSON-LD data handling.

In app.module.ts, import ngx-restangular and the configuration:

import { RestangularModule } from 'ngx-restangular';
import { RestangularConfigFactory } from './backend/services/RestangularConfigFactory';

Add a configuration function:

export function createRestangularConfigFactory(RestangularProvider) {
  return RestangularConfigFactory(RestangularProvider, { baseUrl: 'http://127.0.0.1:8000' });
}

// @NgModule({
  // [...]

And add it to imports:

  imports: [
    // [...],
    RestangularModule.forRoot([], createRestangularConfigFactory)
  ],

If you have scaffolded the project with angular CLI, the configuration function should look like:

import { environment } from '../environments/environment';

// [...]

export function createRestangularConfigFactory(RestangularProvider) {
  return RestangularConfigFactory(RestangularProvider, { baseUrl: environment.apiUrl });
}

This assumes environment files contain a apiUrl property.

Configuration

Configuration sample:

apis:
  http://localhost:8000:
    basePath: 'my-backend'
    hydraPrefix: 'hydra:'
    generators:
      lysis-restangular-services-generator:
        dir: 'services'
        classPath: '../classes'

Services files are generated in my-backend/services.

If dir is not set, the default value is backend-services.

classPath is the path to class files. By default it is set to ../backend-classes.

Note: class files may be the one generated by lysis-typescript-classes-generator .