Skip to content

Commit

Permalink
Fix overwhelming debug's message ouputing in production
Browse files Browse the repository at this point in the history
  • Loading branch information
Dahkenangnon committed Jan 25, 2021
1 parent d02d981 commit f76cce8
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 124 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 1.0.10
Fix [overwhelming debug's message ouputing in production](https://github.com/Dahkenangnon/flutter_feathersjs.dart/discussions/6#discussion-2051014)
## 1.0.9

Documentation is now available from: [https://dahkenangnon.github.io/flutter_feathersjs.dart/](https://dahkenangnon.github.io/flutter_feathersjs.dart/)
Expand Down
30 changes: 27 additions & 3 deletions docs/src/guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,34 @@ Communicate with your feathers js [https://feathersjs.com/](https://feathersjs.c

`Infos: Feathers js is a node framework for real-time applications and REST APIs.`


*__FormData support out the box, auth, reAuth, socketio send event, rest ...__

## Illustration's service infos

In this documentation, we assume that our feathers js api has a **news** services register on **/news**

with the following schema

```js

// News schema
const schema = new Schema({

title: { type: String, required: true },

content: { type: String, required: true },

// They can provide multiple image for a single news
files: { type: Array, required: false },

//The publisher id
author: { type: Schema.Types.ObjectId, ref: 'users', required: true },

}, {
timestamps: true
});
```

NB: I'm working on this doc and an example app which will be available very soon.
## The illustration's app

:warning: This Doc is still dev, so it can contain some misspelled.
The app used for this documentation will be available very soon.
148 changes: 147 additions & 1 deletion docs/src/guide/using-rest.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,149 @@
# Rest methods

This is coming soon.
**__Feathers Js rest client for rest api call__**

You get exactly what feathers server send (*Serialization and Deserialization are not supported.*)
You can get the response sent by feathers js by: response.data
When required, to access the data property of feathers response, you have to do: response.data.data

## find

Retrieves a list of all matching resources from the service

All news

```dart
// Find all news
var newsResponse = await flutterFeathersjs.rest.find(serviceName: "news");
print(newsResponse.data);
```

News matching certain query

```dart
// Find news with query: here with {"_id": "5f7643f0462f4348970cd32e"}
var newsResponse = await flutterFeathersjs.rest.find(serviceName: "news", query: {"_id": "5f7643f0462f4348970cd32e"});
print(newsResponse.data);
```

## get

Retrieve a single resource from the service with an `_id`

```dart
// Get a single new with it _id
var newsResponse = await flutterFeathersjs.rest.get(serviceName: "news", objectId: "5f7643f0462f4348970cd32e");
print(newsResponse.data);
```

## create

Create a new resource with data.

Without files

```dart
// Create a news on the server without file upload
var newsResponse = await flutterFeathersjs.rest.create(
serviceName: "news",
data: {"title": "Using FlutterFeathersjs is easy", "content": "Yes very easy" , "author" :"5f7h43f0462f4348970cd32e"});
```

With files

```dart
// Create a news on the server with file upload
// For this, it is your responsability to configure your feathers js to handle
// file upload, see multer for example
//1. Afther picking filed from devices, ensure you have a list of File object as:
var files = [{ 'filePath': '/data/shared/image.png', 'fileName': 'image.png' },
{ 'filePath': '/data/shared/image.png', 'fileName': 'image.png' }];
//2. Send data with files
var newsResponse = await flutterFeathersjs.rest.create(
serviceName: "news",
data: {"title": "Using FlutterFeathersjs is easy even with file upload", "content": "Yes very easy" , "author" :"5f7h43f0462f4348970cd32e"},
containsFile = true,
fileFieldName = "files", // See the news schema. This is the same string
);
```

## update

Completely replace a single resource with the `_id = objectId`

Without files

```dart
// Update a news on the server without file upload
var newsResponse = await flutterFeathersjs.rest.update(
objectId: "5f7h43f0462f4t48970cd32e",
serviceName: "news",
data: {"title": "Using FlutterFeathersjs is easy", "content": "Yes very easy" , "author" :"5f7h43f0462f4348970cd32e"});
```

With files

```dart
// Create a news on the server with file upload
// For this, it is your responsability to configure your feathers js to handle
// file upload, see multer
//1. Afther picking file from devices, ensure you have a list of File object as:
var files = [{ 'filePath': '/data/shared/image.png', 'fileName': 'image.png' },
{ 'filePath': '/data/shared/image.png', 'fileName': 'image.png' }];
//2. Send data with file
var newsResponse = await flutterFeathersjs.rest.update(
objectId: "5f7h43f0462f4t48970cd32e",
serviceName: "news",
data: {"title": "Using FlutterFeathersjs is easy even with file upload", "content": "Yes very easy" , "author" :"5f7h43f0462f4348970cd32e"},
containsFile = true,
fileFieldName = "files", // See the news schema. This is the same string
);
```

## patch

Completely replace a single resource with the `_id = objectId`

Without files

```dart
// Patch a new on the server without file upload
var newsResponse = await flutterFeathersjs.rest.patch(
objectId: "5f7h43f0462f4t48970cd32e",
serviceName: "news",
data: {"title": "Using FlutterFeathersjs is easy", "content": "Yes very easy" , "author" :"5f7h43f0462f4348970cd32e"});
```

With files

```dart
// Patch a news on the server with file upload
// For this, it is your responsability to configure your feathers js to handle
// file upload, see multer
//1. Afther picking file from devices, ensure you have a list of File object as:
var files = [{ 'filePath': '/data/shared/image.png', 'fileName': 'image.png' },
{ 'filePath': '/data/shared/image.png', 'fileName': 'image.png' }];
//2. Send data with file
var newsResponse = await flutterFeathersjs.rest.patch(
objectId: "5f7h43f0462f4t48970cd32e",
serviceName: "news",
data: {"title": "Using FlutterFeathersjs is easy even with file upload", "content": "Yes very easy" , "author" :"5f7h43f0462f4348970cd32e"},
containsFile = true,
fileFieldName = "files", // See the news schema. This is the same string
);
```

## remove

Remove a single resource with `_id = objectId`:

```dart
// Remove a news
var newsResponse = await flutterFeathersjs.rest.remove(serviceName: "news", objectId: "5f7643f0462f4348970cd32e");
```
8 changes: 0 additions & 8 deletions lib/flutter_feathersjs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ class FlutterFeathersjs {

///Intialize both rest and scoketio client
init({@required String baseUrl, Map<String, dynamic> extraHeaders}) {
// if (Foundation.kReleaseMode) {
// // W're on release mode
// dev = false;
// } else {
// // W're not on release mode
// dev = true;
// }

rest = new RestClient()..init(baseUrl: baseUrl, extraHeaders: extraHeaders);

scketio = new SocketioClient()..init(baseUrl: baseUrl);
Expand Down
104 changes: 54 additions & 50 deletions lib/src/rest_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_feathersjs/src/constants.dart';
import 'package:flutter_feathersjs/src/featherjs_client_base.dart';
import 'package:flutter_feathersjs/src/utils.dart';
import 'package:meta/meta.dart';
import 'package:flutter/foundation.dart' as Foundation;

///
///Feathers Js rest client for rest api call
Expand Down Expand Up @@ -58,19 +59,24 @@ class RestClient extends FlutterFeathersjs {
// Do something with response error

if (e.response != null) {
print(e.response.data);
print(e.response.headers);
print(e.response.request);
//Only send the error message from feathers js server not for Dio
print(e.response);
if (!Foundation.kReleaseMode) {
print(e.response.data);
print(e.response.headers);
print(e.response.request);
//Only send the error message from feathers js server not for Dio
print(e.response);
}

return dio.resolve(e.response.data);
//return e.response.data; //continue
} else {
// Something happened in setting up or sending the request that triggered an Error
print(e.request);
print(e.message);
//By returning null, it means that error is from client
//return null;
if (!Foundation.kReleaseMode) {
// Something happened in setting up or sending the request that triggered an Error
print(e.request);
print(e.message);
//By returning null, it means that error is from client
//return null;
}
return e;
}
}));
Expand Down Expand Up @@ -98,26 +104,37 @@ class RestClient extends FlutterFeathersjs {
var response = await this
.dio
.get("/$serviceName", queryParameters: {"\$limit": 1});
print(response);
if (!Foundation.kReleaseMode) {
print(response);
}

if (response.statusCode == 401) {
print("jwt expired or jwt malformed");
if (!Foundation.kReleaseMode) {
print("jwt expired or jwt malformed");
}

authResponse["error"] = true;
authResponse["message"] = "jwt expired";
authResponse["error_zone"] = Constants.JWT_EXPIRED_ERROR;
} else if (response.statusCode == 200) {
print("Jwt still validated");
if (!Foundation.kReleaseMode) {
print("Jwt still validated");
}
authResponse["error"] = false;
authResponse["message"] = "Jwt still validated";
authResponse["error_zone"] = Constants.NO_ERROR;
} else {
print("Unknown error");
if (!Foundation.kReleaseMode) {
print("Unknown error");
}
authResponse["error"] = true;
authResponse["message"] = "Unknown error";
authResponse["error_zone"] = Constants.UNKNOWN_ERROR;
}
} catch (e) {
print("Unable to connect to the server");
if (!Foundation.kReleaseMode) {
print("Unable to connect to the server");
}
authResponse["error"] = true;
authResponse["message"] = e;
authResponse["error_zone"] = Constants.JWT_ERROR;
Expand All @@ -126,7 +143,9 @@ class RestClient extends FlutterFeathersjs {

///No token is found
else {
print("No old token found. Must reAuth user");
if (!Foundation.kReleaseMode) {
print("No old token found. Must reAuth user");
}
authResponse["error"] = true;
authResponse["message"] = "No old token found. Must reAuth user";
authResponse["error_zone"] = Constants.JWT_NOT_FOUND;
Expand Down Expand Up @@ -207,8 +226,8 @@ class RestClient extends FlutterFeathersjs {
try {
response = await this.dio.get("/$serviceName", queryParameters: query);
} catch (e) {
print("Error in rest::find");
print(e);
// print("Error in rest::find");
// print(e);
}
return response;
}
Expand All @@ -223,8 +242,8 @@ class RestClient extends FlutterFeathersjs {
try {
response = response = await this.dio.get("/$serviceName/$objectId");
} catch (e) {
print("Error in rest::get");
print(e);
// print("Error in rest::get");
// print(e);
}
return response;
}
Expand Down Expand Up @@ -261,16 +280,16 @@ class RestClient extends FlutterFeathersjs {
Response<dynamic> response;

if (!containsFile) {
print('Dio.post without file');
print("Service name is");
print(serviceName);
print("Data are: ");
print(data);
// print('Dio.post without file');
// print("Service name is");
// print(serviceName);
// print("Data are: ");
// print(data);
try {
response = await this.dio.post("/$serviceName", data: data);
print("Response from server is:");
print(response.data);
print(response);
// print("Response from server is:");
// print(response.data);
// print(response);
} catch (e) {
print("Error in rest::create");
print(e);
Expand Down Expand Up @@ -453,36 +472,21 @@ class RestClient extends FlutterFeathersjs {
@required fileFieldName,
List<Map<String, String>> files}) async {
Map<String, dynamic> data = {};
print("Inside makeFormData");
print("nonFilesFieldsMap is ");
print(nonFilesFieldsMap);

if (!Foundation.kReleaseMode) {
print("Inside makeFormData");
print("nonFilesFieldsMap is ");
print(nonFilesFieldsMap);
}

// Non file
if (nonFilesFieldsMap != null) {
print("nonFilesFieldsMap is not null ");
nonFilesFieldsMap.forEach((key, value) {
data["$key"] = value;
});
}
print("nonFilesFieldsMap withoud the files is ");
print(data);
var formData = FormData.fromMap(data);
// // If single file is send
// if (files != null && files.length == 1) {
// print("Receive signle file");
// var fileData = files[0];
// var file = await MultipartFile.fromFile(fileData["filePath"],
// filename: fileData["fileName"]);
// data["$fileFieldName"] = file;
// } else if (files != null && files.length >= 1) {
// print("Receive multiple file");
// List filesList = [];
// for (var fileData in files) {
// var file = await MultipartFile.fromFile(fileData["filePath"],
// filename: fileData["fileName"]);
// filesList.add(file);
// }
// data["$fileFieldName"] = filesList;
// }
for (var fileData in files) {
formData.files.add(MapEntry(
fileFieldName,
Expand Down
Loading

0 comments on commit f76cce8

Please sign in to comment.