Skip to content

Instantly share code, notes, and snippets.

@alkaj
Last active August 9, 2020 21:18
Show Gist options
  • Save alkaj/151ae805bd8ddc8b706d2d6686bbc403 to your computer and use it in GitHub Desktop.
Save alkaj/151ae805bd8ddc8b706d2d6686bbc403 to your computer and use it in GitHub Desktop.
Angular Remote service helper
import { RemoteHelper } from './remote';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ROUTE } from './routes';
const {iget, ipost, iput, idelete } = RemoteHelper;
const headers = new HttpHeaders(
{headers: ['Content-Type: application/json']}
)
@Injectable()
export class RemoteService {
constructor(private http: HttpClient) { }
/**
* Implement the helpers into this remote service
*/
/** GET service */
get(endpoint: ROUTE, params: string) {
return iget(this.http, headers, endpoint, params);
}
/** POST service */
post(endpoint: ROUTE, body: any, params: string = null) {
return ipost(this.http, headers, endpoint, body, params);
}
/** PUT service */
put(endpoint: ROUTE, body: any, params: string = null) {
return iput(this.http, headers, endpoint, body, params);
}
/** DELETE service */
delete(endpoint: ROUTE, item: string|number) {
return idelete(this.http, headers, endpoint, item);
}
/** Start calling the remote api */
// signup(user: User) {
// return this.post(ROUTE.API_SIGNUP, user);
// }
//
// deleteMessage(id: number) {
// return this.delete(ROUTE.API_MESSAGES, id);
// }
}
import { environment } from 'src/environments/environment';
import { HttpClient, HttpHeaders } from '@angular/common/http';
export class RemoteHelper {
static params(p: string) {
return p ? '?' + p.split(',').join('&') : ''; // Expecting query parameters in a string like 'id=3,level=8,page=1'
}
static path(endpoint: string) {
return environment.api_base_url + '/' + environment.api_version + '/' + endpoint;
}
static iget(http: HttpClient, options: HttpHeaders, endpoint: string, params: string) {
return http.get(RemoteHelper.path(endpoint) + RemoteHelper.params(params), {headers: options});
}
static ipost(http: HttpClient, options: HttpHeaders, endpoint: string, body: any, params = null) {
return http.post(RemoteHelper.path(endpoint) + RemoteHelper.params(params), body, {headers: options});
}
static idelete(http: HttpClient, options: HttpHeaders, endpoint: string, item: string|number) {
return http.get(RemoteHelper.path(endpoint) + '/' + item, {headers: options});
}
static iput(http: HttpClient, options: HttpHeaders, endpoint: string, body: any, params = null) {
return http.post(RemoteHelper.path(endpoint) + '/' + body.id + RemoteHelper.params(params), body, {headers: options});
}
}
/** List of routes invoked or navigated to in this app module */
export enum ROUTE {
/** Navigation */
BLANK = '',
HOME = 'home',
ABOUT = 'about',
/** Api calls */
API_SIGNUP = 'users',
API_MESSAGES = 'messages'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment