Skip to content

Commit 2dead8f

Browse files
committed
feat: dart support
1 parent 5b4474d commit 2dead8f

25 files changed

Lines changed: 509 additions & 0 deletions

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ Array [
6464
"key": "csharp",
6565
"title": "C#",
6666
},
67+
Object {
68+
"clients": Array [
69+
Object {
70+
"description": "Dart HTTP client request using the http package",
71+
"key": "http",
72+
"link": "https://pub.dev/packages/http",
73+
"title": "HTTP",
74+
},
75+
],
76+
"default": "http",
77+
"extname": ".dart",
78+
"key": "dart",
79+
"title": "Dart",
80+
},
6781
Object {
6882
"clients": Array [
6983
Object {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import full from '../../../fixtures/requests/full.json';
2+
import { runCustomFixtures } from '../../../fixtures/runCustomFixtures';
3+
import { Request } from '../../../httpsnippet';
4+
5+
runCustomFixtures({
6+
targetId: 'dart',
7+
clientId: 'http',
8+
tests: [
9+
{
10+
it: 'should support false boilerplate option',
11+
input: full as Request,
12+
options: {
13+
showBoilerplate: false,
14+
},
15+
expected: 'boilerplate-option.dart',
16+
},
17+
{
18+
it: 'should support printBody option',
19+
input: full as Request,
20+
options: {
21+
printBody: false,
22+
},
23+
expected: 'print-body-option.dart',
24+
},
25+
{
26+
it: 'should support timeout option',
27+
input: full as Request,
28+
options: {
29+
timeout: 30,
30+
},
31+
expected: 'timeout-option.dart',
32+
},
33+
{
34+
it: 'should generate full request',
35+
input: full as Request,
36+
options: {},
37+
expected: 'full.dart',
38+
},
39+
],
40+
});

src/targets/dart/http/client.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for Dart http package.
4+
*
5+
* @author
6+
* @AI-Generated
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
11+
import { CodeBuilder } from '../../../helpers/code-builder';
12+
import { escapeForSingleQuotes } from '../../../helpers/escape';
13+
import { Client } from '../../targets';
14+
15+
export interface DartHttpOptions {
16+
showBoilerplate?: boolean;
17+
checkErrors?: boolean;
18+
printBody?: boolean;
19+
timeout?: number;
20+
insecureSkipVerify?: boolean;
21+
}
22+
23+
export const http: Client<DartHttpOptions> = {
24+
info: {
25+
key: 'http',
26+
title: 'HTTP',
27+
link: 'https://pub.dev/packages/http',
28+
description: 'Dart HTTP client request using the http package',
29+
},
30+
convert: ({ postData, method, allHeaders, fullUrl }, options = {}) => {
31+
const { blank, push, join } = new CodeBuilder({ indent: ' ' });
32+
33+
const {
34+
showBoilerplate = true,
35+
checkErrors = false,
36+
printBody = true,
37+
timeout = -1,
38+
insecureSkipVerify = false,
39+
} = options;
40+
41+
const indent = showBoilerplate ? 1 : 0;
42+
43+
// Create boilerplate
44+
if (showBoilerplate) {
45+
push('import \'package:http/http.dart\' as http;');
46+
47+
blank();
48+
push('void main() async {');
49+
blank();
50+
}
51+
52+
// Create client with timeout if specified
53+
if (timeout > 0) {
54+
push('final client = http.Client();', indent);
55+
push(`client.timeout = Duration(seconds: ${timeout});`, indent);
56+
blank();
57+
}
58+
59+
// Add headers setup
60+
if (Object.keys(allHeaders).length) {
61+
push('final headers = {', indent);
62+
Object.keys(allHeaders).forEach(key => {
63+
push(`'${key}': '${escapeForSingleQuotes(allHeaders[key])}',`, indent + 1);
64+
});
65+
push('};', indent);
66+
blank();
67+
}
68+
69+
// Prepare request
70+
const headersVar = Object.keys(allHeaders).length ? 'headers' : '{}';
71+
72+
if (postData.text) {
73+
push(`final response = await http.${method.toLowerCase()}(`, indent);
74+
push(` Uri.parse('${fullUrl}'),`, indent);
75+
push(` headers: ${headersVar},`, indent);
76+
push(` body: ${JSON.stringify(postData.text)},`, indent);
77+
push(');', indent);
78+
} else {
79+
push(`final response = await http.${method.toLowerCase()}(`, indent);
80+
push(` Uri.parse('${fullUrl}'),`, indent);
81+
push(` headers: ${headersVar},`, indent);
82+
push(');', indent);
83+
}
84+
85+
// Print response
86+
blank();
87+
push('print(response.statusCode);', indent);
88+
89+
if (printBody) {
90+
push('print(response.body);', indent);
91+
}
92+
93+
// End main block
94+
if (showBoilerplate) {
95+
blank();
96+
push('}');
97+
}
98+
99+
return join();
100+
},
101+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:http/http.dart' as http;
2+
3+
void main() async {
4+
5+
final headers = {
6+
'content-type': 'application/x-www-form-urlencoded',
7+
};
8+
9+
final response = await http.post(
10+
Uri.parse('http://mockbin.com/har'),
11+
headers: headers,
12+
body: "foo=bar&hello=world",
13+
);
14+
15+
print(response.statusCode);
16+
print(response.body);
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:http/http.dart' as http;
2+
3+
void main() async {
4+
5+
final headers = {
6+
'content-type': 'application/json',
7+
};
8+
9+
final response = await http.post(
10+
Uri.parse('http://mockbin.com/har'),
11+
headers: headers,
12+
body: "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}",
13+
);
14+
15+
print(response.statusCode);
16+
print(response.body);
17+
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
final headers = {
2+
'cookie': 'foo=bar; bar=baz',
3+
'accept': 'application/json',
4+
'content-type': 'application/x-www-form-urlencoded',
5+
};
6+
7+
final response = await http.post(
8+
Uri.parse('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value'),
9+
headers: headers,
10+
body: "foo=bar",
11+
);
12+
13+
print(response.statusCode);
14+
print(response.body);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:http/http.dart' as http;
2+
3+
void main() async {
4+
5+
final headers = {
6+
'cookie': 'foo=bar; bar=baz',
7+
};
8+
9+
final response = await http.post(
10+
Uri.parse('http://mockbin.com/har'),
11+
headers: headers,
12+
);
13+
14+
print(response.statusCode);
15+
print(response.body);
16+
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:http/http.dart' as http;
2+
3+
void main() async {
4+
5+
final response = await http.propfind(
6+
Uri.parse('http://mockbin.com/har'),
7+
headers: {},
8+
);
9+
10+
print(response.statusCode);
11+
print(response.body);
12+
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:http/http.dart' as http;
2+
3+
void main() async {
4+
5+
final headers = {
6+
'cookie': 'foo=bar; bar=baz',
7+
'accept': 'application/json',
8+
'content-type': 'application/x-www-form-urlencoded',
9+
};
10+
11+
final response = await http.post(
12+
Uri.parse('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value'),
13+
headers: headers,
14+
body: "foo=bar",
15+
);
16+
17+
print(response.statusCode);
18+
print(response.body);
19+
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:http/http.dart' as http;
2+
3+
void main() async {
4+
5+
final headers = {
6+
'accept': 'application/json',
7+
'x-foo': 'Bar',
8+
'quoted-value': '"quoted" \'string\'',
9+
};
10+
11+
final response = await http.get(
12+
Uri.parse('http://mockbin.com/har'),
13+
headers: headers,
14+
);
15+
16+
print(response.statusCode);
17+
print(response.body);
18+
19+
}

0 commit comments

Comments
 (0)