From 215324d9b98113b6849fab541eac5c10ebc8017d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20I=2E=20Silva?= Date: Tue, 1 Jan 2019 10:22:53 -0800 Subject: [PATCH] fix for relative path import --- README.md | 6 +++-- package.json | 5 ++-- pretender.js | 6 ++++- rollup.config.js | 4 ++-- src/iife-self-placeholder.ts | 2 +- pretender.cjs.js => src/pretender.cjs.js | 6 ++++- pretender.es.js => src/pretender.es.js | 6 ++++- yarn.lock | 29 +++++++++++++++--------- 8 files changed, 43 insertions(+), 21 deletions(-) rename pretender.cjs.js => src/pretender.cjs.js (98%) rename pretender.es.js => src/pretender.es.js (98%) diff --git a/README.md b/README.md index a23c82e..a9a19aa 100644 --- a/README.md +++ b/README.md @@ -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": { @@ -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 diff --git a/package.json b/package.json index c7a2c47..78c11e9 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", @@ -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" diff --git a/pretender.js b/pretender.js index 44b52ed..4974ff5 100644 --- a/pretender.js +++ b/pretender.js @@ -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; @@ -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.' + @@ -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); diff --git a/rollup.config.js b/rollup.config.js index 581dd57..4d4c60c 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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' }, ], diff --git a/src/iife-self-placeholder.ts b/src/iife-self-placeholder.ts index ca5c642..0834b42 100644 --- a/src/iife-self-placeholder.ts +++ b/src/iife-self-placeholder.ts @@ -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; diff --git a/pretender.cjs.js b/src/pretender.cjs.js similarity index 98% rename from pretender.cjs.js rename to src/pretender.cjs.js index 82605e7..b388dc2 100644 --- a/pretender.cjs.js +++ b/src/pretender.cjs.js @@ -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; @@ -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.' + @@ -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); diff --git a/pretender.es.js b/src/pretender.es.js similarity index 98% rename from pretender.es.js rename to src/pretender.es.js index ac7c588..421a1aa 100644 --- a/pretender.es.js +++ b/src/pretender.es.js @@ -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; @@ -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.' + @@ -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); diff --git a/yarn.lock b/yarn.lock index e3b5814..1cfed2e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -2570,11 +2582,6 @@ lodash.unescape@4.0.1: resolved "https://registry.yarnpkg.com/lodash.unescape/-/lodash.unescape-4.0.1.tgz#bf2249886ce514cda112fae9218cdc065211fc9c" integrity sha1-vyJJiGzlFM2hEvrpIYzcBlIR/Jw= -lodash@3.7.x: - 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" @@ -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==