Skip to content

Commit 8d5f6a7

Browse files
committed
Fix preflight.
1 parent b1da40f commit 8d5f6a7

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

laravel/src/SyncApi.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace JamesWildDev\ReactNativeAppHelpers;
44

55
use Illuminate\Support\Facades\Route;
6+
use Illuminate\Support\Arr;
67

78
/**
89
* Represents a sync API as a whole. Use this to configure your API once, then
@@ -107,11 +108,7 @@ public function generateRoutes(): void
107108
foreach ($this->constants as $constant) {
108109
$key = $constant->generateCamelCasedName();
109110

110-
$data = $constant->value;
111-
ksort($data);
112-
$version = hash('sha1', json_encode($data));
113-
114-
$singletons[$key] = compact('version');
111+
$singletons[$key] = Arr::only($constant->getCachedValue(), 'version');
115112
}
116113

117114
$collections = [];

laravel/src/SyncApiConstant.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,28 @@ public function hashData(array $data): string
9898
return hash('sha1', json_encode($data));
9999
}
100100

101-
public function generateConstantRoutes(): void
101+
public function getCachedValue()
102102
{
103-
$kebabCasedName = $this->generateKebabCasedName();
104-
105-
Route::get(
106-
$kebabCasedName,
107-
function () use ($kebabCasedName) {
108-
$data = Cache::remember(
109-
'sync_api_constant_' . $kebabCasedName,
110-
3600,
111-
$this->valueFactory
112-
);
103+
return Cache::remember(
104+
'sync-api-constant-' . $this->generateKebabCasedName(),
105+
3600,
106+
function () {
107+
$data = ($this->valueFactory)();
113108

114109
return [
115-
'version' => $this->hashData($data),
116110
'data' => $data,
111+
'version' => hash('sha1', json_encode($data)),
117112
];
113+
}
114+
);
115+
}
116+
117+
public function generateConstantRoutes(): void
118+
{
119+
Route::get(
120+
$this->generateKebabCasedName(),
121+
function () {
122+
return $this->getCachedValue();
118123
},
119124
);
120125
}

0 commit comments

Comments
 (0)