Skip to content

Commit b42b718

Browse files
committed
fix: content-type
1 parent e729332 commit b42b718

File tree

6 files changed

+75
-25
lines changed

6 files changed

+75
-25
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ All notable changes to this project will be documented in this file.
66

77
---
88

9+
### [0.0.3] - 2024-05-14
10+
11+
### Fix
12+
13+
- fix: ensure content type to be defined
14+
15+
---
16+
917
### [0.0.2] - 2024-02-02
1018

1119
### Docs

dist/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ function generateCurlCommand(options) {
4040
curlCommand += ` -H '${key}: ${value}'`;
4141
}
4242
else {
43-
curlCommand += ` -H '${key}: '`;
43+
if ((key === null || key === void 0 ? void 0 : key.toLowerCase()) === 'content-type') {
44+
curlCommand += ` -H '${key}: application/json'`;
45+
}
46+
else {
47+
curlCommand += ` -H '${key}: '`;
48+
}
4449
}
4550
}
4651
}

lib/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ function generateCurlCommand(options: AxiosRequestConfig): string {
4444
if (value !== undefined) {
4545
curlCommand += ` -H '${key}: ${value}'`;
4646
} else {
47-
curlCommand += ` -H '${key}: '`;
47+
if (key?.toLowerCase() === 'content-type') {
48+
curlCommand += ` -H '${key}: application/json'`;
49+
} else {
50+
curlCommand += ` -H '${key}: '`;
51+
}
4852
}
4953
}
5054
}
@@ -94,7 +98,7 @@ export function curlInterceptor(req: InternalAxiosRequestConfig<any>, callback?:
9498
return req;
9599
};
96100

97-
function print (curlCommand: string): void {
101+
function print(curlCommand: string): void {
98102
const occurredAt = new Date().toISOString();
99103
console.log({ occurredAt, curlCommand });
100104
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "curl2axios",
3-
"version": "0.0.2",
4-
"main": "./dist/index.mjs",
3+
"version": "0.0.3",
4+
"main": "./dist/index.js",
55
"author": "4lessandrodev",
66
"license": "MIT",
77
"peerDependencies": {
88
"axios": "^1.6.7"
99
},
1010
"devDependencies": {
11+
"@types/axios": "^0.14.0",
1112
"@types/node": "^20.11.16",
13+
"axios": "^1.6.7",
1214
"ts-node": "^10.9.2",
1315
"typescript": "^5.3.3"
1416
},

pnpm-lock.yaml

Lines changed: 24 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/index.spec.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import axios from "axios";
22
import { describe, it } from "node:test";
3-
import generateCurl, { bindCurl, curlInterceptor } from "../lib";
3+
import { bindCurl, curlInterceptor } from "../lib";
44
import assert from "node:assert";
55

66
describe('curl-generator', () => {
77

88
it('[1] should generate curl from simple GET', async () => {
99

1010
var command = '';
11-
const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: '";
11+
const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json'";
1212

1313
const instance = axios.create();
1414
const callback = (cmd: string) => command = cmd;
@@ -23,7 +23,7 @@ describe('curl-generator', () => {
2323
it('[2] should generate curl from GET with params', async () => {
2424

2525
var command = '';
26-
const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1?animal=berry' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: '";
26+
const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1?animal=berry' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json'";
2727

2828
const instance = axios.create();
2929
const callback = (cmd: string) => command = cmd;
@@ -40,7 +40,7 @@ describe('curl-generator', () => {
4040
it('[3] should generate curl from GET with auth', async () => {
4141

4242
var command = '';
43-
const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: ' --user 'berry:12345'";
43+
const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json' --user 'berry:12345'";
4444

4545
const instance = axios.create();
4646
const callback = (cmd: string) => command = cmd;
@@ -78,7 +78,7 @@ describe('curl-generator', () => {
7878
it('[5] should generate curl from GET using baseURL', async () => {
7979

8080
var command = '';
81-
const expected = "curl '/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: ' --url 'https://pokeapi.co/api/v2'";
81+
const expected = "curl '/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json' --url 'https://pokeapi.co/api/v2'";
8282

8383
const instance = axios.create({ baseURL: 'https://pokeapi.co/api/v2' });
8484
const callback = (cmd: string) => command = cmd;
@@ -92,12 +92,33 @@ describe('curl-generator', () => {
9292

9393
it('[6] should generate curl from GET using baseURL', async () => {
9494

95-
const expected = "curl '/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: ' --url 'https://pokeapi.co/api/v2'";
95+
const expected = "curl '/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json' --url 'https://pokeapi.co/api/v2'";
9696

9797
const instance = bindCurl(axios.create({ baseURL: 'https://pokeapi.co/api/v2' }));
9898

9999
await instance.get('/berry-flavor/1');
100100

101101
assert(instance.curl === expected, 'O curl [6] gerado ficou diferente');
102102
});
103+
104+
it('[7] should generate curl from POST with auth and content type text', async () => {
105+
106+
var command = '';
107+
const expected = `curl 'https://jsonplaceholder.typicode.com/posts' -X POST -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/text' -H 'Authorization: Bearer token...' --data-raw '{"userName":"sample"}'`;
108+
const instance = axios.create();
109+
const callback = (cmd: string) => command = cmd;
110+
111+
instance.interceptors.request.use((req) => curlInterceptor(req, callback));
112+
113+
await instance.post('https://jsonplaceholder.typicode.com/posts', { userName: 'sample' },
114+
{
115+
headers: {
116+
'Content-Type': 'application/text',
117+
'Authorization': 'Bearer token...'
118+
}
119+
}
120+
);
121+
122+
assert(command === expected, 'O curl [7] gerado ficou diferente');
123+
});
103124
});

0 commit comments

Comments
 (0)