Skip to content

Commit

Permalink
feat: Add dart toggle edge function (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirland authored Oct 23, 2023
1 parent 48c2b8b commit 779353d
Show file tree
Hide file tree
Showing 12 changed files with 669 additions and 0 deletions.
4 changes: 4 additions & 0 deletions backend/xmartchat_edge/.fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"flutterSdkVersion": "3.7.12",
"flavors": {}
}
3 changes: 3 additions & 0 deletions backend/xmartchat_edge/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.dart_tool
/functions/
.fvm/flutter_sdk
21 changes: 21 additions & 0 deletions backend/xmartchat_edge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Supabase Edge Functions Example

This template demonstrates how to run a [Supabase Edge Functions](https://supabase.com/docs/guides/functions) application via Dart Edge.

## Getting Started

Install the dependencies:

```bash
dart pub get
```

Start the application via Dart Edge CLU & the [`supabase` CLI](https://supabase.com/docs/guides/cli):

```bash
supabase init
fvm dart run edge build supabase_functions
supabase functions serve dart_edge
```

For more information, see the [Dart Edge documentation](https://docs.dartedge.dev).
24 changes: 24 additions & 0 deletions backend/xmartchat_edge/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
3 changes: 3 additions & 0 deletions backend/xmartchat_edge/edge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
supabase:
functions:
toggle_message: 'lib/toggle_message.dart'
32 changes: 32 additions & 0 deletions backend/xmartchat_edge/lib/toggle_message.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:convert';

import 'package:edge_http_client/edge_http_client.dart';
import 'package:supabase/supabase.dart';
import 'package:supabase_functions/supabase_functions.dart';

void main() {
final supabaseClient = SupabaseClient(
Deno.env.get('SUPABASE_URL')!,
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!,
httpClient: EdgeHttpClient(),
);

SupabaseFunctions(fetch: (request) async {
final dynamic body = await request.json();
final messageId = body['id'];

final List<dynamic> messages =
await supabaseClient.from('messages').select('body').eq('id', messageId);

var newBody = (messages[0]['body'] as String).toggleCase();
await supabaseClient
.from('messages')
.update({'body': newBody}).eq('id', messageId);

return Response(jsonEncode({"message": "ok"}), status: 200);
});
}

extension _StringExtensions on String {
String toggleCase() => this == toUpperCase() ? toLowerCase() : toUpperCase();
}
Loading

0 comments on commit 779353d

Please sign in to comment.