Skip to content

Commit

Permalink
🔖 release v7.3.0 (#589)
Browse files Browse the repository at this point in the history
* 🔖 release v7.3.0

# chopper

## 7.3.0

- Add support for `@Tag` annotation ([#586](#586))

# chopper_generator

## 7.3.0

- Add support for `@Tag` annotation ([#586](#586))

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: wenchieh <[email protected]>
  • Loading branch information
techouse and lwj1994 authored Apr 3, 2024
1 parent 47084cc commit d2a4c46
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 15 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
sdk: stable
- id: checkout
name: Checkout repository
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633
- name: mono_repo self validate
run: dart pub global activate mono_repo 6.6.1
- name: mono_repo self validate
Expand All @@ -60,7 +60,7 @@ jobs:
sdk: stable
- id: checkout
name: Checkout repository
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633
- id: chopper_pub_upgrade
name: chopper; dart pub upgrade
run: dart pub upgrade
Expand Down Expand Up @@ -122,7 +122,7 @@ jobs:
run: "dart pub global activate coverage '>=1.5.0'"
- id: checkout
name: Checkout repository
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633
- id: chopper_pub_upgrade
name: chopper; dart pub upgrade
run: dart pub upgrade
Expand Down Expand Up @@ -191,7 +191,7 @@ jobs:
sdk: stable
- id: checkout
name: Checkout repository
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633
- id: chopper_pub_upgrade
name: chopper; dart pub upgrade
run: dart pub upgrade
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
with:
sdk: stable
- id: checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.2
with:
fetch-depth: 2
- run: git checkout HEAD^
Expand All @@ -53,7 +53,7 @@ jobs:
with:
sdk: stable
- id: checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.2
- name: Load this version
id: load_this_version
working-directory: ${{ matrix.package }}
Expand Down Expand Up @@ -111,7 +111,7 @@ jobs:
- name: Github release
id: github_release
if: ${{ env.IS_VERSION_GREATER == 1 }}
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
with:
name: ${{ format('{0}-v{1}', matrix.package, env.THIS_VERSION) }}
tag_name: ${{ format('{0}-v{1}', matrix.package, env.THIS_VERSION) }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish_dry_run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
with:
sdk: stable
- id: checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.2
with:
ref: ${{ github.event.pull_request.base.ref }}
- name: Load base version
Expand All @@ -50,7 +50,7 @@ jobs:
with:
sdk: stable
- id: checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.2
- name: Load this version
id: load_this_version
working-directory: ${{ matrix.package }}
Expand Down
4 changes: 4 additions & 0 deletions chopper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 7.3.0

- Add support for `@Tag` annotation ([#586](https://github.com/lejard-h/chopper/pull/586))

## 7.2.0

- Add support for `@FormUrlEncoded` annotation ([#579](https://github.com/lejard-h/chopper/pull/579))
Expand Down
44 changes: 44 additions & 0 deletions chopper/example/tag.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions chopper/example/tag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/// @author luwenjie on 2024/3/20 11:38:11
///
///
///
import "package:chopper/chopper.dart";

import 'definition.dart';

part 'tag.chopper.dart';

Future<void> main() async {
final chopper = ChopperClient(
baseUrl: Uri.parse('http://localhost:8000'),
services: [
// the generated service
TagService.create(ChopperClient()),
],
interceptors: [
TagInterceptor(),
],
converter: JsonConverter(),
);

final myService = chopper.getService<MyService>();

final response = await myService.getMapResource('1');
print(response.body);

final list = await myService.getListResources();
print(list.body);
chopper.dispose();
}

// add a uniform appId header for some path
class BizTag {
final int appId;

BizTag({this.appId = 0});
}

class IncludeBodyNullOrEmptyTag {
bool includeNull = false;
bool includeEmpty = false;

IncludeBodyNullOrEmptyTag(this.includeNull, this.includeEmpty);
}

class TagConverter extends JsonConverter {
FutureOr<Request> convertRequest(Request request) {
final tag = request.tag;
if (tag is IncludeBodyNullOrEmptyTag) {
if (request.body is Map) {
final Map body = request.body as Map;
final Map bodyCopy = {};
for (final MapEntry entry in body.entries) {
if (!tag.includeNull && entry.value == null) continue;
if (!tag.includeEmpty && entry.value == "") continue;
bodyCopy[entry.key] = entry.value;
}
request = request.copyWith(body: bodyCopy);
}
}
}
}

class TagInterceptor implements RequestInterceptor {
FutureOr<Request> onRequest(Request request) {
final tag = request.tag;
if (tag is BizTag) {
request.headers["x-appId"] = tag.appId;
}
return request;
}
}

@ChopperApi(baseUrl: '/tag')
abstract class TagService extends ChopperService {
static TagService create(ChopperClient client) => _$TagService(client);

@get(path: '/bizRequest')
Future<Response> requestWithTag({@Tag() BizTag tag = const BizTag()});

@get(path: '/include')
Future<Response> includeBodyNullOrEmptyTag(
{@Tag()
IncludeBodyNullOrEmptyTag tag = const IncludeBodyNullOrEmptyTag()});
}
22 changes: 22 additions & 0 deletions chopper/lib/src/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,25 @@ final class FormUrlEncoded {
const FormUrlEncoded();
}

///
/// {@template Tag}
/// Adds the argument instance as a request tag.
///
/// ```dart
/// Future<Response> requestWithTag(
/// @Tag() String t1,
/// );
/// ```
/// get tag via `request.tags`
///
/// {@endtemplate}
@immutable
@Target({TargetKind.parameter})
final class Tag {
/// {@macro Tag}
const Tag();
}

/// {@macro ChopperApi}
const chopperApi = ChopperApi();

Expand Down Expand Up @@ -622,3 +641,6 @@ const partFileMap = PartFileMap();

/// {@macro FormUrlEncoded}
const formUrlEncoded = FormUrlEncoded();

/// {@macro Tag}
const tag = Tag();
4 changes: 4 additions & 0 deletions chopper/lib/src/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ base class Request extends http.BaseRequest with EquatableMixin {
final Uri baseUri;
final dynamic body;
final Map<String, dynamic> parameters;
final Object? tag;
final bool multipart;
final List<PartValue> parts;
final bool useBrackets;
Expand All @@ -29,6 +30,7 @@ base class Request extends http.BaseRequest with EquatableMixin {
Map<String, String> headers = const {},
this.multipart = false,
this.parts = const [],
this.tag,
this.useBrackets = false,
this.includeNullQueryVars = false,
}) : assert(
Expand Down Expand Up @@ -62,6 +64,7 @@ base class Request extends http.BaseRequest with EquatableMixin {
List<PartValue>? parts,
bool? useBrackets,
bool? includeNullQueryVars,
Object? tag,
}) =>
Request(
method ?? this.method,
Expand All @@ -74,6 +77,7 @@ base class Request extends http.BaseRequest with EquatableMixin {
parts: parts ?? this.parts,
useBrackets: useBrackets ?? this.useBrackets,
includeNullQueryVars: includeNullQueryVars ?? this.includeNullQueryVars,
tag: tag ?? this.tag,
);

/// Builds a valid URI from [baseUrl], [url] and [parameters].
Expand Down
6 changes: 3 additions & 3 deletions chopper/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: chopper
description: Chopper is an http client generator using source_gen, inspired by Retrofit
version: 7.2.0
version: 7.3.0
documentation: https://hadrien-lejard.gitbook.io/chopper
repository: https://github.com/lejard-h/chopper

Expand All @@ -25,7 +25,7 @@ dev_dependencies:
lints: ">=2.1.1 <4.0.0"
test: ^1.24.4
transparent_image: ^2.0.1
chopper_generator: ^7.1.0
chopper_generator: ^7.2.0

dependency_overrides:
chopper_generator:
Expand All @@ -35,4 +35,4 @@ topics:
- api
- client
- http
- rest
- rest
4 changes: 4 additions & 0 deletions chopper_generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 7.3.0

- Add support for `@Tag` annotation ([#586](https://github.com/lejard-h/chopper/pull/586))

## 7.2.0

- Add support for `@FormUrlEncoded` annotation ([#579](https://github.com/lejard-h/chopper/pull/579))
Expand Down
6 changes: 6 additions & 0 deletions chopper_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ final class ChopperGenerator
_getAnnotations(m, chopper.PartFile);
final Map<String, ConstantReader> fileFieldMap =
_getAnnotation(m, chopper.PartFileMap);
final Map<String, ConstantReader> tag = _getAnnotation(m, chopper.Tag);

final Code? headers = _generateHeaders(m, method!, formUrlEncoded);
final Expression url = _generateUrl(
Expand Down Expand Up @@ -400,6 +401,8 @@ final class ChopperGenerator
);
}

final bool hasTag = tag.isNotEmpty;

final bool useBrackets = Utils.getUseBrackets(method);

final bool includeNullQueryVars = Utils.getIncludeNullQueryVars(method);
Expand All @@ -413,6 +416,7 @@ final class ChopperGenerator
useQueries: hasQuery,
useHeaders: headers != null,
hasParts: hasParts,
tagRefer: hasTag ? refer(tag.keys.first) : null,
useBrackets: useBrackets,
includeNullQueryVars: includeNullQueryVars,
),
Expand Down Expand Up @@ -701,6 +705,7 @@ final class ChopperGenerator
bool useHeaders = false,
bool useBrackets = false,
bool includeNullQueryVars = false,
Reference? tagRefer,
}) =>
refer('Request').newInstance(
[
Expand All @@ -716,6 +721,7 @@ final class ChopperGenerator
},
if (useQueries) 'parameters': refer(Vars.parameters.toString()),
if (useHeaders) 'headers': refer(Vars.headers.toString()),
if (tagRefer != null) 'tag': tagRefer,
if (useBrackets) 'useBrackets': literalBool(useBrackets),
if (includeNullQueryVars)
'includeNullQueryVars': literalBool(includeNullQueryVars),
Expand Down
4 changes: 2 additions & 2 deletions chopper_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: chopper_generator
description: Chopper is an http client generator using source_gen, inspired by Retrofit
version: 7.2.0
version: 7.3.0
documentation: https://hadrien-lejard.gitbook.io/chopper
repository: https://github.com/lejard-h/chopper

Expand All @@ -11,7 +11,7 @@ dependencies:
analyzer: ">=5.13.0 <7.0.0"
build: ^2.4.1
built_collection: ^5.1.1
chopper: ^7.2.0
chopper: ^7.3.0
code_builder: ^4.5.0
dart_style: ^2.3.2
logging: ^1.2.0
Expand Down
Loading

0 comments on commit d2a4c46

Please sign in to comment.