
# An Edge Function in Dart

An Edge Function does not have to be TypeScript. [`spring_boot_starter_edge`](../spring-boot-starter-edge/index.md) compiles a Spring for Dart application to JavaScript and writes the `index.ts` Supabase's Edge Runtime needs; with this starter in the application, the function reaches the project as the caller. See [Supabase Edge Functions](../spring-boot-starter-edge/supabase.md) for the settings.

[`example/supabase/function`](https://github.com/farum-azula/spring_core_dart/tree/main/example/supabase/function) is such a function, named `api`. Its `main` is:

```dart
@SpringBootApplication()
void main() => runFetchApplication(() => springApplication);
```

```bash
dart run build_runner build
dart run spring_boot_starter_edge:build \
  --platform supabase --name api \
  --output ../supabase/functions/api
```

## What it shows

- **Todos through the REST path, as the caller.** A `Todos` repository (`ListCrudRepository<Todo, int>`) with `findByListIdOrderById` and `@Procedure('close_all')`. The token of the request goes on to the project, so the project's row-level security decides what the caller sees. A function has no sockets: only the REST path, no SQL.
- **Roles.** `/health` is open to everyone; `/me` answers the id and the roles the resource server read from the project's token; `/admin` is `@PreAuthorize("hasRole('ADMIN')")`. The bearer tokens are the project's access tokens, with the Supabase starter's defaults.
- **A rate limit of 10 a minute for each user.** Adding a todo has `@RateLimiter('10/1m', key: '#{authentication.name}', name: 'add-todo')`. The counters are in the project's database, through the database function `rate_limit_acquire` called as the service role, so every instance of the function shares the count. With `spring.web.rate-limit.enabled: true` this is the default store of the Supabase starter.

```dart
@PostMapping()
@ResponseStatus(HttpStatus.created)
@RateLimiter('10/1m', key: '#{authentication.name}', name: 'add-todo')
Future<Todo> add(@RequestBody() @Valid() NewTodo todo) => todos.save(
  Todo(
    title: todo.title,
    listId: todo.listId,
    createdAt: DateTime.now().toUtc(),
  ),
);
```

- **What the app and the server call.** `GET /stats` answers who called (the user and the role the resource server read from the checked token, and whether the `apikey` header arrived); `from=fail` answers 422. `POST /send-invite` takes `{email}` and answers `{id}`, made of the caller's id and the address. The app and the server call them through `@EdgeFunction('api/stats')` and `@EdgeFunction('api/send-invite')` of their `@EdgeFunctions` interfaces. The app also runs in a browser, on another origin than the project, so the controller has `@CrossOrigin`, which answers the pre-flight.

The example project has no function written in TypeScript: these two are endpoints of this one.

The function is `[functions.api]` with `verify_jwt = false` in the example's `config.toml`, and `SPRING_SUPABASE_AUTH_ISSUER` under `[edge_runtime.secrets]` for the local stack.

## See also

- [spring_boot_starter_edge](../spring-boot-starter-edge/index.md)
- [What a function does differently](../spring-boot-starter-edge/differences.md#the-rate-limiter)
- [Who a call is made as](credentials.md)
