Skip to content

Commit

Permalink
Merge v1.8.5 - UNAUTHORIZED-error-response
Browse files Browse the repository at this point in the history
 - refactor(api):
   - 🔨 improve payload handling in resolveBody and resolveBodySync
   - 🔨 `UNAUTHORIZED`, `BAD_REQUEST`: Allow `error` as `payload` in response generation.

 - swiss_knife: ^3.2.3
 - yaml: ^3.1.3
 - stream_channel: ^2.1.3
  • Loading branch information
gmpassos authored Dec 25, 2024
2 parents 67c392c + 61b65d9 commit f59e224
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 1.8.5

- refactor(api):
- 🔨 improve payload handling in resolveBody and resolveBodySync
- 🔨 `UNAUTHORIZED`, `BAD_REQUEST`: Allow `error` as `payload` in response generation.

- swiss_knife: ^3.2.3
- yaml: ^3.1.3
- stream_channel: ^2.1.3

## 1.8.4

- `APIRoot`:
Expand Down
6 changes: 5 additions & 1 deletion lib/src/bones_api_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef APILogger = void Function(APIRoot apiRoot, String type, String? message,
/// Bones API Library class.
class BonesAPI {
// ignore: constant_identifier_names
static const String VERSION = '1.8.4';
static const String VERSION = '1.8.5';

static bool _boot = false;

Expand Down Expand Up @@ -2798,12 +2798,14 @@ class APIResponse<T> extends APIMetricSet with APIPayload {
T? payload,
Object? payloadDynamic,
Object? mimeType,
Object? error,
Map<String, APIMetric>? metrics}) {
return APIResponse(APIResponseStatus.UNAUTHORIZED,
headers: headers ?? <String, dynamic>{},
payload: payload,
payloadDynamic: payloadDynamic,
payloadMimeType: mimeType,
error: error,
metrics: metrics);
}

Expand Down Expand Up @@ -2876,12 +2878,14 @@ class APIResponse<T> extends APIMetricSet with APIPayload {
T? payload,
Object? payloadDynamic,
Object? mimeType,
Object? error,
Map<String, APIMetric>? metrics}) {
return APIResponse(APIResponseStatus.BAD_REQUEST,
headers: headers ?? <String, dynamic>{},
payload: payload,
payloadDynamic: payloadDynamic,
payloadMimeType: mimeType,
error: error,
metrics: metrics);
}

Expand Down
16 changes: 10 additions & 6 deletions lib/src/bones_api_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1373,12 +1373,13 @@ class APIServer extends _APIServerBase {
/// Resolves a [payload] to an HTTP body (accepts [payload] as a [Future]).
/// See [resolveBodySync].
static FutureOr<Object?> resolveBody(
dynamic payload, APIResponse apiResponse) {
Object? payload, APIResponse apiResponse) {
if (payload == null) return null;

if (payload is Future) {
return payload.then((value) {
return resolveBody(value, apiResponse);
if (value == null) return null;
return _resolveBodyImpl(value, apiResponse);
}, onError: (e, s) {
return apiResponse.asError(error: 'ERROR: $e\n$s');
});
Expand All @@ -1389,7 +1390,7 @@ class APIServer extends _APIServerBase {

/// Resolves a [payload] to an HTTP body (Does NOT accept [payload] as a [Future]).
/// See [resolveBody].
static Object? resolveBodySync(dynamic payload, APIResponse apiResponse) {
static Object? resolveBodySync(Object? payload, APIResponse apiResponse) {
if (payload == null) return null;

if (payload is Future) {
Expand Down Expand Up @@ -2460,9 +2461,11 @@ final class APIServerWorker extends _APIServerBase {
headers.getAsString('WWW-Authenticate', ignoreCase: true);

if (wwwAuthenticate != null && wwwAuthenticate.isNotEmpty) {
return Response(401, body: payload, headers: headers);
return Response(401,
body: payload ?? apiResponse.error, headers: headers);
} else {
return Response.forbidden(payload, headers: headers);
return Response.forbidden(payload ?? apiResponse.error,
headers: headers);
}
}
case APIResponseStatus.REDIRECT:
Expand All @@ -2481,7 +2484,8 @@ final class APIServerWorker extends _APIServerBase {
return Response(307, body: body, headers: headers);
}
case APIResponseStatus.BAD_REQUEST:
return Response(400, body: payload, headers: headers);
return Response(400,
body: payload ?? apiResponse.error, headers: headers);
case APIResponseStatus.ERROR:
{
var error = apiResponse.error ?? '';
Expand Down
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: bones_api
description: Bones_API - A powerful API backend framework for Dart. It comes with a built-in HTTP Server, route handler, entity handler, SQL translator, and DB adapters.
version: 1.8.4
version: 1.8.5
homepage: https://github.com/Colossus-Services/bones_api

environment:
Expand All @@ -14,7 +14,7 @@ dependencies:
async_events: ^1.2.0
reflection_factory: ^2.4.8
statistics: ^1.1.3
swiss_knife: ^3.2.2
swiss_knife: ^3.2.3
data_serializer: ^1.2.1
shared_map: ^1.1.9
graph_explorer: ^1.0.2
Expand All @@ -41,11 +41,11 @@ dependencies:
postgres: ^2.6.4
mysql1: ^0.20.0
gcloud: ^0.8.18
yaml: ^3.1.2
yaml: ^3.1.3
crypto: ^3.0.6
path: ^1.9.1
archive: ^3.6.1
stream_channel: ^2.1.2
stream_channel: ^2.1.3
http: ^1.2.2
googleapis_auth: ^1.6.0
crclib: ^3.0.0
Expand Down

0 comments on commit f59e224

Please sign in to comment.