1- # MetroJS: HTTPS Client with middleware
2-
1+ [ ![ GitHub License] ( https://img.shields.io/github/license/muze-nl/metro )] ( https://github.com/muze-nl/metro/blob/main/LICENSE )
2+ [ ![ GitHub package.json version] ( https://img.shields.io/github/package-json/v/muze-nl/metro )] ( )
3+ [ ![ NPM Version] ( https://img.shields.io/npm/v/@muze-nl/metro )] ( https://www.npmjs.com/package/@muze-nl/metro )
4+ [ ![ npm bundle size] ( https://img.shields.io/bundlephobia/min/@muze-nl/metro )] ( https://www.npmjs.com/package/@muze-nl/metro )
35[ ![ Project stage: Experimental] [ project-stage-badge: Experimental ]] [ project-stage-page ]
46
7+ # MetroJS: HTTPS Client with middleware
8+
59``` javascript
610import * as metro from ' @muze-nl/metro'
711
@@ -24,6 +28,17 @@ const client = metro.client({
2428 return res .with ({ body })
2529})
2630```
31+ ## Table of Contents
32+ 1 . [ Introduction] ( #introduction )
33+ 2 . [ Quickstart] ( docs/quickstart.md )
34+ 3 . [ Usage] ( #usage )
35+ 4 . [ Middleware] ( #middleware )
36+ 5 . [ Documentation] ( docs/ ) - See also [ metro.muze.nl] ( https://metro.muze.nl/ )
37+ 6 . [ Contributions] ( CONTIBRUTING.md )
38+ 7 . [ License] ( #license )
39+
40+ <a name =" introduction " ></a >
41+ ## Introduction
2742
2843MetroJS is an HTTPS client with support for middleware. Similar to [ ExpressJS] ( https://expressjs.com/ ) , but for the client.
2944
@@ -47,7 +62,8 @@ Both metro.request() and metro.response() are compatible with the normal Request
4762and Response objects, used by the Fetch API. Any code that works with those, will work
4863with the request and response objects in MetroJS.
4964
50- ## Install / Usage
65+ <a name =" usage " ></a >
66+ ## Usage
5167
5268``` bash
5369npm install @muze-nl/metro
@@ -75,6 +91,7 @@ async function main() {
7591}
7692```
7793
94+ <a name =" middleware " ></a >
7895## Using middleware
7996A middleware is a function with ` (request, next) ` as parameters, returning a ` response ` .
8097Both request and response adhere to the [ Fetch API] ( https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API )
@@ -99,60 +116,13 @@ async function myMiddleware(req,next) {
99116Both request and response have a ` with ` function. This allows you to create a new request or response, from
100117the existing one, with one or more options added or changed. The original request or response is not changed.
101118
102- ## Existing middleware
103-
104- With @muze-nl/metro , you get these middleware modules:
105- - [ json] ( ./docs/middleware/json.md )
106- - [ thrower] ( ./docs/middleware/thrower.md )
107- - [ echo mock] ( ./docs/middleware/echomock.md )
108- - [ error mock] ( ./docs/middleware/errormock.md )
109-
110- In addition work is ongoing on these separate middleware libraries:
111- - [ @muze-nl/metro-oauth2 ] ( https://github.com/muze-nl/metro-oauth2/ )
112- - [ @muze-nl/metro-oidc ] ( https://github.com/muze-nl/metro-oidc/ )
113-
114- ## Debugging
115-
116- Middleware is powerful, but can also be difficult to debug. For this reason MetroJS adds a trace feature. This
117- allows you to add a request and response tracer function, which is called before and after each middleware call:
118-
119- ``` javascript
120- const client = metro .client ()
121- metro .trace .add (' mytracer' , {
122- request : (req ) => {
123- console .log (' request' ,req)
124- },
125- response : (res ) => {
126- console .log (' response' ,res)
127- }
128- })
129- ```
130-
131- There is a default trace function that shows the call request/response in a nested fashion:
132-
133- ``` javascript
134- metro .trace .add (' group' , metro .trace .group ())
135- ```
119+ [ Read more about middleware] ( docs/middleware/ )
136120
137- ## Creating middleware
121+ <a name =" license " ></a >
122+ ## License
138123
139- You can just create a async function with ` (req,next) => res ` as its signature. But often it is important
140- to be able to set options specific for that middleware. The best way to do this is to create a module like
141- so:
142-
143- ``` javascript
144- export default function myMiddleware (options )
145- {
146- return async (req ,next ) => {
147- // alter request, using options
148- let res = await next (req)
149- // alter response, using options
150- return res
151- }
152- }
153- ```
124+ This software is licensed under MIT open source license. See the [ License] ( ./LICENSE ) file.
154125
155- See for example the [ jsonmw] ( src/mw/json.mjs ) middleware.
156126
157127[ project-stage-badge: Experimental ] : https://img.shields.io/badge/Project%20Stage-Experimental-yellow.svg
158128[ project-stage-page ] : https://blog.pother.ca/project-stages/
0 commit comments