routup 3.0.0-alpha.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -61,6 +61,8 @@ as all concepts and basics are taught there.
61
61
  ### Handlers
62
62
 
63
63
  Both core and error handlers, can be defined in two different ways.
64
+ Core handler functions can have up to 3 arguments (req, res, next) whereas error handler functions can have up to 4 arguments (err, req, res, next).
65
+ This should be familiar to anyone who has used express before.
64
66
 
65
67
  **`Shorthand`**
66
68
 
@@ -187,17 +189,17 @@ According to the fact that routup is a minimalistic framework,
187
189
  it depends on [plugins](https://github.com/routup/plugins) to cover some
188
190
  typically http framework functions, which are not integrated in the main package.
189
191
 
190
- | Name | Description |
191
- |-----------------------------------------------------------------------------------------------|------------------------------------------------------------------------|
192
- | [body](https://github.com/routup/plugins/tree/master/packages/body) | Read and parse the request body. |
193
- | [cookie](https://github.com/routup/plugins/tree/master/packages/cookie) | Read and parse request cookies and serialize cookies for the response. |
194
- | [decorators](https://github.com/routup/plugins/tree/master/packages/decorators) | Create request handlers with class-, method- & parameter-decorators. |
195
- | [prometheus](https://github.com/routup/plugins/tree/master/packages/prometheus) | Collect and serve metrics for prometheus. |
196
- | [query](https://github.com/routup/plugins/tree/master/packages/query) | Read and parse the query string of the request url. |
197
- | [rate-limit](https://github.com/routup/plugins/tree/master/packages/rate-limit) | Rate limit incoming requests. |
198
- | [rate-limit-redis](https://github.com/routup/plugins/tree/master/packages/rate-limit-redis) | Redis adapter for the rate-limit plugin. |
199
- | [static](https://github.com/routup/plugins/tree/master/packages/static) | Serve static files from a directory. |
200
- | [swagger](https://github.com/routup/plugins/tree/master/packages/swagger) | Serve generated docs from URL or based on a JSON file. |
192
+ | Name | Description |
193
+ |---------------------------------------------------------------------------------------------|------------------------------------------------------------------------|
194
+ | [assets](https://github.com/routup/plugins/tree/master/packages/assets) | Serve static files from a directory. |
195
+ | [body](https://github.com/routup/plugins/tree/master/packages/body) | Read and parse the request body. |
196
+ | [cookie](https://github.com/routup/plugins/tree/master/packages/cookie) | Read and parse request cookies and serialize cookies for the response. |
197
+ | [decorators](https://github.com/routup/plugins/tree/master/packages/decorators) | Create request handlers with class-, method- & parameter-decorators. |
198
+ | [prometheus](https://github.com/routup/plugins/tree/master/packages/prometheus) | Collect and serve metrics for prometheus. |
199
+ | [query](https://github.com/routup/plugins/tree/master/packages/query) | Read and parse the query string of the request url. |
200
+ | [rate-limit](https://github.com/routup/plugins/tree/master/packages/rate-limit) | Rate limit incoming requests. |
201
+ | [rate-limit-redis](https://github.com/routup/plugins/tree/master/packages/rate-limit-redis) | Redis adapter for the rate-limit plugin. |
202
+ | [swagger](https://github.com/routup/plugins/tree/master/packages/swagger) | Serve generated docs from URL or based on a JSON file. |
201
203
 
202
204
  ## Benchmarks
203
205
 
package/dist/index.cjs CHANGED
@@ -1656,10 +1656,7 @@ function isPlugin(input) {
1656
1656
  if (typeof input.name !== 'undefined' && typeof input.name !== 'string') {
1657
1657
  return false;
1658
1658
  }
1659
- if (typeof input.install !== 'function' || input.install.length !== 1) {
1660
- return false;
1661
- }
1662
- return typeof input.version === 'undefined' || typeof input.version === 'string';
1659
+ return typeof input.install === 'function' && input.install.length === 1;
1663
1660
  }
1664
1661
 
1665
1662
  function transformRouterOptions(input) {
@@ -1781,110 +1778,50 @@ class Router {
1781
1778
  }
1782
1779
  return false;
1783
1780
  }
1784
- delete(path, handler) {
1785
- if (isPath(path)) {
1786
- this.use({
1787
- ...handler,
1788
- method: exports.MethodName.DELETE,
1789
- path
1790
- });
1791
- return this;
1792
- }
1793
- this.use({
1794
- ...path,
1795
- method: exports.MethodName.DELETE
1796
- });
1781
+ delete(...input) {
1782
+ this.useForMethod(exports.MethodName.DELETE, ...input);
1797
1783
  return this;
1798
1784
  }
1799
- get(path, handler) {
1800
- if (isPath(path)) {
1801
- this.use({
1802
- ...handler,
1803
- method: exports.MethodName.GET,
1804
- path
1805
- });
1806
- return this;
1807
- }
1808
- this.use({
1809
- ...path,
1810
- method: exports.MethodName.GET
1811
- });
1785
+ get(...input) {
1786
+ this.useForMethod(exports.MethodName.GET, ...input);
1812
1787
  return this;
1813
1788
  }
1814
- post(path, handler) {
1815
- if (isPath(path)) {
1816
- this.use({
1817
- ...handler,
1818
- method: exports.MethodName.POST,
1819
- path
1820
- });
1821
- return this;
1822
- }
1823
- this.use({
1824
- ...path,
1825
- method: exports.MethodName.POST
1826
- });
1789
+ post(...input) {
1790
+ this.useForMethod(exports.MethodName.POST, ...input);
1827
1791
  return this;
1828
1792
  }
1829
- put(path, handler) {
1830
- if (isPath(path)) {
1831
- this.use({
1832
- ...handler,
1833
- method: exports.MethodName.PUT,
1834
- path
1835
- });
1836
- return this;
1837
- }
1838
- this.use({
1839
- ...path,
1840
- method: exports.MethodName.PUT
1841
- });
1793
+ put(...input) {
1794
+ this.useForMethod(exports.MethodName.PUT, ...input);
1842
1795
  return this;
1843
1796
  }
1844
- patch(path, handler) {
1845
- if (isPath(path)) {
1846
- this.use({
1847
- ...handler,
1848
- method: exports.MethodName.PATCH,
1849
- path
1850
- });
1851
- return this;
1852
- }
1853
- this.use({
1854
- ...path,
1855
- method: exports.MethodName.PATCH
1856
- });
1797
+ patch(...input) {
1798
+ this.useForMethod(exports.MethodName.PATCH, ...input);
1857
1799
  return this;
1858
1800
  }
1859
- head(path, handler) {
1860
- if (isPath(path)) {
1861
- this.use({
1862
- ...handler,
1863
- method: exports.MethodName.HEAD,
1864
- path
1865
- });
1866
- return this;
1867
- }
1868
- this.use({
1869
- ...path,
1870
- method: exports.MethodName.HEAD
1871
- });
1801
+ head(...input) {
1802
+ this.useForMethod(exports.MethodName.HEAD, ...input);
1803
+ return this;
1804
+ }
1805
+ options(...input) {
1806
+ this.useForMethod(exports.MethodName.OPTIONS, ...input);
1872
1807
  return this;
1873
1808
  }
1874
- options(path, handler) {
1875
- if (isPath(path)) {
1809
+ // --------------------------------------------------
1810
+ useForMethod(method, ...input) {
1811
+ const base = {
1812
+ method
1813
+ };
1814
+ for(let i = 0; i < input.length; i++){
1815
+ const element = input[i];
1816
+ if (isPath(element)) {
1817
+ base.path = element;
1818
+ continue;
1819
+ }
1876
1820
  this.use({
1877
- ...handler,
1878
- method: exports.MethodName.OPTIONS,
1879
- path
1821
+ ...base,
1822
+ ...element
1880
1823
  });
1881
- return this;
1882
1824
  }
1883
- this.use({
1884
- ...path,
1885
- method: exports.MethodName.OPTIONS
1886
- });
1887
- return this;
1888
1825
  }
1889
1826
  use(...input) {
1890
1827
  const modifyPath = (input)=>{
@@ -1910,6 +1847,7 @@ class Router {
1910
1847
  if (isHandler(item)) {
1911
1848
  item.path = path || modifyPath(item.path);
1912
1849
  this.stack.push(new Layer(item));
1850
+ continue;
1913
1851
  }
1914
1852
  if (isPlugin(item)) {
1915
1853
  if (path) {
@@ -1926,15 +1864,15 @@ class Router {
1926
1864
  // --------------------------------------------------
1927
1865
  install(plugin, context = {}) {
1928
1866
  const name = context.name || plugin.name;
1867
+ const router = new Router({
1868
+ name
1869
+ });
1870
+ plugin.install(router);
1929
1871
  if (context.path) {
1930
- const router = new Router({
1931
- name
1932
- });
1933
- plugin.install(router);
1934
1872
  this.use(context.path, router);
1935
- return this;
1873
+ } else {
1874
+ this.use(router);
1936
1875
  }
1937
- plugin.install(this);
1938
1876
  return this;
1939
1877
  }
1940
1878
  // --------------------------------------------------