Skip to content

Commit d00fa4c

Browse files
committed
Enables nullability in project. Ref: supabase-community/supabase-csharp#34
1 parent 6a9500d commit d00fa4c

13 files changed

+219
-194
lines changed

Gotrue/Api.cs

+43-31
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Api(string url, Dictionary<string, string> headers)
4141
/// <param name="password"></param>
4242
/// <param name="options">Optional Signup data.</param>
4343
/// <returns></returns>
44-
public async Task<Session> SignUpWithEmail(string email, string password, SignUpOptions options = null)
44+
public async Task<Session?> SignUpWithEmail(string email, string password, SignUpOptions? options = null)
4545
{
4646
var body = new Dictionary<string, object> { { "email", email }, { "password", password } };
4747

@@ -51,7 +51,7 @@ public async Task<Session> SignUpWithEmail(string email, string password, SignUp
5151
{
5252
if (!string.IsNullOrEmpty(options.RedirectTo))
5353
{
54-
endpoint = Helpers.AddQueryParams(endpoint, new Dictionary<string, string> { { "redirect_to", options.RedirectTo } }).ToString();
54+
endpoint = Helpers.AddQueryParams(endpoint, new Dictionary<string, string> { { "redirect_to", options.RedirectTo! } }).ToString();
5555
}
5656

5757
if (options.Data != null)
@@ -62,18 +62,25 @@ public async Task<Session> SignUpWithEmail(string email, string password, SignUp
6262

6363
var response = await Helpers.MakeRequest(HttpMethod.Post, endpoint, body, Headers);
6464

65-
// Gotrue returns a Session object for an auto-/pre-confirmed account
66-
var session = JsonConvert.DeserializeObject<Session>(response.Content);
65+
if (!string.IsNullOrEmpty(response.Content))
66+
{
67+
// Gotrue returns a Session object for an auto-/pre-confirmed account
68+
var session = JsonConvert.DeserializeObject<Session>(response.Content!);
6769

68-
// If account is unconfirmed, Gotrue returned the user object, so fill User data
69-
// in from the parsed response.
70-
if (session.User == null)
70+
// If account is unconfirmed, Gotrue returned the user object, so fill User data
71+
// in from the parsed response.
72+
if (session != null && session.User == null)
73+
{
74+
// Gotrue returns a User object for an unconfirmed account
75+
session.User = JsonConvert.DeserializeObject<User>(response.Content!);
76+
}
77+
78+
return session;
79+
}
80+
else
7181
{
72-
// Gotrue returns a User object for an unconfirmed account
73-
session.User = JsonConvert.DeserializeObject<User>(response.Content);
82+
return null;
7483
}
75-
76-
return session;
7784
}
7885

7986
/// <summary>
@@ -82,7 +89,7 @@ public async Task<Session> SignUpWithEmail(string email, string password, SignUp
8289
/// <param name="email"></param>
8390
/// <param name="password"></param>
8491
/// <returns></returns>
85-
public Task<Session> SignInWithEmail(string email, string password)
92+
public Task<Session?> SignInWithEmail(string email, string password)
8693
{
8794
var body = new Dictionary<string, object> { { "email", email }, { "password", password } };
8895
return Helpers.MakeRequest<Session>(HttpMethod.Post, $"{Url}/token?grant_type=password", body, Headers);
@@ -94,7 +101,7 @@ public Task<Session> SignInWithEmail(string email, string password)
94101
/// <param name="email"></param>
95102
/// <param name="options"></param>
96103
/// <returns></returns>
97-
public Task<BaseResponse> SendMagicLinkEmail(string email, SignInOptions options = null)
104+
public Task<BaseResponse> SendMagicLinkEmail(string email, SignInOptions? options = null)
98105
{
99106
var data = new Dictionary<string, string> { { "email", email } };
100107

@@ -104,7 +111,7 @@ public Task<BaseResponse> SendMagicLinkEmail(string email, SignInOptions options
104111
{
105112
if (!string.IsNullOrEmpty(options.RedirectTo))
106113
{
107-
endpoint = Helpers.AddQueryParams(endpoint, new Dictionary<string, string> { { "redirect_to", options.RedirectTo } }).ToString();
114+
endpoint = Helpers.AddQueryParams(endpoint, new Dictionary<string, string> { { "redirect_to", options.RedirectTo! } }).ToString();
108115
}
109116
}
110117

@@ -130,7 +137,7 @@ public Task<BaseResponse> InviteUserByEmail(string email, string jwt)
130137
/// <param name="password">The password of the user.</param>
131138
/// <param name="options">Optional Signup data.</param>
132139
/// <returns></returns>
133-
public Task<Session> SignUpWithPhone(string phone, string password, SignUpOptions options = null)
140+
public Task<Session?> SignUpWithPhone(string phone, string password, SignUpOptions? options = null)
134141
{
135142
var body = new Dictionary<string, object> {
136143
{ "phone", phone },
@@ -143,7 +150,7 @@ public Task<Session> SignUpWithPhone(string phone, string password, SignUpOption
143150
{
144151
if (!string.IsNullOrEmpty(options.RedirectTo))
145152
{
146-
endpoint = Helpers.AddQueryParams(endpoint, new Dictionary<string, string> { { "redirect_to", options.RedirectTo } }).ToString();
153+
endpoint = Helpers.AddQueryParams(endpoint, new Dictionary<string, string> { { "redirect_to", options.RedirectTo! } }).ToString();
147154
}
148155

149156
if (options.Data != null)
@@ -161,7 +168,7 @@ public Task<Session> SignUpWithPhone(string phone, string password, SignUpOption
161168
/// <param name="phone">The phone number of the user.</param>
162169
/// <param name="password">The password of the user.</param>
163170
/// <returns></returns>
164-
public Task<Session> SignInWithPhone(string phone, string password)
171+
public Task<Session?> SignInWithPhone(string phone, string password)
165172
{
166173
var data = new Dictionary<string, object> {
167174
{ "phone", phone },
@@ -187,7 +194,7 @@ public Task<BaseResponse> SendMobileOTP(string phone)
187194
/// <param name="phone">The user's phone number WITH international prefix</param>
188195
/// <param name="token">token that user was sent to their mobile phone</param>
189196
/// <returns></returns>
190-
public Task<Session> VerifyMobileOTP(string phone, string token, MobileOtpType type)
197+
public Task<Session?> VerifyMobileOTP(string phone, string token, MobileOtpType type)
191198
{
192199
var data = new Dictionary<string, string> {
193200
{ "phone", phone },
@@ -203,7 +210,7 @@ public Task<Session> VerifyMobileOTP(string phone, string token, MobileOtpType t
203210
/// <param name="phone">The user's phone number WITH international prefix</param>
204211
/// <param name="token">token that user was sent to their mobile phone</param>
205212
/// <returns></returns>
206-
public Task<Session> VerifyEmailOTP(string email, string token, EmailOtpType type)
213+
public Task<Session?> VerifyEmailOTP(string email, string token, EmailOtpType type)
207214
{
208215
var data = new Dictionary<string, string> {
209216
{ "email", email },
@@ -244,7 +251,7 @@ internal Dictionary<string, string> CreateAuthedRequestHeaders(string jwt)
244251
/// <param name="provider"></param>
245252
/// <param name="scopes">A space-separated list of scopes granted to the OAuth application.</param>
246253
/// <returns></returns>
247-
public string GetUrlForProvider(Provider provider, string scopes = null)
254+
public string GetUrlForProvider(Provider provider, string? scopes = null)
248255
{
249256
var builder = new UriBuilder($"{Url}/authorize");
250257
var attr = Helpers.GetMappedToAttr(provider);
@@ -279,7 +286,7 @@ public Task<BaseResponse> SignOut(string jwt)
279286
/// </summary>
280287
/// <param name="jwt"></param>
281288
/// <returns></returns>
282-
public Task<User> GetUser(string jwt)
289+
public Task<User?> GetUser(string jwt)
283290
{
284291
var data = new Dictionary<string, string> { };
285292

@@ -292,7 +299,7 @@ public Task<User> GetUser(string jwt)
292299
/// <param name="jwt">A valid JWT. Must be a full-access API key (e.g. service_role key).</param>
293300
/// <param name="userId"></param>
294301
/// <returns></returns>
295-
public Task<User> GetUserById(string jwt, string userId)
302+
public Task<User?> GetUserById(string jwt, string userId)
296303
{
297304
var data = new Dictionary<string, string> { };
298305

@@ -305,7 +312,7 @@ public Task<User> GetUserById(string jwt, string userId)
305312
/// <param name="jwt"></param>
306313
/// <param name="attributes"></param>
307314
/// <returns></returns>
308-
public Task<User> UpdateUser(string jwt, UserAttributes attributes)
315+
public Task<User?> UpdateUser(string jwt, UserAttributes attributes)
309316
{
310317
return Helpers.MakeRequest<User>(HttpMethod.Put, $"{Url}/user", attributes, CreateAuthedRequestHeaders(jwt));
311318
}
@@ -320,18 +327,18 @@ public Task<User> UpdateUser(string jwt, UserAttributes attributes)
320327
/// <param name="page">page to show for pagination</param>
321328
/// <param name="perPage">items per page for pagination</param>
322329
/// <returns></returns>
323-
public Task<UserList<User>> ListUsers(string jwt, string filter = null, string sortBy = null, SortOrder sortOrder = SortOrder.Descending, int? page = null, int? perPage = null)
330+
public Task<UserList<User>?> ListUsers(string jwt, string? filter = null, string? sortBy = null, SortOrder sortOrder = SortOrder.Descending, int? page = null, int? perPage = null)
324331
{
325332
var data = TransformListUsersParams(filter, sortBy, sortOrder, page, perPage);
326333

327334
return Helpers.MakeRequest<UserList<User>>(HttpMethod.Get, $"{Url}/admin/users", data, CreateAuthedRequestHeaders(jwt));
328335
}
329336

330-
internal Dictionary<string, string> TransformListUsersParams(string filter = null, string sortBy = null, SortOrder sortOrder = SortOrder.Descending, int? page = null, int? perPage = null)
337+
internal Dictionary<string, string> TransformListUsersParams(string? filter = null, string? sortBy = null, SortOrder sortOrder = SortOrder.Descending, int? page = null, int? perPage = null)
331338
{
332339
var query = new Dictionary<string, string> { };
333340

334-
if (!string.IsNullOrWhiteSpace(filter))
341+
if (filter != null && !string.IsNullOrWhiteSpace(filter))
335342
{
336343
query.Add("filter", filter);
337344
}
@@ -363,8 +370,13 @@ internal Dictionary<string, string> TransformListUsersParams(string filter = nul
363370
/// <param name="password"></param>
364371
/// <param name="userData"></param>
365372
/// <returns></returns>
366-
public Task<User> CreateUser(string jwt, AdminUserAttributes attributes = null)
373+
public Task<User?> CreateUser(string jwt, AdminUserAttributes? attributes = null)
367374
{
375+
if (attributes == null)
376+
{
377+
attributes = new AdminUserAttributes();
378+
}
379+
368380
return Helpers.MakeRequest<User>(HttpMethod.Post, $"{Url}/admin/users", attributes, CreateAuthedRequestHeaders(jwt));
369381
}
370382

@@ -375,7 +387,7 @@ public Task<User> CreateUser(string jwt, AdminUserAttributes attributes = null)
375387
/// <param name="userId"></param>
376388
/// <param name="data"></param>
377389
/// <returns></returns>
378-
public Task<User> UpdateUserById(string jwt, string userId, UserAttributes userData)
390+
public Task<User?> UpdateUserById(string jwt, string userId, UserAttributes userData)
379391
{
380392
return Helpers.MakeRequest<User>(HttpMethod.Put, $"{Url}/admin/users/{userId}", userData, CreateAuthedRequestHeaders(jwt));
381393
}
@@ -397,7 +409,7 @@ public Task<BaseResponse> DeleteUser(string uid, string jwt)
397409
/// </summary>
398410
/// <param name="refreshToken"></param>
399411
/// <returns></returns>
400-
public Task<Session> RefreshAccessToken(string refreshToken)
412+
public Task<Session?> RefreshAccessToken(string refreshToken)
401413
{
402414
var data = new Dictionary<string, string> {
403415
{ "refresh_token", refreshToken }
@@ -415,7 +427,7 @@ public class SignUpOptions : SignInOptions
415427
/// <summary>
416428
/// Optional user metadata.
417429
/// </summary>
418-
public Dictionary<string, object> Data { get; set; }
430+
public Dictionary<string, object>? Data { get; set; }
419431
}
420432

421433
// <summary>
@@ -426,6 +438,6 @@ public class SignInOptions
426438
/// <summary>
427439
/// A URL or mobile address to send the user to after they are confirmed.
428440
/// </summary>
429-
public string RedirectTo { get; set; }
441+
public string? RedirectTo { get; set; }
430442
}
431443
}

Gotrue/Attributes/MapToAttribute.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace Supabase.Gotrue.Attributes
1010
internal class MapToAttribute : Attribute
1111
{
1212
public string Mapping { get; set; }
13-
public string Formatter { get; set; }
13+
public string? Formatter { get; set; }
1414

15-
public MapToAttribute(string mapping, string formatter = null)
15+
public MapToAttribute(string mapping, string? formatter = null)
1616
{
1717
Mapping = mapping;
1818
Formatter = formatter;

0 commit comments

Comments
 (0)