1.2.16 • Published 16 days ago

ngx-coffee v1.2.16

Weekly downloads
-
License
-
Repository
-
Last release
16 days ago

Installing

$ npm install --save ngx-coffee

Quickstart

Import ngx-coffee module in Angular app.

With default config options

import { CoffeeModule } from 'ngx-mask'

@NgModule({
  imports: [
     CoffeeModule.forRoot({
      baseApiUrl: 'https://localhost:5001/api',
      auth: {
        linkedIn: {
            clientId: 'your_client_id',
            scope: 'r_emailaddress,profile,openid,email,r_liteprofile',
            redirectUrl: 'http://localhost:4200/redirecionar'
        }
      }
    }),
  ],
})

Usage

inject "CoffeeService" in constructor

import { CoffeeService } from 'ngx-coffee';

export class PublicComponent implements OnInit {

    constructor(
        private coffeeService: CoffeeService
    ) { }

    ngOnInit(): void {
    }
}

Auth service options

Authenticate with linkedin or LoginPassword

import { CoffeeService } from 'ngx-coffee';
import { ActivatedRoute } from '@angular/router';

export class PublicComponent implements OnInit {

    constructor(
        private coffeeService: CoffeeService,
        private activatedRoute: ActivatedRoute
    ) { }

    ngOnInit(): void {
    }

    signInWithLinkedin(): void {

        this.coffeeService.auth().social.signInWithLinkedIn()
        .subscribe({
            next: () => {
                // do something here
            },
            error: () => {
                // handle error
            }
        });
    }

    // after calling "signInWithLinkedIn()" call "validateLinkedInSignIn(code)" to validate the'code'
    // from the 'redirectUri'
    ngOnInit(): void {
        const code = this.activatedRoute.snapshot.queryParamMap.get('code');

        if(code) {
            this.coffeeService.auth().social.validateLinkedInSignIn(code)
            .subscribe({
                next: () => {
                    window.close();
                },
                error: () => {
                    swindow.close();
                }
            });
        }
    }

    siginWithLoginPassword(): void {
        const model = {
            login: "myemail@email.com",
            password: "123"
        };

        this.coffeeService.auth().siginWithLoginPassword(model)
        .subscribe({
            next: () => {
                // do something here
            },
            error: (error) => {
                // handle error
            }
        });
    }
}

Check if user is authenticated, and get the current user data

import { CoffeeService } from 'ngx-coffee';
import { User } from 'src/app/models/user';

export class PublicComponent implements OnInit {
  currentUser: User;

    constructor(
        private coffeeService: CoffeeService
    ) { }

    ngOnInit(): void {
        this.isAuthenticated();
    }

    private isAuthenticated(): void {
        this.coffeeService.auth<User>(User).isLoggedIn().subscribe({
            next: isAuthenticated => {
                this.getCurrentUser();
                // do something here
            },
            error: (error) => {
                // handle error
            }
        });
    }

    public getCurrentUser(): void {
        this.coffeeService.auth<User>(User).getCurrentUser()
        .subscribe(user => {
            this.currentUser = user;
        });

    }
}

Http services and filters

import { CoffeeService } from 'ngx-coffee';
import { User } from 'src/app/models/user';

export class PublicComponent implements OnInit {
  currentUser: User;

    constructor(
        private coffeeService: CoffeeService
    ) { }

    ngOnInit(): void {
        this.isAuthenticated();
    }

    private getData(): void {
        this.coffeeService
        .get<User>('/user')
        .whereContains((model: User) => model.name, "john")
        .where((model: User) => model.age, ">", 25)
        .returnPaginationListOf(User, {
            currentPage: 1,
            pageSize: 10,
        })
        .subscribe({
            next: (snapshot) => {
                // do something here
            },
            error: () => {
                // handle error
            },
        });
    }

    // it will create or update, depending on the model id
    private saveData(): void {
        const model {
            id: 0, // if 0 creates otherwise update
            name: 'John',
            age: 46
        };

        this.coffeeService.save('user', model)
        .subscribe({
            next: () => {
                // do something here
            },
            error: (error) => {
                // handle error
            }
        });
    }

    private createData(): void {
        const model {
            id: 0, // if 0 creates otherwise update
            name: 'John',
            age: 46
        };

        this.coffeeService.create('user', model)
        .subscribe({
            next: () => {
                // do something here
            },
            error: (error) => {
                // handle error
            }
        });
    }

    private updateData(): void {
        const model {
            id: 1, // if 0 creates otherwise update
            name: 'John',
            age: 46
        };

        this.coffeeService.update('user', model)
        .subscribe({
            next: () => {
                // do something here
            },
            error: (error) => {
                // handle error
            }
        });
    }

    private deleteData(): void {
        this.coffeeService.delete('user', 1)
        .subscribe({
            next: () => {
                // do something here
            },
            error: (error) => {
                // handle error
            }
        });
    }
}

Validate forms

force reactive forms validation

import { CoffeeService } from 'ngx-coffee';

export class PublicComponent implements OnInit {

    constructor(
        private coffeeService: CoffeeService
    ) { }
 
    ngOnInit(): void {
    }

    onSubmit(): void {
        const form = this.fb.group({ 
            name: [null, Validators.Required]
        });

        // will force form validation
        this.coffeeService.forms().validateForm(form);
    }
}

File Utils

transform Base64String to File or File to Base64String

import { CoffeeService } from 'ngx-coffee';

export class PublicComponent implements OnInit {

    constructor(
        private coffeeService: CoffeeService
    ) { }
 
    ngOnInit(): void {
    }

    // File to Base64String
    onNewImage(event): void {
        const file = event.target.files[0];
        const form = { file: file };

        this.coffeeService.fileUtils().fileToBase64(form)
        .subscribe({
            next: (base64String) => {
               console.log(base64String);
            },
            error: () => {}
        });
    }

    // Base64String to File
    onPictureTaken(base64String: string): void {
        const file = event.target.files[0];
        const form = { file: file };

        this.coffeeService.fileUtils().base64ToFile(base64String, 'profile.png')
        .subscribe( {
            next: (file) =>{
                // do something here
            },
            error: () => {
                // handle error
            }
        });
    }
}
1.2.16

16 days ago

1.2.13

20 days ago

1.2.15

20 days ago

1.2.11

1 month ago

1.2.10

1 month ago

1.2.6

1 month ago

1.2.5

2 months ago

1.2.0

2 months ago

1.1.0

2 months ago

1.0.8

2 months ago

1.0.10

2 months ago

1.0.5

2 months ago

1.0.2

2 months ago

1.0.1

2 months ago

1.0.0

2 months ago

0.0.51

3 months ago

0.0.50

3 months ago

0.0.48

3 months ago

0.0.49

3 months ago

0.0.40

3 months ago

0.0.41

3 months ago

0.0.42

3 months ago

0.0.45

3 months ago

0.0.46

3 months ago

0.0.47

3 months ago

0.0.37

4 months ago

0.0.38

4 months ago

0.0.34

4 months ago

0.0.35

4 months ago

0.0.36

4 months ago

0.2.0

4 months ago

0.0.32

7 months ago

0.0.25

11 months ago

0.0.30

11 months ago

0.0.26

11 months ago

0.0.27

11 months ago

0.0.28

11 months ago

0.0.29

11 months ago

0.0.20

1 year ago

0.0.21

1 year ago

0.0.22

1 year ago

0.0.23

1 year ago

0.0.24

12 months ago

0.0.17

1 year ago

0.0.18

1 year ago

0.0.19

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago