diff --git a/README.md b/README.md index ea6ac63..8a4c936 100644 --- a/README.md +++ b/README.md @@ -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" ``` @@ -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} [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"; @@ -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\"}" ``` @@ -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): diff --git a/example/parameters.ts b/example/parameters.ts index 25031f8..8902cf4 100644 --- a/example/parameters.ts +++ b/example/parameters.ts @@ -195,3 +195,8 @@ export const jsonBody = { }, }, }; + +export const customMethod = { + url: "https://jsonplaceholder.typicode.com/posts", + method: "Cheese", +}; diff --git a/src/main.ts b/src/main.ts index d76cb94..3f8dea0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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, @@ -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; }; @@ -163,3 +165,9 @@ const CurlGenerator = function ( }; export { CurlGenerator }; + + +CurlGenerator({ + method: "Cheese", + url: "https://example.com", +}) diff --git a/test/__snapshots__/main.test.ts.snap b/test/__snapshots__/main.test.ts.snap index e7b2d01..c405948 100644 --- a/test/__snapshots__/main.test.ts.snap +++ b/test/__snapshots__/main.test.ts.snap @@ -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"