0.1.7 • Published 11 months ago

ngxs-requests-plugin v0.1.7

Weekly downloads
73
License
MIT
Repository
github
Last release
11 months ago

NgxsRequestsPlugin

NgxsRequestsPlugin is used to store the state of the request and response from the server into the request state

Installation

npm install ngxs-requests-plugin --save

or if you are using yarn\ yarn add ngxs-requests-plugin

Connection

Include NgxsRequestsPluginModule.forRoot() in the same place where you use NgxsModule.forRoot

import { NgxsRequestsPluginModule } from 'ngxs-requests-plugin';

@NgModule({
  imports: [
    NgxsModule.forRoot([...]),
    NgxsRequestsPluginModule.forRoot(),
  ],
})
export class NgxsStoreModule {
}

Using

  • Create an empty class with the RequestState decorator

      import { Injectable } from '@angular/core';
      import { RequestState } from 'ngxs-requests-plugin';
    
      @Injectable()
      @RequestState('signIn')
      export class SignInRequestState {
      }

    The argument of RequestState decorator will be use as a name of the requests state slice. Note: The argument is a required and must be unique for the entire application.

  • Use CreateRequestAction for request creation

    @State<AuthStateModel>({
      name: 'auth',
      defaults: {
        token: null,
      },
    })
    export class AuthState implements NgxsAfterBootstrap {  
      constructor(
        private httpClient: HttpClient
      ) {
      }
    
      @Action(SignIn)
      signIn(ctx: StateContext<AuthStateModel>, action: SignIn) {
        const request = this.httpClient.post('serverUrl/signIn', {});
    
        return ctx.dispatch(new CreateRequestAction({
          state: SignInRequestState,
          request,
          successAction: SignInSuccess,
          failAction: SignInFail,
        }));
      }
    
      @Action(SignInSuccess)
      signInSuccess(ctx: StateContext<AuthStateModel>, action: SignInSuccess) {
        console.log('SignInSuccess');
      }
    
      @Action(SignInFail)
      signInFail(ctx: StateContext<AuthStateModel>, action: SignInSuccess) {
        console.log('SignInFail');
      }
    }

    CreateRequestAction parameters:

    • request - required field. Usually, it's observable returned from the HttpClient
    • state - required field. Class with RequestState decorator
    • successAction - action, which will be called on the successful request
    • failAction - action, which will be called if the request responded with an error
    • metadata - additional data that can be received in successAction and failAction
  • To get the request data and its state, use the Select decorator. Pass the previously created class with the RequestState decorator as an argument toSelect

    @Injectable({
      providedIn: 'root',
    })
    export class AuthService {
      @Select(SignInRequestState)
      signInRequestState$: Observable<IRequest>;
    }

    Also you need to include your class with RequestState decorator in NgxsRequestsPluginModule

    import { NgxsRequestsPluginModule } from 'ngxs-requests-plugin';
    
    @NgModule({
      imports: [
        NgxsModule.forRoot([...]),
        NgxsRequestsPluginModule.forRoot([
          SignInRequestState
        ]),
      ],
    })
    export class NgxsStoreModule {
    }
0.1.7

11 months ago

0.1.7-beta.1

1 year ago

0.1.7-beta.2

1 year ago

0.1.7-beta.0

1 year ago

0.1.6-beta.0

2 years ago

0.1.6

2 years ago

0.1.5-beta.1

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

4 years ago

0.1.0-beta.5

4 years ago

0.1.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0-beta.3

4 years ago

0.1.0-beta.2

4 years ago

0.1.0-beta.4

4 years ago

0.1.0-beta.1

4 years ago

0.1.0-beta.0

4 years ago

0.0.17-beta.2

4 years ago

0.0.17-beta.1

4 years ago

0.0.17-beta.0

4 years ago

0.0.16-beta.0

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6-beta.0

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago