Reading application settings from JSON file in angular application - json

I have implemented the application configuration to be read from JSON file from my angular application so that it is easily configurable post deploment.
We use Octopus deployment tool to deploy the angular application. As you are aware the having he enviornment.ts file gets generated as js file and it
is very difficult to manipulate the settings in Octopus by reading the JS file. Hence I have created config.JSON file that would be outputted to the dist folder and
Octopus deploy can consume the JSON file to make changes whenever needed.
Currently the application is reading values from enviornment ts file which in turn is reading the values set in the Config.JSON file.
I would like to know if my approach is correct ?
Config.JSON
{
"settings": {
"production": false,
"userIdleMinutes": "10",
"corePingIntervalSeconds": "10",
"baseUrl": "http://localhost:57973",
"loginUrl": "/Login",
"adminUrl": "/Admin"
}
}
enviornment.ts
import config from '../assets/config.json'
// The file contents for the current environment will overwrite these during build.
// The build system defaults to the dev environment which uses `environment.ts`, but if you do
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.
export const environment = {
production: config.settings.production,
baseUrl: config.settings.baseUrl,
loginUrl:config.settings.loginUrl,
adminUrl: config.settings.adminUrl,
userIdleMinutes: config.settings.userIdleMinutes,
corePingIntervalSeconds: config.settings.corePingIntervalSeconds
};

