Skip to content

Commit bd1ad42

Browse files
authored
Merge branch 'master' into feat/cleaner-json-in-curl
2 parents c34ae40 + 5e8f161 commit bd1ad42

170 files changed

Lines changed: 1273 additions & 181 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
workflow_dispatch:
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
node-version: [16, 18]
14+
steps:
15+
- name: Checkout branch
16+
uses: actions/checkout@v3
17+
18+
- name: Setup Node
19+
uses: actions/setup-node@v3
20+
with:
21+
node-version: ${{ matrix.node-version }}
22+
23+
- name: Install
24+
run: npm ci
25+
26+
- name: Test
27+
run: npm run test
28+
29+
- name: Lint
30+
run: npm run lint
31+
32+
- name: Build
33+
run: npm run build

src/fixtures/requests/headers.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
{
1010
"name": "x-foo",
1111
"value": "Bar"
12+
},
13+
{
14+
"name": "x-bar",
15+
"value": "Foo"
1216
}
1317
]
1418
}

src/fixtures/requests/multipart-data.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"value": "Hello World",
1616
"fileName": "hello.txt",
1717
"contentType": "text/plain"
18+
},
19+
{
20+
"name": "bar",
21+
"value": "Bonjour le monde"
1822
}
1923
]
2024
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"log": {
3+
"version": "1.2",
4+
"creator": {
5+
"name": "HTTPSnippet",
6+
"version": "1.0.0"
7+
},
8+
"entries": [
9+
{
10+
"request": {
11+
"method": "POST",
12+
"url": "http://mockbin.com/har",
13+
"headers": [
14+
{
15+
"name": "Content-Type",
16+
"value": "multipart/form-data"
17+
}
18+
],
19+
"postData": {
20+
"mimeType": "multipart/form-data"
21+
}
22+
}
23+
}
24+
]
25+
}
26+
}

src/helpers/__snapshots__/utils.test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ Array [
230230
"link": "http://php.net/manual/en/book.curl.php",
231231
"title": "cURL",
232232
},
233+
Object {
234+
"description": "PHP with Guzzle",
235+
"key": "guzzle",
236+
"link": "http://docs.guzzlephp.org/en/stable/",
237+
"title": "Guzzle",
238+
},
233239
Object {
234240
"description": "PHP with pecl/http v1",
235241
"key": "http1",

src/helpers/code-builder.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,25 @@ describe('codeBuilder', () => {
1818
expect(result).toBe(`${indent.repeat(2)}${line}`);
1919
});
2020
});
21+
22+
describe('addPostProcessor', () => {
23+
it('replaces accordingly with one replacer', () => {
24+
const indent = '\t';
25+
const { join, addPostProcessor, push } = new CodeBuilder({ indent });
26+
push('console.log("hello world")');
27+
addPostProcessor(code => code.replace(/console/, 'REPLACED'));
28+
29+
expect(join()).toBe('REPLACED.log("hello world")');
30+
});
31+
32+
it('replaces accordingly with multiple replacers', () => {
33+
const indent = '\t';
34+
const { join, addPostProcessor, push } = new CodeBuilder({ indent });
35+
push('console.log("hello world")');
36+
addPostProcessor(code => code.replace(/world/, 'nurse!!'));
37+
addPostProcessor(code => code.toUpperCase());
38+
39+
expect(join()).toBe('CONSOLE.LOG("HELLO NURSE!!")');
40+
});
41+
});
2142
});

src/helpers/code-builder.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const DEFAULT_INDENTATION_CHARACTER = '';
22
const DEFAULT_LINE_JOIN = '\n';
33

4+
export type PostProcessor = (unreplacedCode: string) => string;
5+
46
export interface CodeBuilderOptions {
57
/**
68
* Desired indentation character for aggregated lines of code
@@ -16,6 +18,7 @@ export interface CodeBuilderOptions {
1618
}
1719

1820
export class CodeBuilder {
21+
postProcessors: PostProcessor[] = [];
1922
code: string[] = [];
2023
indentationCharacter: string = DEFAULT_INDENTATION_CHARACTER;
2124
lineJoin = DEFAULT_LINE_JOIN;
@@ -61,7 +64,22 @@ export class CodeBuilder {
6164
};
6265

6366
/**
64-
* Concatenate all current lines using the given lineJoin
67+
* Concatenate all current lines using the given lineJoin, then apply any replacers that may have been added
68+
*/
69+
join = () => {
70+
const unreplacedCode = this.code.join(this.lineJoin);
71+
const replacedOutput = this.postProcessors.reduce(
72+
(accumulator, replacer) => replacer(accumulator),
73+
unreplacedCode,
74+
);
75+
return replacedOutput;
76+
};
77+
78+
/**
79+
* Often when writing modules you may wish to add a literal tag or bit of metadata that you wish to transform after other processing as a final step.
80+
* To do so, you can provide a PostProcessor function and it will be run automatically for you when you call `join()` later on.
6581
*/
66-
join = () => this.code.join(this.lineJoin);
82+
addPostProcessor = (postProcessor: PostProcessor) => {
83+
this.postProcessors = [...this.postProcessors, postProcessor];
84+
};
6785
}

src/targets/c/libcurl/fixtures/headers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har");
66
struct curl_slist *headers = NULL;
77
headers = curl_slist_append(headers, "accept: application/json");
88
headers = curl_slist_append(headers, "x-foo: Bar");
9+
headers = curl_slist_append(headers, "x-bar: Foo");
910
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
1011

1112
CURLcode ret = curl_easy_perform(hnd);

src/targets/c/libcurl/fixtures/multipart-data.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ struct curl_slist *headers = NULL;
77
headers = curl_slist_append(headers, "content-type: multipart/form-data; boundary=---011000010111000001101001");
88
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
99

10-
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--\r\n");
10+
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"bar\"\r\n\r\nBonjour le monde\r\n-----011000010111000001101001--\r\n");
1111

1212
CURLcode ret = curl_easy_perform(hnd);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CURL *hnd = curl_easy_init();
2+
3+
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
4+
curl_easy_setopt(hnd, CURLOPT_URL, "http://mockbin.com/har");
5+
6+
struct curl_slist *headers = NULL;
7+
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
8+
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
9+
10+
CURLcode ret = curl_easy_perform(hnd);

0 commit comments

Comments
 (0)