Giter Site home page Giter Site logo

artechsio / ngx-sails-socketio Goto Github PK

View Code? Open in Web Editor NEW

This project forked from burntblark/ngx-sails-socketio

0.0 0.0 0.0 264 KB

An Angular module for connecting SailsJs backend through SocketIO.

License: MIT License

TypeScript 93.58% Shell 4.29% JavaScript 2.13%

ngx-sails-socketio's Introduction

ngx-sails-socketio

npm version Build Status Coverage Status

An Angular module for connecting to your SailsJs Backend/API through SocketIO.

Installation

npm i ngx-sails-socketio

Usage

Add SailsModule to your application module.

// Override options to configure connection to your sailsjs backend
const options: SailsOptions = {
   url: "ws://127.0.0.1:1337",
   prefix: "/api", // Optional uri prefix
   environment: SailsEnvironment.DEV,
   query: "__sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=windows&__sails_io_sdk_language=javascript",
   reconnection: true,
   autoConnect: false
};

@NgModule({
 declarations: [
   AppComponent
 ],
 imports: [
   SailsModule.forRoot(options, [AuthInterceptor,])
   ...
 ],
 providers: [],
 bootstrap: [AppComponent]
})

export class AppModule { }

An implementation of the AuthInterceptor class

@Injectable()
export class AuthInterceptor implements SailsInterceptorInterface {

   constructor(private router: Router, private jobs: JobsService) {
   }

   intercept(request: SailsRequestOptions, next: SailsInterceptorHandlerInterface): Observable<SailsResponse> {
       request.clone({
           headers: request.headers.set("Authorization", [token]) // Replace [token] with your auth token
       });
       const response = next.handle(request);

       return response.map(res => {
           if (res.getStatusCode() === 401) {
             // handle unauthorised request.
           }
           return res;
       });
   }
}

Inject the Sails into your components/services and instatiate classes specific to your objective.

class ExampleComponent implements OnInit {

 data: any;

 constructor(private sails: SailsClient) { }

 ngOnInit() {
   const req = new SailsRequest(this.sails);
   req.get('/model/action').subscribe(res => {
     this.data = res.getBody();
   });
 }

}

API

class SailsRequest

Makes a request for a resource to the configured sails server. Has methods that maps to the various supported RESTful API verbs.

Methods

  • get(url: string): Observable<SailsResponse>
  • post(url: string, data: any): Observable<SailsResponse>
  • put(url: string, data: any): Observable<SailsResponse>
  • patch(url: string, body: any): Observable<SailsResponse>
  • delete(url: string): Observable<SailsResponse>

class SailsSubscription

A class for subscribing to resourceful Pub-Sub events emitted from a sailsjs backend.

Methods

  • on(eventName: string): Observable<SailsEvent>
  • off(eventName: string): Observable<SailsEvent>

Example(s)

Under construction

class SailsEvent

A class representing an event on a resource from the server. This follows the structure as described in the sailsjs Pub-Sub event documentation.

class SailsQuery

A class for querying records from the server. Similar to SailsRequests but has methods mapping actions as detail on waterline api used by sailsjs.

interface SailsInterceptorInterface

An interface to construct an iterceptor to use for requests.

Example(s)

An authentication interceptor to set the Authorization header on every request.

@Injectable()
export class AuthInterceptor implements SailsInterceptorInterface {

    constructor(private router: Router) {
    }

    intercept(request: SailsRequestOptions, next: SailsInterceptorHandlerInterface): Promise<SailsResponse> {
        request.clone({
            headers: request.headers.set("Authorization", token)
        });
        
        const response = next.handle(request);

        return response.then(res => {
            if (res.getStatusCode() === 401) {
                this.router.navigateByUrl("login");
            }
            return res;
        });
    }
}

ngx-sails-socketio's People

Contributors

jogboms avatar burntblark avatar tleperou avatar

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.