Skip to content

Commit 663e58f

Browse files
committed
chore: Implement generated server
1 parent 3d27e60 commit 663e58f

31 files changed

+1372
-1080
lines changed
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
import 'package:functions/models.dart';
12
import 'package:globe_functions/globe_functions.dart';
23

4+
@RpcFunction()
5+
User? example1(String? name) {
6+
return User(name: 'John');
7+
}
8+
9+
@HttpFunction()
10+
Future<User> example2() async {
11+
return User(name: 'John');
12+
}
13+
14+
@HttpFunction()
15+
DateTime example3() {
16+
return DateTime.now();
17+
}
18+
319
@HttpFunction()
4-
String details() {
5-
return 'Hello, World!';
20+
Future<String> example4() async {
21+
return 'Hello';
622
}
Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
import 'package:functions/models.dart';
1+
// import 'package:functions/models.dart';
2+
// import 'package:globe_functions/globe_functions.dart';
3+
4+
import 'package:functions/functions.dart';
25
import 'package:globe_functions/globe_functions.dart';
36

4-
@HttpFunction()
5-
Future<User> get(String name, {User? user}) async {
6-
return User(name: name);
7+
@RpcFunction()
8+
Future<Person> get(String name, {Person? user}) async {
9+
return Person('elliot', 30);
710
}
811

9-
@HttpFunction()
10-
Future<int> getNumer(String name, {User? user, DateTime? date}) async {
11-
return 3;
12-
}
12+
// @HttpFunction()
13+
// Future<int> getNumer(String name, {User? user, DateTime? date}) async {
14+
// return 3;
15+
// }
1316

14-
@HttpFunction()
15-
Future<User> updateUserDetails(
16-
String userId,
17-
List<String> roles, [
18-
String? department = 'General',
19-
int accessLevel = 1,
20-
]) async {
21-
return User(name: userId);
22-
}
17+
// @HttpFunction()
18+
// Future<User> updateUserDetails(
19+
// String userId,
20+
// List<String> roles, [
21+
// String? department = 'General',
22+
// int accessLevel = 1,
23+
// ]) async {
24+
// return User(name: userId);
25+
// }
2326

24-
@HttpFunction()
25-
Future<User> createUserProfile(
26-
String name, {
27-
required List<String> permissions,
28-
String? title,
29-
Map<String, dynamic> metadata = const {},
30-
bool isActive = true,
31-
int priority = 0,
32-
}) async {
33-
return User(name: name);
34-
}
27+
// @HttpFunction()
28+
// Future<User> createUserProfile(
29+
// String name, {
30+
// required List<String> permissions,
31+
// String? title,
32+
// Map<String, dynamic> metadata = const {},
33+
// bool isActive = true,
34+
// int priority = 0,
35+
// }) async {
36+
// return User(name: name);
37+
// }
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1-
int calculate() {
2-
return 6 * 7;
1+
class Person {
2+
final String name;
3+
final int age;
4+
5+
Person(this.name, this.age);
6+
7+
Map<String, dynamic> toJson() {
8+
return {
9+
'name': name,
10+
'age': age,
11+
};
12+
}
13+
14+
factory Person.fromJson(Map<String, dynamic> json) {
15+
return Person(
16+
json['name'] as String,
17+
json['age'] as int,
18+
);
19+
}
320
}

examples/functions/lib/rpc_client.g.dart

Lines changed: 0 additions & 109 deletions
This file was deleted.

examples/functions/lib/test.dart

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,107 @@
1-
import 'package:functions/models.dart';
1+
// import 'package:functions/models.dart';
22

3-
import 'rpc_client.g.dart';
3+
// import 'rpc_client.g.dart';
4+
5+
// void main() async {
6+
// final client = RpcClient('http://localhost:8080');
7+
// final r6 = await client.users.details.get('hello', user: User(name: 'world'));
8+
// }
9+
import 'dart:convert';
10+
import 'package:globe_functions/src/spec/serializer.dart';
11+
import 'package:shelf/shelf.dart';
12+
import 'package:shelf/shelf_io.dart' as shelf_io;
13+
import 'package:functions/functions.dart' as i0;
14+
import 'package:functions/api/users/details.dart' as i1;
15+
import 'package:functions/api/users.dart' as i2;
16+
import 'package:functions/models.dart' as i3;
417

518
void main() async {
6-
final client = RpcClient('https://example.com');
19+
final handler = const Pipeline().addMiddleware(logRequests()).addHandler(_onRequest);
20+
final server = await shelf_io.serve(handler, 'localhost', 8080);
21+
print('Serving at http://${server.address.host}:${server.port}');
22+
}
23+
24+
Future<Response> _onRequest(Request request) async {
25+
if (request.method == 'POST' && request.url.path == '_rpc') {
26+
return _onRpcRequest(request);
27+
}
28+
29+
return Response.ok('Not found...');
30+
}
31+
32+
Future<Response> _onRpcRequest(Request request) async {
33+
final body = jsonDecode(await request.readAsString());
34+
final id = body['id'] as String;
35+
final named = body['named'] as Map<String, dynamic>;
36+
final positional = body['positional'] as List<dynamic>;
37+
38+
if (id == 'users.details.get') {
39+
final param0 = Serializers.instance.deserialize<String>(positional[0]);
40+
final userParam = named['user'] == null ? null : i0.Person.fromJson(named['user'] as Map<String, dynamic>);
41+
final result = await i1.get(
42+
param0,
43+
user: userParam
44+
);
45+
final serializedResult = result.toJson();
46+
return Response.ok(
47+
jsonEncode({'result': serializedResult, 'error': null}),
48+
headers: {'content-type': 'application/json'},
49+
);
50+
}
51+
52+
if (id == 'users.example1') {
53+
final param0 = positional[0] == null ? null : Serializers.instance.deserialize<String>(positional[0]);
54+
final result = await i2.example1(
55+
param0
56+
);
57+
final serializedResult = result == null ? null : result.toJson();
58+
return Response.ok(
59+
jsonEncode({'result': serializedResult, 'error': null}),
60+
headers: {'content-type': 'application/json'},
61+
);
62+
}
763

8-
final r5 = await client.users.details();
9-
final r6 = await client.users.details.get('hello', user: User(name: 'world'));
64+
if (id == 'users.example2') {
65+
final result = await i2.example2(
66+
67+
);
68+
final serializedResult = result.toJson();
69+
return Response.ok(
70+
jsonEncode({'result': serializedResult, 'error': null}),
71+
headers: {'content-type': 'application/json'},
72+
);
73+
}
74+
75+
if (id == 'users.example3') {
76+
final result = await i2.example3(
77+
78+
);
79+
final serializedResult = Serializers.instance.serialize<DateTime>(result);
80+
return Response.ok(
81+
jsonEncode({'result': serializedResult, 'error': null}),
82+
headers: {'content-type': 'application/json'},
83+
);
84+
}
85+
86+
if (id == 'users.example4') {
87+
final result = await i2.example4(
88+
89+
);
90+
final serializedResult = Serializers.instance.serialize<String>(result);
91+
return Response.ok(
92+
jsonEncode({'result': serializedResult, 'error': null}),
93+
headers: {'content-type': 'application/json'},
94+
);
95+
}
96+
97+
98+
// No matching function found
99+
return Response.notFound(
100+
jsonEncode({
101+
'result': null,
102+
'error': 'Function not found: $id'
103+
}),
104+
headers: {'content-type': 'application/json'},
105+
);
10106
}
107+

examples/functions/pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ packages:
253253
url: "https://pub.dev"
254254
source: hosted
255255
version: "0.7.1"
256+
json:
257+
dependency: transitive
258+
description:
259+
name: json
260+
sha256: "8708a43b61848f50dbe952131fa60af35e15164ddb275d14ef8b7994ffd492ae"
261+
url: "https://pub.dev"
262+
source: hosted
263+
version: "0.20.4"
256264
json_annotation:
257265
dependency: transitive
258266
description:

packages/globe_functions/analysis_options.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
include: package:lints/recommended.yaml
1515

16-
# Uncomment the following section to specify additional rules.
16+
analyzer:
17+
enable-experiment:
18+
- macros
1719

1820
# linter:
1921
# rules:

packages/globe_functions/lib/src/annotations.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ abstract class GlobeFunction {
22
const GlobeFunction();
33
}
44

5+
class RpcFunction extends GlobeFunction {
6+
const RpcFunction();
7+
}
8+
59
class HttpFunction extends GlobeFunction {
610
const HttpFunction();
711
}
12+
13+
enum GlobeFunctionType { rpc, http }

0 commit comments

Comments
 (0)