Skip to content

Commit bf93fd0

Browse files
feat: v1 improvements (#14)
1 parent 34b574f commit bf93fd0

File tree

85 files changed

+3717
-1513
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3717
-1513
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ jobs:
1616
build:
1717
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1
1818
with:
19-
coverage_excludes: "*.pb*.dart"
19+
coverage_excludes: "**/*.pb*.dart"
2020
min_coverage: 0

README.md

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ To create our Nitric project, we have to create a `nitric.yaml` file. The handle
107107
name: my_profile_api
108108
services:
109109
- match: bin/my_profile_api.dart
110-
start: dart run bin/my_profile_api.dart
110+
start: dart run $SERVICE_PATH
111111
```
112112
113113
## Create a Profile class
@@ -141,34 +141,34 @@ class Profile {
141141
Applications built with Nitric can contain many APIs, let's start by adding one to this project to serve as the public endpoint. Rename `bin/my_profile_api.dart` to `bin/profiles.dart`
142142

143143
```dart
144-
import 'package:uuid/uuid.dart';
144+
import 'package:nitric_sdk/nitric.dart';
145+
import 'package:nitric_sdk/resources.dart';
146+
import 'package:nitric_sdk/src/context/common.dart';
145147
146-
import 'package:nitric_sdk/src/api/collection.dart';
147-
import 'package:nitric_sdk/src/nitric.dart';
148-
import 'package:nitric_sdk/src/resources/common.dart';
148+
import 'package:uuid/uuid.dart';
149149
150150
void main() {
151151
// Create an API named 'public'
152152
final profileApi = api("public");
153153
154-
// Define a collection named 'profiles', then request reading and writing permissions.
155-
final profiles = collection<Profile>("profiles").requires([
156-
CollectionPermission.writing,
157-
CollectionPermission.deleting,
158-
CollectionPermission.reading
154+
// Define a key value store named 'profiles', then request getting, setting and deleting permissions.
155+
final profiles = store("profiles").requires([
156+
KeyValuePermission.getting,
157+
KeyValuePermission.setting,
158+
KeyValuePermission.deleting
159159
]);
160160
}
161161
```
162162

163-
Here we're creating an API named `public` and a collection named `profiles`, then requesting read, write, and delete permissions which allows our function to access the collection.
163+
Here we're creating an API named `public` and a key value store named `profiles`, then requesting get, set, and delete permissions which allows our function to access the key value store.
164164

165165
<Note>
166-
Resources in Nitric like `api` and `collection` represent high-level cloud
166+
Resources in Nitric like `api` and `key value store` represent high-level cloud
167167
resources. When your app is deployed Nitric automatically converts these
168168
requests into appropriate resources for the specific
169169
[provider](https://nitric.io/docs/reference/providers). Nitric also takes care of adding the IAM
170170
roles, policies, etc. that grant the requested access. For example the
171-
`collection` resource uses DynamoDB in AWS or FireStore on Google Cloud.
171+
`key value stores` resource uses DynamoDB in AWS or FireStore on Google Cloud.
172172
</Note>
173173

174174
### Create profiles with POST
@@ -189,7 +189,7 @@ profileApi.post("/profiles", (ctx) async {
189189
final profile = Profile.fromJson(ctx.req.json());
190190
191191
// Store the new profile in the profiles collection
192-
await profiles.key(id).put(profile);
192+
await profiles.set(id, profile);
193193
194194
// Send a success response.
195195
ctx.resp.body = "Profile $id created.";
@@ -206,9 +206,9 @@ profileApi.get("/profiles/:id", (ctx) async {
206206
207207
try {
208208
// Retrieve and return the profile data
209-
final profile = await profiles.key(id).access();
209+
final profile = await profiles.get(id);
210210
ctx.resp.json(profile.toJson());
211-
} on KeyNotFoundException catch (e) {
211+
} on Exception catch (e) {
212212
print(e);
213213
ctx.resp.status = 404;
214214
ctx.resp.body = "Profile $id not found.";
@@ -218,19 +218,6 @@ profileApi.get("/profiles/:id", (ctx) async {
218218
});
219219
```
220220

221-
### List all profiles with GET
222-
223-
```dart
224-
profileApi.get("/profiles", (ctx) async {
225-
// Return all profiles
226-
final allProfiles = await profiles.list();
227-
228-
ctx.resp.json({'profiles': allProfiles});
229-
230-
return ctx;
231-
});
232-
```
233-
234221
### Remove a profile with DELETE
235222

236223
```dart
@@ -239,11 +226,11 @@ profileApi.delete("/profiles/:id", (ctx) async {
239226
240227
// Delete the profile
241228
try {
242-
await profiles.key(id).unset();
229+
await profiles.delete(id);
243230
ctx.resp.body = "Profile $id removed.";
244-
} on KeyNotFoundException catch (e) {
231+
} on Exception catch (e) {
245232
ctx.resp.status = 404;
246-
ctx.resp.body = "Profile $id not found.";
233+
ctx.resp.body = "Profile $id not found. $e";
247234
}
248235
249236
return ctx;
@@ -255,15 +242,9 @@ profileApi.delete("/profiles/:id", (ctx) async {
255242
Now that you have an API defined with handlers for each of its methods, it's time to test it locally.
256243

257244
```bash
258-
npm run dev
245+
nitric start
259246
```
260247

261-
<Note>
262-
the `dev` script starts the Nitric Server using `nitric start`, which provides
263-
local interfaces to emulate cloud resources, then runs your functions and
264-
allows them to connect.
265-
</Note>
266-
267248
Once it starts, the application will receive requests via the API port. You can use the Local Dashboard or any HTTP client to test the API. We'll keep it running for our tests. If you want to update your functions, just save them, they'll be reloaded automatically.
268249

269250
## Test the API
@@ -354,7 +335,7 @@ If you want to go a bit deeper and create some other resources with Nitric, why
354335
Define a bucket named `profilesImg` with reading/writing permissions.
355336

356337
```dart
357-
final profilesImg = bucket("profilesImg").requires([BucketPermission.reading, BucketPermission.writing]);
338+
final profilesImg = Nitric.bucket("profilesImg").requires([BucketPermission.reading, BucketPermission.writing]);
358339
```
359340

360341
### Get a URL to upload a profile image

example/services/nitric_example.dart

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
import 'package:nitric_sdk/nitric.dart';
22
import 'package:nitric_sdk/resources.dart';
3-
import 'package:nitric_sdk/src/context/common.dart';
43
import 'package:uuid/uuid.dart';
54

65
class Profile {
7-
String name;
8-
int age;
9-
String homeTown;
6+
late String name;
7+
late int age;
8+
late String homeTown;
9+
late List<String> contacts;
1010

11-
Profile(this.name, this.age, this.homeTown);
11+
Profile({required this.name, required this.age, required this.homeTown});
1212

1313
Profile.fromJson(Map<String, dynamic> contents)
1414
: name = contents["name"] as String,
1515
age = contents["age"] as int,
16-
homeTown = contents["homeTown"] as String;
16+
homeTown = contents["homeTown"] as String,
17+
contacts = List<String>.from(contents["contacts"]);
1718

18-
Map<String, dynamic> toJson() => {
19-
'name': name,
20-
'age': age,
21-
'homeTown': homeTown,
22-
};
19+
Map<String, dynamic> toJson() =>
20+
{'name': name, 'age': age, 'homeTown': homeTown, 'contacts': contacts};
2321
}
2422

2523
void main() {
24+
var oidc = Nitric.oidcRule(
25+
"profile security",
26+
"https://dev-w7gm5ldb.us.auth0.com",
27+
["https://test-security-definition/"]);
28+
2629
// Create an API named 'public'
27-
final profileApi = Nitric.api("public");
30+
final profileApi = Nitric.api("public",
31+
opts: ApiOptions(security: [
32+
oidc(["user:read"])
33+
]));
2834

2935
// Define a collection named 'profiles', then request reading and writing permissions.
30-
final profiles = Nitric.store<Profile>("profiles").requires([
36+
final profiles = Nitric.store("profiles").requires([
3137
KeyValueStorePermission.getting,
3238
KeyValueStorePermission.deleting,
3339
KeyValueStorePermission.setting
@@ -36,22 +42,22 @@ void main() {
3642
final profilesImg = Nitric.bucket("profilesImg")
3743
.requires([BucketPermission.reading, BucketPermission.writing]);
3844

39-
profilesImg.on(BlobEventType.write, "*", (ctx) async {
40-
return ctx;
41-
});
42-
4345
profileApi.post("/profiles", (ctx) async {
4446
final uuid = Uuid();
4547

4648
final id = uuid.v4();
4749

48-
final profile = Profile.fromJson(ctx.req.json());
49-
50-
// Store the new profile in the profiles collection
51-
await profiles.set(id, profile);
50+
try {
51+
var profile = Profile.fromJson(ctx.req.json());
52+
// Store the new profile in the profiles collection
53+
await profiles.set(id, profile.toJson());
5254

53-
// Send a success response.
54-
ctx.resp.body = "Profile $id created.";
55+
// Send a success response.
56+
ctx.resp.body = "Profile $id created.";
57+
} on Exception catch (e) {
58+
ctx.resp.status = 400;
59+
ctx.resp.body = "An error occurred: $e";
60+
}
5561

5662
return ctx;
5763
});
@@ -62,7 +68,7 @@ void main() {
6268
try {
6369
// Retrieve and return the profile data
6470
final profile = await profiles.get(id);
65-
ctx.resp.json(profile.toJson());
71+
ctx.resp.json(profile);
6672
} on Exception catch (e) {
6773
print(e);
6874
ctx.resp.status = 404;
@@ -123,6 +129,4 @@ void main() {
123129

124130
return ctx;
125131
});
126-
127-
Nitric.run();
128132
}

lib/resource.dart

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/resources.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export 'src/resources/resources.dart';
2+
export 'src/context/common.dart';

lib/src/api/api.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ export 'bucket.dart';
22
export 'keyvalue.dart';
33
export 'secret.dart';
44
export 'topic.dart';
5+
export 'proto.dart';
6+
export 'queue.dart';

0 commit comments

Comments
 (0)