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

Commit 9a62ad9

Browse files
committed
fix api page issues and missing examples
1 parent 2debf8d commit 9a62ad9

File tree

2 files changed

+284
-7
lines changed

2 files changed

+284
-7
lines changed

docs/apis.mdx

Lines changed: 255 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,30 @@ myApi.get("/hello", (ctx) async {
8383

8484
Each API in your application needs a unique name. This name is used to identify the API across your project and in cloud deployments.
8585

86-
```javascript
86+
<CodeSwitcher tabs>
87+
88+
```javascript !!
89+
const myApi = api('my-api-name')
90+
```
91+
92+
```typescript !!
8793
const myApi = api('my-api-name')
8894
```
8995

96+
```python !!
97+
my_api = api('my-api-name')
98+
```
99+
100+
```go !!
101+
myApi := nitric.NewApi("my-api-name")
102+
```
103+
104+
```dart !!
105+
final myApi = Nitric.api("my-api-name");
106+
```
107+
108+
</CodeSwitcher>
109+
90110
### Request Context
91111

92112
Every handler receives a context object (`ctx`) that contains:
@@ -417,6 +437,10 @@ final secureApi = Nitric.api(
417437

418438
You can configure custom domains for your APIs in your stack configuration:
419439

440+
<Tabs>
441+
442+
<TabItem label="AWS">
443+
420444
```yaml title:nitric.prod.yaml
421445
provider: nitric/[email protected]
422446
region: us-east-1
@@ -427,10 +451,30 @@ apis:
427451
- api.example.com
428452
```
429453
454+
</TabItem>
455+
456+
<TabItem label="Azure">
457+
458+
```yaml title:nitric.prod.yaml
459+
# Currently unsupported - request support here: https://github.com/nitrictech/nitric/issues
460+
```
461+
462+
</TabItem>
463+
464+
<TabItem label="GCP">
465+
466+
```yaml title:nitric.prod.yaml
467+
# Currently unsupported - request support here: https://github.com/nitrictech/nitric/issues
468+
```
469+
470+
</TabItem>
471+
472+
</Tabs>
473+
430474
<Note>
431475
Custom domains are currently only supported for AWS deployments. See the [AWS
432-
Custom Domain Prerequisites](#aws-custom-domain-prerequisites) section for
433-
setup details.
476+
Custom Domain Setup](/providers/mappings/aws/apis#custom-domain-prerequisites)
477+
section for setup details.
434478
</Note>
435479

436480
## Project Organization
@@ -439,7 +483,19 @@ apis:
439483

440484
You can split your API routes across multiple services while using the same API resource:
441485

442-
```javascript title:services/users.js
486+
<CodeSwitcher tabs>
487+
488+
```javascript !! title:services/users.js
489+
import { api } from '@nitric/sdk'
490+
491+
const myApi = api('my-api')
492+
493+
myApi.get('/users', async (ctx) => {
494+
// Handle user listing
495+
})
496+
```
497+
498+
```typescript !! title:services/users.ts
443499
import { api } from '@nitric/sdk'
444500

445501
const myApi = api('my-api')
@@ -449,7 +505,65 @@ myApi.get('/users', async (ctx) => {
449505
})
450506
```
451507

452-
```javascript title:services/products.js
508+
```python !! title:services/users.py
509+
from nitric.resources import api
510+
from nitric.application import Nitric
511+
512+
my_api = api('my-api')
513+
514+
@my_api.get("/users")
515+
async def list_users(ctx):
516+
// Handle user listing
517+
pass
518+
519+
Nitric.run()
520+
```
521+
522+
```go !! title:services/users/main.go
523+
package main
524+
525+
import (
526+
"github.com/nitrictech/go-sdk/nitric"
527+
"github.com/nitrictech/go-sdk/nitric/apis"
528+
)
529+
530+
func main() {
531+
myApi := nitric.NewApi("my-api")
532+
533+
myApi.Get("/users", func(ctx *apis.Ctx) {
534+
// Handle user listing
535+
})
536+
537+
nitric.Run()
538+
}
539+
```
540+
541+
```dart !! title:services/users.dart
542+
import 'package:nitric_sdk/nitric.dart';
543+
544+
final myApi = Nitric.api("my-api");
545+
546+
myApi.get("/users", (ctx) async {
547+
// Handle user listing
548+
return ctx;
549+
});
550+
```
551+
552+
</CodeSwitcher>
553+
554+
<CodeSwitcher tabs>
555+
556+
```javascript !! title:services/products.js
557+
import { api } from '@nitric/sdk'
558+
559+
const myApi = api('my-api')
560+
561+
myApi.get('/products', async (ctx) => {
562+
// Handle product listing
563+
})
564+
```
565+
566+
```typescript !! title:services/products.ts
453567
import { api } from '@nitric/sdk'
454568

455569
const myApi = api('my-api')
@@ -459,26 +573,160 @@ myApi.get('/products', async (ctx) => {
459573
})
460574
```
461575

576+
```python !! title:services/products.py
577+
from nitric.resources import api
578+
from nitric.application import Nitric
579+
580+
my_api = api('my-api')
581+
582+
@my_api.get("/products")
583+
async def list_products(ctx):
584+
// Handle product listing
585+
pass
586+
587+
Nitric.run()
588+
```
589+
590+
```go !! title:services/products/main.go
591+
package main
592+
593+
import (
594+
"github.com/nitrictech/go-sdk/nitric"
595+
"github.com/nitrictech/go-sdk/nitric/apis"
596+
)
597+
598+
func main() {
599+
myApi := nitric.NewApi("my-api")
600+
601+
myApi.Get("/products", func(ctx *apis.Ctx) {
602+
// Handle product listing
603+
})
604+
605+
nitric.Run()
606+
}
607+
```
608+
609+
```dart !! title:services/products.dart
610+
import 'package:nitric_sdk/nitric.dart';
611+
612+
final myApi = Nitric.api("my-api");
613+
614+
myApi.get("/products", (ctx) async {
615+
// Handle product listing
616+
return ctx;
617+
});
618+
```
619+
620+
</CodeSwitcher>
621+
462622
### Shared Resources
463623

464624
For better organization, you can define shared API resources:
465625

466-
```javascript title:resources/api.js
626+
<CodeSwitcher tabs>
627+
628+
```javascript !! title:resources/api.js
629+
import { api } from '@nitric/sdk'
630+
631+
export const myApi = api('my-api')
632+
```
633+
634+
```typescript !! title:resources/api.ts
467635
import { api } from '@nitric/sdk'
468636

469637
export const myApi = api('my-api')
470638
```
471639

640+
```python !! title:resources/api.py
641+
from nitric.resources import api
642+
643+
my_api = api('my-api')
644+
```
645+
646+
```go !! title:resources/api/main.go
647+
package api
648+
649+
import (
650+
"github.com/nitrictech/go-sdk/nitric"
651+
"github.com/nitrictech/go-sdk/nitric/apis"
652+
)
653+
654+
var MyApi apis.Api
655+
656+
func init() {
657+
MyApi = nitric.NewApi("my-api")
658+
}
659+
```
660+
661+
```dart !! title:resources/api.dart
662+
import 'package:nitric_sdk/nitric.dart';
663+
664+
final myApi = Nitric.api("my-api");
665+
```
666+
667+
</CodeSwitcher>
668+
472669
Then import and use them in your services:
473670

474-
```javascript title:services/users.js
671+
<CodeSwitcher tabs>
672+
673+
```javascript !! title:services/users.js
674+
import { myApi } from '../resources/api'
675+
676+
myApi.get('/users', async (ctx) => {
677+
// Handle user listing
678+
})
679+
```
680+
681+
```typescript !! title:services/users.ts
475682
import { myApi } from '../resources/api'
476683

477684
myApi.get('/users', async (ctx) => {
478685
// Handle user listing
479686
})
480687
```
481688

689+
```python !! title:services/users.py
690+
from nitric.application import Nitric
691+
from resources.api import my_api
692+
693+
@my_api.get("/users")
694+
async def list_users(ctx):
695+
// Handle user listing
696+
pass
697+
698+
Nitric.run()
699+
```
700+
701+
```go !! title:services/users/main.go
702+
package main
703+
704+
import (
705+
"your-project/resources/api"
706+
"github.com/nitrictech/go-sdk/nitric"
707+
"github.com/nitrictech/go-sdk/nitric/apis"
708+
)
709+
710+
func main() {
711+
api.MyApi.Get("/users", func(ctx *apis.Ctx) {
712+
// Handle user listing
713+
})
714+
715+
nitric.Run()
716+
}
717+
```
718+
719+
```dart !! title:services/users.dart
720+
import '../resources/api.dart';
721+
722+
myApi.get("/users", (ctx) async {
723+
// Handle user listing
724+
return ctx;
725+
});
726+
```
727+
728+
</CodeSwitcher>
729+
482730
## Cloud Provider Support
483731

484732
Each cloud provider has specific features and limitations for API deployments:

docs/providers/mappings/aws/apis.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,32 @@ During deployment the Nitric CLI builds your API's routes, methods and handlers:
2828
- Lambda ARNs are injected into the API definition using the [x-amazon-apigateway-integration object](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-integration.html), creating an [API Gateway Integration](https://docs.aws.amazon.com/apigateway/api-reference/resource/integration/) for each.
2929
- The API definition is deployed as an API Gateway v2 HTTP API, using the `$default` stage name
3030
- IAM policies are created enabling API Gateway to execute the Lambdas
31+
32+
### Custom Domain Prerequisites
33+
34+
To support custom domains with APIs deployed to AWS your domain (or subdomain) will need to be setup as a [hosted zone](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/hosted-zones-working-with.html) in Route 53.
35+
36+
The general steps to setup a hosted zone in Route 53 are as follows:
37+
38+
1. Navigate to Route 53 in the AWS Console
39+
2. Select 'hosted zones' from the left navigation
40+
3. Click 'Create hosted zone'
41+
4. Enter your domain name and choose the 'Public hosted zone' type.
42+
5. Click 'Create hosted zone'
43+
6. You will now be provided with a set of NS DNS records to configure in the DNS provider for your domain
44+
7. Create the required DNS records, then wait for the DNS changes to propagate
45+
46+
Once this is done you will be able to use the hosted zone domain or any direct subdomain with your Nitric APIs.
47+
48+
You can read more about how AWS suggests configuring hosted zones in their documentation on [Making Route 53 the DNS service for a domain that's in use](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/migrate-dns-domain-in-use.html) or [Making Route 53 the DNS service for an inactive domain](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/migrate-dns-domain-inactive.html).
49+
50+
<Note>
51+
If the hosted zone was nitric.io, nitric.io or api.nitric.io would be
52+
supported for APIs, but not public.api.nitric.io since that is a subdomain of
53+
a subdomain.
54+
</Note>
55+
56+
<Note>
57+
DNS propagation of the NS records can take a few seconds to a few hours due to
58+
the nature of DNS.
59+
</Note>

0 commit comments

Comments
 (0)