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
+93-6Lines changed: 93 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# The hit cli 👊
1
+
# The hit CLI 👊
2
2
3
3
`hit` is a productivity-focussed command-line API client that converts individual API endpoints into command-line commands.
4
4
@@ -38,17 +38,17 @@ For example, if the config looks something like:
38
38
}
39
39
```
40
40
41
-
then this api call can be made like so:
41
+
then this API call can be made like so:
42
42
43
43
```bash
44
44
hit run list-users
45
45
```
46
46
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.**
48
48
49
49
### Route Params
50
50
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.
52
52
53
53
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.
54
54
@@ -92,7 +92,7 @@ hit run get-user --user-id 47
92
92
93
93
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.
94
94
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`.
96
96
97
97
98
98
```json
@@ -133,7 +133,7 @@ As mentioned previously, the config file is meant to be committed to git and sha
133
133
134
134
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
135
135
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.
137
137
138
138
Here's an example of setting an API key:
139
139
@@ -192,6 +192,48 @@ the headers used when invoking command `list-users` would:
192
192
2. use the value of `{{API_URL}}` from the active environment.
193
193
3. use the value of `{{API_KEY}}` from what was set in the app settings using the `hit ephenv set` command.
194
194
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
+
195
237
196
238
### Inspecting the response of an API call
197
239
@@ -200,3 +242,48 @@ Normally, running a command would simply output the body of the response of the
200
242
```bash
201
243
hit last view
202
244
```
245
+
246
+
247
+
### Postscripts
248
+
249
+
Postscripts allow you to run any script using the response of a particular API call. The script can be in any language/runtime that is supported on your machine. This can be useful if you want to perform any kind of action using the response of an API call. Some possible-use cases:
250
+
251
+
* The most common use-case for this would be to store an API token that can be obtained from a call to an authentication API endpoint. Using postscripts, the token in the response can be stored in the ephenv such that other commands are able to pass the token in the authentication headers.
252
+
* Another use-case would be to be able to always format the response of a particular command in a particular way. For example, an endpoint returns an array of objects and the command is expected to always display the json array in tabular format. This can be achieved adding a bash postscript to use `jq` to format the response or you could write a custom python/node script to do this as well.
253
+
* This can also be used to chain multiple commands together. For example, from the response of command A we can extract the necessary fields that command B expects as input and invoke command B in a bash based postscript.
254
+
255
+
#### Configuring Postscripts
256
+
257
+
1. All postscripts can be defined under the `postscripts` directory under the `.hit` directory. For example, if you have a script named `store_token.sh`, this would be at the path `.hit/postscripts/store_token.sh`
258
+
2. Which postscript to invoke after a particular command can be configured in the `postscript` field in that particular command's config. The `postscript` field has the following sub-fields:
259
+
1.`command`: This field specifies the run-time needed to invoke the script. This could be `bash` or `python` or `node` and so on.
260
+
2.`file`: This is the name of the file relative to the `postscripts` directory that needs to be invoked.
261
+
262
+
For example, with a config like so:
263
+
264
+
```json
265
+
{
266
+
"commands": {
267
+
"login": {
268
+
"url": "https://authenticate.com/login",
269
+
"method": "POST",
270
+
"body": {
271
+
"user": ":user",
272
+
"pass": ":pass"
273
+
}
274
+
"postscript": {
275
+
"command": "bash",
276
+
"file": "set_token.sh"
277
+
}
278
+
}
279
+
}
280
+
}
281
+
```
282
+
283
+
and a postscript file `postscrtips/set_token.sh` with contents like so:
284
+
285
+
```bash
286
+
hit ephenv set API_TOKEN `cat $HIT_RESPONSE_PATH`
287
+
```
288
+
289
+
Assuming the response of the login call would just be the API token as a string, running `hit run login --user username --password abcd1234` would fetch the token from the API endpoint and store it in the ephenv `API_TOKEN` for the rest of the hit commands to be able to use.
0 commit comments