Skip to content

Commit

Permalink
Simplified the use of trackRequests. Fixed assert against trackedRequ…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
cah-brian-gantzler committed Jun 1, 2022
1 parent 2a8c301 commit cac69d6
Showing 1 changed file with 135 additions and 132 deletions.
267 changes: 135 additions & 132 deletions lib/pretender-config.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import "@miragejs/pretender-node-polyfill/before";
import Pretender from "pretender";
import "@miragejs/pretender-node-polyfill/after";
import { _assert as assert } from "miragejs";
import assert from "../assert";

/**
Mirage Interceptor Class
Mirage Interceptor Class
urlPrefix;
urlPrefix;
namespace;
namespace;
// Creates the interceptor instance
constructor(mirageServer, mirageConfig)
// Creates the interceptor instance
constructor(mirageServer, mirageConfig)
// Allow you to change some of the config options after the server is created
config(mirageConfig)
// Allow you to change some of the config options after the server is created
config(mirageConfig)
// These are the equivalent of the functions that were on the Mirage Server.
// Those Mirage Server functions are redirected to the Interceptors functions for
// backward compatibility
get
post
put
delete
del
patch
head
options
// These are the equivalent of the functions that were on the Mirage Server.
// Those Mirage Server functions are redirected to the Interceptors functions for
// backward compatibility
get
post
put
delete
del
patch
head
options
// Start the interceptor. (Optional) this happens after the mirage server has been completed configured
// and all the models, routes, etc have been defined.
start
// Shutdown the interceptor instance
shutdown
// Start the interceptor. (Optional) this happens after the mirage server has been completed configured
// and all the models, routes, etc have been defined.
start
// Shutdown the interceptor instance
shutdown
*/

