https://github.com/surfstudio/macrofit
Macro version of retrofit.dart
https://github.com/surfstudio/macrofit
Last synced: 22 days ago
JSON representation
Macro version of retrofit.dart
- Host: GitHub
- URL: https://github.com/surfstudio/macrofit
- Owner: surfstudio
- License: apache-2.0
- Created: 2024-07-31T09:07:51.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-15T20:53:40.000Z (about 1 year ago)
- Last Synced: 2025-04-13T07:13:14.405Z (6 months ago)
- Language: Dart
- Size: 1.16 MB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
## ~~Retro~~Macrofit for Dart
macrofit is a type conversion dio client generator based on macro feature and inspired by [retrofit.dart](https://pub.dev/packages/retrofit).
### Setup
Since macro is not stable yet, you need to enable it in your `analysis_options.yaml`:
```yaml
include: package:flutter_lints/flutter.yamlanalyzer:
enable-experiment:
- macros
```Also you need use flag `--enable-experiment=macros` when running your app:
```shell
dart run --enable-experiment=macros example/lib/example.dart
```### Usage
#### Client
Create your client:
```dart
@RestClient()
class ClientExample {ClientExample(this._dio, [this.baseUrl]);
}
```#### Queries
Fill client with your queries. E.g.:
```dart
@RestClient()
class ClientExample {ClientExample(this._dio, [this.baseUrl]);
@MultiPart('some/path')
external Future someRequest(
@Part() String part1,
@Part() File photo,
);@POST('/posts/{userId}')
external Future updateProfile(
@Header('Test') String testHeader,
@Body() String name,
@Body() String surname,
String userId,
);@DELETE('/posts/{id}')
external Future deletePost(
@Header('Test') String id,
);@GET('/posts')
external Future>> getPosts(
@Query() int page,
@Query() int limit,
);
}
```#### Methods
This library supports `GET`, `POST`, `PUT`, `DELETE` methods. Also you can define your own:
```dart
@Custom('/posts/{id}', methodName: 'PATCH')
external Future patchPost(int id, @Body() String title);
```#### Parameters
You can use:
- `@Query` for query parameters (e.g. `?page=1&limit=10`)```dart
@GET('/posts')
external Future>> getPosts(
@Query() int page,
@Query() int limit,
);
```- `@Body` for request body. E.g:
```dart
@POST('/posts')
external Future newPost(
@Body() String title,
@Body() String body,
);
```- `@Header` for headers. E.g:
```dart
@GET('/posts')
external Future translate(
@Header('Locale') String locale,
);
```- `@Part` for multi-part requests. E.g:
```dart
@MultiPart('/posts')
external Future newPost(
@Part() String title,
@Part() File photo,
);
```- `@Path` for path parameters. Make sure to use the same name in the method and in the path. E.g:
```dart
@GET('/posts/{id}')
external Future getPost(int id);
```