mirror of
https://github.com/foss42/apidash.git
synced 2025-06-05 19:14:25 +08:00
feat: js fetch
This commit is contained in:
@ -4,6 +4,7 @@ import 'dart/http.dart';
|
||||
import 'kotlin/okhttp.dart';
|
||||
import 'python/http_client.dart';
|
||||
import 'python/requests.dart';
|
||||
import 'js/fetch.dart';
|
||||
import 'others/har.dart';
|
||||
import 'others/curl.dart';
|
||||
|
||||
@ -17,9 +18,14 @@ class Codegen {
|
||||
case CodegenLanguage.curl:
|
||||
return cURLCodeGen().getCode(requestModel, defaultUriScheme);
|
||||
case CodegenLanguage.har:
|
||||
return HARCodeGen().getCode(requestModel);
|
||||
return HARCodeGen().getCode(requestModel, defaultUriScheme);
|
||||
case CodegenLanguage.dartHttp:
|
||||
return DartHttpCodeGen().getCode(requestModel, defaultUriScheme);
|
||||
case CodegenLanguage.jsFetch:
|
||||
return FetchCodeGen().getCode(requestModel, defaultUriScheme);
|
||||
case CodegenLanguage.nodejsFetch:
|
||||
return FetchCodeGen(isNodeJs: true)
|
||||
.getCode(requestModel, defaultUriScheme);
|
||||
case CodegenLanguage.kotlinOkHttp:
|
||||
return KotlinOkHttpCodeGen().getCode(requestModel, defaultUriScheme);
|
||||
case CodegenLanguage.pythonHttpClient:
|
||||
|
95
lib/codegen/js/fetch.dart
Normal file
95
lib/codegen/js/fetch.dart
Normal file
@ -0,0 +1,95 @@
|
||||
import 'package:apidash/consts.dart';
|
||||
import 'package:jinja/jinja.dart' as jj;
|
||||
import 'package:apidash/utils/utils.dart'
|
||||
show requestModelToHARJsonRequest, padMultilineString;
|
||||
import 'package:apidash/models/models.dart' show RequestModel;
|
||||
|
||||
// ignore: camel_case_types
|
||||
class FetchCodeGen {
|
||||
FetchCodeGen({this.isNodeJs = false});
|
||||
|
||||
final bool isNodeJs;
|
||||
|
||||
String kStringImportNode = """import fetch from 'node-fetch';
|
||||
|
||||
""";
|
||||
|
||||
String kTemplateStart = """let url = '{{url}}';
|
||||
|
||||
let options = {
|
||||
method: '{{method}}'
|
||||
""";
|
||||
|
||||
String kTemplateHeader = """,
|
||||
headers: {{headers}}
|
||||
""";
|
||||
|
||||
String kTemplateBody = """,
|
||||
body:
|
||||
{{body}}
|
||||
""";
|
||||
|
||||
String kStringRequest = """
|
||||
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
|
||||
String? getCode(
|
||||
RequestModel requestModel,
|
||||
String defaultUriScheme,
|
||||
) {
|
||||
try {
|
||||
String result = isNodeJs ? kStringImportNode : "";
|
||||
|
||||
String url = requestModel.url;
|
||||
if (!url.contains("://") && url.isNotEmpty) {
|
||||
url = "$defaultUriScheme://$url";
|
||||
}
|
||||
var rM = requestModel.copyWith(url: url);
|
||||
|
||||
var harJson = requestModelToHARJsonRequest(rM);
|
||||
|
||||
var templateStart = jj.Template(kTemplateStart);
|
||||
result += templateStart.render({
|
||||
"url": harJson["url"],
|
||||
"method": harJson["method"],
|
||||
});
|
||||
|
||||
var headers = harJson["headers"];
|
||||
if (headers.isNotEmpty) {
|
||||
var templateHeader = jj.Template(kTemplateHeader);
|
||||
var m = {};
|
||||
for (var i in headers) {
|
||||
m[i["name"]] = i["value"];
|
||||
}
|
||||
result += templateHeader
|
||||
.render({"headers": padMultilineString(kEncoder.convert(m), 2)});
|
||||
}
|
||||
|
||||
if (harJson["postData"]?["text"] != null) {
|
||||
var templateBody = jj.Template(kTemplateBody);
|
||||
result += templateBody
|
||||
.render({"body": kEncoder.convert(harJson["postData"]["text"])});
|
||||
}
|
||||
result += kStringRequest;
|
||||
return result;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -234,6 +234,8 @@ enum CodegenLanguage {
|
||||
curl("cURL", "bash", "curl"),
|
||||
har("HAR", "json", "har"),
|
||||
dartHttp("Dart (http)", "dart", "dart"),
|
||||
jsFetch("JavaScript (fetch)", "javascript", "js"),
|
||||
nodejsFetch("node.js (fetch)", "javascript", "js"),
|
||||
kotlinOkHttp("Kotlin (okhttp3)", "java", "kt"),
|
||||
pythonHttpClient("Python (http.client)", "python", "py"),
|
||||
pythonRequests("Python (requests)", "python", "py");
|
||||
|
489
test/codegen/js_fetch_codegen_test.dart
Normal file
489
test/codegen/js_fetch_codegen_test.dart
Normal file
@ -0,0 +1,489 @@
|
||||
import 'package:apidash/codegen/js/fetch.dart';
|
||||
import '../request_models.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
final fetchCodeGen = FetchCodeGen();
|
||||
|
||||
group('GET Request', () {
|
||||
test('GET 1', () {
|
||||
const expectedCode = r"""let url = 'https://api.foss42.com';
|
||||
|
||||
let options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelGet1, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 2', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.foss42.com/country/data?code=US';
|
||||
|
||||
let options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelGet2, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 3', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.foss42.com/country/data?code=IND';
|
||||
|
||||
let options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelGet3, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 4', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.foss42.com/humanize/social?num=8700000&digits=3&system=SS&add_space=true&trailing_zeros=true';
|
||||
|
||||
let options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelGet4, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 5', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.github.com/repos/foss42/apidash';
|
||||
|
||||
let options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelGet5, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 6', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.github.com/repos/foss42/apidash?raw=true';
|
||||
|
||||
let options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelGet6, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 7', () {
|
||||
const expectedCode = r"""let url = 'https://api.foss42.com';
|
||||
|
||||
let options = {
|
||||
method: 'GET'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelGet7, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('GET 8', () {
|
||||
const expectedCode =
|
||||
r"""let url = 'https://api.github.com/repos/foss42/apidash?raw=true';
|
||||
|
||||
let options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"User-Agent": "Test Agent"
|
||||
}
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelGet8, "https"), expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('HEAD Request', () {
|
||||
test('HEAD 1', () {
|
||||
const expectedCode = r"""let url = 'https://api.foss42.com';
|
||||
|
||||
let options = {
|
||||
method: 'HEAD'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelHead1, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('HEAD 2', () {
|
||||
const expectedCode = r"""let url = 'http://api.foss42.com';
|
||||
|
||||
let options = {
|
||||
method: 'HEAD'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelHead2, "http"), expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('POST Request', () {
|
||||
test('POST 1', () {
|
||||
const expectedCode = r"""let url = 'https://api.foss42.com/case/lower';
|
||||
|
||||
let options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "text/plain"
|
||||
},
|
||||
body:
|
||||
"{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelPost1, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('POST 2', () {
|
||||
const expectedCode = r"""let url = 'https://api.foss42.com/case/lower';
|
||||
|
||||
let options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelPost2, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('POST 3', () {
|
||||
const expectedCode = r"""let url = 'https://api.foss42.com/case/lower';
|
||||
|
||||
let options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "Test Agent"
|
||||
},
|
||||
body:
|
||||
"{\n\"text\": \"I LOVE Flutter\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelPost3, "https"), expectedCode);
|
||||
});
|
||||
});
|
||||
group('PUT Request', () {
|
||||
test('PUT 1', () {
|
||||
const expectedCode = r"""let url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"name\": \"morpheus\",\n\"job\": \"zion resident\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelPut1, "https"), expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('PATCH Request', () {
|
||||
test('PATCH 1', () {
|
||||
const expectedCode = r"""let url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelPatch1, "https"), expectedCode);
|
||||
});
|
||||
});
|
||||
|
||||
group('DELETE Request', () {
|
||||
test('DELETE 1', () {
|
||||
const expectedCode = r"""let url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
method: 'DELETE'
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelDelete1, "https"), expectedCode);
|
||||
});
|
||||
|
||||
test('DELETE 2', () {
|
||||
const expectedCode = r"""let url = 'https://reqres.in/api/users/2';
|
||||
|
||||
let options = {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body:
|
||||
"{\n\"name\": \"marfeus\",\n\"job\": \"accountant\"\n}"
|
||||
};
|
||||
|
||||
let status;
|
||||
fetch(url, options)
|
||||
.then(res => {
|
||||
status = res.status;
|
||||
return res.json()
|
||||
})
|
||||
.then(body => {
|
||||
console.log(status);
|
||||
console.log(body);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(status);
|
||||
console.error('error:' + err);
|
||||
});
|
||||
""";
|
||||
expect(fetchCodeGen.getCode(requestModelDelete2, "https"), expectedCode);
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user