Skip to content

Commit

Permalink
fix for relative path import
Browse files Browse the repository at this point in the history
  • Loading branch information
givanse committed Jan 1, 2019
1 parent 654f317 commit 215324d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 21 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ with an express/sinatra style syntax for defining routes and their handlers.
Pretender will temporarily replace native XMLHttpRequest and Fetch , intercept
all requests, and direct them to little pretend service you've defined.

**:warning: Pretender only works in the browser!**

```javascript
const PHOTOS = {
"10": {
Expand All @@ -40,6 +38,10 @@ const server = new Pretender(function() {
$.get('/photos/12', {success() => { ... }})
```
## Usage examples
## The Server DSL
The server DSL is inspired by express/sinatra. Pass a function to the Pretender constructor
that will be invoked with the Pretender instance as its context. Available methods are
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pretender",
"version": "3.1.0",
"main": "./pretender.js",
"module": "./pretender.es.js",
"module": "./src/pretender.es.js",
"description": "Pretender is a mock server library for XMLHttpRequest and Fetch, that comes with an express/sinatra style syntax for defining routes and their handlers.",
"license": "MIT",
"engines": {
Expand All @@ -29,7 +29,7 @@
"coveralls": "^2.11.3",
"es6-promise": "^4.0.5",
"eslint": "^5.11.1",
"jshint": "^2.8.0",
"jshint": "^2.9.7",
"karma": "^1.7.0",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.1",
Expand All @@ -48,6 +48,7 @@
"typescript-eslint-parser": "^21.0.2"
},
"dependencies": {
"@babel/plugin-external-helpers": "^7.2.0",
"@xg-wang/whatwg-fetch": "^3.0.0",
"fake-xml-http-request": "^2.0.0",
"route-recognizer": "^0.3.3"
Expand Down
6 changes: 5 additions & 1 deletion pretender.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var Pretender = (function (self, RouteRecognizer, FakeXMLHttpRequest, FakeFetch)
var anchor = document.createElement('a');
anchor.href = url;
if (!anchor.host) {
// eslint-disable-next-line no-self-assign
anchor.href = anchor.href; // IE: load the host and protocol
}
var pathname = anchor.pathname;
Expand Down Expand Up @@ -243,6 +244,7 @@ var Pretender = (function (self, RouteRecognizer, FakeXMLHttpRequest, FakeFetch)
return this._passthroughCheck('getAllResponseHeaders', arguments);
};
if (ctx.pretender._nativeXMLHttpRequest.prototype._passthroughCheck) {
// eslint-disable-next-line no-console
console.warn('You created a second Pretender instance while there was already one running. ' +
'Running two Pretender servers at once will lead to unexpected results and will ' +
'be removed entirely in a future major version.' +
Expand Down Expand Up @@ -322,7 +324,9 @@ var Pretender = (function (self, RouteRecognizer, FakeXMLHttpRequest, FakeFetch)
var note = 'Remember to `return [status, headers, body];` in your route handler.';
throw new Error('Nothing returned by handler for ' + path + '. ' + note);
}
var status = statusHeadersAndBody[0], headers = pretender.prepareHeaders(statusHeadersAndBody[1]), body = pretender.prepareBody(statusHeadersAndBody[2], headers);
var status = statusHeadersAndBody[0];
var headers = pretender.prepareHeaders(statusHeadersAndBody[1]);
var body = pretender.prepareBody(statusHeadersAndBody[2], headers);
pretender.handleResponse(request, async, function () {
request.respond(status, headers, body);
pretender.handledRequest(verb, path, request);
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ module.exports = {
'var RouteRecognizer = self.RouteRecognizer;\n',
},
{
file: 'pretender.es.js',
file: 'src/pretender.es.js',
format: 'es'
},
{
file: 'pretender.cjs.js',
file: 'src/pretender.cjs.js',
format: 'cjs'
},
],
Expand Down
2 changes: 1 addition & 1 deletion src/iife-self-placeholder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// This is just a placeholder for the build step
// See the IIFE output in the Rollup config
export default null;
export default window;
6 changes: 5 additions & 1 deletion pretender.cjs.js → src/pretender.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function parseURL(url) {
var anchor = document.createElement('a');
anchor.href = url;
if (!anchor.host) {
// eslint-disable-next-line no-self-assign
anchor.href = anchor.href; // IE: load the host and protocol
}
var pathname = anchor.pathname;
Expand Down Expand Up @@ -239,6 +240,7 @@ function interceptor(ctx) {
return this._passthroughCheck('getAllResponseHeaders', arguments);
};
if (ctx.pretender._nativeXMLHttpRequest.prototype._passthroughCheck) {
// eslint-disable-next-line no-console
console.warn('You created a second Pretender instance while there was already one running. ' +
'Running two Pretender servers at once will lead to unexpected results and will ' +
'be removed entirely in a future major version.' +
Expand Down Expand Up @@ -318,7 +320,9 @@ Pretender.prototype = {
var note = 'Remember to `return [status, headers, body];` in your route handler.';
throw new Error('Nothing returned by handler for ' + path + '. ' + note);
}
var status = statusHeadersAndBody[0], headers = pretender.prepareHeaders(statusHeadersAndBody[1]), body = pretender.prepareBody(statusHeadersAndBody[2], headers);
var status = statusHeadersAndBody[0];
var headers = pretender.prepareHeaders(statusHeadersAndBody[1]);
var body = pretender.prepareBody(statusHeadersAndBody[2], headers);
pretender.handleResponse(request, async, function () {
request.respond(status, headers, body);
pretender.handledRequest(verb, path, request);
Expand Down
6 changes: 5 additions & 1 deletion pretender.es.js → src/pretender.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function parseURL(url) {
var anchor = document.createElement('a');
anchor.href = url;
if (!anchor.host) {
// eslint-disable-next-line no-self-assign
anchor.href = anchor.href; // IE: load the host and protocol
}
var pathname = anchor.pathname;
Expand Down Expand Up @@ -235,6 +236,7 @@ function interceptor(ctx) {
return this._passthroughCheck('getAllResponseHeaders', arguments);
};
if (ctx.pretender._nativeXMLHttpRequest.prototype._passthroughCheck) {
// eslint-disable-next-line no-console
console.warn('You created a second Pretender instance while there was already one running. ' +
'Running two Pretender servers at once will lead to unexpected results and will ' +
'be removed entirely in a future major version.' +
Expand Down Expand Up @@ -314,7 +316,9 @@ Pretender.prototype = {
var note = 'Remember to `return [status, headers, body];` in your route handler.';
throw new Error('Nothing returned by handler for ' + path + '. ' + note);
}
var status = statusHeadersAndBody[0], headers = pretender.prepareHeaders(statusHeadersAndBody[1]), body = pretender.prepareBody(statusHeadersAndBody[2], headers);
var status = statusHeadersAndBody[0];
var headers = pretender.prepareHeaders(statusHeadersAndBody[1]);
var body = pretender.prepareBody(statusHeadersAndBody[2], headers);
pretender.handleResponse(request, async, function () {
request.respond(status, headers, body);
pretender.handledRequest(verb, path, request);
Expand Down
29 changes: 18 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
dependencies:
"@babel/highlight" "^7.0.0"

"@babel/helper-plugin-utils@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.0.0.tgz#bbb3fbee98661c569034237cc03967ba99b4f250"
integrity sha512-CYAOUCARwExnEixLdB6sDm2dIJ/YgEAKDM1MOeMeZu9Ld/bDgVo8aiWrXwcY7OBh+1Ea2uUcVRcxKk0GJvW7QA==

"@babel/highlight@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0.tgz#f710c38c8d458e6dd9a201afb637fcb781ce99e4"
Expand All @@ -18,6 +23,13 @@
esutils "^2.0.2"
js-tokens "^4.0.0"

"@babel/plugin-external-helpers@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-external-helpers/-/plugin-external-helpers-7.2.0.tgz#7f4cb7dee651cd380d2034847d914288467a6be4"
integrity sha512-QFmtcCShFkyAsNtdCM3lJPmRe1iB+vPZymlB4LnDIKEBj2yKQLQKtoxXxJ8ePT5fwMl4QGg303p4mB0UsSI2/g==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@sinonjs/formatio@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@sinonjs/formatio/-/formatio-2.0.0.tgz#84db7e9eb5531df18a8c5e0bfb6e449e55e654b2"
Expand Down Expand Up @@ -2339,16 +2351,16 @@ jsbn@~0.1.0:
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"
integrity sha1-peZUwuWi3rXyAdls77yoDA7y9RM=

jshint@^2.8.0:
version "2.9.5"
resolved "https://registry.yarnpkg.com/jshint/-/jshint-2.9.5.tgz#1e7252915ce681b40827ee14248c46d34e9aa62c"
integrity sha1-HnJSkVzmgbQIJ+4UJIxG006apiw=
jshint@^2.9.7:
version "2.9.7"
resolved "https://registry.yarnpkg.com/jshint/-/jshint-2.9.7.tgz#038a3fa5c328fa3ab03ddfd85df88d3d87bedcbd"
integrity sha512-Q8XN38hGsVQhdlM+4gd1Xl7OB1VieSuCJf+fEJjpo59JH99bVJhXRXAh26qQ15wfdd1VPMuDWNeSWoNl53T4YA==
dependencies:
cli "~1.0.0"
console-browserify "1.1.x"
exit "0.1.x"
htmlparser2 "3.8.x"
lodash "3.7.x"
lodash "~4.17.10"
minimatch "~3.0.2"
shelljs "0.3.x"
strip-json-comments "1.0.x"
Expand Down Expand Up @@ -2570,11 +2582,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/lodash.unescape/-/lodash.unescape-4.0.1.tgz#bf2249886ce514cda112fae9218cdc065211fc9c"
integrity sha1-vyJJiGzlFM2hEvrpIYzcBlIR/Jw=

[email protected]:
version "3.7.0"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.7.0.tgz#3678bd8ab995057c07ade836ed2ef087da811d45"
integrity sha1-Nni9irmVBXwHreg27S7wh9qBHUU=

lodash@^3.8.0:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"
Expand All @@ -2585,7 +2592,7 @@ lodash@^4.0.1, lodash@^4.17.0, lodash@^4.17.10, lodash@^4.5.0:
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
integrity sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==

lodash@^4.17.11, lodash@^4.17.5:
lodash@^4.17.11, lodash@^4.17.5, lodash@~4.17.10:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==
Expand Down

0 comments on commit 215324d

Please sign in to comment.