GithubHelp home page GithubHelp logo

darekf77 / ng2-rest Goto Github PK

View Code? Open in Web Editor NEW
26.0 6.0 6.0 9.58 MB

Isomorphic, simple, robust REST API library for Browser and NodeJS, ( ts / js ) apps

TypeScript 87.33% CSS 0.18% HTML 11.86% JavaScript 0.52% SCSS 0.11%
isomorphic rest-api jsonp request-cancellation rest rxjs-observables nodejs-library angular-rest react-rest vue-rest

ng2-rest's Introduction

ng2-rest

Robust isomorphic REST framework for browser (Angular, React etc.) and backend (NodeJS) JavaScript/TypeScript apps.

Features:

  • Compatible with Angular (2+) (newest 13,14 also...) , React, Vue , NodeJS (works everywhere)
  • Compatible with RxJS operators (exhaustMap, switchMap, request cancelation etc.)
  • Based on axios => excellent alternative to Angular's HttpClient
  • JSONP api request handling
  • Transfer class instance from server to client and back
  • Elegant way of dealing with REST api ( similar to ExpressJS routes definitions )

(more documentation soon... )

To install this package run:

npm install ng2-rest --save

Import Resource class:

import { Resource } from 'ng2-rest/browser';

Resource

Fit you existing API (not only REST) into new fluent objects with Resource class observables. Use power of async in new angular templates;

template.html

Users:
<ul   *ngIf="model.allUsers() | async; else loader; let users" >

  <li  *ngFor="let user of users"> 
  		{{user.id}} {{user.fullName()}} 
		  <br>
		<input type="name" [(NgModel)]="user.name" >
		<button (click)="model.update(user)" > Update </button>
  </li>

</ul>

<ng-template #loader> loading users...  </ng-template>

component.ts

class User {
	name: string;
	surname: string;
	id: number;

	fullName() {
		return `Surname: ${this.surname}, Name: ${this.name}`;
	}
}

// Express.js style url endpoint model
// you can ommit "<User>" part is you don't wanna see response interface
// also you can ommit third argument ",User" is you don't wanna
// map response object to User class objects
const rest = Resource.create<User>("http://yourbackend.com/api","users/:id",{'':User} )

class UserComponent {

   // Prepare your beautiful interface
    model = {
	 allUsers: () => rest.model()
		 .array
		 .get()
		 .observable // Observable resposne (useful for Angular 2+ html templates)
     .pipe( map({ body } => body.json) ) // get all users, body.json => User[] 

	 userBy: (id) => rest.model({id})
		 .get() // Promise response by default
		 .then({ body } => console.log(body.json)) // get user by id,  body.json => User

	 update: async (user:User) =>{
		 try {
			await rest.model({id:user.id}).put(user) // Promise response by default

			alert('Update sucess')
		 } catch(e) {
			alert(e)
		 }	
	 	}
    }

	constructor() { }

}

Specification

Example UrlParams[] : [ { sort: true },{ filter: 'id,5' }, { filter: 'name,test' } ]

Name Parameters Description
.array. get,post,put,head,delete,jsonp for everything, but with arrays
get UrlParams[] get model by parameters
post model, UrlParams[] post object model
put model, UrlParams[] put object model
head model, UrlParams[] get head for model
delete UrlParams[] remove object by params
jsonp UrlParams[] get jsonp data

Production mode

=== Nice things to do in production mode:

1. Disable warnings.

If you don't wanna see warning, disable it like this:

if (environment.production) {
  Resource.enableWarnings = false;
}

Angular 2+ ngZone

If you are using Angular 2+ you need to do this in your root app.component:

constructor(zone:NgZone) {
    Resource.initAngularNgZone(zone)
}

ng2-rest's People

Contributors

darekf77 avatar darekf77igt avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ng2-rest's Issues

Cannot find name 'require'.

I am getting the following error upon installing the npm package. I am using ionic 2.

typescript: node_modules/ng2-rest/src/mock-backend.ts, line: 1
Cannot find name 'require'.

   L1:  const faker = require('faker');
   L2:  import { Http } from './http';

a 11.0.35 release for ng2-rest with updated lodash

Hi, @darekf77, there are several high severity vulnerabilities introduced by package lodash:

Issue Description

I noticed that a vulnerability is introduced in [email protected]:
Vulnerability(high severity) CVE-2018-16487, CVE-2019-10744 and SNYK-JS-LODASH-608086 affect package lodash (versions:<4.17.11)
The above vulnerable package is referenced by [email protected] via:
[email protected][email protected]

Since [email protected] is referenced by 8,745 downstream projects (e.g., morphi 4.0.22 (latest version), tnp 0.0.98 (latest version), firedev 2.0.96 (latest version), tnp-helpers 0.0.37 (latest version), tnp-db 0.0.27 (latest version)), the vulnerability CVE-2018-16487, CVE-2019-10744 and SNYK-JS-LODASH-608086 can be propagated into these downstream projects and expose security threats to them via the following package dependency paths:
(1)[email protected][email protected][email protected][email protected][email protected][email protected][email protected]
(2)[email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]
......

If [email protected].* removes the vulnerable package from the above version, then its fixed version can help downstream users decrease their pain.

Given the large number of downstream users, could you help update your package to remove the vulnerability from [email protected] ?

Fixing suggestions

In [email protected], maybe you can kindly try to perform the following upgrade(not crossing major version) :
lodash 4.17.4 ➔ 4.17.21;

Note:
[email protected](>=4.17.21) has fixed the vulnerability CVE-2018-16487, CVE-2019-10744 and SNYK-JS-LODASH-608086.

Thank you for your attention to this issue and welcome to share other ways to resolve the issue.

Best regards,
^_^

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.