|
| 1 | +## The following is for the BETA version only! |
| 2 | + |
| 3 | +You may install the beta via: |
| 4 | + |
| 5 | +`npm install swagger@beta -g` |
| 6 | + |
| 7 | +## Configuration |
| 8 | + |
| 9 | +Swagger-Node application configuration is driven by the file `default.yaml` in the application's config directory. By |
| 10 | + default, the configuration file looks something like this: |
| 11 | + |
| 12 | +```yaml |
| 13 | +# swagger configuration file |
| 14 | + |
| 15 | +# values in the swagger hash are system configuration for swagger-node |
| 16 | +swagger: |
| 17 | + |
| 18 | + fittingsDirs: [ api/fittings, node_modules ] |
| 19 | + defaultPipe: null |
| 20 | + swaggerControllerPipe: swagger_controllers # defines the standard processing pipe for controllers |
| 21 | + |
| 22 | + # values defined in the bagpipes key are the bagpipes pipes and fittings definitions |
| 23 | + # (see https://github.com/apigee-127/bagpipes) |
| 24 | + bagpipes: |
| 25 | + |
| 26 | + _router: |
| 27 | + name: swagger_router |
| 28 | + mockMode: false |
| 29 | + mockControllersDirs: [ api/mocks ] |
| 30 | + controllersDirs: [ api/controllers ] |
| 31 | + |
| 32 | + _swagger_validate: |
| 33 | + name: swagger_validator |
| 34 | + validateResponse: true |
| 35 | + |
| 36 | + # pipe for all swagger-node controllers |
| 37 | + swagger_controllers: |
| 38 | + - onError: json_error_handler |
| 39 | + - cors |
| 40 | + - swagger_security |
| 41 | + - _swagger_validate |
| 42 | + - express_compatibility |
| 43 | + - _router |
| 44 | + |
| 45 | + # pipe to serve swagger (endpoint is in swagger.yaml) |
| 46 | + swagger_raw: |
| 47 | + name: swagger_raw |
| 48 | + |
| 49 | +# any other values in this file are just loaded into the config for application access... |
| 50 | +``` |
| 51 | + |
| 52 | +Important things to note: |
| 53 | + |
| 54 | +* All configuration for the Swagger-Node system is under the `swagger` key |
| 55 | +* Overall system behavior is driven by configuring the [Bagpipes](https://github.com/apigee-127/bagpipes) library |
| 56 | +* You may include other values and sections as you wish, they will just be loaded into the config for your application |
| 57 | + to access. |
| 58 | + |
| 59 | +Let's walk through the configuration: |
| 60 | + |
| 61 | +### fittingsDirs |
| 62 | + |
| 63 | +Defines the directories Bagpipes will search for fittings that are defined or used in the bagpipes section below. |
| 64 | + |
| 65 | +### defaultPipe |
| 66 | + |
| 67 | +If no pipe is explicitly declared for a path or operation, this pipe will be played when that endpoint is hit. |
| 68 | + |
| 69 | +### swaggerControllerPipe |
| 70 | + |
| 71 | +This names the standard pipe that plays for the swagger-node controllers (declared in the swagger.yaml with the |
| 72 | +extension `x-swagger-router-controller`). We'll look at how that's defined in a second. |
| 73 | + |
| 74 | +### bagpipes |
| 75 | + |
| 76 | +This block is the configuration passed to the [bagpipes](https://github.com/apigee-127/bagpipes) underlying the |
| 77 | +application. As you can see, it defines not only the flow, but configuration of the elements as well. Let's look at |
| 78 | +those as well. |
| 79 | + |
| 80 | +#### _router |
| 81 | + |
| 82 | +This configures the standard swagger-node router (currently swagger-tools). It tells it how to find your controllers, |
| 83 | +your mock controllers, and whether to run in mock mode. |
| 84 | + |
| 85 | +#### _swagger_validate |
| 86 | + |
| 87 | +This configures the swagger validator (currently swagger-tools). You can turn response validation on and off here. |
| 88 | + |
| 89 | +#### swagger_controllers |
| 90 | + |
| 91 | +Because this is specified as your controller pipe (in the `swaggerControllerPipe` setting above), this pipe plays |
| 92 | +for all paths and operations where you'veare specified a controller extension (`x-swagger-router-controller`). |
| 93 | + |
| 94 | +The default pipe is as follows: |
| 95 | + |
| 96 | +1. set an error handler that converts all errors to JSON |
| 97 | +2. run the [cors](https://www.npmjs.com/package/cors) module |
| 98 | +3. execute swagger security (currently swagger-tools) |
| 99 | +4. run swagger validator (currently swagger-tools) |
| 100 | +5. add a few commonly used Express functions (if not already present) to request (path, query, get) and response (json, |
| 101 | + get, set, status). |
| 102 | +6. run the router (currently swagger-tools) |
| 103 | + |
| 104 | +As you can see, you have full control over the pipeline and may add or remove elements you need for your specific |
| 105 | +application and environment. |
| 106 | + |
| 107 | +#### swagger_raw |
| 108 | + |
| 109 | +This serves your swagger file - on the path that is defined in your `api/swagger/swagger.yaml` and tagged with the |
| 110 | +`x-swagger-pipe` extension. It looks like this: |
| 111 | + |
| 112 | +```yaml |
| 113 | + /swagger: |
| 114 | + x-swagger-pipe: swagger_raw |
| 115 | +``` |
| 116 | +
|
| 117 | +Note: This automatically filters out all sections that are swagger extensions (`x-*`) by using a predefined regular |
| 118 | +expression: `/^(?!x-.*)/`. |
| 119 | + |
| 120 | +Naturally, if you don't wish to serve your swagger on this path or at all, you may change or remove this. |
| 121 | + |
| 122 | +This also conveniently serves as an example of how to map a path in your Swagger to a pipe. You may, of course, define |
| 123 | +and use any pipes you wish using any of the Bagpipes operations or add your own in your fittings directory. |
0 commit comments