Skip to content

Commit

Permalink
Make miragejs a dev dependency
Browse files Browse the repository at this point in the history
Using embroider macros we can detect if ember-cli-mirage and miragejs
are present and only include this code when that is true.
  • Loading branch information
jrjohnson committed Jan 23, 2022
1 parent 46b7e43 commit 173c81a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 51 deletions.
117 changes: 67 additions & 50 deletions addon/mirage/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import RSVP from 'rsvp';
import { Response } from 'miragejs';

import { extractFormData, extractFileMetadata } from './utils';
import {
macroCondition,
dependencySatisfies,
importSync,
} from '@embroider/macros';

const NETWORK = {
wired: 50000, // 500 Mb/s
Expand All @@ -14,61 +19,73 @@ const NETWORK = {
};

export function upload(fn, options = { network: null, timeout: null }) {
return function (db, request) {
let speed = Infinity;
if (
macroCondition(
dependencySatisfies('miragejs', '*') &&
dependencySatisfies('ember-cli-mirage', '*')
)
) {
const { Response } = importSync('miragejs');
return function (db, request) {
let speed = Infinity;

if (NETWORK[options.network]) {
speed = NETWORK[options.network] * 1024;
}
if (NETWORK[options.network]) {
speed = NETWORK[options.network] * 1024;
}

let { file, data } = extractFormData(request.requestBody);
let loaded = 0;
let total = file.value.size;
let { file, data } = extractFormData(request.requestBody);
let loaded = 0;
let total = file.value.size;

return new RSVP.Promise((resolve) => {
let start = new Date().getTime();
let metadata = extractFileMetadata(file.value);
return new RSVP.Promise((resolve) => {
let start = new Date().getTime();
let metadata = extractFileMetadata(file.value);

let upload = () => {
let timedOut =
options.timeout && new Date().getTime() - start > options.timeout;
if (timedOut || loaded >= total) {
request.upload.onprogress({
lengthComputable: true,
total,
loaded: Math.min(loaded, total),
});
let upload = () => {
let timedOut =
options.timeout && new Date().getTime() - start > options.timeout;
if (timedOut || loaded >= total) {
request.upload.onprogress({
lengthComputable: true,
total,
loaded: Math.min(loaded, total),
});

metadata
.then((metadata) => {
request.requestBody = Object.assign(
{
[file.key]: metadata,
},
data
);
if (timedOut) {
resolve(new Response(408));
return;
}
metadata
.then((metadata) => {
request.requestBody = Object.assign(
{
[file.key]: metadata,
},
data
);
if (timedOut) {
resolve(new Response(408));
return;
}

resolve(fn.call(this, db, request));
})
.catch((error) => {
resolve(new Response(500, {}, { error: error.message }));
resolve(fn.call(this, db, request));
})
.catch((error) => {
resolve(new Response(500, {}, { error: error.message }));
});
} else {
request.upload.onprogress({
lengthComputable: true,
total,
loaded,
});
} else {
request.upload.onprogress({
lengthComputable: true,
total,
loaded,
});

loaded += speed / 20;
setTimeout(upload, 50);
}
};
upload();
});
};
loaded += speed / 20;
setTimeout(upload, 50);
}
};
upload();
});
};
} else {
throw new Error(
'You must add ember-cli-mirage and miragejs to your app to use this helper.'
);
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@
"dependencies": {
"@ember/render-modifiers": "^2.0.0",
"@ember/test-waiters": "^3.0.0",
"@embroider/macros": "^1.0.0",
"@glimmer/component": "^1.0.4",
"@glimmer/tracking": "^1.0.4",
"broccoli-funnel": "^3.0.8",
"ember-cli-babel": "^7.26.6",
"ember-cli-htmlbars": "^5.7.1",
"ember-cli-typescript": "^4.2.1",
"ember-modifier": "^2.1.2 || ^3.0.0",
"miragejs": "^0.1.43",
"tracked-built-ins": "^2.0.0"
},
"devDependencies": {
Expand Down Expand Up @@ -118,6 +118,7 @@
"eslint-plugin-qunit": "^7.0.0",
"fake-xml-http-request": "^2.1.2",
"loader.js": "^4.7.0",
"miragejs": "^0.1.43",
"npm-run-all": "^4.1.5",
"prettier": "^2.3.2",
"qunit": "^2.16.0",
Expand Down

0 comments on commit 173c81a

Please sign in to comment.