From f110b7c959a7e06ecbe82ce8f095558a8f0ebc94 Mon Sep 17 00:00:00 2001 From: Arun Prakash Date: Sun, 12 May 2024 14:37:37 +0530 Subject: [PATCH] chore: Bump wordpress_client version to 8.4.7 and fix bug in utility functions --- CHANGELOG.md | 5 +++ README.md | 2 +- lib/src/operations/create.dart | 2 +- lib/src/operations/custom.dart | 2 +- lib/src/operations/delete.dart | 2 +- lib/src/operations/list.dart | 2 +- lib/src/operations/retrieve.dart | 2 +- lib/src/operations/update.dart | 2 +- lib/src/request_executor_base.dart | 35 +++++++++++++++++++ lib/src/responses/wordpress_raw_response.dart | 3 ++ lib/src/utilities/helpers.dart | 3 +- pubspec.yaml | 2 +- 12 files changed, 53 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d46add..bfa5cf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -285,6 +285,11 @@ - ๐Ÿ›  Added validations for entered `baseUrl`. - ๐Ÿ’ฅRenamed `executeGuarded` to `guardAsync` and added a new `guard` method to guard synchronous functions. +## ๐Ÿ› 8.4.7 + +- ๐Ÿฉน Bug fixes +- ๐Ÿ›  Fix validations for entered `baseUrl`; Supporting sites with custom REST Api paths + ## Legend - ๐ŸŽ‰ New features or major changes diff --git a/README.md b/README.md index c17a788..bca10f5 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Add `wordpress_client` in your `pubspec.yaml`: ```dart dependencies: - wordpress_client: ^8.4.6 + wordpress_client: ^8.4.7 ``` > ๐Ÿ’ก Ensure you get the [latest version here](https://pub.dev/packages/wordpress_client). diff --git a/lib/src/operations/create.dart b/lib/src/operations/create.dart index 00b67c1..e501b89 100644 --- a/lib/src/operations/create.dart +++ b/lib/src/operations/create.dart @@ -12,6 +12,6 @@ base mixin CreateOperation on IRequestInterface { Future createRaw(R request) async { final wpRequest = await request.build(baseUrl); - return executor.execute(wpRequest); + return executor.raw(wpRequest); } } diff --git a/lib/src/operations/custom.dart b/lib/src/operations/custom.dart index 31ea06a..20acec4 100644 --- a/lib/src/operations/custom.dart +++ b/lib/src/operations/custom.dart @@ -17,6 +17,6 @@ base mixin CustomOperation on IRequestInterface { Future raw(R request) async { final wpRequest = await request.build(baseUrl); - return executor.execute(wpRequest); + return executor.raw(wpRequest); } } diff --git a/lib/src/operations/delete.dart b/lib/src/operations/delete.dart index 6c44a1f..f6de97d 100644 --- a/lib/src/operations/delete.dart +++ b/lib/src/operations/delete.dart @@ -14,6 +14,6 @@ base mixin DeleteOperation on IRequestInterface { Future deleteRaw(R request) async { final wpRequest = await request.build(baseUrl); - return executor.execute(wpRequest); + return executor.raw(wpRequest); } } diff --git a/lib/src/operations/list.dart b/lib/src/operations/list.dart index 138665b..4a9f2a8 100644 --- a/lib/src/operations/list.dart +++ b/lib/src/operations/list.dart @@ -16,6 +16,6 @@ base mixin ListOperation on IRequestInterface { ) async { final wpRequest = await request.build(baseUrl); - return executor.execute(wpRequest); + return executor.raw(wpRequest); } } diff --git a/lib/src/operations/retrieve.dart b/lib/src/operations/retrieve.dart index 6998d45..589492b 100644 --- a/lib/src/operations/retrieve.dart +++ b/lib/src/operations/retrieve.dart @@ -12,6 +12,6 @@ base mixin RetrieveOperation on IRequestInterface { Future retrieveRaw(R request) async { final wpRequest = await request.build(baseUrl); - return executor.execute(wpRequest); + return executor.raw(wpRequest); } } diff --git a/lib/src/operations/update.dart b/lib/src/operations/update.dart index 1af4efe..a16afa9 100644 --- a/lib/src/operations/update.dart +++ b/lib/src/operations/update.dart @@ -12,6 +12,6 @@ base mixin UpdateOperation on IRequestInterface { Future updateRaw(R request) async { final wpRequest = await request.build(baseUrl); - return executor.execute(wpRequest); + return executor.raw(wpRequest); } } diff --git a/lib/src/request_executor_base.dart b/lib/src/request_executor_base.dart index dc9fb7c..f7672b9 100644 --- a/lib/src/request_executor_base.dart +++ b/lib/src/request_executor_base.dart @@ -33,6 +33,41 @@ abstract base class IRequestExecutor { ); } + @internal + Future raw(WordpressRequest request) async { + return guardAsync( + function: () async { + request = await _handleRequestMiddlewares( + request: request, + middlewares: middlewares, + ); + + return _handleResponseMiddlewares( + middlewares: middlewares, + response: await execute(request), + ); + }, + onError: (error, stackTrace) async { + final isMiddlewareAbortedException = + error is MiddlewareAbortedException; + + return WordpressRawResponse( + data: null, + code: isMiddlewareAbortedException + ? -RequestErrorType.middlewareAborted.index + : -RequestErrorType.internalGenericError.index, + extra: { + 'error': error, + 'stackTrace': stackTrace, + }, + message: isMiddlewareAbortedException + ? error.message + : '$error\n\n$stackTrace', + ); + }, + ); + } + @internal Future> create( WordpressRequest request, diff --git a/lib/src/responses/wordpress_raw_response.dart b/lib/src/responses/wordpress_raw_response.dart index c1d2746..198fc30 100644 --- a/lib/src/responses/wordpress_raw_response.dart +++ b/lib/src/responses/wordpress_raw_response.dart @@ -188,6 +188,9 @@ final class WordpressRawResponse { ); } + /// Returns the error and stack trace if the response is a failure. + (Object, StackTrace) getError() => (extra['error'], extra['stackTrace']); + /// Maps this instance to a [WordpressResponse] instance. /// /// The [onSuccess] is called if the response is a success. diff --git a/lib/src/utilities/helpers.dart b/lib/src/utilities/helpers.dart index ae37958..9b5ea21 100644 --- a/lib/src/utilities/helpers.dart +++ b/lib/src/utilities/helpers.dart @@ -140,7 +140,8 @@ bool isValidRestApiUrl(Uri uri, {bool forceHttps = false}) { return false; } - return uri.pathSegments.first == 'wp-json'; + // It is possible to not have wp-json on some websites but I don't plan on supporting those in this package because it goes outside of the standards defined by WordPress. + return uri.pathSegments.contains('wp-json'); } bool isValidPortNumber(int port) => port >= 0 && port <= 65535; diff --git a/pubspec.yaml b/pubspec.yaml index 9cf3bc1..b8e9b8a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: wordpress_client description: A library to interact with the Wordpress REST API. Supports most of the common endpoints and all of the CRUD operations on the endpoints. -version: 8.4.6 +version: 8.4.7 topics: - wordpress - fluent