Skip to content

Commit 1306ffe

Browse files
committed
Updated proxy to redirect api-calls to localhost:3000
1 parent 6a37969 commit 1306ffe

File tree

7 files changed

+72
-48
lines changed

7 files changed

+72
-48
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"version": "0.0.0",
44
"scripts": {
55
"ng": "ng",
6-
"start": "ng serve --proxy-config proxy.conf.js",
6+
"json:server": "json-server --watch mocks/data.json",
7+
"start": "ng serve --proxy-config proxy.conf.json",
78
"build": "ng build --prod",
89
"test": "ng test",
910
"lint": "ng lint",
10-
"e2e": "ng e2e",
11-
"json:server": "json-server --watch mocks/data.json"
11+
"e2e": "ng e2e"
1212
},
1313
"private": true,
1414
"dependencies": {

proxy.conf.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

proxy.conf.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"/api/*": {
3+
"target": "http://localhost:3000",
4+
"secure": false,
5+
"logLevel": "debug",
6+
"pathRewrite": {
7+
"^/api": ""
8+
}
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { ApiService } from './api.service';
4+
5+
describe('ApiService', () => {
6+
beforeEach(() => TestBed.configureTestingModule({}));
7+
8+
it('should be created', () => {
9+
const service: ApiService = TestBed.get(ApiService);
10+
expect(service).toBeTruthy();
11+
});
12+
});

src/app/core/services/api.service.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
2+
import { Injectable } from '@angular/core';
3+
import { environment as env } from '@env/environment';
4+
import { Observable, throwError } from 'rxjs';
5+
import { catchError } from 'rxjs/operators';
6+
7+
const BASE_URL = env.serverUrl;
8+
9+
@Injectable({
10+
providedIn: 'root'
11+
})
12+
export class ApiService {
13+
private options = { headers: new HttpHeaders().set('Content-Type', 'application/json') };
14+
15+
constructor(private httpClient: HttpClient) {}
16+
17+
public get(path: string, params: HttpParams = new HttpParams()): Observable<any> {
18+
return this.httpClient.get(BASE_URL + path, { params }).pipe(catchError(this.formatErrors));
19+
}
20+
21+
public put(path: string, body: object = {}): Observable<any> {
22+
return this.httpClient
23+
.put(BASE_URL + path, JSON.stringify(body), this.options)
24+
.pipe(catchError(this.formatErrors));
25+
}
26+
27+
public post(path: string, body: object = {}): Observable<any> {
28+
return this.httpClient
29+
.post(BASE_URL + path, JSON.stringify(body), this.options)
30+
.pipe(catchError(this.formatErrors));
31+
}
32+
33+
public delete(path: string): Observable<any> {
34+
return this.httpClient.delete(BASE_URL + path).pipe(catchError(this.formatErrors));
35+
}
36+
37+
public formatErrors(error: any): Observable<any> {
38+
return throwError(error.error);
39+
}
40+
}

src/app/core/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './auth.service';
22
export * from './json-api.service';
33
export * from './project.service';
4+
export * from './api.service';

src/app/core/services/project.service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ import { JsonApiService } from './json-api.service';
44
import { Observable } from 'rxjs';
55

66
import { Project } from '../models/project.model';
7+
import { ApiService } from './api.service';
78

89
const routes = {
9-
projects: '/projects.json'
10+
projects: '/projects'
1011
};
1112

1213
@Injectable({
1314
providedIn: 'root'
1415
})
1516
export class ProjectService {
1617

17-
constructor(private jsonApiService: JsonApiService) {}
18+
constructor(
19+
private jsonApiService: JsonApiService,
20+
private apiService: ApiService) {}
1821

1922
getAll(): Observable<Project[]> {
20-
return this.jsonApiService
21-
.fetch(routes.projects);
23+
return this.apiService.get(routes.projects);
2224
}
2325

2426
}

0 commit comments

Comments
 (0)