Skip to content

Add a type wrapper to the Indexes resources #183

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

Merged
merged 1 commit into from
Jun 22, 2022
Merged
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
3 changes: 2 additions & 1 deletion lib/src/client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:meilisearch/src/key.dart';
import 'package:meilisearch/src/query_parameters/indexes_query.dart';
import 'package:meilisearch/src/query_parameters/keys_query.dart';
import 'package:meilisearch/src/query_parameters/tasks_query.dart';
import 'package:meilisearch/src/result.dart';
Expand Down Expand Up @@ -34,7 +35,7 @@ abstract class MeiliSearchClient {
MeiliSearchIndex index(String uid);

/// Return list of all existing indexes.
Future<List<MeiliSearchIndex>> getIndexes();
Future<Result<MeiliSearchIndex>> getIndexes({IndexesQuery? params});

/// Find index by matching [uid]. Throws error if index is not exists.
Future<MeiliSearchIndex> getIndex(String uid);
Expand Down
12 changes: 6 additions & 6 deletions lib/src/client_impl.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:dio/dio.dart';
import 'package:meilisearch/src/client_task_impl.dart';
import 'package:meilisearch/src/query_parameters/indexes_query.dart';
import 'package:meilisearch/src/query_parameters/keys_query.dart';
import 'package:meilisearch/src/query_parameters/tasks_query.dart';
import 'package:meilisearch/src/result.dart';
Expand Down Expand Up @@ -74,13 +75,12 @@ class MeiliSearchClientImpl implements MeiliSearchClient {
}

@override
Future<List<MeiliSearchIndex>> getIndexes() async {
final response = await http.getMethod<List<dynamic>>('/indexes');
Future<Result<MeiliSearchIndex>> getIndexes({IndexesQuery? params}) async {
final response = await http.getMethod<Map<String, dynamic>>('/indexes',
queryParameters: params?.toQuery());

return response.data!
.cast<Map<String, dynamic>>()
.map((item) => MeiliSearchIndexImpl.fromMap(this, item))
.toList();
return Result<MeiliSearchIndex>.fromMapWithType(
response.data!, (item) => MeiliSearchIndexImpl.fromMap(this, item));
}

@override
Expand Down
16 changes: 16 additions & 0 deletions lib/src/query_parameters/indexes_query.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class IndexesQuery {
final int? offset;
final int? limit;

IndexesQuery({
this.limit,
this.offset,
});

Map<String, dynamic> toQuery() {
return <String, dynamic>{
'offset': this.offset,
'limit': this.limit,
}..removeWhere((key, value) => value == null);
}
}
7 changes: 3 additions & 4 deletions test/indexes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ void main() {
await client.createIndex(uid).waitFor();

final index = await client.getRawIndex(uid);
final keys = ['uid', 'name', 'primaryKey', 'createdAt', 'updatedAt'];
final keys = ['uid', 'primaryKey', 'createdAt', 'updatedAt'];

expect(index.keys, containsAll(keys));
expect(index.keys.length, keys.length);
expect(index['primaryKey'], isNull);
expect(index['name'], equals(uid));
});

test('throws exception with a non-existing index', () async {
Expand All @@ -91,8 +90,8 @@ void main() {
await client.createIndex(randomUid()).waitFor();
await client.createIndex(randomUid()).waitFor();
await client.createIndex(randomUid()).waitFor();
var indexes = await client.getIndexes();
expect(indexes.length, 3);
var response = await client.getIndexes();
expect(response.total, 3);
});

test('Create index object with UID', () async {
Expand Down
4 changes: 2 additions & 2 deletions test/utils/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ String get testServer {
}

Future<void> deleteAllIndexes() async {
var indexes = await client.getIndexes();
for (var item in indexes) {
var data = await client.getIndexes();
for (var item in data.results) {
await item.delete();
}
}
Expand Down