Skip to content

Commit 982ae06

Browse files
committed
be able to inject API_URL
1 parent 27b4ade commit 982ae06

File tree

10 files changed

+28
-30
lines changed

10 files changed

+28
-30
lines changed

config/webpack.aot.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const commonConfig = require('./webpack.common.js');
66
const ManifestPlugin = require('webpack-manifest-plugin');
77

88
const ENV = process.env.NODE_ENV = process.env.ENV = 'production';
9+
const API_URL = process.env.API_URL || '';
910

1011
module.exports = webpackMerge(commonConfig, {
1112
entry: {
@@ -22,10 +23,7 @@ module.exports = webpackMerge(commonConfig, {
2223
new ManifestPlugin(),
2324
new webpack.DefinePlugin({
2425
'ENV': JSON.stringify(ENV),
25-
'process.env': {
26-
'ENV': JSON.stringify(ENV),
27-
'NODE_ENV': JSON.stringify(ENV)
28-
}
26+
'API_URL': JSON.stringify(API_URL)
2927
}),
3028
new webpack.LoaderOptionsPlugin({
3129
minimize: true,

config/webpack.dev.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ const webpackMerge = require('webpack-merge');
44
const commonConfig = require('./webpack.common.js');
55

66
const ENV = process.env.ENV = process.env.NODE_ENV = 'development';
7+
const API_URL = process.env.API_URL || '';
78

89
module.exports = webpackMerge(commonConfig, {
910
devtool: 'cheap-module-source-map',
1011
output: {
1112
path: helpers.root('dist'),
1213
filename: '[name].js',
1314
sourceMapFilename: '[name].map',
14-
chunkFilename: '[id].chunk.js'
15+
chunkFilename: '[id].chunk.js',
16+
publicPath: '/'
1517
},
1618
plugins: [
1719
new webpack.DefinePlugin({
1820
'ENV': JSON.stringify(ENV),
19-
'process.env': {
20-
'ENV': JSON.stringify(ENV),
21-
'NODE_ENV': JSON.stringify(ENV)
22-
}
21+
'API_URL': JSON.stringify(API_URL)
2322
}),
2423
new webpack.LoaderOptionsPlugin({
2524
options: {

server.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ app.get('*', (req, res) => {
1919
});
2020

2121
app.listen(4200);
22+
console.log("server is running on http://localhost:4200");

src/app/core/core.module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {ProfileDataResolver} from "./services/profile-data.resolver";
1313

1414
export function createMyHttp(xhrBackend: XHRBackend, requestOptions: RequestOptions) {
1515
const ngHttp = new Http(xhrBackend, requestOptions);
16-
return new MyHttp(ngHttp);
16+
return new MyHttp(ngHttp, API_URL);
1717
}
1818

1919
@NgModule({

src/app/core/http/http.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
Headers
99
} from "@angular/http";
1010

11-
const mergeAuthToken = (options:RequestOptionsArgs) => {
11+
const mergeAuthToken = (options: RequestOptionsArgs) => {
1212
let newOptions = new RequestOptions({}).merge(options);
1313
let newHeaders = new Headers(newOptions.headers);
1414
newHeaders.set('x-auth-token', localStorage.getItem('jwt'));
@@ -19,31 +19,31 @@ const mergeAuthToken = (options:RequestOptionsArgs) => {
1919
@Injectable()
2020
export class MyHttp {
2121

22-
constructor(public http:Http) {
22+
constructor(private http: Http, private baseUrl: string = '') {
2323
}
2424

25-
get(url:string, options?:RequestOptionsArgs):Observable<Response> {
26-
return this.http.get(url, mergeAuthToken(options));
25+
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
26+
return this.http.get(this.baseUrl + url, mergeAuthToken(options));
2727
}
2828

29-
post(url:string, body:any, options?:RequestOptionsArgs):Observable<Response> {
30-
return this.http.post(url, body, mergeAuthToken(options));
29+
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
30+
return this.http.post(this.baseUrl + url, body, mergeAuthToken(options));
3131
}
3232

33-
put(url:string, body:any, options?:RequestOptionsArgs):Observable<Response> {
34-
return this.http.put(url, body, mergeAuthToken(options));
33+
put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
34+
return this.http.put(this.baseUrl + url, body, mergeAuthToken(options));
3535
}
3636

37-
delete(url:string, options?:RequestOptionsArgs):Observable<Response> {
38-
return this.http.delete(url, mergeAuthToken(options));
37+
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
38+
return this.http.delete(this.baseUrl + url, mergeAuthToken(options));
3939
}
4040

41-
patch(url:string, body:any, options?:RequestOptionsArgs):Observable<Response> {
42-
return this.http.patch(url, body, mergeAuthToken(options));
41+
patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
42+
return this.http.patch(this.baseUrl + url, body, mergeAuthToken(options));
4343
}
4444

45-
head(url:string, options?:RequestOptionsArgs):Observable<Response> {
46-
return this.http.head(url, mergeAuthToken(options));
45+
head(url: string, options?: RequestOptionsArgs): Observable<Response> {
46+
return this.http.head(this.baseUrl + url, mergeAuthToken(options));
4747
}
4848

4949
}

src/app/environment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let _decorateModuleRef = function identity<T>(value: T): T {
1313
return value;
1414
};
1515

16-
if ('production' === process.env.ENV) {
16+
if ('production' === ENV) {
1717
// Production
1818
disableDebugTools();
1919
enableProdMode();

src/app/pages/home/feed/feed.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Observable} from "rxjs/Observable";
22
import {Injectable} from "@angular/core";
3-
import {MyHttp} from "../../../core/http/http";
3+
import {MyHttp} from "../../../core/http";
44
import {Micropost} from "../../../core/domains";
55

66
const url = '/api/feed';

src/custom-typings.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ declare module '*';
66

77
// Extra variables that live on Global that will be replaced by webpack DefinePlugin
88
declare var ENV: string;
9-
declare var HMR: boolean;
9+
declare var API_URL: string;
1010
declare var System: SystemJS;
1111

1212
interface SystemJS {
@@ -15,7 +15,7 @@ interface SystemJS {
1515

1616
interface GlobalEnvironment {
1717
ENV: string;
18-
HMR: boolean;
18+
API_URL: boolean;
1919
SystemJS: SystemJS;
2020
System: SystemJS;
2121
}

src/polyfills.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import "core-js/es7/reflect";
1919
import "zone.js/dist/zone";
2020
import "ts-helpers";
2121

22-
if ('production' === process.env.ENV) {
22+
if ('production' === ENV) {
2323
} else {
2424
Error['stackTraceLimit'] = Infinity;
2525
require('zone.js/dist/long-stack-trace-zone');

src/vendor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require("!style!css!./vendor.css");
1111

1212
toastr.options.preventDuplicates = true;
1313

14-
if ('production' === process.env.ENV) {
14+
if ('production' === ENV) {
1515
} else {
1616
}
1717

0 commit comments

Comments
 (0)