easy-connect
A package makes life easier. Joking, it does the next best thing. It facilitates http requests and authentications, session caching and local caching and instantiaing instances in runtime in angular4.x, react.js and vue.js 2. The packages seeks to eleminate boilerplate code by inversion of control using containers and convenience annotations. It also uses easy-injectionjs as its dependency injection api to resolve depedencies in runtime and resolve nested depedencies in classes.
To install it:
npm i -S easy-connect
or
yarn add easy-connect
Defining a model:
; @// If you're using token auth use@// If you want to cache your model use it cache data automatically@@ @ // to query the api endpoint using a certain query key userId: number; @id // It is used to get data, delete data or update data by id @ id: number; title: string; body: string;
Both @key and @query can have names to be utilized by through out the application:
; @// If you're using token auth use@// If you want to cache your model use it cache data automatically@@ @ userId: number; @id // It is used to get data, delete data or update data by id @ id: number; title: string; body: string;
Both @key and @query can also have further configurations: like extented apir url and whether that url is part of the same api used:
; @// If you're using token auth use@// If you want to cache your model use it cache data automatically@@ @ // to query the api endpoint using a certain query key userId: number; @id // It is used to get data, delete data or update data by id @ id: number; title: string; body: string;
Using easy-injectionjs decorators with easy-connect to resolve nested dependencies:
Class Company:
@ name: string; catchPhrase: string; bs:string; public get : string return thisname + ' out catch phrase ' + thiscatchPhrase + ' and Bs: ' + thisbs
Class Address:
@ street: string; suite: string; city: string; zipcode: number; @ geo: Geo;
Class User
@@@@ @id id: number; @ name: string; email: string; phone: string; website: string; public get : string return thisname + thisemail @ company: Company; @ address: Address;
easy-connect will resolve all nested classes dependencies in runtime using easy-injectionjs
For angular 4.x:
Using easy-connect as a service
In app.module.ts:
;@
; {} // or easy: EasyConnect; /* constructor() { this.easy = new EasyConnect(); } */ { let id = 10; let key = 'Hola'; /* returns an observable after it caches the data into the session and cache if it cacheable it caches and updates it every 15 seconds */ thiseasy; // Observable of User[] thiseasy; // Observable of User thiseasy; // Observable of User /* Or you can skip the the session storage if your api updates faster that 15 seconds you can use */ thiseasy; // Observable of User[] thiseasy; // Observable of User thiseasy; // Observable of User // to query thiseasy; // Observable of query // post, put and delete. thiseasy; // Oservable of any thiseasy; // Observable of any thiseasy; // Observable of any // You can also pass a different key for the id if need be thiseasy; // Observable of any thiseasy; // Observable of any thiseasy; Oservable of any }
All of Easy methods can take extra url params if u don't want to use @api urls
Containers:
Containers are a key factor of inversion of control in easy-connect. You can use containers instead of Easy if all you need to do is use the api for typical rest api usage. list, search, create, update and delete.
In Model class:
@@@ @ userId: number; @id @ id: number; title: string; body: string;
An angular component:
;; @ implements OnInit private _post: Post; { } { ; this_post = this_postuserId = 20; this_postid = 200; this_posttitle = 'Sal says hi'; this_postbody = 'Awesomeness overload'; } public { } public { if thisposts } get : Post return <Post> get : Post return get : Post return public { ; } public { this_posttitle = 'Sal is always hungry :D' ; } public { ; }
Get_All method: requires a class. It is typically used to get data from the rest api.
All: requires a class. It returns an array of the class type, and that array is the same array fetched by Get_All.
Get_Query: requires a class, a query argument: it can be either the parameter name as in id or, if @query('ID') has a name like "ID", the name given in the @query, and a value for the query argument like the previous example.
Query: requres a class: It return an array of the Get_Query result.
Get_Current: can take an object or an object and an argument: if it only take an object, it will get the object by it's id, the @id decorated property, but if an argument is passed it will get the object a key decorated with the @key decorator. Much like the @query it can be used either with the property name if the @key doesn't have a name or the name given to the @key('ID').
Current: return the data returned by Get_Current.
Add: requires a class and an object. It posts the object to the api endpoint and updates the result set of All.
Update: requires a class and an object. It updates the object to the api endpoint and updates the result set of "All". If the class has "updateById" url, it will update the object by @id, else it with use the "update" url to update the object.
Delete: requires a class and an object. It deletes the object from the api end point and updates the result set of "All". If the class has "deleteById" url, it will delete the object by @id, else with the use of "delete" url.
Authentication:
In any class or module:
Token authentication:
; auth: EasyTokenAuth; { thisauth = loginUrl: 'Your api login url' logoutUrl: 'Your api logout url' registerUrl: 'Your api registeration url' validateUrl: 'your api validation url' prefix: 'your api token prefix' // If empty it assumes that it is Bearer key:'Your api token key in body if returned in the body' ; }
Session or Cookie authentication:
The easiest way to configure authentication:
Create a class and decorate the class using @authConfig. It takes two parameters:
1- Authentication urls 2- Authentication type
The second parameter is optional:
Example (Token authentication):
@
Example (Session authentication):
@
Using this methods allows for the following usages for common authentication functionalities anywhere though out the application
// observable of any. // observable of any. // observable of any. // observable of any.
The more complicated way:
; auth: EasyAuth; { thisauth = loginUrl: 'Your api login url' logoutUrl: 'Your api logout url' registerUrl: 'Your api registeration url' validateUrl: 'your api validation url' ; }
or
; auth: EasyTokenAuth; { thisauth = loginUrl:'LOGIN_URL' logoutUrl: 'LOGOUT_URL' validateDataUrl: 'DATA_VALIDATION_URL' validateUrl: 'VALIDATION_URL' registerUrl:'REGISTERATION_URL' prefix: 'TOKEN_PREFIX' key:'token' // if the token is returned in the response body tokenExpiry: 6480000 // how long to save the token in milliseconds ; }
Both authentication types implement the following methods:
thisauth; // observable of any.thisauth; // observable of any.thisauth; // observable of any.thisauth; // observable of any.