Skip to content

Commit

Permalink
Added support for initialHeaders via window.__ALTAIR_INITIAL_HEADERS__.
Browse files Browse the repository at this point in the history
Closes #609.
  • Loading branch information
imolorhe committed Mar 9, 2019
1 parent 181ddee commit 878e611
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
16 changes: 15 additions & 1 deletion packages/altair-static/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@ export interface RenderOptions {
* Initial variables to be added
*/
initialVariables?: string;

/**
* Initial headers object to be added
* @example
* {
* 'X-GraphQL-Token': 'asd7-237s-2bdk-nsdk4'
* }
*/
initialHeaders?: Object;
}

export const renderAltair = ({
baseURL = './',
endpointURL,
subscriptionsEndpoint,
initialQuery,
initialVariables
initialVariables,
initialHeaders,
}: RenderOptions = {}) => {
let altairHtml = readFileSync(resolve(__dirname, 'dist/index.html'), 'utf8');

Expand All @@ -54,6 +64,10 @@ export const renderAltair = ({
renderedOptions += `window.__ALTAIR_INITIAL_VARIABLES__ = '${initialVariables}';`;
}

if (initialHeaders) {
renderedOptions += `window.__ALTAIR_INITIAL_HEADERS__ = ${JSON.stringify(initialHeaders)};`;
}

const renderedOptionsInScript = `<script>${renderedOptions}</script>`;
return altairHtml.replace('<body>', `<body>${renderedOptionsInScript}`);
};
Expand Down
1 change: 1 addition & 0 deletions src/app/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ export default {
subscriptionsEndpoint: window['__ALTAIR_SUBSCRIPTIONS_ENDPOINT__'] || '',
query: window['__ALTAIR_INITIAL_QUERY__'] || '',
variables: window['__ALTAIR_INITIAL_VARIABLES__'] || '',
headers: window['__ALTAIR_INITIAL_HEADERS__'],
}
};
20 changes: 15 additions & 5 deletions src/app/reducers/headers/headers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { Action } from '@ngrx/store';

import * as headers from '../../actions/headers/headers';
import config from 'app/config';

const getInitialHeadersState = () => {
let initialHeaders: State = [];
if (config.initialData.headers) {
initialHeaders = Object.keys(config.initialData.headers).map(key => ({
key,
value: config.initialData.headers[key]
}));
}
initialHeaders = [ ...initialHeaders, { key: '', value: '' } ];

return initialHeaders;
};

export interface Header {
key: string;
Expand All @@ -11,11 +25,7 @@ export interface State extends Array<Header> {
[index: number]: Header;
}

export const initialState: State = [
{ key: '', value: '' },
{ key: '', value: '' },
{ key: '', value: '' },
];
export const initialState: State = getInitialHeadersState();

export function headerReducer(state = initialState, action: headers.Action): State {
switch (action.type) {
Expand Down

0 comments on commit 878e611

Please sign in to comment.