Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.

Commit c2868a3

Browse files
committed
add dotenv better support and proxy server config
1 parent 363fbb8 commit c2868a3

File tree

8 files changed

+65
-15
lines changed

8 files changed

+65
-15
lines changed

gatsby-config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const postcssPresetEnv = require('postcss-preset-env');
22

3-
const dotenvParsed = require('./scripts/load-dotenv-config');
4-
console.log(`\n\n\n>> gatsby-config.js\n${JSON.stringify(dotenvParsed, null, 2)}\n\n\n`);
3+
const { activeEnv } = require('./scripts/load-dotenv-config');
54

65
const gatsbyConfig = {
76
siteMetadata: {
@@ -66,4 +65,14 @@ if (pathPrefix) {
6665
gatsbyConfig.plugins.push(`gatsby-plugin-offline`);
6766
}
6867

68+
const isDevelopment = activeEnv === 'dev' || activeEnv === 'development';
69+
const proxyUrl = process.env.PROXY_URL || undefined;
70+
if (isDevelopment && proxyUrl) {
71+
const proxyPrefix = process.env.API_PREFIX || 'api';
72+
gatsbyConfig.proxy = {
73+
prefix: `/${proxyPrefix}`,
74+
url: proxyUrl,
75+
};
76+
}
77+
6978
module.exports = gatsbyConfig;

gatsby-node.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const path = require('path');
22
const webpack = require('webpack');
33

4-
const dotenvParsed = require('./scripts/load-dotenv-config');
5-
console.log(`\n\n\n>> gatsby-node.js\n${JSON.stringify(dotenvParsed, null, 2)}\n\n\n`);
4+
const { dotenvParsed } = require('./scripts/load-dotenv-config');
65

76
exports.onCreateWebpackConfig = ({ actions }) => {
87
// make sure to have the process.env configs from the .env file available on the /src/* files
98
const plugins = [];
10-
// const { parsed: dotenvParsed } = dotenvConfig;
119
if (dotenvParsed) {
1210
const webpackDefinePluginConfig = {};
1311
Object.keys(dotenvParsed).forEach(key => {

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "gatsby-typescript-app-starter",
33
"private": true,
44
"description": "A simple typescript application starter to get up and developing quickly with Gatsby",
5-
"version": "0.2.0",
5+
"version": "0.3.0",
66
"author": "Erko Bridee <erko.bridee@gmail.com>",
77
"dependencies": {
88
"abortcontroller-polyfill": "^1.3.0",
@@ -41,6 +41,7 @@
4141
"eslint": "^5.16.0",
4242
"eslint-config-prettier": "^5.0.0",
4343
"eslint-plugin-prettier": "^3.1.0",
44+
"express": "^4.17.1",
4445
"fs-extra": "^8.1.0",
4546
"gatsby": "^2.13.41",
4647
"gatsby-image": "^2.2.7",
@@ -63,6 +64,7 @@
6364
"postcss-preset-env": "^6.6.0",
6465
"prettier": "^1.18.2",
6566
"react-intl-translations-manager": "^5.0.3",
67+
"request": "^2.88.0",
6668
"svgo": "1.2.2",
6769
"ts-jest": "^24.2.0",
6870
"typescript": "^3.5.2",
@@ -78,6 +80,8 @@
7880
"starter",
7981
"sass",
8082
"typescript",
83+
"dotenv",
84+
"proxy support",
8185
"jest",
8286
"unit tests",
8387
"unit tests coverage"
@@ -104,7 +108,7 @@
104108
"gatsby-clean": "gatsby clean",
105109
"gatsby-develop": "gatsby develop -H 0.0.0.0",
106110
"gatsby-build": "gatsby build --prefix-paths",
107-
"gatsby-serve": "gatsby serve -H 0.0.0.0",
111+
"gatsby-serve": "node scripts/proxied-http-server.js",
108112
"clean:build": "node scripts/fs/rm .build/",
109113
"clean:coverage": "node scripts/fs/rm coverage/",
110114
"clean": "npm-run-all -p clean:coverage clean:build fonticons:clean gatsby-clean",
@@ -114,7 +118,7 @@
114118
"develop": "npm-run-all -s base gatsby-develop",
115119
"start": "npm run develop",
116120
"build": "npm-run-all -s clean base unit-tests gatsby-build",
117-
"serve": "npm-run-all -s build gatsby-serve",
121+
"serve": "ACTIVE_ENV=development npm-run-all -s build gatsby-serve",
118122
"gh-deploy": "gh-pages -d public",
119123
"deploy": "PREFIX_PATH=true npm-run-all -s build gh-deploy"
120124
},

scripts/load-dotenv-config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ const dotenvConfig = dotenv.config({
99
path: dotenvPath,
1010
});
1111

12+
/*
1213
if (dotenvConfig.error) {
1314
console.log(`\n\n\ndotenv file not found: '${dotenvPath}'\n\n\n`);
1415
}
16+
*/
1517

1618
const { parsed: dotenvParsed } = dotenvConfig;
1719

1820
if (dotenvParsed) {
1921
console.log(`\n\ndotenv file '${dotenvPath}' loaded\n${JSON.stringify(dotenvParsed, null, 2)}\n\n`);
2022
}
2123

22-
module.exports = dotenvParsed;
24+
module.exports = {
25+
activeEnv,
26+
dotenvParsed,
27+
};

scripts/proxied-http-server.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
based on:
3+
https://github.com/gatsbyjs/gatsby/blob/2.13.30/packages/gatsby/src/commands/develop.js#L237
4+
*/
5+
const express = require('express');
6+
const request = require('request');
7+
8+
const gatsbyConfig = require('../gatsby-config');
9+
const { proxy } = gatsbyConfig;
10+
11+
const app = express();
12+
13+
app.use(express.static('public'));
14+
15+
if (proxy) {
16+
const { prefix, url } = proxy;
17+
app.use(`${prefix}/*`, (req, res) => {
18+
const proxiedUrl = url + req.originalUrl;
19+
req.pipe(
20+
request(proxiedUrl).on(`error`, err => {
21+
const message = `Error when trying to proxy request "${req.originalUrl}" to "${proxiedUrl}"`;
22+
23+
report.error(message, err);
24+
res.status(500).end();
25+
})
26+
).pipe(res);
27+
});
28+
}
29+
30+
const port = process.env.PORT || 9000;
31+
app.listen(port, () => console.log(`server started, listening on port ${port}`));

src/helpers/definitions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ export interface IRequestOptions {
150150
method?: RequestMethod;
151151
host?: string;
152152
api?: string;
153+
/** uses the values defined on the .env file for the host and api options */
154+
useDefaultPrefix?: boolean;
153155
urlPath: string;
154156
variables?: IDictionary;
155157
parameters?: IDictionary;

src/helpers/fetch/buildRequest.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ function buildResponseOutput<R extends IAPIResponse = TAPIResponse>(
4747
*/
4848
export async function buildRequest<R extends IAPIResponse = IAPIResponse>(options: IRequestOptions): Promise<R> {
4949
let { parameters } = options;
50+
const { useDefaultPrefix = false } = options;
5051
const {
5152
authorization,
5253
method = RequestMethod.GET,
5354
contentType = RequestContentTypes.JSON,
5455
protocol,
55-
host,
56-
api,
56+
host = useDefaultPrefix ? process.env.API_HOST || undefined : undefined,
57+
api = useDefaultPrefix ? process.env.API_PREFIX || undefined : undefined,
5758
urlPath,
5859
variables = {},
5960
headers: extraHeaders = {},

0 commit comments

Comments
 (0)