Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Or you can download the file you need from the dist folder
```js
import {CurlGenerator} from "curl-generator";

const curlSnippet = CurlGenerator({url: "https://jsonplaceholder.typicode.com/posts/1"});
const curlSnippet = CurlGenerator({url: "https://jsonplaceholder.typicode.com/posts/1"});
// curlSnippet => curl "https://jsonplaceholder.typicode.com/posts/101"
```

Expand All @@ -33,14 +33,12 @@ Currently the library export just CurlGenerator, and it's a function with just 1
```js
/**
* @param {string} url - the request url
* @param {string} [param.method] - a value between ("GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" | "CONNECT" | "TRACE" | "QUERY") it's case insensitive
* @param {string} [param.method] - any string are ok, but TS autocompletes for standard HTTP methods ("GET", "POST", etc...) and "QUERY" (case insensitive)
* @param {Object<string, string>} [param.headers] - an object containing the headers of the request
* @param {Object} [body] - the body of the request
*/
```

**Note:** The QUERY method is not yet an official HTTP standard but is based on an [IETF draft proposal](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-safe-method-w-body).

Example of a more "advanced" use
```js
import {CurlGenerator} from "curl-generator";
Expand All @@ -57,7 +55,7 @@ const params = {
"key2": `a "complex" value`
}
}
const curlSnippet = CurlGenerator(params);
const curlSnippet = CurlGenerator(params);
// curlSnippet => curl "https://jsonplaceholder.typicode.com/posts" -X POST -H "Content-type: application/json; charset=UTF-8" -d "{\"id\":\"123-456-789\",\"key1\":\"value 1\",\"key2\":\"a \\\"complex\\\" value\"}"
```

Expand All @@ -72,7 +70,7 @@ const options = {
output: "test.txt",
silent: true
}
const curlSnippet = CurlGenerator(params, options);
const curlSnippet = CurlGenerator(params, options);
// curl "https://jsonplaceholder.typicode.com/posts/1" --output test.txt --silent
```
Currently the following options are supported (you can submit a PR if you need others):
Expand Down
5 changes: 5 additions & 0 deletions example/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,8 @@ export const jsonBody = {
},
},
};

export const customMethod = {
url: "https://jsonplaceholder.typicode.com/posts",
method: "Cheese",
};
12 changes: 10 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type CurlAdditionalOptions = {

type CurlRequest = {
// Query is not official HTTP method, but it's in a RFC and we want to support it. https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-safe-method-w-body
method?: "GET" | "get" | "POST" | "post" | "PUT" | "put" | "PATCH" | "patch" | "DELETE" | "delete" | "HEAD" | "head" | "OPTIONS" | "options" | "CONNECT" | "connect" | "TRACE" | "trace" | "QUERY" | "query",
// All the list of values provide TS auto-completion, but still allow custom methods as string. This is to support non-standard methods like "Cheese"
method?: "GET" | "get" | "POST" | "post" | "PUT" | "put" | "PATCH" | "patch" | "DELETE" | "delete" | "HEAD" | "head" | "OPTIONS" | "options" | "CONNECT" | "connect" | "TRACE" | "trace" | "QUERY" | "query" | (string & {}),
headers?: StringMap,
body?: CurlBody,
url: string,
Expand Down Expand Up @@ -79,7 +80,8 @@ const getCurlMethod = function (method?: string): string {
TRACE: "-X TRACE",
QUERY: "-X QUERY",
};
result = ` ${types[method.toUpperCase()]}`;
const curlOption = types[method.toUpperCase()] || `-X ${method}`;
result = ` ${curlOption}`;
}
return slash + newLine + result;
};
Expand Down Expand Up @@ -163,3 +165,9 @@ const CurlGenerator = function (
};

export { CurlGenerator };


CurlGenerator({
method: "Cheese",
url: "https://example.com",
})
5 changes: 5 additions & 0 deletions test/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`customMethod 1`] = `
"curl https://jsonplaceholder.typicode.com/posts \\
-X Cheese"
`;

exports[`del1 1`] = `
"curl https://jsonplaceholder.typicode.com/posts/1 \\
-X DELETE"
Expand Down