@trenskow/app 0.6.11 → 0.6.14
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 +30 -37
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -216,7 +216,7 @@ Endpoints has the [`mount`](#mount) method, which "mounts" a router to the speci
|
|
|
216
216
|
|
|
217
217
|
There is a variant of the `mount` method called [`parameter`](#parameter) which is used to mount an endpoint with a dynamic path – whereas the path is treated like an input parameter (like express' `.param` method). Parameters also supports a transform function, which is able to transform the parameter into something else (eg. a user identifier into a user object).
|
|
218
218
|
|
|
219
|
-
Lastly there is the [`.middleware`](#middleware) method, which is used to attach middleware. Middleware is defined as a router, which
|
|
219
|
+
Lastly there is the [`.middleware`](#middleware) method, which is used to attach middleware. Middleware is defined as a router, which have the type [`Router`](#router-2) and therefore cannot act as an endpoint. You can regard them like transforms or service providers for the request.
|
|
220
220
|
|
|
221
221
|
> `Endpoint` extends `Router`.
|
|
222
222
|
|
|
@@ -228,9 +228,9 @@ All handlers and routers support async functions (and non-async). No need to cal
|
|
|
228
228
|
|
|
229
229
|
Where express gives you the `(req, res, next)` parameters for each handler, this application instead just provides a single parameter, the "`context` object", which contains all the information needed to process the request.
|
|
230
230
|
|
|
231
|
-
Middleware can
|
|
231
|
+
Middleware can assign values to the context to provide data and services, which is then available for subsequent endpoints, routers and handlers.
|
|
232
232
|
|
|
233
|
-
When a request is incoming, the `context`object looks like this.
|
|
233
|
+
When a request is incoming, the `context` object looks like this.
|
|
234
234
|
|
|
235
235
|
| Name | Description | Type |
|
|
236
236
|
| ---------------- | ------------------------------------------------------------ | :-------------------------: |
|
|
@@ -241,7 +241,7 @@ When a request is incoming, the `context`object looks like this.
|
|
|
241
241
|
| `path` | An object that has properties representing different paths. | Object |
|
|
242
242
|
| `path.full` | An array of strings that joined represent the path of the fully requested path. | Array of String |
|
|
243
243
|
| `path.current` | An array of strings that joined represents the path currently being processed. | Array of String |
|
|
244
|
-
| `path.remaining` | An array of strings that joined represents the path that is above the currently processed path. Setting this will rewrite the remaining path. | Array of String |
|
|
244
|
+
| `path.remaining` | An array of strings that joined represents the path that is above the currently processed path. Setting this will rewrite the remaining path (useful when serving single page applications to a browser). | Array of String |
|
|
245
245
|
| `query` | An object holding the URL query parameters as an object ([keys has been converted to camel case](#query-parameters)). | Object |
|
|
246
246
|
| `state` | A string indicating the current state of the request – possible values are `'routing'`, `'rendering'`, `'completed'` or `'aborted'`. | String |
|
|
247
247
|
| `abort` | A function that aborts the request. It takes the parameters `(error, brutally)`, where `error` is the error that needs to be handled by the [renderer](#renderer) – and `brutally` which indicates if the connection should also be closed. | AsyncFunction |
|
|
@@ -267,15 +267,15 @@ JavaScript is a camel cased language. HTTP is a mixture of different case types.
|
|
|
267
267
|
|
|
268
268
|
Case is automatically converted in both directions, so if you do `context.response.headers.contentType = 'application/json'` it will automatically be converted to `Content-Type: application/json` when the response is sent.
|
|
269
269
|
|
|
270
|
-
The same goes for request headers like `Accept-Language: en`
|
|
270
|
+
The same goes for request headers like `Accept-Language: en` which is accessible through `context.request.headers.acceptLanguage`.
|
|
271
271
|
|
|
272
272
|
##### Query parameters
|
|
273
273
|
|
|
274
|
-
Request with `?my-parameter=value` is accessible through `context.query.myParameter` .
|
|
274
|
+
Request with quuries like `?my-parameter=value` is accessible through `context.query.myParameter` .
|
|
275
275
|
|
|
276
276
|
##### Mount paths
|
|
277
277
|
|
|
278
|
-
When [match mode](#constructor) is set to `'loosely'` (default) a request
|
|
278
|
+
When [match mode](#constructor) is set to `'loosely'` (default) a request with the path component `my-route` or `my_route` will match an endpoint mounted at `myRoute`.
|
|
279
279
|
|
|
280
280
|
#### Endpoints, routers and handlers
|
|
281
281
|
|
|
@@ -287,22 +287,13 @@ Endpoints takes care of a path component. As example the `/this/is/my/path/` pat
|
|
|
287
287
|
|
|
288
288
|
Endpoints can have a couple of things mounted / attached to it – those are.
|
|
289
289
|
|
|
290
|
-
* Other endpoints
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
* Parameters
|
|
290
|
+
* Other endpoints (using the [`.mount`](#mount) method of [`Endpoint`](#endpoint-2))
|
|
291
|
+
* Parameters (using the [`.parameter`](#parameter) method of [`Endpoint`](#endpoint-2))
|
|
294
292
|
* which is also a mounted endpoint – but where the path is dynamic and assigned to the `context.parameters` object.
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
* Handlers
|
|
298
|
-
* using the [`.use`](#use) method of [`Router`](#router-2) or [`Endpoint`](#endpoint-2)
|
|
299
|
-
|
|
300
|
-
* Middleware
|
|
293
|
+
* Handlers (using the [`.use`](#use) method of [`Router`](#router-2) or [`Endpoint`](#endpoint-2) )
|
|
294
|
+
* Middleware (using the [`.middleware`](#middleware) method of [`Endpoint`](#endpoint-2) )
|
|
301
295
|
* sets a [`Router`](#routers-2) router to that the request will be passed through.
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
* Methods
|
|
305
|
-
* using the [`.get`, `.post`, `.put`, `.delete`, etc.](#get-post-put-delete-etc) method of [`Endpoint`](#endpoint-2)
|
|
296
|
+
* Methods (using the [`.get`, `.post`, `.put`, `.delete`, etc.](#get-post-put-delete-etc) method of [`Endpoint`](#endpoint-2))
|
|
306
297
|
* When a method returns the request ends and the returned value is send to the client as a response (through the [`Application#renderer`](#renderer))
|
|
307
298
|
|
|
308
299
|
|
|
@@ -311,7 +302,7 @@ Endpoints can have a couple of things mounted / attached to it – those are.
|
|
|
311
302
|
Whenever a function (such as [`.mount`](#mount) or [`.parameter`](#parameters) or [`.root`](#root)) takes an endpoint as a parameter, it can be provided in any of the following ways.
|
|
312
303
|
|
|
313
304
|
* An instance of [`Endpoint`](#endpoint-2).
|
|
314
|
-
* An object that has a `default` key that has an instance of `Endpoint` as the value (useful when using inline imports).
|
|
305
|
+
* An object that has a `default` key that has an instance of `Endpoint` as the value (useful when using inline imports as `await import('my-endpoint.js')`).
|
|
315
306
|
|
|
316
307
|
##### Routers
|
|
317
308
|
|
|
@@ -322,7 +313,7 @@ A router is the same as above, except it only supports [`.use`](#use).
|
|
|
322
313
|
As above, whenever a function takes a router as a parameter, it can be provided in any of the following ways.
|
|
323
314
|
|
|
324
315
|
* An instance of [`Router`](#router-2).
|
|
325
|
-
* An object that has a `default` key that has an instance of `Router` as the value (useful when using inline imports).
|
|
316
|
+
* An object that has a `default` key that has an instance of `Router` as the value (useful when using inline imports as `await import('my-route.js')`).).
|
|
326
317
|
|
|
327
318
|
##### Handlers
|
|
328
319
|
|
|
@@ -340,7 +331,7 @@ Whenever a function takes a handler as a parameter, it can be provided in any of
|
|
|
340
331
|
|
|
341
332
|
### `Application`
|
|
342
333
|
|
|
343
|
-
The `Application` class holds an application and is responsible for handling and bootstrapping request from the server. It does not provide any routing on
|
|
334
|
+
The `Application` class holds an application and is responsible for handling and bootstrapping request from the server. It does not provide any routing on its own, instead it has a [`root`](#root) method, which is used to set the root router.
|
|
344
335
|
|
|
345
336
|
> If no root route has been set, all requests will be responded with `404 Not Found`.
|
|
346
337
|
|
|
@@ -352,17 +343,17 @@ The `Application` class takes an "options" object as it's parameter.
|
|
|
352
343
|
|
|
353
344
|
##### Parameters
|
|
354
345
|
|
|
355
|
-
| Name | Description
|
|
356
|
-
| ------------------------ |
|
|
357
|
-
| options | An object representing the options.
|
|
358
|
-
| `options.port` | The port at which to listen for incoming connections.
|
|
346
|
+
| Name | Description | Type | Required | Default value |
|
|
347
|
+
| ------------------------ | ------------------------------------------------------------ | :-----------------------: | :------: | :--------------------------: |
|
|
348
|
+
| options | An object representing the options. | Object | | {} |
|
|
349
|
+
| `options.port` | The port at which to listen for incoming connections. | Number | | `0` (automatically assigned) |
|
|
359
350
|
| `options.RequestType` | An object that inherits from the [`Request`](#request-2) class (an `http.IncomingMessage` subclass) that is used as the request object in routes. | class | | [`Request`](#Request) |
|
|
360
|
-
| `options.ResponseType` | An object that inherits from the [`Response`](#response-2) class (`http.ServerResponse` subclass) that is used as the response object in routes.
|
|
361
|
-
| `path` | An object that represents path related options.
|
|
362
|
-
| `path.matchMode` | Indicates [how to match requests to mounted paths](#mount-paths).
|
|
363
|
-
| `options.server` | An object that represents how to instantiate the HTTP server.
|
|
364
|
-
| `options.server.create` | A function that is able to create a server.
|
|
365
|
-
| `options.server.options` | An object to be passed as options when creating a server.
|
|
351
|
+
| `options.ResponseType` | An object that inherits from the [`Response`](#response-2) class (`http.ServerResponse` subclass) that is used as the response object in routes. | class | | [`Response`](#Response) |
|
|
352
|
+
| `path` | An object that represents path related options. | Object | | `{}` |
|
|
353
|
+
| `path.matchMode` | Indicates [how to match requests to mounted paths](#mount-paths) (eg. should the path be converted to camel case). | `'loosely'` or `'strict'` | | `'loosely'` |
|
|
354
|
+
| `options.server` | An object that represents how to instantiate the HTTP server. | Object | | `{}` |
|
|
355
|
+
| `options.server.create` | A function that is able to create a server. | Function | | `http.createServer` |
|
|
356
|
+
| `options.server.options` | An object to be passed as options when creating a server. | Object | | `{}` |
|
|
366
357
|
|
|
367
358
|
#### Events
|
|
368
359
|
|
|
@@ -499,7 +490,7 @@ You can only call these methods once per method per endpoint – calling it mult
|
|
|
499
490
|
|
|
500
491
|
These also ends routing. After a method route has been called, the routing will go strait to the renderer.
|
|
501
492
|
|
|
502
|
-
> Notice: If no `head` method is implemented on endpoint, `get` will instead be called (if
|
|
493
|
+
> Notice: If no `head` method is implemented on endpoint, `get` will instead be called (if ). When client requests a `head` the result will be ignored.
|
|
503
494
|
|
|
504
495
|
> Returns the endpoint.
|
|
505
496
|
|
|
@@ -518,11 +509,13 @@ Below is an example on how to use the method.
|
|
|
518
509
|
````javascript
|
|
519
510
|
default export ({ endpoint }) => {
|
|
520
511
|
endpoint
|
|
521
|
-
.get(
|
|
512
|
+
.get(
|
|
513
|
+
async (context) => 'Hello, world!',
|
|
514
|
+
() => console.info("Said hello."));
|
|
522
515
|
};
|
|
523
516
|
````
|
|
524
517
|
|
|
525
|
-
> In the above example `'Hello, world!'` is immediately send to the [renderer](#renderer) and the request ends.
|
|
518
|
+
> In the above example `'Hello, world!'` is immediately send to the [renderer](#renderer) and the request ends. The second handler is also executed, but as it returns `undefined` its return value is ignored.
|
|
526
519
|
|
|
527
520
|
###### Catch all
|
|
528
521
|
|