Skip to content

Commit f39d2b6

Browse files
authored
Merge pull request #27 from laplace-demon-ai/fix/rename-option-field-as-source-key
fix: rename option key as source_key and remove from unncecessary str…
2 parents 471f717 + e8032ad commit f39d2b6

16 files changed

Lines changed: 54 additions & 104 deletions

config/demon-dextral-horn.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
* Sample rule logic for showcasing different prefetching strategies/scenarios.
3131
*/
3232
[
33-
'id' => 'sample_rule_id',
3433
'description' => 'Sample/generalized rule description for prefetching related data',
3534
'trigger' => [
3635
'method' => Request::METHOD_GET,
@@ -48,7 +47,7 @@
4847
'query_key' => [
4948
'strategy' => IncrementStrategy::class,
5049
'options' => [
51-
'key' => 'query_key',
50+
'source_key' => 'query_key',
5251
'increment' => 1,
5352
],
5453
],
@@ -64,10 +63,10 @@
6463
'method' => Request::METHOD_GET,
6564
'route' => 'sample.target.route.with.forward.value',
6665
'query_params' => [
67-
'query_trigger_key' => [
66+
'query_target_key' => [
6867
'strategy' => ForwardValueStrategy::class,
6968
'options' => [
70-
'key' => 'query_target_key',
69+
'source_key' => 'query_trigger_key',
7170
],
7271
],
7372
],
@@ -93,7 +92,6 @@
9392
'route_key' => [
9493
'strategy' => ResponseValueStrategy::class,
9594
'options' => [
96-
'key' => 'route_key',
9795
'position' => 'data.id', // Full path to the value in the response data
9896
],
9997
],
@@ -111,7 +109,6 @@
111109
'route_key' => [
112110
'strategy' => ResponsePluckStrategy::class,
113111
'options' => [
114-
'key' => 'route_key',
115112
'position' => 'data.*.id',
116113
'limit' => 3,
117114
'order' => OrderType::DESC->value,
@@ -126,7 +123,6 @@
126123
* Sample rule logic for handling login requests including JWT token extraction, laravel session handling etc.
127124
*/
128125
[
129-
'id' => 'login_request_id',
130126
'description' => 'Handle login request and prefetch data for authenticated user',
131127
'trigger' => [
132128
'method' => Request::METHOD_POST,
@@ -144,7 +140,6 @@
144140
'authorization' => [
145141
'strategy' => ResponseJwtStrategy::class,
146142
'options' => [
147-
'key' => 'authorization',
148143
'position' => 'data.access_token',
149144
],
150145
],
@@ -162,7 +157,7 @@
162157
'session_cookie' => [
163158
'strategy' => ResponseSessionHeaderStrategy::class,
164159
'options' => [
165-
'key' => config('session.cookie', 'laravel_session'),
160+
'source_key' => config('session.cookie', 'laravel_session'),
166161
],
167162
],
168163
],

src/Resolvers/Strategies/Source/ForwardValueStrategy.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public function handle(
2525
?ResponseData $responseData,
2626
?array $options = []
2727
): mixed {
28-
$key = Arr::get($options, 'key');
28+
$sourceKey = Arr::get($options, 'source_key');
2929

3030
// Throw an exception if the required "key" option is missing, or request does not have the key.
31-
if ($key === null || ! Arr::has($requestData?->queryParams ?? [], $key)) {
32-
throw new MissingStrategyOptionException(self::class, 'key');
31+
if ($sourceKey === null || ! Arr::has($requestData?->queryParams ?? [], $sourceKey)) {
32+
throw new MissingStrategyOptionException(self::class, 'source_key');
3333
}
3434

3535
// Return the current value as is.
36-
return Arr::get($requestData?->queryParams ?? [], $key);
36+
return Arr::get($requestData?->queryParams ?? [], $sourceKey);
3737
}
3838
}

src/Resolvers/Strategies/Source/ResponseJwtStrategy.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ public function handle(
2525
?ResponseData $responseData,
2626
?array $options = []
2727
): mixed {
28-
$key = Arr::get($options, 'key');
2928
$position = Arr::get($options, 'position');
3029

31-
if ($key === null || $position === null) {
32-
throw new MissingStrategyOptionException(self::class, 'key/position');
30+
if ($position === null) {
31+
throw new MissingStrategyOptionException(self::class, 'position');
3332
}
3433

3534
// Get the JWT token from the response data provided position

src/Resolvers/Strategies/Source/ResponsePluckStrategy.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ public function handle(
2727
?array $options = []
2828
): mixed {
2929
$position = Arr::get($options, 'position');
30-
$key = Arr::get($options, 'key'); // e.g. route_key
3130
$limitRaw = Arr::get($options, 'limit', null);
3231
$limit = $limitRaw === null ? null : (int) $limitRaw;
3332
$order = strtolower((string) Arr::get($options, 'order', ''));
3433

35-
if ($key === null || $position === null) {
36-
throw new MissingStrategyOptionException(self::class, 'key/position');
34+
if ($position === null) {
35+
throw new MissingStrategyOptionException(self::class, 'position');
3736
}
3837

3938
// Normalize position by removing any wildcard characters for consistent access. e.g. 'data.*.id' will be 'id'

src/Resolvers/Strategies/Source/ResponseSessionHeaderStrategy.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public function handle(
2525
?ResponseData $responseData,
2626
?array $options = []
2727
): mixed {
28-
$key = Arr::get($options, 'key'); // The key for the session cookie e.g. laravel_session
28+
$sourceKey = Arr::get($options, 'source_key'); // The key for the session cookie e.g. laravel_session
2929

30-
if ($key === null) {
31-
throw new MissingStrategyOptionException(self::class, 'key');
30+
if ($sourceKey === null) {
31+
throw new MissingStrategyOptionException(self::class, 'source_key');
3232
}
3333

3434
$headersData = $responseData?->headers;
@@ -37,7 +37,7 @@ public function handle(
3737

3838
foreach ($headersData?->setCookie ?? [] as $cookie) {
3939
// Check if the cookie matches the session cookie name (laravel_session)
40-
if (str_contains($cookie, $key)) {
40+
if (str_contains($cookie, $sourceKey)) {
4141
$sessionCookie = $cookie;
4242

4343
break;

src/Resolvers/Strategies/Source/ResponseValueStrategy.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ public function handle(
2626
?array $options = []
2727
): mixed {
2828
$position = Arr::get($options, 'position'); // e.g. data.id
29-
$key = Arr::get($options, 'key'); // e.g. route_key
3029

31-
if ($key === null || $position === null) {
32-
throw new MissingStrategyOptionException(self::class, 'key/position');
30+
if ($position === null) {
31+
throw new MissingStrategyOptionException(self::class, 'position');
3332
}
3433

3534
return Arr::get(json_decode($responseData?->content, true), $position);

src/Resolvers/Strategies/Transform/IncrementStrategy.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ public function handle(
2525
?ResponseData $responseData,
2626
?array $options = []
2727
): int {
28-
$key = Arr::get($options, 'key');
28+
$sourceKey = Arr::get($options, 'source_key');
2929
$increment = (int) Arr::get($options, 'increment', 1);
3030

31-
// Throw an exception if the required "key" option is missing.
32-
if ($key === null) {
33-
throw new MissingStrategyOptionException(self::class, 'key');
31+
// Throw an exception if the required "source_key" option is missing.
32+
if ($sourceKey === null) {
33+
throw new MissingStrategyOptionException(self::class, 'source_key');
3434
}
3535

3636
// Get the current value from the request data, setting a default 1 if not present (e.g. query parameter 'page' for the pagination)
37-
$currentValue = (int) Arr::get($requestData?->queryParams ?? [], $key, 1);
37+
$currentValue = (int) Arr::get($requestData?->queryParams ?? [], $sourceKey, 1);
3838

3939
// Increment the current value.
4040
return $currentValue + $increment;

tests/Resolvers/CookiesResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function it_tests_if_set_cookie_header_of_response_is_forwarded(): void
4141
'session_cookie' => [
4242
'strategy' => ResponseSessionHeaderStrategy::class,
4343
'options' => [
44-
'key' => $cookieName,
44+
'source_key' => $cookieName,
4545
],
4646
],
4747
],

tests/Resolvers/HeadersResolverTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ public function it_tests_if_jwt_access_token_is_added_to_authorization_header():
127127
'authorization' => [
128128
'strategy' => ResponseJwtStrategy::class,
129129
'options' => [
130-
'key' => 'authorization',
131130
'position' => 'data.access_token',
132131
],
133132
],
@@ -162,7 +161,6 @@ public function it_tests_if_jwt_token_prioritized_over_existing_authorization_he
162161
'authorization' => [
163162
'strategy' => ResponseJwtStrategy::class,
164163
'options' => [
165-
'key' => 'authorization',
166164
'position' => 'data.access_token',
167165
],
168166
],

tests/Resolvers/QueryParamResolverTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function it_resolves_query_parameters_for_increment_strategy(): void
4545
'query_key' => [
4646
'strategy' => IncrementStrategy::class,
4747
'options' => [
48-
'key' => 'query_key',
48+
'source_key' => 'query_key',
4949
'increment' => $increment,
5050
],
5151
],
@@ -96,11 +96,11 @@ public function it_can_resolve_multiple_query_parameters(): void
9696
'query_params' => [
9797
'first' => [
9898
'strategy' => IncrementStrategy::class,
99-
'options' => ['key' => 'first', 'increment' => $incrementFirst],
99+
'options' => ['source_key' => 'first', 'increment' => $incrementFirst],
100100
],
101101
'second' => [
102102
'strategy' => IncrementStrategy::class,
103-
'options' => ['key' => 'second', 'increment' => $incrementSecond],
103+
'options' => ['source_key' => 'second', 'increment' => $incrementSecond],
104104
],
105105
],
106106
];
@@ -132,7 +132,7 @@ public function it_can_resolve_query_parameters_for_forward_value_strategy(): vo
132132
'query_target_key' => [
133133
'strategy' => ForwardValueStrategy::class,
134134
'options' => [
135-
'key' => 'query_trigger_key',
135+
'source_key' => 'query_trigger_key',
136136
],
137137
],
138138
],
@@ -160,7 +160,7 @@ public function it_can_resolve_array_query_parameters_for_forward_value_strategy
160160
'query_params' => [
161161
'items' => [
162162
'strategy' => ForwardValueStrategy::class,
163-
'options' => ['key' => 'items'],
163+
'options' => ['source_key' => 'items'],
164164
],
165165
],
166166
];
@@ -192,7 +192,7 @@ public function it_forwards_query_param_without_transoformation(): void
192192
'query_key' => [
193193
'strategy' => ForwardValueStrategy::class,
194194
'options' => [
195-
'key' => 'query_key',
195+
'source_key' => 'query_key',
196196
],
197197
],
198198
],
@@ -221,7 +221,7 @@ public function it_throws_missing_strategy_option_exception_when_query_key_missi
221221
'query_key' => [
222222
'strategy' => ForwardValueStrategy::class,
223223
'options' => [
224-
'key' => 'query_key',
224+
'source_key' => 'query_key',
225225
],
226226
],
227227
],

0 commit comments

Comments
 (0)