if you are using octopus, instead of doing that, in your environment.prod.ts add some tokens and replace them on each environment by octopus
(right now I don't have access to show you how, but something like below)
environment.prod.ts
export const environment = {
production: #{production},
baseUrl: "#{baseUrl}",
//... rest of your configs
};
then add a step to replace these variables with the variables defined in environment in main.*.js file on each deployment

Related

How to serve a json file with parcel without bundling it?

I'm making a project with parcel and typescript. It works great, but for one library I'm using, I would need to host a bunch of .json-files at a fixed directory:
The files look as following:
index.html
index.ts
tiles/
| file.json
| file0.json
| subdirectory with *.json
In package.json, I include them as parcel *.html tiles/* (for start) and parcel build index.html tiles/*, but this results in the json files to be build into some .js file. I however need them to be served as is.
Any hints on how to tell parcel not to bundle them?
There is a npm-package doing exactly this: https://www.npmjs.com/package/parcel-plugin-static-files-copy
npm install -D parcel-plugin-static-files-copy to install (for development only)
Then, in package.json, add:
"staticFiles": {
"staticPath": [
{
"staticPath": "tiles", -- copy all files from this directory at the root from your project...
"staticOutDir": "tiles/" -- ... to this directory in dist/, so it becomes dist/tiles/<files>
}
]
}
```
Note that you have to define a `staticPath` in the list `staticPath`, this is a bit confusing at first.
You can override the transformer plugin used for JSON files in the .parcelrc file. It defaults to using #parcel/transformer-json, but you can use #parcel/transformer-raw to treat JSON files as assets.
.parcelrc
{
"extends": "#parcel/config-default",
"transformers": {
"*.json": ["#parcel/transformer-raw"]
}
}
If you're using Parcel 2, you can do something like this:
import jsonUrl from 'url:./data.json'
// Fetch the file asynchronously from the URL
fetch(jsonUrl)
.then(response => response.json())
.then(json => ...)
... etc. ...

How to ng build angular with json so that after ng build, json values can be replaced manually to show different results?

If not possible any alternative to ng build?
Problem is in the build json values are embeded in main.js making impossible to alter json values later. I want to create a report in HTML with a json file format using angular which can be viewed offline. So later when i wish to change value, i only need to alter value in json file.
You want to create a config file, add the file in angular.json so that it's included in the dist folder when your app is built, then add a couple of properties to the tsconfig file so that your typescript knows how to import a json file and treat it as a module.
I wrote a detailed blog post walking you through how to do it step-by-step and how it works. You can find that here => https://dev.to/kylerjohnson26/build-your-angular-app-once-deploy-anywhere-5ggk
This will allow you to easily interact with your json config values from your code simply by importing it wherever you want to use it. Most of all, most deployment tools (like Octopus) have native support for json config files so you can easily replace values based on which environment you're deploying to. This is the simplest way to setup your config so that you can "build once and deploy anywhere".
a way is to add your json in assets in your angular-json file
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico",
"myJson.json",
"web.config"
],
here is supposed that the file is in same folder of the main. If you put it in assets folder, you can try user the path "./assets/myJson.json",
in this way, after build, the file will be left and you can edit it.
One approach would be to store your JSON in the assets folder and then fetch it with an HTTP GET. Then when your app is deployed, you can simply swap out the JSON file for a different one.
Store the json - assets/test.json:
{
"message": "hello"
}
Get the JSON with HttpClient:
#Component({
selector: 'my-app',
templateUrl: './my-app.component.html'
})
export class MyAppComponent implements OnInit {
constructor(private http: HttpClient) {
}
ngOnInit() {
this.http.get('/assets/test.json').subscribe(resp => {
alert(resp['message']);
});
}
}
Here's a stackblitz demonstrating this.

Proper way to include lists and other external data inside a TypeScript React App

I created a React App using the create-react-app npm package. Everything works fine, however I'm not sure how to handle external files.
I created a POC that holds a json object of the entries for a select field. I can import the object and it works properly. However, I'm used to having files like this available on the server so that they can be edited without having to re-build the program.
I converted the file to a .json and I'm able to import it without a problem. However, if I try to move it to the "public" folder, I'm no longer able to import it. If it remains within the src folder it gets included within the bundle and I'm not able to edit it directly.
Is including the file within the bundle the standard way of handling data that can change (whether by requiring update or i18n)??? If not, how do I go about configuring the app to allow me to import it from the public folder once it's deployed?
This should work:
Add data.json to public directory and use fetch in componentDidMount
componentDidMount() {
fetch('data.json').then(data => {
data.json().then(response => {
console.log(response)
this.setState({ data }); // you can set data to state
});
})
}
content of data.json
{
"data": "some data"
}
If you are trying to use some external data e.g. from server, you should not import it like any other files (i.e. import a.js from './a') you should make HTTP request in order to retrieve these data

In Angular, imported json file as indexed type always return undefined

You will find instructions to reproduce on your own device at the bottom.
I have a basic Angular project I created using Angular CLI, running on TypeScript 3.1.3, with nothing much added aside a class and a json file.
I created a class ResourcesService with the following command with Angular CLI:
ng generate service Resources
I'm basically using it to load json files, as a mean of localising (instead of using Angular unfinished builtin internationalisation features).
The following is my class, as well as the json file:
ResourcesBundle.json
{
"label.changeLanguage": "Change language",
"label.education": "Education",
"label.experience": "Experiences",
"label.skills": "Skills",
"label.summary": "Summary",
"label.language.english": "English",
"label.language.french": "French"
}
resources.service.ts
import * as resources from '../assets/resources/ResourcesBundle.json';
#Injectable({
providedIn: 'root'
})
export class ResourcesService {
constructor() {}
public getString(label: string): string {
let resource: string = resources[label];
return resource;
}
}
Of course, in order to be able to import the json file that way, I've set "resolveJsonModule": true in tsconfig.json.
The service by itself is working properly. I can inject it and call the getString method, and it's running without any error.
However, no matter what value I pass to the getString method, the returned value is always undefined. I've even tried to hard code the value for label = 'label.summary', but it's still returning undefined. The only time it's working properly is when I write the string directly between the brackets:
let resource: string;
label = 'label.summary';
resource = resources[label]; // resource == undefined
resource = resources['label.summary']; // resource == 'Summary'
Within the TS on VSCode, the content of resources is as following:
label.changeLanguage
label.education
label.experience
label.language.english
label.language.french
label.skills
label.summary
When using console.log(resources), the console was displaying something like this on Firefox:
Object {
label.changeLanguage: "Change language"
label.education: "Education"
label.experience: "Experience"
label.language.english: "English"
label.language.french: "French"
label.skills: "Skills"
label.summary: "Summary"
}
So the json is properly loaded, but it can only be used with hard coded string.
The only other solution I found was to give up the json file and initialise an indexed type directly in the code:
private resources: { [key: string]: string } = {
'label.changeLanguage': 'Change language',
'label.education': 'Education',
'label.experience': 'Experiences',
'label.skills': 'Skills',
'label.summary': 'Summary',
'label.language.english': 'English',
'label.language.french': 'French'
};
However, I don't think that's a good approach, at all...
In the case of the imported json file, why does it always return undefined when I use a variable? Or otherwise, why does it work only with a hard coded string between the brackets?
Edit:
You will find below a stackblitz link to a demo project:
https://stackblitz.com/edit/angular-h2aspf?file=tsconfig.json
If you run it on the browser, it will work properly (the console will properly display Change language).
However, if you download it and run it locally, you will notice that the console will display undefined instead.
To run it locally:
You must have npm and Angular CLI
Download and unzip the stackblitz demo in a folder
Run npm i in the project folder
Run ng serve --open
Open the console on your browser, it should be displaying undefined, instead of the expected value (Change language on stackblitz)
Edit:
According to a comment on the Angular CLI issue, a workaround is to set "esModuleInterop": true in tsconfig.json, and to change the import statement from:
import * as resources from '../assets/resources/ResourcesBundle.json';
To this:
import resources from '../assets/resources/ResourcesBundle.json';
Original answer:
After checking multiple times on different devices, I think this is a bug directly related to Angular (current version: 7.0.2).
To take the example I gave in the question again:
https://stackblitz.com/edit/angular-h2aspf?file=tsconfig.json
On the browser, this demo is outputting Change language on the console.
On locale device:
Download and unzip the stackblitz demo in a folder
Run npm i in the project folder
If you run with ng serve, you will notice undefined in the web browser console
Stop Angular, then run again with ng serve --prod. The web browser console is now properly outputting Change language
I've opened the following issues for Angular and Angular CLI projects on GitHub for this problem:
Angular: Issue #26785: Imported json file as indexed type always giving undefined when Angular is not running in production mode
Angular CLI: Issue #12781: Imported json file as indexed type always giving undefined, but not when running ng serve --prod

Module.exports vs plain json for config files

I see multiple ways to create config files in Node.js. One uses module.exports in js file, one just use plain json object.
// config1.js
module.exports = {
config_1: "value 1",
config_2: "value 2"
}
// config2.json
{
"config_1": "value 1",
"config_2": "value 2"
}
Is there any advantages of using module.exports in config file? What are the differences?
javascript CommonJS Module
comments
conditionals
loops and such to populate defaults
code to change config based on NODE_ENV or similar
code to look for external files for SSL keys, API credentials, etc
easier to have fallbacks and defaults
JSON file
easy to parse and update with external tools
compatible with pretty much every programming language out there
pure data that can be loaded without being executed
easy to pretty print
JSON could start as the basis and all the code items described above about CommonJS module could live in a config.js module that reads config.json as it's starting point
So I always start with a commonjs module for the convenience, but keep any logic in there simple. If your config.js has bugs and needs tests, it's probably too complicated. KISS. If I know for a fact other things are going to want poke around in my config, I'll use a JSON file.
Thanks #jonathan-ong, looks like config.js (NOT JSON file) works as expected and I could put some comments.
module.exports = {
// Development Environment
development: {
database: {
host: '127.0.0.1',
login: 'dev',
password: 'dev'
}
},
// Production Environment
production: {
database: {
host: '127.0.0.1',
login: 'prod',
password: 'prod'
}
}
};
js files have their own perks as #Peter Lyons mentioned. But if I don't have to access external resources for API keys and etc. I would prefer JSON for config files.
Simple reason being I would not have to touch my code for just for the sake of making changes to config files. I can simply make an API to edit these json files to add,update or remove any configuration key-value pair. Even add whole new config file for a separate environment using an API.
And use config module to consume these configurations