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

Commit 4fe659f

Browse files
change survey application to use infrastructure best practice
1 parent a262998 commit 4fe659f

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

docs/guides/nodejs/survey-application.mdx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ cd surveys-backend
4141
npm install
4242
```
4343

44-
You can now delete all files in the services/ folder, we'll create new services in this guide.
44+
You can now delete all files in the `services/` folder, we'll create new services in this guide.
4545

4646
## Add Runtime Type Safety
4747

@@ -91,9 +91,8 @@ Create and configure the cloud resources your backend will use, such as storage
9191
```ts title:resources/resources.ts
9292
import { bucket, kv, topic } from '@nitric/sdk'
9393

94-
export const output = bucket('receipts').allow('read', 'write')
95-
export const submissions = kv('submissions').allow('set', 'get')
96-
export const submitted = topic('form-submitted').allow('publish')
94+
export const output = bucket('receipts')
95+
export const submissions = kv('submissions')
9796
export const receipts = topic('form-submitted')
9897
```
9998

@@ -103,10 +102,12 @@ Create an API route that validates and stores form submissions, then triggers fu
103102

104103
```ts title:services/forms.ts
105104
import { api } from '@nitric/sdk'
106-
import { submissions, submitted } from '../resources/resources'
105+
import { submissions, receipts } from '../resources/resources'
107106
import { formSchemas } from '../form/schema'
108107

109108
const formApi = api('forms')
109+
const formSubmissions = submissions.allow('set')
110+
const submittableReceipts = receipts.allow('publish')
110111

111112
formApi.post('/forms/:formId', async (ctx) => {
112113
const formId = ctx.req.params.formId
@@ -129,8 +130,8 @@ formApi.post('/forms/:formId', async (ctx) => {
129130
const data = parsed.data
130131
const id = `${formId}-${Date.now()}`
131132

132-
await submissions.set(id, data)
133-
await submitted.publish({ id, formId })
133+
await formSubmissions.set(id, data)
134+
await submittableReceipts.publish({ id, formId })
134135

135136
ctx.res.json({ msg: 'Submission received', id })
136137
})
@@ -212,12 +213,15 @@ Guests: ${guests}`,
212213
Listen for submitted events and generate a formatted PDF receipt from the stored data.
213214

214215
```ts title:services/pdfs.ts
215-
import { receipts, submissions, output } from '../resources/resources'
216+
import { submissions, receipts, output } from '../resources/resources'
216217
import { buildReceipt } from '../form/receipt'
217218

219+
const formSubmissions = submissions.allow('get')
220+
const writeableOutput = output.allow('write')
221+
218222
receipts.subscribe(async (ctx) => {
219223
const { id, formId } = ctx.req.json()
220-
const submission = await submissions.get(id)
224+
const submission = await formSubmissions.get(id)
221225

222226
if (!submission) {
223227
console.error(`No submission found for ID: ${id}`)
@@ -228,7 +232,7 @@ receipts.subscribe(async (ctx) => {
228232
const buffer = await buildReceipt(submission, formId)
229233

230234
// Store the PDF file in the bucket
231-
const file = output.file(`${id}.pdf`)
235+
const file = writeableOutput.file(`${id}.pdf`)
232236
await file.write(buffer)
233237

234238
console.log(`Receipt stored for ${id}`)
@@ -261,10 +265,11 @@ import { api } from '@nitric/sdk'
261265
import { output } from '../resources/resources'
262266

263267
const receiptApi = api('receipts')
268+
const readableOutput = output.allow('read')
264269

265270
receiptApi.get('/receipts/:id', async (ctx) => {
266271
const id = ctx.req.params.id
267-
const file = output.file(`${id}.pdf`)
272+
const file = readableOutput.file(`${id}.pdf`)
268273
const url = await file.getDownloadUrl()
269274
ctx.res.body = url
270275
})

docs/guides/nodejs/survey-website.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ updated_at: 2025-05-08
1414

1515
# Build a Frontend for Your Nitric Survey App with React
1616

17-
In this guide, youll build a simple web application using React that connects to the survey backend we created with Nitric.
17+
In this guide, you'll build a simple web application using React that connects to the survey backend we created with Nitric.
1818

1919
## What You'll Build
2020

@@ -42,7 +42,9 @@ cd ..
4242
Next, configure your `nitric.yaml` to enable and configure the website:
4343

4444
<Note>
45-
Along with the website configuration, you will need to add the `preview` key to your `nitric.yaml`, as website support is currently a preview feature and subject to change.
45+
Along with the website configuration, you will need to add the `preview` key
46+
to your `nitric.yaml`, as website support is currently a preview feature and
47+
subject to change.
4648
</Note>
4749

4850
```yaml title:nitric.yaml
@@ -149,7 +151,7 @@ const SurveyForm = () => {
149151
</label>
150152

151153
<label>
152-
Rating (15):
154+
Rating (1-5):
153155
<input
154156
type="number"
155157
name="rating"
@@ -241,6 +243,7 @@ region: us-east-2
241243
```
242244
243245
## Deploy
246+
244247
Deploy to AWS using the `nitric up` command. Ensure you have set up your [AWS credentials](/providers/pulumi/aws#usage) correctly.
245248

246249
```bash

0 commit comments

Comments
 (0)