Skip to content

Commit 0c94d77

Browse files
committed
update README to include section on nested commands
1 parent 1ce8b8e commit 0c94d77

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

README.md

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ For example, if the config looks something like:
3838
}
3939
```
4040

41-
then this api call can be made like so:
41+
then this API call can be made like so:
4242

4343
```bash
4444
hit run list-users
4545
```
4646

47-
The `.hit/` directory is meant to be added to git and hence can be shared by developers in a team.
47+
**The `.hit/` directory is meant to be added to git and hence can be shared by developers in a team.**
4848

4949
### Route Params
5050

51-
But api endpoint routes are never as simple as the example above. There can be any number of variables in the route. For example, an endpoint to retrieve a single user would include the id of the user to be retrieved in the route. `hit` would not be considered productivity-focussed if we had to go in and update the route in the config file every time we wanted to retrieve a different user.
51+
But API endpoint routes are never as simple as the example above. There can be any number of variables in the route. For example, an endpoint to retrieve a single user would include the id of the user to be retrieved in the route. `hit` would not be considered productivity-focussed if we had to go in and update the route in the config file every time we wanted to retrieve a different user.
5252

5353
To handle such cases, the `hit` config has the ability to specify which parts of the route represent variables and the values for such variables can then passed as command-line options. Variables can be denoted by prefixing them with colon `:`. So if the url in the config has `:userId`, then `userId` would be a variable.
5454

@@ -92,7 +92,7 @@ hit run get-user --user-id 47
9292

9393
Most software development set ups have multiple environments where their APIs are deployed such as a production/prod environment, a staging or dev or sandbox env or even separate environments for different features being developed. `hit` has the ability to define and use a set of variables that can have different values based on the currently active environment.
9494

95-
Environment variables can be used in the config by enclosing them in double curly braces (`{{` `}}`) and can be defined in the config under the top level field `envs`.
95+
Environment variables can be used in the config by enclosing them in double curly braces (`{{` `}}`) and can be defined in the config under the top level field `envs`.
9696

9797

9898
```json
@@ -133,7 +133,7 @@ As mentioned previously, the config file is meant to be committed to git and sha
133133

134134
Environment variables discussed above are good for nearly-static variables that don't change often and would be good to share in the team but there might be variables in a workflow that are meant to be kept secret. Good examples of such variables are access tokens and api keys. For such variables, `hit` has support for "Ephemeral Environment Variables" or `ephenv`s
135135

136-
`ephenv`s can be set from the `hit` cli directly as opposed to in the config. `hit` stores these values in app settings on the local machine and hence these values don't show up in the config and are not shareable.
136+
`ephenv`s can be set from the `hit` cli directly as opposed to in the config. `hit` stores these values in app settings on the local machine and hence these values don't show up in the config and are not shareable.
137137

138138
Here's an example of setting an API key:
139139

@@ -192,6 +192,48 @@ the headers used when invoking command `list-users` would:
192192
2. use the value of `{{API_URL}}` from the active environment.
193193
3. use the value of `{{API_KEY}}` from what was set in the app settings using the `hit ephenv set` command.
194194

195+
### Nested Sub-Commands
196+
197+
So far we've covered being able to add commands directly as key-value pairs in the top level `commands` field of the config file. This works great in the beginning when we have just a few commands but as the number of api endpoints increase, our list of commands would also increase and it might get cluttered to maintain the commands. To add some sort of structure to the config file, the `hit` config supports organizing commands into nested sub-commands.
198+
199+
What this means is that instead of having to maintain commands like `get-user`, `list-users`, `delete-user`, `create-user`, we can organize the config to have a high level `users` command with the corresponding sub-commands as `get`, `list`, `delete`, `create`.
200+
201+
The config supports arbitrary level of nesting.
202+
203+
For example, with a config like so:
204+
205+
```json
206+
{
207+
"envs": {
208+
"prod": {
209+
"API_URL": "https://prod.api.com"
210+
},
211+
"dev": {
212+
"API_URL": "https://dev.api.com"
213+
}
214+
},
215+
"commands": {
216+
"users": {
217+
"list": {
218+
"url": "{{API_URL}}/users",
219+
"method": "GET",
220+
},
221+
"get": {
222+
"url": "{{API_URL}}/users/:userId",
223+
"method": "GET",
224+
}
225+
}
226+
}
227+
}
228+
```
229+
230+
the available commands would be:
231+
232+
```
233+
hit run users list
234+
hit run users get --user-id 47
235+
```
236+
195237

196238
### Inspecting the response of an API call
197239

0 commit comments

Comments
 (0)