Skip to content

Commit

Permalink
Kick jsonp - fixes MithrilJS#2682
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanHoyer committed Jun 6, 2022
1 parent efdb563 commit 716d1e1
Show file tree
Hide file tree
Showing 16 changed files with 8 additions and 362 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

## What is Mithril.js?

A modern client-side JavaScript framework for building Single Page Applications. It's small (<!-- size -->9.34 KB<!-- /size --> gzipped), fast and provides routing and XHR utilities out of the box.
A modern client-side JavaScript framework for building Single Page Applications. It's small (<!-- size -->9.18 KB<!-- /size --> gzipped), fast and provides routing and XHR utilities out of the box.

Mithril.js is used by companies like Vimeo and Nike, and open source platforms like Lichess 👍.

Expand Down
15 changes: 0 additions & 15 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,6 @@ m.request({

---

#### m.jsonp(options) - [docs](jsonp.md)

```javascript
m.jsonp({
url: "/api/v1/users/:id",
params: {id: 1},
callbackKey: "callback",
})
.then(function(result) {
console.log(result)
})
```

---

#### m.parseQueryString(querystring) - [docs](parseQueryString.md)

```javascript
Expand Down
2 changes: 1 addition & 1 deletion docs/buildPathname.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Argument | Type | Required | Descripti

### How it works

The `m.buildPathname` creates a [path name](paths.md) from a path template and a parameters object. It's useful for building URLs, and it's what [`m.route`](route.md), [`m.request`](request.md), and [`m.jsonp`](jsonp.md) all use internally to interpolate paths. It uses [`m.buildQueryString`](buildQueryString.md) to generate the query parameters to append to the path name.
The `m.buildPathname` creates a [path name](paths.md) from a path template and a parameters object. It's useful for building URLs, and it's what [`m.route`](route.md) and [`m.request`](request.md) use internally to interpolate paths. It uses [`m.buildQueryString`](buildQueryString.md) to generate the query parameters to append to the path name.

```javascript
var pathname = m.buildPathname("/path/:id", {id: "user", a: 1, b: 2})
Expand Down
103 changes: 0 additions & 103 deletions docs/jsonp.md

This file was deleted.

1 change: 0 additions & 1 deletion docs/nav-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
- [m.mount](mount.md)
- [m.route](route.md)
- [m.request](request.md)
- [m.jsonp](jsonp.md)
- [m.parseQueryString](parseQueryString.md)
- [m.buildQueryString](buildQueryString.md)
- [m.buildPathname](buildPathname.md)
Expand Down
6 changes: 3 additions & 3 deletions docs/paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Documentation on how to work with paths in Mithril.js

-----

[`m.route`](route.md), [`m.request`](request.md), and [`m.jsonp`](jsonp.md) each have a concept called a path. This is used to generate the URL you route to or fetch from.
[`m.route`](route.md) and [`m.request`](request.md) each have a concept called a path. This is used to generate the URL you route to or fetch from.

### Path types

Expand All @@ -20,7 +20,7 @@ There are two general types of paths: raw paths and parameterized paths.
- Raw paths are simply strings used directly as URLs. Nothing is substituted or even split. It's just normalized with all the parameters appended to the end.
- Parameterized paths let you insert values into paths, escaped by default for convenience and safety against URL injection.

For [`m.request`](request.md) and [`m.jsonp`](jsonp.md), these can be pretty much any URL, but for [routes](route.md), these can only be absolute URL path names without schemes or domains.
For [`m.request`](request.md) these can be pretty much any URL, but for [routes](route.md), these can only be absolute URL path names without schemes or domains.

### Path parameters

Expand All @@ -29,7 +29,7 @@ Path parameters are themselves pretty simple. They come in two forms:
- `:foo` - This injects a simple `params.foo` into the URL, escaping its value first.
- `:foo...` - This injects a raw `params.foo` path into the URL without escaping anything.

You're probably wondering what that `params` object is supposed to be. It's pretty simple: it's the `params` in either [`m.route.set(path, params)`](route.md#mrouteset), [`m.request({url, params})`](request.md#signature), or [`m.jsonp({url, params})`](jsonp.md#signature).
You're probably wondering what that `params` object is supposed to be. It's pretty simple: it's the `params` in either [`m.route.set(path, params)`](route.md#mrouteset), [`m.request({url, params})`](request.md#signature).

When receiving routes via [`m.route(root, defaultRoute, routes)`](route.md#signature), you can use these parameters to *extract* values from routes. They work basically the same way as generating the paths, just in the opposite direction.

Expand Down
2 changes: 1 addition & 1 deletion docs/redraw.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Documentation on m.redraw(), which schedules an update of all components mounted

Updates the DOM after a change in the application data layer.

You DON'T need to call it if data is modified within the execution context of an event handler defined in a Mithril.js view, or after request completion when using `m.request`/`m.jsonp`. The [autoredraw](autoredraw.md) system, which is built on top of `m.redraw()` will take care of it.
You DON'T need to call it if data is modified within the execution context of an event handler defined in a Mithril.js view, or after request completion when using `m.request`. The [autoredraw](autoredraw.md) system, which is built on top of `m.redraw()` will take care of it.

You DO need to call it in `setTimeout`/`setInterval`/`requestAnimationFrame` callbacks, or callbacks from 3rd party libraries.

Expand Down
2 changes: 1 addition & 1 deletion docs/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ Mithril.js' `m.request` uses `XMLHttpRequest` instead of `fetch()` for a number

Currently, due to lack of browser support, `fetch()` typically requires a [polyfill](https://github.com/github/fetch), which is over 11kb uncompressed - nearly three times larger than Mithril.js' XHR module.

Despite being much smaller, Mithril.js' XHR module supports many important and not-so-trivial-to-implement features like [URL interpolation](#dynamic-urls), querystring serialization and [JSON-P requests](jsonp.md), in addition to its ability to integrate seamlessly to Mithril.js' autoredrawing subsystem. The `fetch` polyfill does not support any of those, and requires extra libraries and boilerplates to achieve the same level of functionality.
Despite being much smaller, Mithril.js' XHR module supports many important and not-so-trivial-to-implement features like [URL interpolation](#dynamic-urls) and querystring serialization in addition to its ability to integrate seamlessly to Mithril.js' autoredrawing subsystem. The `fetch` polyfill does not support any of those, and requires extra libraries and boilerplates to achieve the same level of functionality.

In addition, Mithril.js' XHR module is optimized for JSON-based endpoints and makes that most common case appropriately terse - i.e. `m.request(url)` - whereas `fetch` requires an additional explicit step to parse the response data as JSON: `fetch(url).then(function(response) {return response.json()})`

Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ m.route = require("./route")
m.render = require("./render")
m.redraw = mountRedraw.redraw
m.request = request.request
m.jsonp = request.jsonp
m.parseQueryString = require("./querystring/parse")
m.buildQueryString = require("./querystring/build")
m.parsePathname = require("./pathname/parse")
Expand Down
19 changes: 0 additions & 19 deletions mithril.js
Original file line number Diff line number Diff line change
Expand Up @@ -1379,24 +1379,6 @@ var _17 = function($window, oncompletion) {
else if (body instanceof $window.FormData || body instanceof $window.URLSearchParams) xhr.send(body)
else xhr.send(JSON.stringify(body))
}),
jsonp: makeRequest(function(url, args, resolve, reject) {
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
var script = $window.document.createElement("script")
$window[callbackName] = function(data) {
delete $window[callbackName]
script.parentNode.removeChild(script)
resolve(data)
}
script.onerror = function() {
delete $window[callbackName]
script.parentNode.removeChild(script)
reject(new Error("JSONP request failed"))
}
script.src = url + (url.indexOf("?") < 0 ? "?" : "&") +
encodeURIComponent(args.callbackKey || "callback") + "=" +
encodeURIComponent(callbackName)
$window.document.documentElement.appendChild(script)
}),
}
}
var request = _17(typeof window !== "undefined" ? window : null, mountRedraw0.redraw)
Expand Down Expand Up @@ -1799,7 +1781,6 @@ m.route = _26(typeof window !== "undefined" ? window : null, mountRedraw)
m.render = render
m.redraw = mountRedraw.redraw
m.request = request.request
m.jsonp = request.jsonp
m.parseQueryString = parseQueryString
m.buildQueryString = buildQueryString
m.parsePathname = parsePathname
Expand Down
2 changes: 1 addition & 1 deletion mithril.min.js

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions request/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var buildPathname = require("../pathname/build")
var hasOwn = require("../util/hasOwn")

module.exports = function($window, oncompletion) {
var callbackCount = 0

function PromiseProxy(executor) {
return new Promise(executor)
}
Expand Down Expand Up @@ -197,23 +195,5 @@ module.exports = function($window, oncompletion) {
else if (body instanceof $window.FormData || body instanceof $window.URLSearchParams) xhr.send(body)
else xhr.send(JSON.stringify(body))
}),
jsonp: makeRequest(function(url, args, resolve, reject) {
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
var script = $window.document.createElement("script")
$window[callbackName] = function(data) {
delete $window[callbackName]
script.parentNode.removeChild(script)
resolve(data)
}
script.onerror = function() {
delete $window[callbackName]
script.parentNode.removeChild(script)
reject(new Error("JSONP request failed"))
}
script.src = url + (url.indexOf("?") < 0 ? "?" : "&") +
encodeURIComponent(args.callbackKey || "callback") + "=" +
encodeURIComponent(callbackName)
$window.document.documentElement.appendChild(script)
}),
}
}
116 changes: 0 additions & 116 deletions request/tests/test-jsonp.js

This file was deleted.

Loading

0 comments on commit 716d1e1

Please sign in to comment.