|
1 | 1 | using System.Text.Json; |
| 2 | +using InertiaCore.Utils; |
2 | 3 | using Microsoft.AspNetCore.Http; |
3 | 4 | using Microsoft.AspNetCore.Http.Extensions; |
4 | 5 | using Microsoft.AspNetCore.Mvc; |
| 6 | +using System.Text; |
5 | 7 |
|
6 | 8 | namespace InertiaCore.Extensions; |
7 | 9 |
|
8 | 10 | internal static class InertiaExtensions |
9 | 11 | { |
10 | | - internal static IEnumerable<string> Only(this object obj, IEnumerable<string> only) => |
11 | | - obj.GetType().GetProperties().Select(c => c.Name) |
12 | | - .Intersect(only, StringComparer.OrdinalIgnoreCase).ToList(); |
13 | | - |
14 | | - internal static List<string> GetPartialData(this ActionContext context) => |
15 | | - context.HttpContext.Request.Headers["X-Inertia-Partial-Data"] |
16 | | - .FirstOrDefault()?.Split(",") |
17 | | - .Where(s => !string.IsNullOrEmpty(s)) |
18 | | - .ToList() ?? new List<string>(); |
19 | | - |
20 | 12 | internal static bool IsInertiaPartialComponent(this ActionContext context, string component) => |
21 | | - context.HttpContext.Request.Headers["X-Inertia-Partial-Component"] == component; |
| 13 | + context.HttpContext.Request.Headers[InertiaHeader.PartialComponent] == component; |
22 | 14 |
|
23 | 15 | internal static string RequestedUri(this HttpContext context) => |
24 | 16 | Uri.UnescapeDataString(context.Request.GetEncodedPathAndQuery()); |
25 | 17 |
|
26 | 18 | internal static string RequestedUri(this ActionContext context) => context.HttpContext.RequestedUri(); |
27 | 19 |
|
28 | 20 | internal static bool IsInertiaRequest(this HttpContext context) => |
29 | | - bool.TryParse(context.Request.Headers["X-Inertia"], out _); |
| 21 | + bool.TryParse(context.Request.Headers[InertiaHeader.Inertia], out _); |
30 | 22 |
|
31 | 23 | internal static bool IsInertiaRequest(this ActionContext context) => context.HttpContext.IsInertiaRequest(); |
32 | 24 |
|
33 | 25 | internal static string ToCamelCase(this string s) => JsonNamingPolicy.CamelCase.ConvertName(s); |
34 | 26 |
|
35 | 27 | internal static bool Override<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
36 | 28 | { |
37 | | - if (dictionary.ContainsKey(key)) |
38 | | - { |
39 | | - dictionary[key] = value; |
40 | | - return true; |
41 | | - } |
42 | | - |
43 | | - dictionary.Add(key, value); |
44 | | - return false; |
| 29 | + if (dictionary.TryAdd(key, value)) return false; |
| 30 | + dictionary[key] = value; |
| 31 | + |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + internal static Task<object?> ResolveAsync(this Func<object?> func) |
| 36 | + { |
| 37 | + var rt = func.Method.ReturnType; |
| 38 | + |
| 39 | + if (!rt.IsGenericType || rt.GetGenericTypeDefinition() != typeof(Task<>)) |
| 40 | + return Task.Run(func.Invoke); |
| 41 | + |
| 42 | + var task = func.DynamicInvoke() as Task; |
| 43 | + return task!.ResolveResult(); |
| 44 | + } |
| 45 | + |
| 46 | + internal static async Task<object?> ResolveResult(this Task task) |
| 47 | + { |
| 48 | + await task.ConfigureAwait(false); |
| 49 | + var result = task.GetType().GetProperty("Result"); |
| 50 | + |
| 51 | + return result?.GetValue(task); |
| 52 | + } |
| 53 | + |
| 54 | + internal static string MD5(this string s) |
| 55 | + { |
| 56 | + using var md5 = System.Security.Cryptography.MD5.Create(); |
| 57 | + var inputBytes = Encoding.UTF8.GetBytes(s); |
| 58 | + var hashBytes = md5.ComputeHash(inputBytes); |
| 59 | + |
| 60 | + var sb = new StringBuilder(); |
| 61 | + foreach (var t in hashBytes) |
| 62 | + sb.Append(t.ToString("x2")); |
| 63 | + |
| 64 | + return sb.ToString(); |
45 | 65 | } |
46 | 66 | } |
0 commit comments