Skip to content
This repository has been archived by the owner on May 11, 2020. It is now read-only.

Commit

Permalink
Check missing tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisjanvier committed Nov 8, 2019
1 parent 7cde7b4 commit d3113b3
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
35 changes: 35 additions & 0 deletions docs/references.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## Functions

<dl>
<dt><a href="#getApiTokenName">getApiTokenName(api)</a> ⇒ <code>string</code></dt>
<dd><p>return the token name as environment variable from api name</p>
</dd>
<dt><a href="#getMissingEnvironmentTokens">getMissingEnvironmentTokens(apis, environment)</a> ⇒ <code>Array.&lt;string&gt;</code></dt>
<dd><p>Returns an array of missing token names from the environment variables</p>
</dd>
<dt><a href="#transformBinaryToUtf8">transformBinaryToUtf8(value)</a> ⇒ <code>string</code></dt>
<dd><p>Transform a Binay (Buffer) into string</p>
</dd>
Expand All @@ -15,6 +21,35 @@
</dd>
</dl>

<a name="getApiTokenName"></a>

## getApiTokenName(api) ⇒ <code>string</code>
return the token name as environment variable from api name

**Kind**: global function
**Returns**: <code>string</code> - The token name

| Param | Type | Description |
| --- | --- | --- |
| api | <code>object</code> | the api object configuration |

**Example**
```js
api name: rick-and-morty => token name RICK_AND_MORTY_TOKEN
```
<a name="getMissingEnvironmentTokens"></a>

## getMissingEnvironmentTokens(apis, environment) ⇒ <code>Array.&lt;string&gt;</code>
Returns an array of missing token names from the environment variables

**Kind**: global function
**Returns**: <code>Array.&lt;string&gt;</code> - an array of missing token name from environment variables

| Param | Type | Description |
| --- | --- | --- |
| apis | <code>Array.&lt;object&gt;</code> | an array of configured apis |
| environment | <code>object</code> | the environment from process.env |

<a name="transformBinaryToUtf8"></a>

## transformBinaryToUtf8(value) ⇒ <code>string</code>
Expand Down
7 changes: 5 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ for (const api of config.apis) {
target: api.url,
ignorePath: false,
pathRewrite: path => path.replace(`/${api.name}`, ''),
onProxyReq: proxyReq =>
proxyReq.setHeader(`${api.tokenKey || 'authorization'}`, `${api.tokenPrefix || 'Bearer'} ${api.token}`),
onProxyReq: proxyReq => {
if (api.requiresAuthentication) {
proxyReq.setHeader(`${api.tokenKey || 'authorization'}`, `${api.tokenPrefix || 'Bearer'} ${api.token}`);
}
},
};
app.use(`/${api.name}`, myna(api), proxy(proxyOptions));
}
4 changes: 3 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const globalConfiguration = rc('webmyna', { apis: [], recordingsPath: null });
* @returns {string} The token name
*/
export const getApiTokenName = api => {
return api.name ? `${api.name.toUpperCase().replace(/-/g, '_')}_TOKEN` : 'fakeWebMynaTokenName';
return typeof api.name === 'string' && !!api.name
? `${api.name.toUpperCase().replace(/-/g, '_')}_TOKEN`
: 'fakeWebMynaTokenName';
};

/**
Expand Down
32 changes: 32 additions & 0 deletions src/config.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getApiTokenName, getMissingEnvironmentTokens } from './config.js';

describe('config', () => {
describe('getApiTokenName', () => {
it('should return "fakeWebMynaTokenName" if api.name is not a string', () => {
const testGetApiTokenName = api => expect(getApiTokenName(api)).toEqual('fakeWebMynaTokenName');
testGetApiTokenName({ name: null });
testGetApiTokenName({ name: undefined });
testGetApiTokenName({ name: 345 });
testGetApiTokenName({ name: { foo: 'bar' } });
testGetApiTokenName({ name: '' });
testGetApiTokenName({});
});

it('should return a well formated token name', () => {
expect(getApiTokenName({ name: 'rick-and-morty' })).toEqual('RICK_AND_MORTY_TOKEN');
});
});

describe('getMissingEnvironmentTokens', () => {
it('should return an array of missing token in precess.env', () => {
const apis = [
{ name: 'api-1', requiresAuthentication: false },
{ name: 'api-2', requiresAuthentication: true },
{ name: 'api-3', requiresAuthentication: true },
];
const processEnv = {};
processEnv['API_2_TOKEN'] = 'foo';
expect(getMissingEnvironmentTokens(apis, processEnv)).toEqual(['API_3_TOKEN']);
});
});
});
16 changes: 11 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import signale from 'signale';

import { app } from './app.js';
import config from './config.js';
import * as appJs from './app.js';
import config, { getMissingEnvironmentTokens } from './config.js';

app.listen(config.proxyPort, () => {
signale.info(`Web Myna is starded on port ${config.proxyPort} in environment ${config.env}`);
});
const missingTokens = getMissingEnvironmentTokens();

if (missingTokens.length) {
signale.error('IL MANQUE DES TOKENS', missingTokens);
} else {
appJs.app.listen(config.proxyPort, () => {
signale.info(`Web Myna is starded on port ${config.proxyPort} in environment ${config.env}`);
});
}
2 changes: 1 addition & 1 deletion website/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"tagline": "Record then emulate your api.s for testing and development",
"docs": {
"references": {
"title": "References"
"title": "references"
}
},
"links": {
Expand Down

0 comments on commit d3113b3

Please sign in to comment.