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

Commit d9c778e

Browse files
authored
docs: Add new rule for runtime access. (#597)
1 parent ca87f14 commit d9c778e

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

src/pages/getting-started/resources-overview.mdx

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,138 @@ void main() {
108108

109109
<Note>Always declare resources outside of runtime/callback code</Note>
110110

111+
### Don't use runtime methods in top level code
112+
113+
Calling runtime methods in top-level code can lead to unintended side effects. Nitric resources should be accessed in a deterministic way to avoid unintentional side-effects.
114+
115+
A working example:
116+
117+
<CodeGroup>
118+
119+
```typescript
120+
import { api, bucket } from '@nitric/sdk'
121+
122+
const files = bucket('files').allow('read')
123+
124+
// ❌ This access will not work.
125+
const fileContents = files.file('example.txt').read()
126+
127+
api('public').get('/files/:name', (ctx) => {
128+
// ✅ This access will work.
129+
const fileContents = files.file('example.txt').read()
130+
})
131+
```
132+
133+
```python
134+
from nitric.resources import api, bucket
135+
from nitric.application import Nitric
136+
137+
public_api = api('public')
138+
139+
files = bucket('files').allow('read')
140+
141+
# ❌ This access will not work.
142+
file_contents = files.file('example.txt').read()
143+
144+
@public_api.get("/files/:name")
145+
async def get_file(ctx):
146+
# ✅ This access will work.
147+
file_contents = files.file('example.txt').read()
148+
149+
Nitric.run()
150+
```
151+
152+
```go
153+
package main
154+
155+
import (
156+
"fmt"
157+
158+
"github.com/nitrictech/go-sdk/handler"
159+
"github.com/nitrictech/go-sdk/nitric"
160+
)
161+
162+
func main() {
163+
api, err := nitric.NewApi("public")
164+
if err != nil {
165+
fmt.Println(err)
166+
return
167+
}
168+
169+
170+
bucket, err := nitric.NewBucket("files").Allow(nitric.BucketRead)
171+
172+
// ❌ This access will not work
173+
fileContents, _ := bucket.File("example.txt").Read()
174+
175+
api.Get("/files/:name", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
176+
// ✅ This access will work
177+
fileContents, _ := bucket.File("example.txt").Read()
178+
179+
return next(ctx)
180+
})
181+
182+
if err := nitric.Run(); err != nil {
183+
fmt.Println(err)
184+
}
185+
186+
fmt.Println("Service has started")
187+
}
188+
```
189+
190+
```dart
191+
import 'package:nitric_sdk/nitric.dart';
192+
193+
void main() {
194+
final helloApi = Nitric.api("public");
195+
196+
final files = Nitric.bucket("files").allow([BucketPermission.read]);
197+
198+
// ❌ This access will not work
199+
final fileContents = files.file("example.txt").read();
200+
201+
helloApi.get("/files/:name", (ctx) async {
202+
// ✅ This access will work
203+
final fileContents = await files.file("example.txt").read();
204+
205+
return ctx;
206+
});
207+
}
208+
```
209+
210+
</CodeGroup>
211+
212+
<Note>
213+
If you want to limit resource access to a single instance/access consider
214+
implementing a lazy singleton.
215+
</Note>
216+
217+
<CodeGroup>
218+
219+
```typescript
220+
import { api, bucket } from '@nitric/sdk'
221+
222+
const files = bucket('files').allow('read')
223+
224+
// 👀 Singleton value
225+
let fileContents: string
226+
227+
// Lazy access method
228+
const getFileContents = async () => {
229+
if (!fileContents) {
230+
fileContents = await files.file('example.txt').read()
231+
}
232+
233+
return fileContents
234+
}
235+
236+
api('public').get('/files/:name', (ctx) => {
237+
const fileContents = await getFileContents()
238+
})
239+
```
240+
241+
</CodeGroup>
242+
111243
## Best Practices
112244

113245
### ✅ Re-use declarations for shared resources

0 commit comments

Comments
 (0)