UNPKG

10.5 kBMarkdownView Raw
1# snowboard
2
3[![Build Status](https://travis-ci.org/bukalapak/snowboard.svg?branch=master)](https://travis-ci.org/bukalapak/snowboard)
4[![Docker Repository on Quay](https://quay.io/repository/bukalapak/snowboard/status)](https://quay.io/repository/bukalapak/snowboard)
5[![npm version](https://badgen.net/npm/v/snowboard)](https://www.npmjs.com/package/snowboard)
6
7API blueprint toolkit.
8
9![Screenshot with playground enabled](examples/screenshot.png)
10
11## Installation
12
13```
14$ npm install -g snowboard
15```
16
17Alternatively, you can also use options below:
18
19### Docker
20
21You can also use automated build docker image on `quay.io/bukalapak/snowboard`:
22
23```
24$ docker pull quay.io/bukalapak/snowboard
25$ docker run -it --rm quay.io/bukalapak/snowboard help
26```
27
28To run snowboard with the current directory mounted to `/doc`:
29
30```
31$ docker run -it --rm -v $PWD:/doc quay.io/bukalapak/snowboard html -o output.html API.apib
32```
33
34### Tarball executables
35
36The latest executables for supported platforms are available from the [release page](https://github.com/bukalapak/snowboard/releases).
37
38Just extract and start using it:
39
40```
41$ wget https://github.com/bukalapak/snowboard/releases/download/${version}/snowboard-${version}.${os}-${arch}.tar.gz
42$ tar -zxvf snowboard-${version}.${os}-${arch}.tar.gz
43$ ./snowboard -h
44```
45
46## Usage
47
48Let's say we have API Blueprint document called `API.apib`, like:
49
50```apib
51# API
52## GET /message
53+ Response 200 (text/plain)
54
55 Hello World!
56```
57
58There are some scenarios we can perform, like:
59
60```
61# generate HTML documentation
62$ snowboard html -o index.html API.apib
63
64# run mock server
65$ snowboard mock API.apib
66
67# and more, see sections below
68```
69
70## HTML Documentation
71
72To generate HTML documentation, we can do:
73
74```
75$ snowboard html -o output.html API.apib
76```
77
78Above command will generate `output.html` using `snowboard` default template (called `winter`).
79
80Flag `-o` has two different behaviors depending on the value passed:
81
82- When you pass directory name, like `-o outputDir`, snowboard will create 3 files for HTML, javascript, and CSS into the `outputDir`.
83
84- When you pass file name, like `-o output.html`, snowboard will only generate a single HTML file with javascript and CSS embedded. It also applies when you don't specify flag `-o`.
85
86### Custom Template
87
88If you want to use a custom template, you can use flag `-t` for that:
89
90```
91$ snowboard html -o output.html -t awesome-template.html API.apib
92```
93
94To see how the template looks like, you can see `snowboard` default template in [templates/winter.html](templates/winter.html).
95
96### Custom Base Path
97
98By default base path for produced HTML documentation is `/`. If you need to serve documentation on subdirectory like, `https://doc.example.com/super-project`, you can customize base path using a configuration file:
99
100```yaml
101html:
102 basePath: /super-project
103```
104
105Then, you can pass the configuration using flag `-c`:
106
107```
108$ snowboard html -o outputDir -c config.yaml API.apib
109```
110
111### Custom CSS
112
113Snowboard let you add external CSS for small customization. You can add it under `html` config:
114
115```yaml
116html:
117 stylesheets:
118 - custom.css
119 - path/to/another-custom.css
120```
121
122### Sidebar Group Order
123
124Snowboard let you to sort sidebar group alphabetically (`name`) or as written (`auto`):
125
126```yaml
127html:
128 sidebar:
129 groupOrder: "name" # default: "auto"
130```
131
132### HTTP Server
133
134If you want to access HTML documentation via HTTP, you can use `http` sub-command:
135
136```
137$ snowboard http -t awesome-template.html API.apib
138```
139
140With this flag, you can access HTML documentation on `localhost:8088`.
141
142If you need to customize binding address, you can use flag `-b`.
143
144### API Playground
145
146You can activate the playground feature to let your users interact with your staging or even production API.
147
148The Playground can be activate using flag `--playground` or using a configuration file, by setting `enabled: false`.
149
150Playground requires a configuration contains supported environments and the name of the default environment. On each environment, you can set an authentication option.
151
152Here's the example of playground configuration along with the different authentication combination supported:
153
154```yaml
155html:
156 playground:
157 enabled: true
158 env: development
159 environments:
160 development:
161 url: http://localhost:8087/
162 auth:
163 name: apikey
164 options:
165 key: api-dev-key
166 header: X-Api-Key
167 staging:
168 url: https://staging.example.com/
169 auth:
170 name: basic
171 options:
172 username: admin
173 password: secret
174 production:
175 url: https://api.example.com
176 auth:
177 name: oauth2
178 options:
179 authorizeUrl: https://accounts.example.com/oauth/authorize
180 tokenUrl: https://accounts.example.com/oauth/access_token
181 callbackUrl: https://www.example.com
182 clientId: <client-id>
183 clientSecret: <client-secret>
184 scopes: <scope-names>
185```
186
187Once you have a configuration file, you can do:
188
189```
190$ snowboard html -o outputDir -c config.yaml API.apib
191
192# http sub-command works too
193$ snowboard http -c config.yaml API.apib
194```
195
196To disable playground on particular environment, you can add `playground: false` under environment configuration, like:
197
198```yaml
199html:
200 playground:
201 enabled: true
202 env: development
203 environments:
204 development:
205 url: http://localhost:8087/
206 playground: false
207 staging:
208 url: https://staging.example.com/
209```
210
211## Mock Server
212
213Another snowboard useful feature is having a mock server. You can use `mock` sub-command for that.
214
215```
216$ snowboard mock API.apib
217```
218
219Then you can use `localhost:8087` for accessing mock server. You can customize the address using flag `-b`.
220
221For multiple responses, you can set `Prefer` header to select a specific response:
222
223```
224Prefer: status=200
225```
226
227You can also supply multiple inputs for `mock` sub-command:
228
229```
230$ snowboard mock API.apib OTHER.apib
231```
232
233### Mock Server Authentication
234
235The mock server supports various authentication, you can enable that by passing a configuration file using flag `-c`, like:
236
237```
238$ snowboard mock -c config.yaml API.apib
239```
240
241Here's the example of the configuration file for mock server:
242
243**API key authentication**
244
245```yaml
246mock:
247 auth:
248 name: apikey
249 options:
250 key: <api-key>
251 header: <Header-Name>
252```
253
254**Basic authentication**
255
256```yaml
257mock:
258 auth:
259 name: basic
260 options:
261 username: <username>
262 password: <password>
263```
264
265## Split API Blueprint
266
267When you have documentation split across files, you can use `apib` sub-command to allow `snowboard` to produce single formatted API blueprint.
268
269```
270$ snowboard apib -o API.apib project/split.apib
271```
272
273To let you split your documentation as several files, snowboard provides some neat feature you can use:
274
275### External Files
276
277You can split your API blueprint document to several files and use `partial` helper to includes it to your main document.
278
279```
280{{partial "some-resource.apib"}}
281```
282
283Alternatively, you can also use HTML comment syntax to include those files:
284
285```html
286<!-- partial(some-resource.apib) -->
287```
288
289or
290
291```html
292<!-- include(some-resource.apib) -->
293```
294
295### Seed Files
296
297As your API blueprint document become large, you might move some value to a separate file for an easier organization and modification. Snowboard supports this.
298
299Just place your values into a json file, say, `seed.json`:
300
301```json
302{
303 "official": {
304 "username": "olaf"
305 }
306}
307```
308
309Then on your API blueprint document, you can use `seed` comment helper:
310
311```apib
312# API
313
314<!-- seed(seed.json) -->
315
316Our friendly username is {{.official.username}}.
317```
318
319It also supports multiple seeds.
320
321## Other Features
322
323Besides the above features, snowboard also has several useful features for working with API blueprint:
324
325### Validate API blueprint
326
327Besides rendering to HTML, snowboard also support validates API blueprint document. You can use `lint` sub-command.
328
329```
330$ snowboard lint API.apib
331```
332
333Using flag `--json`, you will receive output as JSON format.
334
335### Render API Element JSON
336
337In case you need to get API element JSON output for further processing, you can use:
338
339```
340$ snowboard json API.apib
341```
342
343### List Routes
344
345If you need to list all available routes for current API blueprints, you can do:
346
347```
348$ snowboard list API.apib ANOTHER.apib
349```
350
351Using flag `--json`, you will receive output as JSON format.
352
353## SSL Support
354
355To enable HTTPS server, both `http`, and `mock` subcommand supports SSL configuration. You can do:
356
357```
358# http server
359$ snowboard http -S -C cert.pem -K key.pem API.apib
360
361# mock server
362$ snowboard mock -S -C cert.pem -K key.pem API.apib
363```
364
365For example, for local development, you can utilize [mkcert](https://github.com/FiloSottile/mkcert) to create your local development certificate and use it with snowboard:
366
367```
368# generate localhost certificate
369$ mkcert -install
370$ mkcert localhost
371
372# use the generated certificate with snowboard http or mock subcommand
373$ snowboard http -S -C localhost.pem -K localhost-key.pem API.apib
374
375# you can now access using https://localhost:8088/
376```
377
378## Watcher Support
379
380To enable auto-regeneration on input files updates, you can add global flag `--watch`
381
382```
383$ snowboard html --watch -o output.html -t awesome-template.html -s API.apib
384```
385
386Optionally, you can also use `--watch-interval` to enable polling interval.
387
388```
389$ snowboard html --watch --watch-interval 100ms -o output.html -t awesome-template.html -s API.apib
390```
391
392This watcher works on all sub-commands.
393
394## Help
395
396As usual, you can also see all supported flags by passing `-h`:
397
398```
399$ snowboard
400Usage: snowboard <command> [options] <input>
401
402API blueprint toolkit
403
404Options:
405 -v, --version output the version number
406 -w, --watch watch for the files changes
407 -n, --watch-interval <value> set watch interval. This activates polling watcher. Accepted format: 100ms, 1s, etc
408 -h, --help output usage information
409
410Commands:
411 lint [options] <input> validate API blueprint
412 html [options] <input> render HTML documentation
413 http [options] <input> HTML documentation via HTTP server
414 apib [options] <input> render API blueprint
415 json [options] <input> render API elements json
416 mock [options] <input> [inputs...] run mock server
417 list [options] <input> [inputs...] list routes
418```