Skip to content

Commit

Permalink
Chore: configured husky + lint_staged
Browse files Browse the repository at this point in the history
  • Loading branch information
Akalanka47000 committed Jun 22, 2023
1 parent 1b68c53 commit fc2ed74
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
.history
.svn/

# VS Code
.vscode

# IntelliJ related
*.iml
*.ipr
Expand Down
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

dart run lint_staged
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ analyzer:
errors:
deprecated_member_use_from_same_package: ignore
file_names: ignore
unused_import: ignore
4 changes: 2 additions & 2 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ packages:
- packages/*

scripts:
analyze:
exec: dart analyze .
test:
exec: flutter test
63 changes: 49 additions & 14 deletions packages/enhanced_http/lib/enhanced_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ class EnhancedHttp extends StreamedRequest with Interceptor, Utils {
late final String? baseURL;
late final Map<String, String>? headers;

EnhancedHttp({String this.baseURL = "", Map<String, String>? headers, InterceptorOptions? interceptors}) {
EnhancedHttp(
{String this.baseURL = "",
Map<String, String>? headers,
InterceptorOptions? interceptors}) {
this.headers = headers ?? {'Content-Type': "application/json"};
this.interceptors = interceptors ?? InterceptorOptions();
}

Future<dynamic> get(String path, {Map<String, String>? headers, InterceptorOptions? interceptors, String? responseType}) async {
Future<dynamic> get(String path,
{Map<String, String>? headers,
InterceptorOptions? interceptors,
String? responseType}) async {
return await request(() async {
final url = Uri.parse('$baseURL$path');
if (isStream(headers, responseType)) {
return await streamedRequest('GET', url, mergeHeaders(this.headers, headers), responseType: responseType);
return await streamedRequest(
'GET', url, mergeHeaders(this.headers, headers),
responseType: responseType);
}
return await http.get(
url,
Expand All @@ -31,11 +39,17 @@ class EnhancedHttp extends StreamedRequest with Interceptor, Utils {
}

Future<dynamic> post(String path,
{dynamic payload, Map<String, String>? headers, InterceptorOptions? interceptors, List<dynamic>? files, String? responseType}) async {
{dynamic payload,
Map<String, String>? headers,
InterceptorOptions? interceptors,
List<dynamic>? files,
String? responseType}) async {
final url = Uri.parse('$baseURL$path');
return await request(() async {
if (isStream(headers, responseType)) {
return await streamedRequest('POST', url, mergeHeaders(this.headers, headers), payload: payload, files: files, responseType: responseType);
return await streamedRequest(
'POST', url, mergeHeaders(this.headers, headers),
payload: payload, files: files, responseType: responseType);
}
return await http.post(
url,
Expand All @@ -45,11 +59,14 @@ class EnhancedHttp extends StreamedRequest with Interceptor, Utils {
}, interceptors);
}

Future<dynamic> _update(String method, String path, payload, headers, interceptors, files, responseType) async {
Future<dynamic> _update(String method, String path, payload, headers,
interceptors, files, responseType) async {
final url = Uri.parse('$baseURL$path');
return await request(() async {
if (isStream(headers, responseType)) {
return await streamedRequest(method, url, mergeHeaders(this.headers, headers), payload: payload, files: files, responseType: responseType);
return await streamedRequest(
method, url, mergeHeaders(this.headers, headers),
payload: payload, files: files, responseType: responseType);
} else {
payload ??= {};
if (method == "PUT") {
Expand All @@ -69,20 +86,35 @@ class EnhancedHttp extends StreamedRequest with Interceptor, Utils {
}

Future<dynamic> put(String path,
{dynamic payload, Map<String, String>? headers, InterceptorOptions? interceptors, List<dynamic>? files, String? responseType}) async {
return _update("PUT", path, payload, headers, interceptors, files, responseType);
{dynamic payload,
Map<String, String>? headers,
InterceptorOptions? interceptors,
List<dynamic>? files,
String? responseType}) async {
return _update(
"PUT", path, payload, headers, interceptors, files, responseType);
}

Future<dynamic> patch(String path,
{dynamic payload, Map<String, String>? headers, InterceptorOptions? interceptors, List<dynamic>? files, String? responseType}) async {
return _update("PATCH", path, payload, headers, interceptors, files, responseType);
{dynamic payload,
Map<String, String>? headers,
InterceptorOptions? interceptors,
List<dynamic>? files,
String? responseType}) async {
return _update(
"PATCH", path, payload, headers, interceptors, files, responseType);
}

Future<dynamic> delete(String path, {Map<String, String>? headers, InterceptorOptions? interceptors, String? responseType}) async {
Future<dynamic> delete(String path,
{Map<String, String>? headers,
InterceptorOptions? interceptors,
String? responseType}) async {
return await request(() async {
final url = Uri.parse('$baseURL$path');
if (isStream(headers, responseType)) {
return await streamedRequest('DELETE', url, mergeHeaders(this.headers, headers), responseType: responseType);
return await streamedRequest(
'DELETE', url, mergeHeaders(this.headers, headers),
responseType: responseType);
}
return await http.delete(
url,
Expand All @@ -91,7 +123,10 @@ class EnhancedHttp extends StreamedRequest with Interceptor, Utils {
}, interceptors);
}

Future<dynamic> head(String path, {Map<String, String>? headers, InterceptorOptions? interceptors, String? responseType}) async {
Future<dynamic> head(String path,
{Map<String, String>? headers,
InterceptorOptions? interceptors,
String? responseType}) async {
return await request(() async {
return await http.head(
Uri.parse('$baseURL$path'),
Expand Down
14 changes: 10 additions & 4 deletions packages/enhanced_http/lib/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class Interceptor {

String? defaultErrorMessage;

Future<dynamic> request(Function request, InterceptorOptions? _interceptors) async {
_interceptors =
InterceptorOptions(response: interceptors?.response ?? _interceptors?.response, error: interceptors?.error ?? _interceptors?.error);
Future<dynamic> request(
Function request, InterceptorOptions? _interceptors) async {
_interceptors = InterceptorOptions(
response: interceptors?.response ?? _interceptors?.response,
error: interceptors?.error ?? _interceptors?.error);
try {
final response = await request();
dynamic data;
Expand All @@ -28,7 +30,11 @@ class Interceptor {
data = response;
}
}
final decoded = {"status": response.statusCode, "data": data, "headers": response.headers};
final decoded = {
"status": response.statusCode,
"data": data,
"headers": response.headers
};
if (response.statusCode >= 200 && response.statusCode <= 299) {
if (_interceptors.response != null) {
return _interceptors.response!(decoded);
Expand Down
3 changes: 2 additions & 1 deletion packages/enhanced_http/lib/streamed_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import 'package:path/path.dart' as p;
import 'package:http_parser/http_parser.dart';

class StreamedRequest {
Future<dynamic> streamedRequest(method, url, headers, {payload, files, responseType}) async {
Future<dynamic> streamedRequest(method, url, headers,
{payload, files, responseType}) async {
final request = http.MultipartRequest(method, url);
request.headers.addAll(headers);
if (payload != null) {
Expand Down
8 changes: 6 additions & 2 deletions packages/enhanced_http/lib/utils.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class Utils {
Map<String, String> mergeHeaders(Map<String, String>? h1, Map<String, String>? h2) => {...?h1, ...?h2};
Map<String, String> mergeHeaders(
Map<String, String>? h1, Map<String, String>? h2) =>
{...?h1, ...?h2};

bool isStream(Map<String, String>? headers, [String? responseType = "json"]) {
if ([headers?["Content-Type"], headers?["content-type"]].contains("multipart/form-data") || responseType == "stream") return true;
if ([headers?["Content-Type"], headers?["content-type"]]
.contains("multipart/form-data") ||
responseType == "stream") return true;
return false;
}
}
44 changes: 42 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
ansi:
dependency: transitive
description:
name: ansi
sha256: ce4857dd957e8e4fcf1c2f409baa5b262a717a6516085d97532621c47ff87c6e
url: "https://pub.dev"
source: hosted
version: "0.2.2"
ansi_codes:
dependency: transitive
description:
name: ansi_codes
sha256: "48aec9524e26ef234b0376b6bcf92edd9b89ad9c8994ddc7c3b52729af553bed"
url: "https://pub.dev"
source: hosted
version: "0.1.1"
ansi_escapes:
dependency: transitive
description:
name: ansi_escapes
sha256: e2a111254904ff663b8d4be744073f7e327157c895a0ad57ad8de7938297bc8c
url: "https://pub.dev"
source: hosted
version: "1.1.0"
ansi_styles:
dependency: transitive
description:
Expand Down Expand Up @@ -125,10 +149,10 @@ packages:
dependency: "direct dev"
description:
name: husky
sha256: b86dfca0537bbaca94af26ce659fe553a39b265ff2abfb49f5498ff55fa5138d
sha256: "69a52b079786cb5d21da0e985d57016c3e3d7876cb62df40cf4dd5fc51212df5"
url: "https://pub.dev"
source: hosted
version: "0.1.3"
version: "0.1.6"
io:
dependency: transitive
description:
Expand All @@ -145,6 +169,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.8.1"
lint_staged:
dependency: "direct dev"
description:
name: lint_staged
sha256: "0d07e3892eacbfce4fe275a17541f7aacc438c1abda0aed67b9a810e65bd1e0b"
url: "https://pub.dev"
source: hosted
version: "0.3.0"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -321,6 +353,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.0"
verbose:
dependency: transitive
description:
name: verbose
sha256: aa5a0aa5c39a4615997fc8f524f680ea6190f1f04c20c51fffb3b7ffab044854
url: "https://pub.dev"
source: hosted
version: "0.1.0"
yaml:
dependency: "direct main"
description:
Expand Down
6 changes: 5 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ dependencies:

dev_dependencies:
flutter_lints: 1.0.0
husky: 0.1.3
husky: 0.1.6
lint_staged: 0.3.0
melos: 3.1.0

lint_staged:
'packages/**.dart': dart format --fix && dart fix --apply

0 comments on commit fc2ed74

Please sign in to comment.