-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use fake post() instead of fetch()
- Loading branch information
Showing
4 changed files
with
42 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,33 @@ | ||
import Metric from './model/metric.js'; | ||
import fetch from './util/fake_fetch.js'; | ||
import { post } from './util/fake_fetch.js'; | ||
import { METRIC_ENDPOINT } from './config.js'; | ||
import Logger from './util/logger.js'; | ||
import { | ||
InvalidMetricTypeError, | ||
ErrorSendingMetric, | ||
} from '../shared/model/error.js'; | ||
import { InvalidMetricTypeError } from '../shared/model/error.js'; | ||
|
||
export default class MetricService { | ||
static async emit(metric) { | ||
static emit(metric) { | ||
if (!(metric instanceof Metric)) { | ||
throw new InvalidMetricTypeError(); | ||
} | ||
|
||
const metricPayload = getMetricPayload(metric); | ||
const metricJson = getMetricJson(metricPayload); | ||
const metricString = `${metric.dimension}=${metric.value}`; | ||
|
||
// Here we would make a `fetch` request to our metric service | ||
// For now, let's just log mimic an async request | ||
return fetch(metricJson) | ||
.then((response) => { | ||
Logger.debug(`Metric sent! Server returned status: ${response.status}`); | ||
Logger.info(metricString); | ||
}) | ||
.catch((error) => { | ||
Logger.error(new ErrorSendingMetric(error)); | ||
}); | ||
Logger.debug(`Sending metric: ${metricString}`); | ||
return post(METRIC_ENDPOINT, metricPayload); | ||
} | ||
} | ||
|
||
// when are private fields and methods coming? :-( | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields | ||
|
||
function getMetricPayload(metric) { | ||
const timestamp = new Date().toISOString(); | ||
|
||
const payload = { | ||
timestamp, | ||
dimension: metric.dimension, | ||
value: metric.value, | ||
}; | ||
|
||
return payload; | ||
} | ||
|
||
function getMetricJson(payload) { | ||
return JSON.stringify(payload); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,30 @@ | ||
import { FEEDBACK_ENDPOINT } from '../config.js'; | ||
import { RequestSendingError } from '../model/error.js'; | ||
|
||
export function post(url, payload) { | ||
const timestamp = new Date().toISOString(); | ||
const timestampedPayload = { ...payload, timestamp }; | ||
|
||
return fetch(url, { | ||
method: 'POST', | ||
mode: 'cors', | ||
headers: { 'Content-Type': 'application/json' }, | ||
redirect: 'follow', | ||
referrerPolicy: 'no-referrer', | ||
body: JSON.stringify(timestampedPayload), | ||
}); | ||
} | ||
|
||
// A simple `fetch` mock so we can unit test via node without extra dependencies | ||
// With some hardcoded logic just to help with local manual testing ;-) | ||
// After using a isomorphic fetch library + snowpack/webpack we can unmock this function | ||
function fetch(url, payload) { | ||
const success = Promise.resolve({ status: 200 }); | ||
const fail = Promise.reject(new RequestSendingError({ status: 400 })); | ||
|
||
export default async function fetch() { | ||
return Promise.resolve({ status: 200 }); | ||
if (url === FEEDBACK_ENDPOINT) { | ||
const feedback = JSON.parse(payload.body).feedback; | ||
return feedback === 'fail' ? fail : success; | ||
} | ||
return success; | ||
} |