wordpress_client 9.1.0
wordpress_client: ^9.1.0 copied to clipboard
A library to interact with the Wordpress REST API. Supports most of the common endpoints and all of the CRUD operations on the endpoints.
WordPress Client
The modern, strongly‑typed WordPress REST API client for Dart & Flutter — fast, flexible, and production‑ready.
🚀 Features #
- 📦 API discovery support.
- ⏲️ Measures request completion time.
- 📝 Supports all CRUD operations.
- 🌐 Supports all common endpoints.
- 🎨 Custom Requests & Authorization systems.
- 🔐 3 Popular authorization methods.
- 🗄️ Third‑party Database integration.
- 🔧 Middlewares for request & response operations.
- 🎣 Events for preprocessing response.
- 🚀 Execute requests in Parallel (with configurable error handling).
- 🧠 Optional in‑memory caching for GET requests. final client = WordpressClient.forSite( siteUrl: Uri.parse('https://example.com'),
- 🌊 Fluent Queries );
Or pass the REST base directly:
```dart
final client = WordpressClient(
baseUrl: Uri.parse('https://example.com/wp-json/wp/v2'),
);
Add an auth quickly (helpers available):
final client = WordpressClient.forSite(
siteUrl: Uri.parse('https://example.com'),
bootstrapper: (b) => b
.withDefaultAuthorization(
// Built-in helpers
WordpressAuth.appPassword(user: 'user', appPassword: 'xxxx-xxxx'),
// or: WordpressAuth.usefulJwt(user: 'user', password: 'pass', device: 'device-id')
// or: WordpressAuth.basicJwt(user: 'user', password: 'pass')
)
.build(),
);
Enable caching via middleware (optional):
final client = WordpressClient(
baseUrl: Uri.parse('https://example.com/wp-json/wp/v2'),
bootstrapper: (b) => b
.withCache(
ttl: const Duration(minutes: 5), // optional, default 1 minute
// cache: MyCustomCacheManager(), // optional custom store
// clearOnWrite: false, // keep cache after POST/PUT/PATCH/DELETE
)
.build(),
);
📝 Notes: applies to GET requests; default store is in-memory (custom stores supported); cache clears by default on writes.
📩 Send a request #
Retrieve 20 recent posts in ascending order:
final request = ListPostRequest(
page: 1,
perPage: 20,
order: Order.asc,
);
final response = await client.posts.list(request);
// Dart 3 pattern matching
switch (response) {
case WordpressSuccessResponse():
final data = response.data; // List<Post>
break;
case WordpressFailureResponse():
final error = response.error; // WordpressError
break;
}
// Or use helpers
final posts = (await client.posts.list(ListPostRequest(perPage: 20))).map(
onSuccess: (s) => s.data,
onFailure: (f) => <Post>[],
);
final justPosts = (await client.posts.list(ListPostRequest(perPage: 20))).dataOrThrow();
Convenience extensions are available under client.<interface>.extensions
for quick lookups and pagination:
final post = await client.posts.extensions.getById(123);
final allMedia = await client.media.extensions.listAll(perPage: 100);
🌊 Fluent queries (no seed required) #
Build and send list requests fluently without constructing request classes manually. Each interface exposes a query
property that auto‑seeds the correct list request type:
final res = await client.posts.query
.withPage(1)
.withPerPage(20)
.withSearch('welcome')
.withOrder(Order.desc)
.execute();
// Advanced: mutate the underlying seed
final res2 = await client.posts.query
.configureSeed((seed) {
seed.context = RequestContext.view;
})
.execute();
// Or access it directly if you need to inspect/change fields
final builder = client.posts.query;
builder.seedRequest.context = RequestContext.view;
final res3 = await builder.execute();
Notes:
- Some auto‑seeded builders (e.g., revisions/navigation) default to placeholder IDs. Set them via
configureSeed
(orseedRequest
) beforeexecute()
. - Fluent helpers like
withPage
,withPerPage
,withSearch
,withOrder
,withOrderBy
,withCategories
,withTags
, and more are available; anything not covered can be set on the seed.
📚 Advanced docs (Wiki) #
Deep-dives and more examples live in the Wiki:
- 🧭 Getting Started: Usage
- 📩 Sending Requests
- 🛡 Authorization
- ⚡ Parallel Requests
- 🧠 Caching
- 🔗 Supported REST Methods
- 🧩 Pattern Directory Items
- 🧰 Using Custom Requests
- 🧪 Raw Requests
- 🔄 Middlewares
- 🧪 Local Testing & Docker setup (see Wiki: Usage/Testing)
- 📜 API Changelog
🤝 Feedback & Contributing #
- 🐛 For bugs or feature requests, use the issue tracker.
- 💡 Contributions are always appreciated. PRs are welcome!
Contributor tips (scripts, release process, consistency guards) are documented in the Wiki.
📄 License #
This project is MIT licensed.