Skip to content

Commit

Permalink
feat: testing for metrics works now too
Browse files Browse the repository at this point in the history
  • Loading branch information
mjcuva committed Aug 3, 2023
1 parent ca30c30 commit 75a2830
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@types/node-fetch": "^2.6.2",
"api": "^6.1.0",
"content-type": "^1.0.5",
"crypto-js": "^4.1.1",
"find-cache-dir": "^3.3.2",
"flat-cache": "^3.0.4",
"flatted": "^3.2.7",
Expand Down
13 changes: 5 additions & 8 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import flatted from 'flatted';
import { getProjectBaseUrl } from './lib/get-project-base-url';
import { log } from './lib/log';
import { buildSetupView } from './lib/setup-readme-view';
import { testMetrics } from './lib/test-metrics';
import { testVerifyWebhook } from './lib/test-verify-webhook';
import verifyWebhook from './lib/verify-webhook';

Expand Down Expand Up @@ -68,6 +67,7 @@ const findApiKey = (req, keys: [ApiKey]) => {

// See comment at the auth definition below
let apiKey = '';
let readmeProject;

const readme = userFunction => {
// IDK if this is the best thing to do, but feels
Expand All @@ -79,12 +79,11 @@ const readme = userFunction => {
}
return async (req, res, next) => {
readmeSDK.auth(apiKey);
let readmeProject;
try {
readmeProject = (await readmeSDK.getProject()).data;
} catch (e) {
// TODO: better way of handling this error
console.log('Error fetching project, is your ReadMe API key correct');
console.log('Error fetching project, is your ReadMe API key correct?');

Check warning on line 86 in packages/node/src/index.ts

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected console statement

Check warning on line 86 in packages/node/src/index.ts

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected console statement

Check warning on line 86 in packages/node/src/index.ts

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected console statement
console.log(e);

Check warning on line 87 in packages/node/src/index.ts

View workflow job for this annotation

GitHub Actions / build (14)

Unexpected console statement

Check warning on line 87 in packages/node/src/index.ts

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected console statement

Check warning on line 87 in packages/node/src/index.ts

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected console statement
}
const baseUrl = `${req.protocol}://${req.get('host')}${req.baseUrl}`;
Expand All @@ -102,16 +101,12 @@ const readme = userFunction => {
const { grouping } = splitIntoUserAndConfig(user);
return res.send(grouping);
} else if (req.path === '/readme-setup' && env === 'development') {
const setupHtml = buildSetupView({ baseUrl });
const setupHtml = buildSetupView({ baseUrl, subdomain: readmeProject.subdomain, apiKey });
return res.send(setupHtml);
} else if (req.path === '/webhook-test' && env === 'development') {
const email = req.query.email;
const webhookData = await testVerifyWebhook(baseUrl, email, readmeProject.jwtSecret);
return res.json({ ...webhookData });
} else if (req.path === '/metrics-test' && env === 'development') {
// TODO: not implemented yet
const metricsData = await testMetrics(apiKey);
return res.json({ ...metricsData });
}

const user = await userFunction(req, res);
Expand All @@ -134,6 +129,8 @@ const readme = userFunction => {
// readme.auth('api-key');
function auth(key) {
apiKey = key;
// Reset the cache for the ReadMe project if the api key changes
readmeProject = undefined;
return readme;
}

Expand Down
26 changes: 13 additions & 13 deletions packages/node/src/lib/setup-readme-view.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function buildSetupView({ baseUrl }) {
import sha512 from 'crypto-js/sha512';

export function buildSetupView({ baseUrl, apiKey, subdomain }) {
const webhookVerifiedHtml = `
<section class="card">
<div id="webhook-test">
Expand Down Expand Up @@ -124,18 +126,16 @@ export function buildSetupView({ baseUrl }) {
`;

const metricsScriptHtml = `
fetch('${baseUrl}/metrics-test')
.then(response => response.json())
.then(data => {
const { metricsVerified } = data;
document.getElementById('metrics-test').classList.add('hidden');
if (!metricsVerified) {
document.getElementById('metrics-fail').classList.remove('hidden');
} else {
document.getElementById('metrics-success').classList.remove('hidden');
window.metricsSuccess = true;
}
});
const token = '${sha512(apiKey).toString()}';
const query = new URLSearchParams(\`token=\${token}&subdomain=${subdomain}\`);
const socket = new WebSocket(new URL(\`?\${query}\`, 'wss://m.readme.io'));
socket.addEventListener('message', async ({ data }) => {
document.getElementById('metrics-test').classList.add('hidden');
document.getElementById('metrics-success').classList.remove('hidden');
});
// TODO: should we do something here if it takes too long?
// Maybe show some trouble shooting steps?
`;

return `
Expand Down
7 changes: 0 additions & 7 deletions packages/node/src/lib/test-metrics.ts

This file was deleted.

0 comments on commit 75a2830

Please sign in to comment.