Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Enable passing in Extras using Custom Annotations #706

Merged
merged 16 commits into from
Sep 6, 2024

Conversation

farmery
Copy link
Contributor

@farmery farmery commented Sep 6, 2024

Summary

This PR adds functionality that allows developers to pass extras with requests using custom classes.

Problem

Imagine you want every request in your app to include some additional metadata, such as an id and a region. Without this feature, you would have to annotate each request manually with something like Extra({'id': '1234', 'region': 'ng'}). While this approach works, it requires passing the key-value pairs each time, which introduces several risks:

Developers may inadvertently misspell a key (e.g., regoin instead of region).
A required key might be forgotten or omitted.
The repeated manual input increases the chance of inconsistency,

Solution

Introduce TypedExtras, which developers can extend to provide extras in a structured and type-safe manner. By defining fields within the custom class, developers can avoid manual key-value entry, ensuring consistency and reducing the risk of errors.

Example:

class MetaData extend TypedExtras {
 final String id;
 final String region;
 const MetaData({required this.id, required region});
}

@MetaData(
  id: '1234',
  region: 'ng',
)
@GET("/get")
Future<String> fetchData();

// Which will generate 
final _extras = <String, dynamic>{
 'id': '1234',
 'region': 'ng',
}

@trevorwang
Copy link
Owner

Hi @farmery , thanks for the great work.

Lets bump the lib versions and update the change logs before we merge that changes.

@farmery
Copy link
Contributor Author

farmery commented Sep 6, 2024

Hi @trevorwang I just updated the change logs and versions

@trevorwang trevorwang merged commit f1409e8 into trevorwang:master Sep 6, 2024
2 checks passed
@trevorwang
Copy link
Owner

New version has been deployed @farmery

@moshe5745
Copy link
Contributor

@farmery What a timing!
I came to implement some features in our app by passing headers and deleting them in the interceptor.
Came to the changelog and see this... Thank you very much!

@trevorwang Could you copy-paste the example provided here to the docs?

@moshe5745
Copy link
Contributor

@farmery
Is it possible to use TypedExtras only for typing without the default value?
I mean I have some API endpoint. I want the extras to be typed.
Right now when I extract them in the interceptor I have Map<String, dynamic>. Is it possible to infer the type somehow? Or is it a feature on the Dio side?

@trevorwang
Copy link
Owner

@farmery What a timing! I came to implement some features in our app by passing headers and deleting them in the interceptor. Came to the changelog and see this... Thank you very much!

@trevorwang Could you copy-paste the example provided here to the docs?

Would you like to submit a PR for this? @moshe5745

@moshe5745
Copy link
Contributor

@farmery What a timing! I came to implement some features in our app by passing headers and deleting them in the interceptor. Came to the changelog and see this... Thank you very much!
@trevorwang Could you copy-paste the example provided here to the docs?

Would you like to submit a PR for this? @moshe5745

Sure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants