Skip to content

Add environment variable for API data #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@babel/preset-env": "^7.11.5",
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-node-resolve": "^8.0.0",
"@rollup/plugin-replace": "^2.4.2",
"@rollup/plugin-typescript": "^6.0.0",
"@storybook/addon-actions": "^6.2.8",
"@storybook/addon-essentials": "^6.2.8",
Expand Down
14 changes: 13 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import replace from '@rollup/plugin-replace';

const production = !process.env.ROLLUP_WATCH;

Expand Down Expand Up @@ -74,7 +75,18 @@ export default {

// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser()
production && terser(),

replace({
process: JSON.stringify({ //Use this in Svelte
env: {
isProd: production
}
}),
globalThis: JSON.stringify({ //Use this in TS
__IS_PROD__: production
})
})
],
watch: {
clearScreen: false
Expand Down
5 changes: 4 additions & 1 deletion src/data/getCurrentQuestions/getCurrentQuestions.request.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { getCurrentQuestionsExample } from '../getCurrentQuestions'

const IS_PROD = globalThis.__IS_PROD__

// CHANGE PROMISE TO QUESTION TYPE WHEN LEVEL - DIFFICULTY IS FIXED
const getCurrentQuestions = async (): Promise<any[]> => {
// eslint-disable-next-line no-constant-condition
if (false) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO

if (IS_PROD) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO
return fetch('http://127.0.0.1:8001/api/codechallenges/current/questions/', {
headers: {
'Content-Type': 'application/json',
Expand Down
4 changes: 3 additions & 1 deletion src/data/getExpiredQuestions/getExpiredQuestions.request.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { getExpiredQuestionsExample } from '../getExpiredQuestions'

const IS_PROD = globalThis.__IS_PROD__

// CHANGE PROMISE TO QUESTION TYPE WHEN LEVEL - DIFFICULTY IS FIXED
const getExpiredQuestions = async (): Promise<any[]> => {
// eslint-disable-next-line no-constant-condition
if (false) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO
if (IS_PROD) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO
return fetch('http://127.0.0.1:8001/api/codechallenges/expired/questions/', {
headers: {
'Content-Type': 'application/json',
Expand Down
4 changes: 3 additions & 1 deletion src/data/getQuestion/getQuestion.request.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Question } from '../../types'
import { getQuestionsExample } from '../getQuestions'

const IS_PROD = globalThis.__IS_PROD__

const getQuestion = async (questionId: number): Promise<Question> => {
// eslint-disable-next-line no-constant-condition
if (false) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO
if (IS_PROD) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO
return fetch('http://127.0.0.1:8001/api/codechallenges/questions/' + questionId, {
headers: {
'Content-Type': 'application/json',
Expand Down
4 changes: 3 additions & 1 deletion src/data/getQuestions/getQuestions.request.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Question } from '../../types'
import { getQuestionsExample } from '../getQuestions'

const IS_PROD = globalThis.__IS_PROD__

const getQuestions = async (): Promise<Question[]> => {
// eslint-disable-next-line no-constant-condition
if (false) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO
if (IS_PROD) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO
return fetch('http://127.0.0.1:8001/api/codechallenges/questions/', {
headers: {
'Content-Type': 'application/json',
Expand Down
7 changes: 7 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {}

declare global {
// eslint-disable-next-line no-var
// eslint-disable-next-line no-unused-vars
var __IS_PROD__:string
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason why this has to be a var and not let or const?

}
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,14 @@
is-module "^1.0.0"
resolve "^1.17.0"

"@rollup/plugin-replace@^2.4.2":
version "2.4.2"
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.4.2.tgz#a2d539314fbc77c244858faa523012825068510a"
integrity sha512-IGcu+cydlUMZ5En85jxHH4qj2hta/11BHq95iHEyb2sbgiN0eCdzvUcHw5gt9pBL5lTi4JDYJ1acCoMGpTvEZg==
dependencies:
"@rollup/pluginutils" "^3.1.0"
magic-string "^0.25.7"

"@rollup/plugin-typescript@^6.0.0":
version "6.1.0"
resolved "https://registry.yarnpkg.com/@rollup/plugin-typescript/-/plugin-typescript-6.1.0.tgz#289e7f0ea12fd659bd13ad59dda73b9055538b83"
Expand Down Expand Up @@ -7714,7 +7722,7 @@ lz-string@^1.4.4:
resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26"
integrity sha1-wNjq82BZ9wV5bh40SBHPTEmNOiY=

magic-string@^0.25.2:
magic-string@^0.25.2, magic-string@^0.25.7:
version "0.25.7"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==
Expand Down