Skip to content

Commit b3266eb

Browse files
author
ghaerdi
committed
Merge branch 'dev'
2 parents 670577b + 0e8bf4f commit b3266eb

4 files changed

Lines changed: 55 additions & 12 deletions

File tree

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<h1 align="center">Piwo</h1>
22

33
<p align="center">
4-
<img src="https://cdn.discordapp.com/attachments/845424135018250283/865702946892546098/unknown.png" width="800">
4+
<img src="https://cdn.discordapp.com/attachments/845424135018250283/869636557328482344/unknown.png" width="800">
55
</p>
66

77
## About
88

9-
Piwo is a friendly command-line tool to do HTTP request to a API server. It support JSON.
9+
Piwo is a friendly command-line tool to do HTTP request to a API server. Sending request body as JSON.
1010

1111
## Installation
1212

@@ -88,4 +88,33 @@ piwo POST localhost:3000/signup username=foo password=bar
8888

8989
```console
9090
piwo DELETE localhost:3000/your_foo/remove
91+
```
92+
### Sending a property with array value type
93+
94+
```console
95+
piwo POST localhost:3000/cli/piwo tags=[typescript deno cli http]
96+
```
97+
98+
### Sendin a property with object value type
99+
100+
```console
101+
piwo POST localhost:3000/cli/registry cli={name=piwo description="your friendly HTTP cli tool"}
102+
```
103+
104+
## Some nice tips
105+
106+
### How URL argument works
107+
When you're doing a request you can omit the protocol, Piwo will make a request with https, if get not response then will try with http protocol and then will response with response of the server of a msg that couldn't connect when no server is found.
108+
109+
When you send the URL with protocol Piwo will not check the other protocol.
110+
111+
If you're sure that the server is on http protocol, we recommend you to pass the protocol in the url, is faster because piwo will do a direct request with the procotol and will not check the https procotol.
112+
113+
If the url is a localhost, then will try directly with HTTP.
114+
115+
### Sending a body
116+
117+
You can separate the values with commas, but a space is always required to differentiate to the others values.
118+
```
119+
piwo POST localhost:3000/ foo=bar, bar=foo
91120
```

cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const commands: Commands = {
2828
PATCH: patchCommnand
2929
};
3030

31-
const args = parse(Deno.args);
31+
const args = parse(Deno.args, { stopEarly: true });
3232

3333
if (hasArgs(args)) {
3434
const { method, url, body, flags } = handleArgs(args);

handlers/handleBodyInput.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,31 @@
66
*
77
*/
88

9-
function handleBodyInput(array: string[]) {
10-
let stringified = "{";
9+
function handleBodyInput(body: string[]) {
10+
const regex = /[a-zA-Z0-9]+=?|("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*=)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g;
1111

12-
array.forEach((property, index) => {
13-
stringified += property.split("=").map(word =>`"${word}"`).join(": ");
14-
stringified += index !== array.length - 1 ? ", " : "";
15-
});
12+
const stringified = body.map((property) => {
13+
property = property.replace(/,/g, "");
14+
const [key, value] = property.split("=");
15+
if (value?.includes(" ")) {
16+
return `${key}="${value}"`
17+
}
18+
return property.includes(" ") ? `"${property}"` : property;
19+
}).join(" ");
1620

17-
stringified += "}"
21+
const result = stringified.replace(regex, (match) => {
22+
if (/=/.test(match)) {
23+
return `"${match.slice(0, -1)}": `;
24+
}
25+
if (/"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/.test(match)) {
26+
return `${match},`;
27+
}
1828

19-
return stringified as BodyInit;
29+
return `"${match}",`;
30+
})
31+
32+
const lastCommaIndex = result.lastIndexOf(",");
33+
return `{ ${result.slice(0, lastCommaIndex) + result.slice(lastCommaIndex + 1)} }`;
2034
}
2135

2236
export default handleBodyInput;

info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88

99
export default {
1010
name: "Piwo",
11-
version: "v0.2.0",
11+
version: "v0.3.0",
1212
}

0 commit comments

Comments
 (0)