Skip to content

Commit 6e1a601

Browse files
committed
feat: implement region option
1 parent 91a4516 commit 6e1a601

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

Functions/Client.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ private async Task<HttpResponseMessage> HandleRequest(
126126

127127
options.Headers["X-Client-Info"] = Util.GetAssemblyVersion(typeof(Client));
128128

129+
if (options.FunctionRegion != FunctionRegion.Any)
130+
{
131+
options.Headers["x-region"] = options.FunctionRegion.ToString();
132+
}
133+
129134
var builder = new UriBuilder(url);
130135
var query = HttpUtility.ParseQueryString(builder.Query);
131136

@@ -166,4 +171,3 @@ private async Task<HttpResponseMessage> HandleRequest(
166171
}
167172
}
168173
}
169-

Functions/InvokeFunctionOptions.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public class InvokeFunctionOptions
3636
/// Http method of the Request
3737
/// </summary>
3838
public HttpMethod HttpMethod { get; set; } = HttpMethod.Post;
39+
40+
/// <summary>
41+
/// Region of the request
42+
/// </summary>
43+
public FunctionRegion FunctionRegion { get; set; } = FunctionRegion.Any;
3944
}
4045

4146
/// <summary>
@@ -45,6 +50,40 @@ public class FunctionRegion : IEquatable<FunctionRegion>
4550
{
4651
private string _region;
4752

53+
public static FunctionRegion Any { get; } = new FunctionRegion("any");
54+
55+
public static FunctionRegion ApNortheast1 { get; } =
56+
new FunctionRegion("ap-northeast-1");
57+
58+
public static FunctionRegion ApNortheast2 { get; } =
59+
new FunctionRegion("ap-northeast-2");
60+
61+
public static FunctionRegion ApSouth1 { get; } = new FunctionRegion("ap-south-1");
62+
63+
public static FunctionRegion ApSoutheast1 { get; } =
64+
new FunctionRegion("ap-southeast-1");
65+
66+
public static FunctionRegion ApSoutheast2 { get; } =
67+
new FunctionRegion("ap-southeast-2");
68+
69+
public static FunctionRegion CaCentral1 { get; } = new FunctionRegion("ca-central-1");
70+
71+
public static FunctionRegion EuCentral1 { get; } = new FunctionRegion("eu-central-1");
72+
73+
public static FunctionRegion EuWest1 { get; } = new FunctionRegion("eu-west-1");
74+
75+
public static FunctionRegion EuWest2 { get; } = new FunctionRegion("eu-west-2");
76+
77+
public static FunctionRegion EuWest3 { get; } = new FunctionRegion("eu-west-3");
78+
79+
public static FunctionRegion SaEast1 { get; } = new FunctionRegion("sa-east-1");
80+
81+
public static FunctionRegion UsEast1 { get; } = new FunctionRegion("us-east-1");
82+
83+
public static FunctionRegion UsWest1 { get; } = new FunctionRegion("us-west-1");
84+
85+
public static FunctionRegion UsWest2 { get; } = new FunctionRegion("us-west-2");
86+
4887
/// <summary>
4988
/// Define the region for requests
5089
/// </summary>
@@ -94,7 +133,7 @@ public bool Equals(FunctionRegion other)
94133
public static explicit operator FunctionRegion(string region) =>
95134
new FunctionRegion(region);
96135

97-
public override string? ToString() => _region;
136+
public override string ToString() => _region;
98137
}
99138
}
100139
}

FunctionsTests/ClientTests.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IdentityModel.Tokens.Jwt;
4+
using System.Net.Http;
45
using System.Text;
56
using System.Threading.Tasks;
67
using Microsoft.IdentityModel.Tokens;
@@ -19,8 +20,8 @@ public class ClientTests
1920
[TestInitialize]
2021
public void Initialize()
2122
{
22-
_token = GenerateToken("37c304f8-51aa-419a-a1af-06154e63707a");
23-
_client = new Client("http://localhost:9000");
23+
_token = GenerateToken("super-secret-jwt-token-with-at-least-32-characters-long");
24+
_client = new Client("http://localhost:54321/functions/v1");
2425
}
2526

2627
[TestMethod("Invokes a function.")]
@@ -34,6 +35,7 @@ public async Task Invokes()
3435
new InvokeFunctionOptions
3536
{
3637
Body = new Dictionary<string, object> { { "name", "supabase" } },
38+
HttpMethod = HttpMethod.Post,
3739
}
3840
);
3941

@@ -45,6 +47,7 @@ public async Task Invokes()
4547
new InvokeFunctionOptions
4648
{
4749
Body = new Dictionary<string, object> { { "name", "functions" } },
50+
HttpMethod = HttpMethod.Post,
4851
}
4952
);
5053

@@ -58,12 +61,25 @@ public async Task Invokes()
5861
new InvokeFunctionOptions
5962
{
6063
Body = new Dictionary<string, object> { { "name", "functions" } },
64+
HttpMethod = HttpMethod.Post,
6165
}
6266
);
6367

6468
var bytes = await result3.ReadAsByteArrayAsync();
6569

6670
Assert.IsInstanceOfType(bytes, typeof(byte[]));
71+
72+
var result4 = await _client.Invoke(
73+
function,
74+
_token,
75+
new InvokeFunctionOptions
76+
{
77+
Body = [],
78+
HttpMethod = HttpMethod.Get,
79+
}
80+
);
81+
82+
Assert.IsTrue(result4.Contains(function));
6783
}
6884

6985
private static string GenerateToken(string secret)
@@ -83,4 +99,4 @@ private static string GenerateToken(string secret)
8399
return tokenHandler.WriteToken(securityToken);
84100
}
85101
}
86-
}
102+
}

supabase/functions/hello/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ import { serve } from "https://deno.land/std@0.131.0/http/server.ts"
66

77
console.log("Hello from Functions!")
88

9-
serve(async (req) => {
10-
const { name } = await req.json()
9+
serve(async (req: Request) => {
10+
let value = req.url.substring(req.url.lastIndexOf("/") + 1)
11+
if (req.body != null) {
12+
const { name } = await req.json()
13+
value = name
14+
}
15+
1116
const data = {
12-
message: `Hello ${name}!`,
17+
message: `Hello ${value}!`,
1318
}
1419

20+
console.log("response", JSON.stringify(data))
21+
1522
return new Response(
1623
JSON.stringify(data),
1724
{ headers: { "Content-Type": "application/json" } },

0 commit comments

Comments
 (0)