Commit graph

59 commits

Author SHA1 Message Date
Isiah Meadows
58f1c74394
Streamline route/request path handling and split params + body in requests (#2361)
Fixes #2360
Fixes #1138
Fixes #1788 a little less hackishly
Probably fixes a few other issues I'm not aware of.

This more or less goes with @lhorie's comment here, just with a minor name
change from `query` to `params`:

https://github.com/MithrilJS/mithril.js/issues/1138#issuecomment-231363395

Specifically, here's what this patch entails:

- I changed `data` and `useBody` to `params` and `body` in `m.request`.
  Migration is trivial: just use `params` or `body` depending on which you
  intend to send. Most servers do actually care where the data goes, so you can
  generally pretty easily translate this accordingly. If you *really* need the
  old behavior, pass the old value in `params` and if `method === "GET"` or
  `method === "TRACE"`, also in `body`.
- I opened up all methods to have request bodies.
- I fixed `m.parseQueryString` to prefer later values over earlier values and
  to ensure that objects and arrays are persisted across both hash and query
  param parsing. That method also accepts an existing key/value map to append
  to, to simplify deduplication.
- I normalized path interpolation to be identical between routes and requests.
- I no longer include interpolated values in query strings. If you need to
  duplicate values again, rename the interpolation to be a distinct property
  and pass the value you want to duplicate as it.
- I converted `m.route` to use pre-compiled routes instead of its existing
  system of dynamic runtime checking. This shouldn't have a major effect on
  performance short-term, but it'll ease the migration to built-in userland
  components and make it a little easier to reconcile. It'll also come handy
  for large numbers of routes.
- I added support for matching routes like `"/:file.:ext"` or
  `"/:lang-:region"`, giving each defined semantics.
- I added support for matching against routes with static query strings, such
  as `"/edit?type=image": { ... }`.
- I'm throwing a few new informative errors.
- And I've updated the docs accordingly.

I also made a few drive-by edits:

- I fixed a bug in the `Stream.HALT` warning where it warned all but the first
  usage when the intent was to warn only on first use.
- Some of the tests were erroneously using `Stream.HALT` when they should've
  been using `Stream.SKIP`. I've fixed the tests to only test that
  `Stream.HALT === Stream.SKIP` and that it only warns on first use.
- The `m.request` and `m.jsonp` docs signatures were improved to more clearly
  explain how `m.request(url, options?)` and `m.jsonp(url, options?)` translate
  to `m.request(options)` and `m.jsonp(options)` respectively.

-----

There is some justification to these changes:

- In general, it matters surprisingly more than you would expect how things
  translate to HTTP requests. So the comment there suggesting a thing that
  papers over the difference has led to plenty of confusion in both Gitter and
  in GitHub issues.

- A lot of servers expect a GET with a body and no parameters, and leaving
  `m.request` open to working with that makes it much more flexible.

- Sometimes, servers expect a POST with query parameters *instead* of a JSON
  object. I've seen this quite a bit, even with more popular REST APIs like
  Stack Overflow's.

- I've encountered a few servers that expect both parameters and a body, each
  with distinct semantic meaning, so the separation makes it much easier to
  translate into a request.

- Most of the time, path segments are treated individually, and URL-escaping
  the contents is much less error-prone. It also avoids being potentially
  lossy, and when the variable in question isn't trusted, escaping the path
  segment enables you to pass it through the URL and not risk being redirected
  to unexpected locations, avoiding some risks of vulnerabilities and client
  side crashes.

If you really don't care how the template and parameters translate to an
eventual URL, just pass the same object for the `params` and `body` and use
`:param...` for each segment. Either way, the more explicit nature should help
a lot in making the intent clearer, whether you care or not.
2019-05-29 09:28:40 -04:00
Oscar
db0b5e26ba fix small typo (#2408)
m.route.params() -> m.route.param()
2019-04-23 06:59:08 -04:00
Ayush Jain
982fdf5737 Corrected the case issue of 'JavaScript' throughout the docs (#2401)
* corrected JavaScript spelling
2019-04-01 08:14:38 -04:00
tebe
8e7f85f389 Fixed wrong setItem params (#2381) 2019-02-25 14:19:42 -05:00
Yogesh Khatri
a9172f1129 Feature onmatch route param (#2371)
* added route param in onmatch

* fix docs

* included test

* docs fix
2019-02-07 05:12:53 -05:00
Gilbert
eaa1c13664 Show how to use each routing strategy in docs (#2332) 2018-12-06 18:30:43 -05:00
Isiah Meadows
26b8d994ce
Remove m.prop + m.withAttr (#2317)
* Remove `m.prop` + `m.withAttr`

- For many uses, `m.withAttr` is *more* verbose than just directly using
  an event handler
- If you're using it with a bound callback, you're literally wasting a
  single character in the human readable version (and you're *saving*
  them in the minified output).
- It sometimes obscures your intent, if overused.
- Functions are easier to compress than `m.withAttr`, resulting in
  slightly smaller bundles.
- `m.withAttr` is overused anyways.
- `m.prop` is basically useless without `m.withAttr`, and the API
  doesn't have the same benefits it had with 0.2.x.

* Update changelog
2018-11-30 20:41:24 -05:00
Isiah Meadows
4a641092dc
Officially drop IE9-10 support, pull out our hacks (#2296)
- I also fixed a bunch of related comments
- I had to polyfill `requestAnimationFrame` for Node
- Drive-by: run `eslint . --fix`
- Drive-by: update transpiling info in CONTRIBUTING.md
- Drive-by: we aren't the only ones going semicolon-free
2018-11-27 18:04:15 -05:00
Miguel Espinoza
561b13c8a4 Add example for m.route.set with params (#2229)
Add example on how to use m.route.set with params
2018-11-07 18:47:49 -05:00
Pierre-Yves Gérardy
93c84a5786 [docs] typo 2018-05-10 09:35:03 +02:00
Pierre-Yves Gérardy
d13a61a4d8 [docs] clarify the component/RouteResolver distinction 2018-05-10 09:34:04 +02:00
Pat Cavit
c335aa70ca
docs: latest site updates (#2126) 2018-04-20 08:36:08 -07:00
Pat Cavit
6fb77b7771
docs: update with latest fixes (#2116) 2018-04-12 00:11:11 -07:00
Pierre-Yves Gérardy
085f5642be Docs: Correct typos [skip CI] 2017-05-04 16:28:06 +02:00
Pierre-Yves Gérardy
0a1c634d99 Clarify m.route.params in RouteResolver.onmatch 2017-05-04 16:05:39 +02:00
Yuri
a560bca85d Improve the documentation of routing parameters
I was stuck on this for a while earlier today: it turns out that `m.route.param` will not return the right results in the `onmatch` function on a `RouterResolver`, though it will inside of a `render` function on the same. This behavior is documented in the `onmatch` section but not the `m.route.param` section, which is where I was looking when I ran up against this earlier.

From an API design perspective it would make sense to me for `m.route.param` to return valid results inside `onmatch`, but I don't know enough about Mithril's internals to know how complex it would be to implement.
2017-05-03 21:21:57 -04:00
David
e26e1f10c0 docs: URL Param + Variadic route doc (#1813) 2017-04-26 09:34:28 -07:00
Pierre-Yves Gérardy
c08b3f3c20 docs: more m.route docs fixes (#1712) 2017-03-14 16:31:57 -07:00
Leo
1d48a30936 m.route.param docs #1711 2017-03-14 18:32:23 -04:00
Красимир Беров
72359a76e3 Remove an obsolete line (#1681) 2017-03-04 23:38:34 +01:00
spacejack
fc4d5f1892 resolver.render return type description consistent with m.render 2017-02-12 15:14:06 -05:00
spacejack
7e0a1db89f API docs signature fixes for route resolver, jsonp, fragment 2017-02-11 21:31:10 -05:00
Yoshiki Shibukawa
c7e0b204f8 docs: Update route.md (#1600)
Fix typo
2017-02-07 23:02:39 -08:00
Leo Horie
24b7150b3c tweaks in docs 2017-01-15 10:35:29 -05:00
Leo Horie
d397e66e03 lint links 2017-01-08 21:29:26 -05:00
Leo
d40b324645 doc fixes 2017-01-08 14:47:38 -05:00
Leo Horie
3bac29bf78 Clarify distinctions between RouteResolvers and components 2017-01-08 11:34:56 -05:00
Patrik Johnson
4c84eafbdd Remove extra code from example that is not required, since it's confusingly similar to previous block 2017-01-05 15:08:43 +02:00
Leo Horie
dc72aaa35b beef up the docs about differences between route resolvers and components 2016-12-20 11:15:10 -05:00
Leo Horie
de07a54881 add support for route state #1480 2016-12-17 21:50:04 -05:00
Leo Horie
55968edf34 make note in docs about m.route singletonness 2016-12-16 15:28:07 -05:00
Leo Horie
b9a479d45e lint docs 2016-12-07 12:45:42 -05:00
Leo Horie
04b860cefc refer to relevant section from onmatch docs 2016-12-07 11:18:54 -05:00
Leo Horie
549b773fea Merge remote-tracking branch 'origin/rewrite' into rewrite 2016-12-07 10:10:08 -05:00
Leo Horie
ae916664de tweaks to docs 2016-12-07 10:09:49 -05:00
John Long
30d130cc7a Update route docs to reference state var 2016-12-07 07:57:32 -07:00
Leo Horie
f1f52445ec more router fixes 2016-12-07 07:57:58 -05:00
Leo Horie
846d6ce24b router fixes 2016-12-06 10:44:25 -05:00
Leo Horie
3fb160c12e Merge remote-tracking branch 'origin/rewrite' into rewrite
# Conflicts:
#	docs/route.md
2016-12-06 00:10:30 -05:00
Leo Horie
3134202d24 fix tests 2016-12-06 00:09:09 -05:00
Pat Cavit
4e9505335c Fix TOC link, remove m.request.run ref (#1456) 2016-12-05 11:41:20 -08:00
Leo Horie
cdb9017a72 update docs, add descriptions for api methods 2016-12-02 17:51:07 -05:00
Leo Horie
87dcbbf5c7 small tweaks to docs 2016-12-01 01:49:19 -05:00
Leo Horie
f49fd1c6a2 Merge remote-tracking branch 'origin/rewrite' into rewrite 2016-10-03 10:56:47 -04:00
Leo Horie
d0a4993681 clean up 2016-10-03 10:56:44 -04:00
Hugo Freitas
45ebf2f2db Fix RouteResolver link 2016-09-29 12:45:12 -03:00
Pierre-Yves Gérardy
67433a7816 Correct the RouteResolver.onmatch() signature (#1327) 2016-09-20 19:44:19 -07:00
Leo Horie
55f0dfa4dd Merge remote-tracking branch 'origin/rewrite' into rewrite
Conflicts:
	docs/route.md
2016-09-12 09:48:30 -04:00
Leo Horie
8492e0c733 route docs tweak 2016-09-12 09:47:45 -04:00
Pierre-Yves Gérardy
837c92349b docs/route.md: fix inner links (#1302) 2016-09-08 11:48:29 -07:00