Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 8d13383

Browse files
amend dart guide with template changes
1 parent 53bfb74 commit 8d13383

File tree

3 files changed

+63
-77
lines changed

3 files changed

+63
-77
lines changed

docs/guides/dart/serverless-rest-api-example.mdx

Lines changed: 37 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tags:
77
languages:
88
- dart
99
published_at: 2024-04-24
10+
updated_at: 2024-12-24
1011
---
1112

1213
# Building a REST API with Nitric
@@ -40,18 +41,10 @@ There is also an extended section of the guide that adds file operations using a
4041

4142
## Getting started
4243

43-
Start by creating a base Dart
44+
Start by creating a new Nitric project from the dart template.
4445

4546
```bash
46-
dart create -t console my-profile-api
47-
```
48-
49-
Add the Nitric SDK and the uuid dependency by adding it to your `pubspec.yaml`.
50-
51-
```yaml
52-
dependencies:
53-
nitric_sdk: ^1.2.0
54-
uuid: ^4.3.3
47+
nitric new my-profile-api dart-starter
5548
```
5649

5750
Next, open the project in your editor of choice.
@@ -63,62 +56,29 @@ cd my-profile-api
6356
The scaffolded project should have the following structure:
6457

6558
```text
66-
bin/
67-
├── my_profile_api.dart
68-
lib/
69-
├── my_profile_api.dart
70-
test/
71-
├── my_profile_api_test.dart
59+
services/
60+
├── api.dart
7261
.gitignore
7362
analysis_options.yaml
74-
CHANGELOG.md
75-
pubspec.lock
63+
dart.dockerfile
64+
dart.dockerfile.dockerignore
65+
nitric.yaml
7666
pubspec.yaml
7767
README.md
7868
```
7969

80-
To create our Nitric project, we have to create a `nitric.yaml` file. The handlers key will point to where our
70+
As we will be generating IDs for each profile, add the uuid dependency by adding it to your `pubspec.yaml`.
8171

8272
```yaml
83-
name: my_profile_api
84-
services:
85-
- match: bin/my_profile_api.dart
86-
start: dart run $SERVICE_PATH
87-
```
88-
89-
## Create a Profile class
90-
91-
We will create a class to represent the profiles that we will store in the key value store. We will add `toJson` and `fromJson` functions to assist.
92-
93-
```dart
94-
class Profile {
95-
String name;
96-
int age;
97-
String homeTown;
98-
99-
Profile(this.name, this.age, this.homeTown);
100-
101-
Profile.fromJson(Map<String, dynamic> contents)
102-
: name = contents["name"] as String,
103-
age = contents["age"] as int,
104-
homeTown = contents["homeTown"] as String;
105-
106-
Map<String, dynamic> toJson() => {
107-
'name': name,
108-
'age': age,
109-
'homeTown': homeTown,
110-
};
111-
}
112-
73+
dart pub add uuid
11374
```
11475

11576
## Building the API
11677

117-
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`
78+
Applications built with Nitric can contain many APIs, let's start by adding one to this project to serve as the public endpoint.
11879

11980
```dart
12081
import 'package:nitric_sdk/nitric.dart';
121-
import 'package:nitric_sdk/resources.dart';
12282
12383
import 'package:uuid/uuid.dart';
12484
@@ -128,22 +88,22 @@ void main() {
12888
12989
// Define a key value store named 'profiles', then request get, set and delete permissions.
13090
final profiles = Nitric.kv("profiles").allow([
131-
KeyValuePermission.get,
132-
KeyValuePermission.set,
133-
KeyValuePermission.delete
91+
KeyValueStorePermission.get,
92+
KeyValueStorePermission.set,
93+
KeyValueStorePermission.delete
13494
]);
13595
}
13696
```
13797

138-
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.
98+
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 service to access the key value store.
13999

140100
<Note>
141101
Resources in Nitric like `api` and `key value store` represent high-level
142102
cloud resources. When your app is deployed Nitric automatically converts these
143103
requests into appropriate resources for the specific
144104
[provider](https://nitric.io/docs/reference/providers). Nitric also takes care
145105
of adding the IAM roles, policies, etc. that grant the requested access. For
146-
example the `key value stores` resource uses DynamoDB in AWS or FireStore on
106+
example the `key value store` resource uses DynamoDB in AWS or FireStore on
147107
Google Cloud.
148108
</Note>
149109

@@ -162,7 +122,7 @@ profileApi.post("/profiles", (ctx) async {
162122
163123
final id = uuid.v4();
164124
165-
final profile = Profile.fromJson(ctx.req.json());
125+
final profile = ctx.req.json();
166126
167127
// Store the new profile in the profiles kv store
168128
await profiles.set(id, profile);
@@ -183,7 +143,7 @@ profileApi.get("/profiles/:id", (ctx) async {
183143
try {
184144
// Retrieve and return the profile data
185145
final profile = await profiles.get(id);
186-
ctx.res.json(profile.toJson());
146+
ctx.res.json(profile);
187147
} on Exception catch (e) {
188148
print(e);
189149
ctx.res.status = 404;
@@ -221,7 +181,7 @@ Now that you have an API defined with handlers for each of its methods, it's tim
221181
nitric start
222182
```
223183

