An Edge Function in Dart
An Edge Function does not have to be TypeScript. spring_boot_starter_edge 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 for the settings.
example/supabase/function is such a function, named api. Its main is:
@SpringBootApplication()
void main() => runFetchApplication(() => springApplication);
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
Todosrepository (ListCrudRepository<Todo, int>) withfindByListIdOrderByIdand@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.
/healthis open to everyone;/meanswers the id and the roles the resource server read from the project's token;/adminis@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 functionrate_limit_acquirecalled as the service role, so every instance of the function shares the count. Withspring.web.rate-limit.enabled: truethis is the default store of the Supabase starter.
@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 /statsanswers who called (the user and the role the resource server read from the checked token, and whether theapikeyheader arrived);from=failanswers 422.POST /send-invitetakes{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@EdgeFunctionsinterfaces. 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.