Skip to content

Commit a02b0bd

Browse files
Add API key to curl generated requests
Fixes #60
1 parent d47d801 commit a02b0bd

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/exporters/curl.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ export class CurlExporter implements FormatExporter {
1515
(options.elasticsearchUrl ?? "").replace(/\/$/, "") ??
1616
"http://localhost:9200";
1717
const escapedSingleQuote = options.windows ? "''" : "'\"'\"'";
18+
const envPrefix = options.windows ? "$env:" : "$";
19+
const auth = ` -H "Authorization: ApiKey ${envPrefix}ELASTIC_API_KEY"`;
1820
let output = "";
1921
for (const request of requests) {
20-
let headers = "";
22+
let headers = auth;
2123
let body = "";
2224
if (request.body) {
23-
headers = ' -H "Content-Type: application/json"';
25+
headers += ' -H "Content-Type: application/json"';
2426
body =
2527
" -d '" +
2628
JSON.stringify(request.body).replaceAll("'", escapedSingleQuote) +

tests/convert.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe("convert", () => {
5757
elasticsearchUrl: "http://localhost:9876",
5858
}),
5959
).toEqual(
60-
'curl -X GET "http://localhost:9876/"\ncurl -X POST -H "Content-Type: application/json" -d \'{"query":{"term":{"user.id":"kimchy\'"\'"\'s"}}}\' "http://localhost:9876/my-index/_search?from=40&size=20"\n',
60+
'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" "http://localhost:9876/"\ncurl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d \'{"query":{"term":{"user.id":"kimchy\'"\'"\'s"}}}\' "http://localhost:9876/my-index/_search?from=40&size=20"\n',
6161
);
6262
});
6363

@@ -68,7 +68,7 @@ describe("convert", () => {
6868
windows: true,
6969
}),
7070
).toEqual(
71-
'curl -X GET "http://localhost:9876/"\ncurl -X POST -H "Content-Type: application/json" -d \'{"query":{"term":{"user.id":"kimchy\'\'s"}}}\' "http://localhost:9876/my-index/_search?from=40&size=20"\n',
71+
'curl -X GET -H "Authorization: ApiKey $env:ELASTIC_API_KEY" "http://localhost:9876/"\ncurl -X POST -H "Authorization: ApiKey $env:ELASTIC_API_KEY" -H "Content-Type: application/json" -d \'{"query":{"term":{"user.id":"kimchy\'\'s"}}}\' "http://localhost:9876/my-index/_search?from=40&size=20"\n',
7272
);
7373
});
7474

0 commit comments

Comments
 (0)