Skip to content

Commit 936b627

Browse files
Add more documentation
1 parent 5341f58 commit 936b627

File tree

3 files changed

+118
-4
lines changed

3 files changed

+118
-4
lines changed

src/help.js

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ Now, we can pass the variable to thq request like so:
319319
320320
'''console
321321
$ *req* run users --page 2
322-
GET *https://reqres.in/api/users?page=2*
322+
GET *https://reqres.in/api/users?page=2*
323323
*Status*: 200 OK
324324
*Headers* |(14 hidden)|:
325325
age: 5997
@@ -358,7 +358,111 @@ $ *req* run users --page 2
358358
This time, we can see the second page of results.
359359
You can learn more about variables in 'req help variables'.
360360
361-
Now, lets try creating a flow.
361+
Now, lets try creating a flow. Suppose you have an API at 'https://localhost:3000/'
362+
where you can log in and sign up. Lets create requests and a flow for that.
363+
364+
Open up '.req/login.http' in your text editor and add this:
365+
366+
'''http
367+
POST https://localhost:3000/login HTTP/1.1
368+
Content-Type: application/json
369+
370+
{
371+
"username": "{username}",
372+
"password": "{password}"
373+
}
374+
'''
375+
376+
Now, lets create the signup request in '.req/signup.http':
377+
378+
'''http
379+
POST https://localhost:3000/signup HTTP/1.1
380+
Content-Type: application/json
381+
382+
{
383+
"username": "{username}",
384+
"password": "{password}",
385+
"email": "{email}"
386+
}
387+
'''
388+
389+
We can also create a third request to view user info:
390+
391+
'''http
392+
GET https://localhost:3000/user HTTP/1.1
393+
Authorization: Bearer {token}
394+
'''
395+
396+
Let's test these out.
397+
398+
'''console
399+
$ *req* run signup --username user --password pass --email "user@place.com"
400+
POST *https://localhost:3000/signup*
401+
*Status*: 201 CREATED
402+
*Headers* |(14 hidden)|:
403+
age: 5997
404+
*Body* |(application/json)|:
405+
{
406+
"username": "user",
407+
"email": "user@place.com",
408+
"password": "pass"
409+
}
410+
411+
$ *req* run login --username user --password pass
412+
POST *https://localhost:3000/login*
413+
*Status*: 200 OK
414+
*Headers* |(14 hidden)|:
415+
age: 5997
416+
*Body* |(application/json)|:
417+
{
418+
"token": "sdfj30J02fj29f2p"
419+
}
420+
421+
$ *req* run user --token sdfj30J02fj29f2p
422+
GET *https://localhost:3000/user*
423+
*Status*: 200 OK
424+
*Headers* |(14 hidden)|:
425+
age: 5997
426+
*Body* |(application/json)|:
427+
{
428+
"username": "user",
429+
"email": "user@place.com",
430+
}
431+
'''
432+
433+
Thats cool, but we need to run three requests to get the user info. We can
434+
automate this by creating a flow. Lets create a flow called 'auth' in
435+
'.req/auth.flow.js':
436+
437+
'''js
438+
(async () => {
439+
const {username, password, email} = variables;
440+
441+
console.log("Signup with", username, password);
442+
const signup = await run('signup', {username, password, email});
443+
444+
console.log("Login with", username, password);
445+
const login = await run('login', {username, password});
446+
const {token} = login.body;
447+
448+
console.log("Fetching user data with token", token);
449+
const user = await run('user', {token});
450+
console.log("User data", user.body);
451+
})()
452+
'''
453+
454+
This flow runs all three requests at once. Try running it:
455+
456+
'''console
457+
$ *req* run auth
458+
Signup with user pass
459+
Login with user pass
460+
Fetching user data with token nggypnglydngraad
461+
User data {
462+
"username": "user",
463+
"email": "user@place.com"
464+
}
465+
'''
362466
`),
363467
flow: makeDoc(`
364468
Flows are JavaScript files which tie together requests. You can use flows to
@@ -371,12 +475,12 @@ All flows have access to
371475
2. The 'run' function, which can be used to run other flows and requests. When
372476
running a requst his function returns a Promise which resolves to the
373477
response, which is an object containing the status, headers, and response body.
374-
When running a flow, this function returns the value the flow returns.
478+
When running a flow, this function returns the value the flow returns.
375479
376480
A sample flow looks like this:
377481
378482
'''javascript
379-
(async () => {
483+
(async () => { // This is not necessary and is only used for async/await
380484
await run("register", {username: "morpheus", password: "matrix"})
381485
const {body: {token}} = await run("login", {username: "morpheus", password: "matrix"})
382486
const user_data = await run("user", {token})

src/run.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ function runHttp(content, args) {
151151
console.error(color.red('Failed to send request!'))
152152

153153
if (e.message.toLowerCase().includes('invalid url')) console.error('You passed an invalid URL!')
154+
if (e.code === 'ECONNREFUSED') console.error('Connection refused!')
154155
else console.error(e);
155156

156157
process.exit(1)

test/.req/signup.http

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
POST https://localhost:3000/signup HTTP/1.1
2+
Content-Type: application/json
3+
4+
{
5+
"username": "{username}",
6+
"password": "{password}",
7+
"email": "{email}"
8+
}
9+

0 commit comments

Comments
 (0)