224-
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.
184+
Once it starts, the application will receive requests via the API port. You can use the [Local Dashboard](/docs/get-started/foundations/projects/local-development) 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.
225185

226186
## Test the API
227187

@@ -259,22 +219,29 @@ curl --location --request DELETE 'http://localhost:4001/profiles/[id]'
259219

260220
## Deploy to the cloud
261221

262-
At this point, you can deploy the application to any supported cloud provider. Start by setting up your credentials and any configuration for the cloud you prefer:
222+
At this point, you can deploy what you've built to any of the supported cloud providers. To do this start by setting up your credentials and any configuration for the cloud you prefer:
263223

264224
- [AWS](/providers/pulumi/aws)
265225
- [Azure](/providers/pulumi/azure)
266-
- [Google Cloud](/providers/pulumi/gcp)
226+
- [GCP](/providers/pulumi/gcp)
227+
228+
Next, we'll need to create a `stack`. A stack represents a deployed instance of an application, which is a key value store of resources defined in your project. You might want separate stacks for each environment, such as stacks for `dev`, `test` and `prod`. For now, let's start by creating a `dev` stack.
267229

268-
Next, we'll need to create a `stack`. Stacks represent deployed instances of an application, including the target provider and other details such as the deployment region. You'll usually define separate stacks for each environment such as development, testing and production. For now, let's start by creating a `dev` stack.
230+
The `stack new` command below will create a stack named `dev` that uses the `aws` provider.
269231

270232
```bash
271-
nitric stack new
233+
nitric stack new dev aws
272234
```
273235

274-
```
275-
? What should we name this stack? dev
276-
? Which provider do you want to deploy with? aws
277-
? Which region should the stack deploy to? us-east-1
236+
Continue by checking your stack file `nitric.dev.yaml` and adding in your preferred region, let's use `us-east-1`.
237+
238+
```yaml
239+
# The nitric provider to use
240+
provider: nitric/aws@latest
241+
# The target aws region to deploy to
242+
# See available regions:
243+
# https://docs.aws.amazon.com/general/latest/gr/lambda-service.html
244+
region: us-east-1
278245
```
279246
280247
### AWS
@@ -284,19 +251,15 @@ nitric stack new
284251
with free tier pricing you should consider the costs of the deployment.
285252
</Note>
286253
287-
In the previous step we called our stack `dev`, let's try deploying it with the `up` command.
254+
We called our stack `dev`, let's try deploying it with the `up` command
288255

289256
```bash
290257
nitric up
291-
┌───────────────────────────────────────────────────────────────┐
292-
| API | Endpoint |
293-
| main | https://XXXXXXXX.execute-api.us-east-1.amazonaws.com |
294-
└───────────────────────────────────────────────────────────────┘
295258
```
296259

297-
When the deployment is complete, go to the relevant cloud console and you'll be able to see and interact with your API. If you'd like to make changes to the API you can apply those changes by rerunning the `up` command. Nitric will automatically detect what's changed and just update the relevant cloud resources.
260+
When the deployment is complete, go to the relevant cloud console and you'll be able to see and interact with your API.
298261

299-
When you're done testing your application you can tear it down from the cloud, use the `down` command:
262+
To tear down your application from the cloud, use the `down` command:
300263

301264
```bash
302265
nitric down

docs/guides/go/serverless-rest-api-example.mdx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tags:
77
languages:
88
- go
99
published_at: 2023-08-11
10-
updated_at: 2024-10-03
10+
updated_at: 2024-12-24
1111
---
1212

1313
# Building your first API with Nitric
@@ -263,9 +263,21 @@ nitric stack new dev aws
263263

264264
Continue by checking your stack file `nitric.dev.yaml` and adding in your preferred region, let's use `us-east-1`.
265265

266+
```yaml
267+
# The nitric provider to use
268+
provider: nitric/aws@latest
269+
# The target aws region to deploy to
270+
# See available regions:
271+
# https://docs.aws.amazon.com/general/latest/gr/lambda-service.html
272+
region: us-east-1
273+
```
274+
266275
### AWS
267276
268-
Note: You are responsible for staying within the limits of the free tier or any costs associated with deployment.
277+
<Note>
278+
Cloud deployments incur costs and while most of these resource are available
279+
with free tier pricing you should consider the costs of the deployment.
280+
</Note>
269281
270282
We called our stack `dev`, let's try deploying it with the `up` command
271283

docs/guides/nodejs/serverless-rest-api-example.mdx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ languages:
88
- typescript
99
- javascript
1010
published_at: 2023-06-16
11-
updated_at: 2024-05-15
11+
updated_at: 2024-12-24
1212
---
1313

1414
# Building a REST API with Nitric
@@ -259,6 +259,17 @@ Next, we'll need to create a `stack`. Stacks represent deployed instances of an
259259
nitric stack new dev
260260
```
261261

262+
Continue by checking your stack file `nitric.dev.yaml` and adding in your preferred region, let's use `us-east-1`.
263+
264+
```yaml
265+
# The nitric provider to use
266+
provider: nitric/aws@latest
267+
# The target aws region to deploy to
268+
# See available regions:
269+
# https://docs.aws.amazon.com/general/latest/gr/lambda-service.html
270+
region: us-east-1
271+
```
272+
262273
### AWS
263274
264275
<Note>

0 commit comments

Comments
 (0)