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

Commit 9d7fc77

Browse files
committed
code tab updates wip
1 parent 95c5c10 commit 9d7fc77

File tree

23 files changed

+471
-121
lines changed

23 files changed

+471
-121
lines changed

docs/apis.mdx

Lines changed: 177 additions & 15 deletions
Large diffs are not rendered by default.

docs/batch.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ batch-services:
6868

6969
Within a Batch we create Job Definitions, by creating a new Job with a unique name and defining a handler function that will be executed when the job is submitted.
7070

71-
<CodeSwitcher>
71+
<CodeSwitcher tabs>
7272

7373
```typescript !!
7474
import { job, JobContext } from '@nitric/sdk'
@@ -138,7 +138,7 @@ void main() {
138138

139139
Jobs may be submitted from Nitric `services` or other `batches` using the `submit` method on the job reference. When submitting a job you can provide a payload that will be passed to the job handler function.
140140

141-
<CodeSwitcher>
141+
<CodeSwitcher tabs>
142142

143143
```javascript !!
144144
import * as nitric from '@nitric/sdk'

docs/further-reading/how-devs-use-nitric.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ An API named 'milkyway' with:
4040
- A storage bucket named planets_images.
4141
- This service intends to read and write content to the bucket.
4242

43-
<CodeSwitcher>
43+
<CodeSwitcher tabs>
4444

4545
```javascript !!
4646
import { api, kv, bucket } from '@nitric/sdk'

docs/get-started/foundations/infrastructure/resources.mdx

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,19 @@ The Nitric deployment engine does not evaluate runtime code at deployment time a
2020

2121
A working example:
2222

23-
<CodeSwitcher>
23+
<CodeSwitcher tabs>
24+
25+
```javascript !!
26+
import { api, bucket } from '@nitric/sdk'
27+
28+
// ✅ This declaration will work
29+
const files = bucket('files').allow('read')
30+
31+
api('public').get('/files/:name', (ctx) => {
32+
// ❌ This declaration will not work, as this is only called at runtime.
33+
const badBucket = bucket('wont-work').allow('read')
34+
})
35+
```
2436

2537
```typescript !!
2638
import { api, bucket } from '@nitric/sdk'
@@ -113,7 +125,21 @@ Calling runtime methods in top-level code can lead to unintended side effects. N
113125

114126
A working example:
115127

116-
<CodeSwitcher>
128+
<CodeSwitcher tabs>
129+
130+
```javascript !!
131+
import { api, bucket } from '@nitric/sdk'
132+
133+
const files = bucket('files').allow('read')
134+
135+
// ❌ This access will not work.
136+
const fileContents = files.file('example.txt').read()
137+
138+
api('public').get('/files/:name', (ctx) => {
139+
// ✅ This access will work.
140+
const fileContents = files.file('example.txt').read()
141+
})
142+
```
117143