Expand Down Expand Up @@ -65,9 +65,11 @@ export default class PretenderConfig {

mirageServer;

trackRequests;

create(mirageServer, config) {
this.mirageServer = mirageServer;
this.pretender = this._create(mirageServer);
this.pretender = this._create(mirageServer, config);

/**
Mirage uses [pretender.js](https://github.com/trek/pretender) as its xhttp interceptor. In your Mirage config, `this.pretender` refers to the actual Pretender instance, so any config options that work there will work here as well.
Expand Down Expand Up @@ -107,7 +109,7 @@ export default class PretenderConfig {
let handler = mirageServer.registerRouteHandler(verb, path, args);
let fullPath = this._getFullPath(path);
let timing =
config.timing !== undefined ? config.timing : () => this.timing;
config.timing !== undefined ? config.timing : () => this.timing;
return this.pretender?.[verb](fullPath, handler, timing);
};

Expand All @@ -118,22 +120,22 @@ export default class PretenderConfig {
}
});
}


config(config) {
let useDefaultPassthroughs =
typeof config.useDefaultPassthroughs !== "undefined"
? config.useDefaultPassthroughs
: true;
typeof config.useDefaultPassthroughs !== "undefined"
? config.useDefaultPassthroughs
: true;
if (useDefaultPassthroughs) {
this._configureDefaultPassthroughs();
}

let didOverridePretenderConfig =
config.trackRequests !== undefined && this.pretender;
config.trackRequests !== undefined &&
config.trackRequests !== this.trackRequests;
assert(
!didOverridePretenderConfig,
"You cannot modify Pretender's request tracking once the server is created"
!didOverridePretenderConfig,
"You cannot modify Pretender's request tracking once the server is created"
);

/**
Expand Down Expand Up @@ -234,90 +236,91 @@ export default class PretenderConfig {
* @return {Object} A new Pretender instance.
* @public
*/
_create(mirageServer) {
_create(mirageServer, config) {
if (typeof window !== "undefined") {
this.trackRequests = config.trackRequests || false;
return new Pretender(
function () {
this.passthroughRequest = function (verb, path, request) {
if (mirageServer.shouldLog()) {
console.log(
`Mirage: Passthrough request for ${verb.toUpperCase()} ${
request.url
}`
);
}
};

this.handledRequest = function (verb, path, request) {
if (mirageServer.shouldLog()) {
console.groupCollapsed(
`Mirage: [${request.status}] ${verb.toUpperCase()} ${
request.url
}`
function () {
this.passthroughRequest = function (verb, path, request) {
if (mirageServer.shouldLog()) {
console.log(
`Mirage: Passthrough request for ${verb.toUpperCase()} ${
request.url
}`
);
}
};

this.handledRequest = function (verb, path, request) {
if (mirageServer.shouldLog()) {
console.groupCollapsed(
`Mirage: [${request.status}] ${verb.toUpperCase()} ${
request.url
}`
);
let { requestBody, responseText } = request;
let loggedRequest, loggedResponse;

try {
loggedRequest = JSON.parse(requestBody);
} catch (e) {
loggedRequest = requestBody;
}

try {
loggedResponse = JSON.parse(responseText);
} catch (e) {
loggedResponse = responseText;
}

console.groupCollapsed("Response");
console.log(loggedResponse);
console.groupEnd();

console.groupCollapsed("Request (data)");
console.log(loggedRequest);
console.groupEnd();

console.groupCollapsed("Request (raw)");
console.log(request);
console.groupEnd();

console.groupEnd();
}
};

let originalCheckPassthrough = this.checkPassthrough;
this.checkPassthrough = function (request) {
let shouldPassthrough = mirageServer.passthroughChecks.some(
(passthroughCheck) => passthroughCheck(request)
);
let { requestBody, responseText } = request;
let loggedRequest, loggedResponse;

try {
loggedRequest = JSON.parse(requestBody);
} catch (e) {
loggedRequest = requestBody;
}
if (shouldPassthrough) {
let url = request.url.includes("?")
? request.url.substr(0, request.url.indexOf("?"))
: request.url;

try {
loggedResponse = JSON.parse(responseText);
} catch (e) {
loggedResponse = responseText;
this[request.method.toLowerCase()](url, this.passthrough);
}

console.groupCollapsed("Response");
console.log(loggedResponse);
console.groupEnd();

console.groupCollapsed("Request (data)");
console.log(loggedRequest);
console.groupEnd();

console.groupCollapsed("Request (raw)");
console.log(request);
console.groupEnd();

console.groupEnd();
}
};

let originalCheckPassthrough = this.checkPassthrough;
this.checkPassthrough = function (request) {
let shouldPassthrough = mirageServer.passthroughChecks.some(
(passthroughCheck) => passthroughCheck(request)
);

if (shouldPassthrough) {
let url = request.url.includes("?")
? request.url.substr(0, request.url.indexOf("?"))
: request.url;

this[request.method.toLowerCase()](url, this.passthrough);
}

return originalCheckPassthrough.apply(this, arguments);
};

this.unhandledRequest = function (verb, path) {
path = decodeURI(path);
let namespaceError = "";
if (this.namespace === "") {
namespaceError =
"There is no existing namespace defined. Please define one";
} else {
namespaceError = `The existing namespace is ${this.namespace}`;
}
assert(
`Your app tried to ${verb} '${path}', but there was no route defined to handle this request. Define a route for this endpoint in your routes() config. Did you forget to define a namespace? ${namespaceError}`
);
};
},
{ trackRequests: mirageServer.shouldTrackRequests() }
return originalCheckPassthrough.apply(this, arguments);
};

this.unhandledRequest = function (verb, path) {
path = decodeURI(path);
let namespaceError = "";
if (this.namespace === "") {
namespaceError =
"There is no existing namespace defined. Please define one";
} else {
namespaceError = `The existing namespace is ${this.namespace}`;
}
assert(
`Your app tried to ${verb} '${path}', but there was no route defined to handle this request. Define a route for this endpoint in your routes() config. Did you forget to define a namespace? ${namespaceError}`
);
};
},
{ trackRequests: this.trackRequests }
);
}
}
Expand Down Expand Up @@ -420,31 +423,31 @@ export default class PretenderConfig {
// if there is a urlPrefix and a namespace
if (this.urlPrefix && this.namespace) {
if (
this.namespace[0] === "/" &&
this.namespace[this.namespace.length - 1] === "/"
this.namespace[0] === "/" &&
this.namespace[this.namespace.length - 1] === "/"
) {
namespace = this.namespace
.substring(0, this.namespace.length - 1)
.substring(1);
.substring(0, this.namespace.length - 1)
.substring(1);
}

if (
this.namespace[0] === "/" &&
this.namespace[this.namespace.length - 1] !== "/"
this.namespace[0] === "/" &&
this.namespace[this.namespace.length - 1] !== "/"
) {
namespace = this.namespace.substring(1);
}

if (
this.namespace[0] !== "/" &&
this.namespace[this.namespace.length - 1] === "/"
this.namespace[0] !== "/" &&
this.namespace[this.namespace.length - 1] === "/"
) {
namespace = this.namespace.substring(0, this.namespace.length - 1);
}

if (
this.namespace[0] !== "/" &&
this.namespace[this.namespace.length - 1] !== "/"
this.namespace[0] !== "/" &&
this.namespace[this.namespace.length - 1] !== "/"
) {
namespace = this.namespace;
}
Expand All @@ -453,33 +456,33 @@ export default class PretenderConfig {
// if there is a namespace and no urlPrefix
if (this.namespace && !this.urlPrefix) {
if (
this.namespace[0] === "/" &&
this.namespace[this.namespace.length - 1] === "/"
this.namespace[0] === "/" &&
this.namespace[this.namespace.length - 1] === "/"
) {
namespace = this.namespace.substring(0, this.namespace.length - 1);
}

if (
this.namespace[0] === "/" &&
this.namespace[this.namespace.length - 1] !== "/"
this.namespace[0] === "/" &&
this.namespace[this.namespace.length - 1] !== "/"
) {
namespace = this.namespace;
}

if (
this.namespace[0] !== "/" &&
this.namespace[this.namespace.length - 1] === "/"
this.namespace[0] !== "/" &&
this.namespace[this.namespace.length - 1] === "/"
) {
let namespaceSub = this.namespace.substring(
0,
this.namespace.length - 1
0,
this.namespace.length - 1
);
namespace = `/${namespaceSub}`;
}

if (
this.namespace[0] !== "/" &&
this.namespace[this.namespace.length - 1] !== "/"
this.namespace[0] !== "/" &&
this.namespace[this.namespace.length - 1] !== "/"
) {
namespace = `/${this.namespace}`;
}
Expand All @@ -497,7 +500,7 @@ export default class PretenderConfig {
// otherwise, if there is a urlPrefix, use that as the beginning of the path
if (urlPrefix.length) {
fullPath +=
urlPrefix[urlPrefix.length - 1] === "/" ? urlPrefix : `${urlPrefix}/`;
urlPrefix[urlPrefix.length - 1] === "/" ? urlPrefix : `${urlPrefix}/`;
}

// add the namespace to the path
Expand All @@ -521,7 +524,7 @@ export default class PretenderConfig {

return fullPath;
}

start() {
// unneeded for pretender implementation
}
Expand Down

0 comments on commit cac69d6

Please sign in to comment.