0.2.2 • Published 4 years ago

@lemmings/http-client v0.2.2

Weekly downloads
1
License
-
Repository
-
Last release
4 years ago

HttpClient

NOTE: Sorry i won't support on this library anymore. You can go to @pogojs/angular-http-client library for support and feature updates.

Table of contents

Warning

The library works well with Angular 10 and Typescript versions >= 3.8.

Getting started

Step 1: Install @lemmings/http-client:

NPM

npm install --save @lemmings/http-client

YARN

yarn add @lemmings/http-client

Step 2: Add new variable environment.ts:

export const environment = {
    production: false,
    domainUrl: 'https://jsonplaceholder.typicode.com/',
    loginUrl: 'router navigate login (ex: /login)',
    accessToken: 'localStorage token key (ex: token_key)',
    apiUrl: { // API url
        baseUrl: 'https://jsonplaceholder.typicode.com/',
        primaryUrl: '?optional',
        secondaryUrl: '?optional',
        tertiaryUrl: '?optional',
        quaternaryUrl: '?optional',
    },
};

NOTE: 
domainUrl: required
loginUrl: required
accessToken: required
baseUrl: required

Step 3: app.module:

import { HttpClientModule } from '@lemmings/http-client';

@NgModule({
  declarations: [AppComponent],
  imports: [HttpClientModule.forRoot(environment),],
  bootstrap: [AppComponent]
})
export class AppModule {}

Step 4: app.service.ts:

import { Injectable } from '@angular/core';
import { HttpClientService } from '@lemmings/http-client';

@Injectable()
export class AppService {
    constructor(private http: HttpClientService) {

    }

    getPosts(params: any) {
        const uri = ['posts'].join('/');
        return this.http.getNotToken(uri, params);
    }
    
    createPosts() {
    	
    }
}

Step 5: app.component.ts:

import { Component, OnInit }      from '@angular/core';
import { AppService }             from './app.service.ts';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
    providers: [AppService]
})
export class AppComponent implements OnInit {
    constructor(private service: AppService) {
    }

    ngOnInit(): void {
        this.getPosts();
    }
    
    private getPosts() {
        const params = '';
        
        this.service.getPosts(params)
            .subscribe((res) => {
                console.log('res', res);
            }
        );
    }
    
    // Posts with FormData
    public createPosts() {
    	const fileUpload = event.target.files || []; // you can handle get File or Multiple File upload
    	const data = { ...this.generalForm.value };
    	
    	const params = {
    		data: data,
    		file: fileUpload,
    	};
    	
    	// You just need to create params
        this.service.createPosts(params).subscriber((res) => {
        	// Handle code success here!
        }, (err) => {
        	// Handle code error here! 
        })
    }
}

API Method

NameMethodTokenParamsDescription
getgetdefaultpath:uri params:? optional options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }get method options: for url from another repo api with by default primaryUrl.
getNotTokengetnopath:uri params:? optional options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }get method not send token options: for url from another repo api with by default primaryUrl.
getWithParamsTokengetdefaultpath:uri token: token params:? optional options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }get method options: for url from another repo api with by default primaryUrl. ex: https://jsonplaceholder.typicode.com/posts?token=token
-----------------------------------------------------------------
postpostdefaultpath:uri body: data options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }post method
postNotTokenpostnopath:uri body: data options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }post method
postWithParamsTokenpostdefaultpath:uri body: data token: token options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }post method ex: https://jsonplaceholder.typicode.com/posts?token=token
postWithFormDatapostdefaultpath:uri body: data options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }post method
-----------------------------------------------------------------
putputdefaultpath:uri body: data options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }put method
putNotTokenputnopath:uri body: data options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }Not support
putWithParamsTokenputdefaultpath:uri body: data token: token options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }put method ex: https://jsonplaceholder.typicode.com/posts?token=token
putWithFormDataputdefaultpath:uri body: data options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }put method
-----------------------------------------------------------------
deletedeletedefaultpath:uri options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }delete method
deleteNotTokendeletenoNot DefinedNot support
deleteWithParamsTokendeletedefaultpath:uri token: token options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }delete method ex: https://jsonplaceholder.typicode.com/posts?token=token
deleteWithParamsdeletedefaultpath:uri body: data options:? { apiUrl: primaryUrl or secondaryUrl or tertiaryUrl or quaternaryUrl }delete method

Contributing

Contributions are welcome. You can start by looking at issues with label Help wanted or creating new Issue with proposal or bug report. Note that we are using https://conventionalcommits.org/ commits format.

Inspiration

This component is inspired by HttpClient. Check theirs amazing work and components :)

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

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