118144
```typescript !!
119145
import { api, bucket } from '@nitric/sdk'
@@ -239,7 +265,32 @@ When many services share a resource it's helpful to re-use resource declaration
239265

240266
For example:
241267

242-
<CodeSwitcher>
268+
<CodeSwitcher tabs>
269+
270+
```javascript !!
271+
// lib/resources.ts
272+
import { api, topic } from '@nitric/sdk'
273+
274+
export const publicApi = api('public')
275+
276+
export const updateTopic = topic('updates')
277+
278+
// services/api.ts
279+
import { publicApi, updateTopic } from '../lib/resources'
280+
281+
const publisher = updateTopic.allow('publish')
282+
283+
publicApi.post('/update', async (ctx) => {
284+
await publisher.publish({ test: 'message' })
285+
})
286+
287+
// services/updates.ts
288+
import { updateTopic } from '../lib/resources'
289+
290+
updateTopic.subscribe((ctx) => {
291+
console.log('got the message')
292+
})
293+
```
243294

244295
```typescript !!
245296
// lib/resources.ts
@@ -411,7 +462,31 @@ void main() {
411462

412463
Creating resource permissions in the same context as the resources can make those permissions leak. This is demonstrated in the below example:
413464

414-
<CodeSwitcher>
465+
<CodeSwitcher tabs>
466+
467+
```javascript !!
468+
// lib/resources.ts
469+
import { api, bucket } from '@nitric/sdk'
470+
471+
export const publicApi = api('public')
472+
473+
export const bucketOne = bucket('bucket-one').allow('read')
474+
export const bucketTwo = bucket('bucket-two').allow('read')
475+
476+
// services/service-one.ts
477+
import { publicApi, bucketOne } from '../lib/resources'
478+
479+
publicApi.get('bucket-one/file/:name', (ctx) => {
480+
// do something with the file
481+
})
482+
483+
// services/service-two.ts
484+
import { publicApi, bucketTwo } from '../lib/resources'
485+
486+
publicApi.get('bucket-two/file/:name', (ctx) => {
487+
// do something with the file
488+
})
489+
```
415490

416491
```typescript !!
417492
// lib/resources.ts

docs/get-started/foundations/projects/index.mdx

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,30 @@ We recommend starting with a template, using the `nitric new` CLI command. These
2424

2525
Here are some examples of basic Nitric project structures:
2626

27-
<Tabs>
27+
<CodeTabs>
2828

29-
<TabItem label="Go">
29+
<TabItem label="JavaScript">
3030

31-
```text Go
31+
```text JavaScript
3232
my-project/
3333
├── nitric.yaml
3434
├── nitric.aws.yaml
3535
└── services/
36-
├── service1/
37-
│ └── main.go
38-
└── service2/
39-
└── main.go
36+
├── service1.js
37+
└── service2.js
4038
```
4139

4240
</TabItem>
4341

44-
<TabItem label="JavaScript">
42+
<TabItem label="TypeScript">
4543

4644
```text JavaScript
4745
my-project/
4846
├── nitric.yaml
4947
├── nitric.aws.yaml
5048
└── services/
51-
├── service1.js
52-
└── service2.js
49+
├── service1.ts
50+
└── service2.ts
5351
```
5452

5553
</TabItem>
@@ -67,7 +65,35 @@ my-project/
6765

6866
</TabItem>
6967

70-
</Tabs>
68+
<TabItem label="Go">
69+
70+
```text Go
71+
my-project/
72+
├── nitric.yaml
73+
├── nitric.aws.yaml
74+
└── services/
75+
├── service1/
76+
│ └── main.go
77+
└── service2/
78+
└── main.go
79+
```
80+
81+
</TabItem>
82+
83+
<TabItem label="Dart">
84+
85+
```text
86+
my-project/
87+
├── nitric.yaml
88+
├── nitric.aws.yaml
89+
└── services/
90+
├── service1.dart
91+
└── service2.dart
92+
```
93+
94+
</TabItem>
95+
96+
</CodeTabs>
7197

7298
This flexibility allows you to build apps as a single monolithic service, multiple microservices, or something else entirely.
7399

docs/get-started/quickstart.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ Nitric's language templates enable hot-reloading by default, so at this point, y
276276

277277
Start by opening the `hello` service in your editor and adding a new route to the API, then save, and execute the file:
278278

279-
<CodeSwitcher>
279+
<CodeSwitcher tabs>
280280

281281
```typescript !! title:services/hello.ts
282282
import { api } from '@nitric/sdk'

docs/guides/nodejs/amazon-cognito.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ nitric new
3232

3333
In this new Nitric project there will be an example API with a single `GET: /hello/:name` route. We'll start by securing this route.
3434

35-
<CodeSwitcher>
35+
<CodeSwitcher tabs>
3636

3737
```typescript !! title:services/hello.ts
3838
import { api } from '@nitric/sdk'
@@ -66,7 +66,7 @@ helloApi.get('/hello/:name', async (ctx) => {
6666

6767
To add this API Gateway authentication we need to create a `security definition` and then apply that definition to specific routes or the entire API. Here we'll update the `main` API by adding a new security definition named `cognito`.
6868

69-
<CodeSwitcher>
69+
<CodeSwitcher tabs>
7070

7171
```typescript !! title:services/hello.ts
7272
import { api } from '@nitric/sdk'
@@ -123,7 +123,7 @@ Next, we need to ensure this security definition is applied, otherwise it won't
123123

124124
In this example we'll apply the security at the API level, ensuring all routes require authentication.
125125

126-
<CodeSwitcher>
126+
<CodeSwitcher tabs>
127127

128128
```typescript !! title:services/hello.ts
129129
import { api } from '@nitric/sdk'
@@ -197,7 +197,7 @@ In the example below we're simply checking whether the user is a member of a par
197197
handlers or middleware.
198198
</Note>
199199

200-
<CodeSwitcher>
200+
<CodeSwitcher tabs>
201201

202202
```typescript !! title:services/hello.ts
203203
import { api, HttpContext, HttpMiddleware } from '@nitric/sdk'

docs/index.mdx

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@ It's what's missing between applications and infrastructure automation.
1616

1717
Oh, and it supports basically any language, like JavaScript, TypeScript, Python, Go, you name it.
1818

19-
<div className="flex items-center flex-col gap-4 mt-10">
20-
21-
<div className="not-prose">
22-
<LanguageSwitch />
23-
</div>
24-
2519
<div className="w-full">
2620

27-
<CodeSwitcher className="xl:max-h-[300px]">
21+
<CodeSwitcher className="xl:max-h-[300px]" tabs>
2822

2923
```javascript !! title:services/api.js
3024
import { api } from '@nitric/sdk'
@@ -103,8 +97,6 @@ void main() {
10397

10498
</div>
10599

106-
</div>
107-
108100
If you're familiar Nitric already, you might want to jump to the [Installation](/getting-started/installation), [Guides](/guides) or [Resources](/apis) sections. Otherwise, keep reading to learn more about Nitric.
109101

110102
---
@@ -128,14 +120,20 @@ You might have one service that handles everything, or a service for each route.
128120
129121
Resources are the building blocks of your apps. They're the databases, queues, buckets, etc. that your services interact with. To request a resource, import the resource type and create one with a name.
130122
131-
<CodeSwitcher>
123+
<CodeSwitcher tabs>
132124
133125
```javascript !! title:services/example.js
134126
import { bucket } from '@nitric/sdk'
135127

136128
const profiles = bucket('profiles').allow('read', 'write', 'delete')
137129
```
138130

131+
```typescript !! title:services/example.ts
132+
import { bucket } from '@nitric/sdk'
133+
134+
const profiles = bucket('profiles').allow('read', 'write', 'delete')
135+
```
136+
139137
```python !! title:services/example.py
140138
from nitric.resources import bucket
141139
from nitric.application import Nitric
@@ -262,7 +260,7 @@ Nitric uses this to find your services, then it turns each service into a contai
262260

263261
So, a project structure might look something like this:
264262

265-
<Tabs>
263+
<CodeTabs>
266264

267265
<TabItem label="JavaScript">
268266

@@ -277,6 +275,19 @@ example/
277275

278276
</TabItem>
279277

278+
<TabItem label="TypeScript">
279+
280+
```bash
281+
example/
282+
├── nitric.yaml
283+
├── services/
284+
│ └── orders.ts
285+
│ └── users.ts
286+
└── package.json
287+
```
288+
289+
</TabItem>
290+
280291
<TabItem label="Python">
281292

282293
```bash
@@ -319,7 +330,7 @@ example/
319330

320331
</TabItem>
321332

322-
</Tabs>
333+
</CodeTabs>
323334

324335
---
325336

@@ -443,7 +454,7 @@ Here are some ways you can extend or escape Nitric.
443454

444455
If you need to access a service in a way Nitric doesn't support, you can always use the provider's SDK directly. Code like this will always work:
445456

446-
<CodeSwitcher>
457+
<CodeSwitcher tabs>
447458

448459
```javascript !!
449460
// Import the AWS SDK
@@ -454,6 +465,15 @@ const s3 = new S3Client({ region: 'us-east-1' })
454465
const { Contents } = await s3.listObjectsV2({ Bucket: 'my-bucket' })
455466
```
456467

468+
```typescript !!
469+
// Import the AWS SDK
470+
import { S3Client } from '@aws-sdk/client-s3'
471+
472+
// Use it like you normally would
473+
const s3 = new S3Client({ region: 'us-east-1' })
474+
const { Contents } = await s3.listObjectsV2({ Bucket: 'my-bucket' })
475+
```
476+
457477
```python !!
458478
# Import the boto3 (AWS) SDK
459479
from boto3 import client

0 commit comments

Comments
 (0)