Skip to content

Commit d9674c7

Browse files
committed
Use API server passed during SPA build time
XSnippet is an Open Source project that can be deployed anywhere anytime by anyone. Therefore, we must be prepared and provide facilities to pass non-default XSnippet API server URI. Since we do not support runtime configuration yet, let's pass it via environment variables during SPA build time.
1 parent d869f2c commit d9674c7

File tree

7 files changed

+28
-9
lines changed

7 files changed

+28
-9
lines changed

src/actions/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import parseLinkHeader from 'parse-link-header';
2+
import * as misc from '../misc';
23

34
export const setRecentSnippets = snippets => ({
45
type: 'SET_RECENT_SNIPPETS',
@@ -14,7 +15,7 @@ export const fetchRecentSnippets = marker => (dispatch) => {
1415
let qs = '';
1516
if (marker) { qs = `&marker=${marker}`; }
1617

17-
return fetch(`//api.xsnippet.org/snippets?limit=20${qs}`)
18+
return fetch(misc.getApiUri(`/snippets?limit=20${qs}`))
1819
.then((response) => {
1920
const links = parseLinkHeader(response.headers.get('Link'));
2021

@@ -30,7 +31,7 @@ export const setSnippet = snippet => ({
3031
});
3132

3233
export const fetchSnippet = id => dispatch => (
33-
fetch(`//api.xsnippet.org/snippets/${id}`)
34+
fetch(misc.getApiUri(`/snippets/${id}`))
3435
.then(response => response.json())
3536
.then(json => dispatch(setSnippet(json)))
3637
);
@@ -41,13 +42,13 @@ export const setSyntaxes = syntaxes => ({
4142
});
4243

4344
export const fetchSyntaxes = dispatch => (
44-
fetch('//api.xsnippet.org/syntaxes')
45+
fetch(misc.getApiUri('/syntaxes'))
4546
.then(response => response.json())
4647
.then(json => dispatch(setSyntaxes(json)))
4748
);
4849

4950
export const postSnippet = (snippet, onSuccess) => dispatch => (
50-
fetch('//api.xsnippet.org/snippets', {
51+
fetch(misc.getApiUri('/snippets'), {
5152
method: 'POST',
5253
headers: {
5354
'Accept': 'application/json',

src/components/RecentSnippetItem.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { Link } from 'react-router-dom';
33
import brace from 'brace';
44

55
import * as misc from '../misc';
6+
import conf from '../conf';
67

78
const RecentSnippetItem = ({ snippet }) => {
89
const { modesByName } = brace.acequire('ace/ext/modelist');
910
const mode = modesByName[snippet.get('syntax')] || modesByName.text;
1011
const syntax = mode.caption;
1112
const snippetTitle = snippet.get('title') || `#${snippet.get('id')}, Untitled`;
1213
const download = () => misc.downloadSnippet(snippet);
13-
const rawUrl = process.env.RAW_SNIPPETS_URL_FORMAT.replace('%s', snippet.get('id'));
14+
const rawUrl = conf.RAW_SNIPPET_URI_FORMAT.replace('%s', snippet.get('id'));
1415

1516
return (
1617
<li className="recent-snippet-item">

src/components/Snippet.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'brace/theme/textmate';
88
import Spinner from './common/Spinner';
99
import * as actions from '../actions';
1010
import * as misc from '../misc';
11+
import conf from '../conf';
1112

1213
import '../styles/Snippet.styl';
1314

@@ -50,7 +51,7 @@ export class Snippet extends React.Component {
5051
const snippetTitle = snippet.get('title') || `#${snippet.get('id')}, Untitled`;
5152
const mode = modesByName[snippet.get('syntax')] || modesByName.text;
5253
const syntax = mode.caption;
53-
const rawUrl = process.env.RAW_SNIPPETS_URL_FORMAT.replace('%s', snippet.get('id'));
54+
const rawUrl = conf.RAW_SNIPPET_URI_FORMAT.replace('%s', snippet.get('id'));
5455

5556
return (
5657
<div className="snippet" key="Snippet">

src/conf/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
// REST APIs have a base URI to which the endpoint paths are appended. This
3+
// one sets a base URI for the XSnippet API we need to communicate with.
4+
API_BASE_URI: process.env.API_BASE_URI || '//api.xsnippet.org',
5+
6+
// When expanded with a snippet ID, it points to a raw snippet page (i.e. a
7+
// plain/text page with snippet content and without markup).
8+
RAW_SNIPPET_URI_FORMAT: process.env.RAW_SNIPPET_URI_FORMAT || '//xsnippet.org/%s/raw',
9+
};

src/misc/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import brace from 'brace';
22
import 'brace/ext/modelist';
33

4+
import conf from '../conf';
5+
46
export const regExpEscape = string => string.replace(/[-[\]{}()*+?.,\\^$|]/g, '\\$&');
57

68
export function download(text, name, mime) {
@@ -49,3 +51,7 @@ export function formatDate(d) {
4951

5052
return ISOdate.split('-').reverse().join('.');
5153
}
54+
55+
export function getApiUri(endpoint) {
56+
return `${conf.API_BASE_URI}${endpoint}`;
57+
}

tests/components/Snippet.test.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ describe('Snippet', () => {
3535
});
3636

3737
it('should show snippet if snippet was passed as props', () => {
38-
process.env.RAW_SNIPPETS_URL_FORMAT = '//xsnippet.org/%s/raw';
3938
const wrapper = shallow(<Snippet snippet={snippet} />, { disableLifecycleMethods: true });
4039

4140
expect(wrapper.type()).not.toEqual(Spinner);

webpack.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,11 @@ module.exports = () => {
215215
new CleanWebpackPlugin([path.resolve(__dirname, 'dist')]),
216216

217217
// Propagate (and set) environment variables down to the application. We
218-
// use them to configure application behaviour.
218+
// use them to configure application behaviour. Please note, 'null' here
219+
// means 'unset'.
219220
new webpack.EnvironmentPlugin({
220-
RAW_SNIPPETS_URL_FORMAT: '//xsnippet.org/%s/raw',
221+
API_BASE_URI: null,
222+
RAW_SNIPPET_URI_FORMAT: null,
221223
}),
222224

223225
// Similar to JavaScript, we use [chunkhash] in order to invalidate

0 commit comments

Comments
 (0)