Skip to content

Commit

Permalink
adds canon_const_* vars to urlswap for generator apis
Browse files Browse the repository at this point in the history
  • Loading branch information
jhmullen authored and davelandry committed Jul 2, 2019
1 parent 527bdae commit 2c06950
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
25 changes: 24 additions & 1 deletion packages/cms/src/api/mortarRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@ const catcher = e => {
return [];
};

const LANGUAGE_DEFAULT = process.env.CANON_LANGUAGE_DEFAULT || "canon";
const LANGUAGES = process.env.CANON_LANGUAGES || LANGUAGE_DEFAULT;
const LOGINS = process.env.CANON_LOGINS || false;
const PORT = process.env.CANON_PORT || 3300;
const NODE_ENV = process.env.NODE_ENV || "development";

const canonVars = {
CANON_API: process.env.CANON_API,
CANON_LANGUAGES: LANGUAGES,
CANON_LANGUAGE_DEFAULT: LANGUAGE_DEFAULT,
CANON_LOGINS: LOGINS,
CANON_LOGLOCALE: process.env.CANON_LOGLOCALE,
CANON_LOGREDUX: process.env.CANON_LOGREDUX,
CANON_PORT: PORT,
NODE_ENV
};

Object.keys(process.env).forEach(k => {
if (k.startsWith("CANON_CONST_")) {
canonVars[k.replace("CANON_CONST_", "")] = process.env[k];
}
});

const throttle = new PromiseThrottle({
requestsPerSecond: 10,
promiseImplementation: Promise
Expand Down Expand Up @@ -198,7 +221,7 @@ module.exports = function(app) {
/** */
function createGeneratorFetch(r, attr) {
// Generators use <id> as a placeholder. Replace instances of <id> with the provided id from the URL
let url = urlSwap(r, {...req.params, ...cache, ...attr, locale});
let url = urlSwap(r, {...req.params, ...cache, ...attr, ...canonVars, locale});
if (url.indexOf("http") !== 0) {
const origin = `http${ req.connection.encrypted ? "s" : "" }://${ req.headers.host }`;
url = `${origin}${url.indexOf("/") === 0 ? "" : "/"}${url}`;
Expand Down
7 changes: 4 additions & 3 deletions packages/cms/src/components/editors/GeneratorEditor.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios";
import React, {Component} from "react";
import {connect} from "react-redux";
import AceWrapper from "./AceWrapper";
import SimpleGeneratorEditor from "./SimpleGeneratorEditor";
import SimpleVisualizationEditor from "./SimpleVisualizationEditor";
Expand Down Expand Up @@ -105,7 +106,7 @@ class GeneratorEditor extends Component {
previewPayload(forceSimple) {
const {data} = this.state;
const {api} = data;
const {attr, previews, variables, locale} = this.props;
const {attr, previews, variables, locale, env} = this.props;
if (api) {
// The API will have <ids> in it that needs to be replaced with the current preview.
// Use urlSwap to swap ANY instances of variables between brackets (e.g. <varname>)
Expand All @@ -117,7 +118,7 @@ class GeneratorEditor extends Component {
}
lookup[`id${i + 1}`] = p.id;
});
const url = urlSwap(api, Object.assign({}, attr, variables, lookup));
const url = urlSwap(api, Object.assign({}, attr, env, variables, lookup));
axios.get(url).then(resp => {
const payload = resp.data;
let {simple} = this.state;
Expand Down Expand Up @@ -342,4 +343,4 @@ class GeneratorEditor extends Component {
}
}

export default GeneratorEditor;
export default connect(state => ({env: state.env}))(GeneratorEditor);
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios";
import React, {Component} from "react";
import {connect} from "react-redux";
import {Alert, Intent} from "@blueprintjs/core";
import urlSwap from "../../utils/urlSwap";
import Button from "../Button";
Expand All @@ -21,7 +22,7 @@ const vizLookup = {

const reservedWords = ["topojson"];

export default class SimpleVisualizationEditor extends Component {
class SimpleVisualizationEditor extends Component {

constructor(props) {
super(props);
Expand Down Expand Up @@ -56,7 +57,7 @@ export default class SimpleVisualizationEditor extends Component {

firstBuild() {
const {object} = this.state;
const {previews, variables} = this.props;
const {previews, variables, env} = this.props;
const {data} = object;
if (data) {
// The API will have an <id> in it that needs to be replaced with the current preview.
Expand All @@ -69,7 +70,7 @@ export default class SimpleVisualizationEditor extends Component {
}
lookup[`id${i + 1}`] = p.id;
});
const url = urlSwap(data, Object.assign({}, variables, lookup));
const url = urlSwap(data, Object.assign({}, env, variables, lookup));
axios.get(url).then(resp => {
const payload = resp.data;
this.setState({payload}, this.compileCode.bind(this));
Expand Down Expand Up @@ -221,3 +222,5 @@ export default class SimpleVisualizationEditor extends Component {

}
}

export default connect(state => ({env: state.env}))(SimpleVisualizationEditor);

0 comments on commit 2c06950

Please sign in to comment.