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

Dev #13

Merged
merged 9 commits into from
Mar 1, 2024
Merged

Dev #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/vania-dart.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Vania Dart

on:
push:
branches: [ "main", "dev"]
pull_request:
branches: [ "main", "dev"]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup Dart
uses: dart-lang/[email protected]
with:
sdk: stable

# Get Flutter packages
- name: Install dependencies
run: dart pub get

# Check formatting
- name: Format check
run: dart format --output=none --set-exit-if-changed .

# Analyze the source code
- name: Analyze project source
run: dart analyze

# Run tests
- name: Run tests
run: dart test
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
## 0.0.3+1
## 0.1.0

- Fixed bug: Authentication refresh token
- Initial beta release
- Fixed a bug related to WebSocket data events
- Corrected authentication check functionality
- Added `isAuthorized` feature
- Exported `query_builder` from Eloquent package for enhanced functionality

## 0.0.4

- Fixed bug: Authentication refresh token

## 0.0.3+1

Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

## Creating a Pull Request

Before creating a pull request please:
Before creating a pull request, please follow these steps:

1. Fork the repository and create your branch from `main`.
1. Fork the repository and create your branch from `dev`.
2. Install all dependencies (`dart pub get`).
3. Squash your commits and ensure you have a meaningful, [semantic](https://www.conventionalcommits.org/en/v1.0.0/) commit message.
4. Add tests! Pull Requests without 100% test coverage will not be approved.
5. Ensure the existing test suite passes locally.
6. Format your code (`dart format .`).
7. Analyze your code (`dart analyze --fatal-infos --fatal-warnings .`).
8. Create the Pull Request.
8. Create the Pull Request targeting the `dev` branch.
9. Verify that all status checks are passing.
26 changes: 16 additions & 10 deletions lib/src/authentication/authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Auth {

String _userGuard = 'default';

//Auth([String guard = 'default']) : _userGuard = guard;
bool _isAuthorized = false;

final Map<String, dynamic> _user = {};

Expand All @@ -25,6 +25,8 @@ class Auth {
return this;
}

bool get isAuthorized => _isAuthorized;

Map<String, dynamic>? user() => _user[_userGuard];

dynamic id() => _user[_userGuard]['id'];
Expand All @@ -44,20 +46,24 @@ class Auth {
.refreshToken(token.replaceFirst('Bearer ', ''), _userGuard, expiresIn);
}

Future<bool> check(String token) async {
Model? authenticatable =
Config().get('auth')['guards'][_userGuard]['provider'];
Future<bool> check(String token, {Map<String, dynamic>? user}) async {
Map<String, dynamic> payload = HasApiTokens()
.verify(token.replaceFirst('Bearer ', ''), _userGuard, 'access_token');

if (user == null) {
Model? authenticatable =
Config().get('auth')['guards'][_userGuard]['provider'];

if (authenticatable == null) {
throw InvalidArgumentException('Authenticatable class not found');
if (authenticatable == null) {
throw InvalidArgumentException('Authenticatable class not found');
}
user =
await authenticatable.query().where('id', '=', payload['id']).first();
}

Map<String, dynamic> payload = HasApiTokens()
.verify(token.replaceFirst('Bearer ', ''), _userGuard, 'access_token');
Map<String, dynamic>? user =
await authenticatable.query().where('id', '=', payload['id']).first();
if (user != null) {
_user[_userGuard] = user;
_isAuthorized = true;
return true;
} else {
throw Unauthenticated(message: 'Invalid token');
Expand Down
2 changes: 1 addition & 1 deletion lib/src/database/migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class Migration {
}

void index(ColumnIndex type, String name, List<String> columns) {
if (type == ColumnIndex.INDEX) {
if (type == ColumnIndex.indexKey) {
indexes.add('INDEX `$name` (${columns.map((e) => "`$e`").join(',')})');
} else {
indexes.add(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/enum/column_index.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
enum ColumnIndex {
unique,
INDEX,
indexKey,
flutext,
spatial,
}
5 changes: 4 additions & 1 deletion lib/src/http/request/request_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import 'package:vania/src/websocket/web_socket_handler.dart';
import 'package:vania/vania.dart';

Future httpRequestHandler(HttpRequest req) async {
/// Check the incoming request is web socket or not
if (Config().get("websocket") && WebSocketTransformer.isUpgradeRequest(req)) {
WebSocketHandler().handler(req);
} else {
try {
/// Check if cors is enabled
HttpCros(req);

Request request =
Expand All @@ -31,13 +33,14 @@ Future httpRequestHandler(HttpRequest req) async {
await middlewares.first.handle(request);
}

/// Controller and method handler
ControllerHandler(route: route, request: request).call();
} on BaseHttpException catch (e) {
e.call().makeResponse(req.response);
} on InvalidArgumentException catch (e) {
print(e.message);
} catch (e) {
print("catch ${e.toString()}");
print(e.toString());
}
}
}
4 changes: 2 additions & 2 deletions lib/src/http/validation/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ class Rules {

/// check field character is given max length
static bool maxLength(Map<String, dynamic> data, dynamic value, String max) {
return value.toString().length < num.parse(max.toString());
return value.toString().length <= num.parse(max.toString());
}

/// check field character is given min length
static bool minLength(Map<String, dynamic> data, dynamic value, String min) {
return value.toString().length > num.parse(min.toString());
return value.toString().length >= num.parse(min.toString());
}

/// check field character is between given length
Expand Down
12 changes: 6 additions & 6 deletions lib/src/websocket/web_socket_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class WebSocketHandler implements WebSocketEvent {

websocket.listen((data) {
Map<String, dynamic> payload = jsonDecode(data);
String event = payload[WEB_SOCKET_EVENT_KEY];
String event = payload[webScoketEventKey];

/// client join the room
if (event == WEB_SOCKET_JOIN_ROOM_EVENT_NAME) {
String? roomId = payload[WEB_SOCKET_ROOM_KEY];
if (event == webSocketJoinRoomEventName) {
String? roomId = payload[webSocketRoomKey];
if (roomId != null) {
_session.joinRoom(sessionId, roomId);
client.joinRoom(roomId);
Expand All @@ -42,8 +42,8 @@ class WebSocketHandler implements WebSocketEvent {
}

/// client left the room
if (event == WEB_SOCKET_LEFT_ROOM_EVENT_NAME) {
String? roomId = payload[WEB_SOCKET_ROOM_KEY];
if (event == webSocketLeftRoomEventName) {
String? roomId = payload[webSocketRoomKey];
if (roomId != null) {
_session.leftRoom(sessionId, roomId);
client.leftRoom(roomId);
Expand All @@ -57,7 +57,7 @@ class WebSocketHandler implements WebSocketEvent {
/// response
/// });
/// ```
dynamic message = payload[WEB_SOCKET_MESSAGE_KEY];
dynamic message = payload[webSocketMessageKey];

Function? controller = _events[event];

Expand Down
12 changes: 6 additions & 6 deletions lib/src/websocket/websocket_constants.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const String WEB_SOCKET_JOIN_ROOM_EVENT_NAME = 'joinRoom';
const String webSocketJoinRoomEventName = 'joinRoom';

const String WEB_SOCKET_LEFT_ROOM_EVENT_NAME = 'leftRoom';
const String webSocketLeftRoomEventName = 'leftRoom';

const String WEB_SOCKET_EVENT_KEY = 'event';
const String webScoketEventKey = 'event';

const String WEB_SOCKET_MESSAGE_KEY = 'message';
const String webSocketMessageKey = 'data';

const String WEB_SOCKET_SENDER_KEY = 'sender';
const String webSocketSenderKey = 'sender';

const String WEB_SOCKET_ROOM_KEY = 'room';
const String webSocketRoomKey = 'room';
1 change: 1 addition & 0 deletions lib/vania.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export 'src/database/mysql_driver.dart';
export 'src/database/postgresql_driver.dart';
export 'src/database/migration.dart';
export 'src/enum/column_index.dart';
export 'package:eloquent/src/query/query_builder.dart';

export 'src/authentication/authentication.dart';
export 'src/authentication/authenticate.dart';
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: vania
description: Fast, simple, and powerful backend framework for Dart built with ❤️
version: 0.0.4
version: 0.1.0
homepage: https://vdart.dev
repository: https://github.com/vania-dart/framework
issue_tracker: https://github.com/vania-dart/framework/issues
Expand All @@ -12,10 +12,11 @@ screenshots:
path: assets/logo.png

environment:
sdk: ^3.2.3
sdk: ^3.0.0

# Add regular dependencies here.
dependencies:
crypto: ^3.0.3
dart_jsonwebtoken: ^2.13.0
eloquent: ^3.0.0
meta: ^1.12.0
Expand Down
24 changes: 12 additions & 12 deletions test/unit/route_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,42 @@ void main() {
Router.get('/get', () {});
RouteData data = Router().routes.first;
expect(data.path, '/get');
expect(data.method, 'GET');
expect(data.method, 'get');
});

test('post route', () {
Router.post('/post', () {});
RouteData data = Router().routes.first;
expect(data.path, '/post');
expect(data.method, 'POST');
expect(data.method, 'post');
});

test('delete route', () {
Router.delete('/delete', () {});
RouteData data = Router().routes.first;
expect(data.path, '/delete');
expect(data.method, 'DELETE');
expect(data.method, 'delete');
});

test('put route', () {
Router.put('/put', () {});
RouteData data = Router().routes.first;
expect(data.path, '/put');
expect(data.method, 'PUT');
expect(data.method, 'put');
});

test('patch route', () {
Router.patch('/patch', () {});
RouteData data = Router().routes.first;
expect(data.path, '/patch');
expect(data.method, 'PATCH');
expect(data.method, 'patch');
});

test('options route', () {
Router.options('/options', () {});
RouteData data = Router().routes.first;
expect(data.path, '/options');
expect(data.method, 'OPTIONS');
expect(data.method, 'options');
});

test('group route test', () {
Expand All @@ -62,22 +62,22 @@ void main() {
List<RouteData> data = Router().routes;

expect(data[0].path, '/get');
expect(data[0].method, 'GET');
expect(data[0].method, 'get');

expect(data[1].path, '/post');
expect(data[1].method, 'POST');
expect(data[1].method, 'post');

expect(data[2].path, '/delete');
expect(data[2].method, 'DELETE');
expect(data[2].method, 'delete');

expect(data[3].path, '/put');
expect(data[3].method, 'PUT');
expect(data[3].method, 'put');

expect(data[4].path, '/patch');
expect(data[4].method, 'PATCH');
expect(data[4].method, 'patch');

expect(data[5].path, '/options');
expect(data[5].method, 'OPTIONS');
expect(data[5].method, 'options');
});

tearDownAll(() {
Expand Down
Loading