You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+33-32Lines changed: 33 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,20 +17,28 @@ $ npm install outdoc -D
17
17
## Usage
18
18
19
19
Add the following codes into your main file
20
-
```
20
+
21
+
```js
22
+
const { OutDoc } =require('outdoc')
23
+
if (process.env.NODE_ENV==="test") {
24
+
OutDoc.init()
25
+
}
21
26
```
22
27
28
+
Run the command
29
+
23
30
```bash
24
31
$ npx outdoc [test command] [options]
25
32
```
26
33
27
-
Usually it could be:
34
+
Usually it could be, for example:
28
35
29
36
```bash
30
37
$ npx outdoc npm test -t project-name
31
38
```
32
39
33
-
Finally it will generate an api.yaml in your root folder by default
40
+
Adn it will generate an api.yaml in your root folder by defaults
41
+
34
42
35
43
## Options
36
44
@@ -43,49 +51,42 @@ Finally it will generate an api.yaml in your root folder by default
43
51
-h, --help display help for command
44
52
```
45
53
54
+
## Not adding extra codes
55
+
You might don't wanna add any extra codes in your project only for generating API document, then you can do:
46
56
57
+
First, Check if the **main** in your package.json pointing to your app entry file which export your nodejs server. If not, please add the attribute **outdoc** pointing to it. If you are writting a Typescript project, you need to add the following configuration.
47
58
48
-
49
-
50
-
## Configuration
51
-
Check if the field `main` in your package.json pointing to the file where the node server exported.
52
-
53
-
If not, add `output.main` pointing to the file, e.g.:
54
-
55
-
```
59
+
```json
56
60
{
57
-
...
58
-
"outdoc": {
59
-
"main": "./server/index.js"
60
-
}
61
+
"outdoc": {
62
+
"main": "./src/app.ts"
63
+
}
61
64
}
62
65
```
63
66
67
+
Then run the command with the option **-f**
64
68
65
-
## Typescript projects
66
-
Add `output.main` in your package.json pointing to the file where the nodejs server exported, e.g.:
67
-
69
+
```bash
70
+
$ npx outdoc npm test -f
68
71
```
69
-
{
70
-
...
71
-
"outdoc": {
72
-
"main": "./src/app.ts"
73
-
}
74
-
}
75
72
76
-
```
77
-
afte that you can run the script as usual
73
+
#### What is happending behind `-f`
74
+
75
+
By using the option `-f`, Outdoc will first check the **outdoc.main** in your package.json, if it not exist, Outdoc will use **main** in the package.json to find the app entry file.
76
+
77
+
Then Outdoc will copy the entry app file and insert codes into it, and use the new copied file as the entry app file, the concrete steps are:
78
78
79
+
1. Find your app entry file from package.json, for example the file is named as app.js
80
+
2. Copy app.js to a new temporary file named tmp_outdoc_file
81
+
3. Insert Outdoc.init into app.js and start running the program to generate the API doc
82
+
4. After the generation finished, copy back the content of tmp_outdoc_file to app.js and remove tmp_outdoc_file
79
83
80
-
## Behind the screen
81
84
82
-
Outdoc make use the node module `async_hooks` to understand all the HTTP request to the nodejs server.
85
+
## Notes
83
86
84
-
When you running the e2e testing, you are like telling outdoc "ok, this is a 200 request if you pass in such request body", "and this endpoint can return 403 with a code 100", etc. Outdoc will generate the api doc based on that.
87
+
Outdoc can only understand tests who are sending and receiving real HTTP requests, for example using the [supertest](https://github.com/visionmedia/supertest) in your test cases.
85
88
86
-
So if you wanna have a completed API doc, you need
87
-
1. Writing e2e test covering all the cases of your API.
88
-
2. Running e2e test with real http request, that means testing tools like supertest is a fit, but fastify.inject won't work.
89
+
Mocked HTTP request won't work with Ourdoc, like fastify.inject.
0 commit comments