File tree Expand file tree Collapse file tree 7 files changed +55
-12
lines changed
client-common/src/transporter Expand file tree Collapse file tree 7 files changed +55
-12
lines changed Original file line number Diff line number Diff line change @@ -168,7 +168,29 @@ export function algoliasearch(
168
168
parentApiKey,
169
169
restrictions = { } ,
170
170
} : GenerateSecuredApiKeyOptions ) : string {
171
- const queryParameters = serializeQueryParameters ( restrictions ) ;
171
+ let mergedRestrictions = restrictions ;
172
+ if ( restrictions . searchParams ) {
173
+ // merge searchParams with the root restrictions
174
+ mergedRestrictions = {
175
+ ...restrictions ,
176
+ ...restrictions . searchParams ,
177
+ } ;
178
+
179
+ delete mergedRestrictions . searchParams ;
180
+ }
181
+
182
+ mergedRestrictions = Object . keys ( mergedRestrictions )
183
+ . sort ( )
184
+ . reduce (
185
+ ( acc , key ) => {
186
+ // eslint-disable-next-line no-param-reassign
187
+ acc [ key ] = ( mergedRestrictions as any ) [ key ] ;
188
+ return acc ;
189
+ } ,
190
+ { } as Record < string , unknown >
191
+ ) ;
192
+
193
+ const queryParameters = serializeQueryParameters ( mergedRestrictions ) ;
172
194
return Buffer . from (
173
195
createHmac ( 'sha256' , parentApiKey )
174
196
. update ( queryParameters )
Original file line number Diff line number Diff line change @@ -42,16 +42,14 @@ export function serializeUrl(
42
42
}
43
43
44
44
export function serializeQueryParameters ( parameters : QueryParameters ) : string {
45
- const isObjectOrArray = ( value : any ) : boolean =>
46
- Object . prototype . toString . call ( value ) === '[object Object]' ||
47
- Object . prototype . toString . call ( value ) === '[object Array]' ;
48
-
49
45
return Object . keys ( parameters )
46
+ . filter ( ( key ) => parameters [ key ] !== undefined )
47
+ . sort ( )
50
48
. map (
51
49
( key ) =>
52
50
`${ key } =${ encodeURIComponent (
53
- isObjectOrArray ( parameters [ key ] )
54
- ? JSON . stringify ( parameters [ key ] )
51
+ Object . prototype . toString . call ( parameters [ key ] ) === '[object Array]'
52
+ ? parameters [ key ] . join ( ',' )
55
53
: parameters [ key ]
56
54
) . replaceAll ( '+' , '%20' ) } `
57
55
)
Original file line number Diff line number Diff line change @@ -70,7 +70,29 @@ export function searchClient(
70
70
parentApiKey,
71
71
restrictions = { } ,
72
72
} : GenerateSecuredApiKeyOptions ) : string {
73
- const queryParameters = serializeQueryParameters ( restrictions ) ;
73
+ let mergedRestrictions = restrictions ;
74
+ if ( restrictions . searchParams ) {
75
+ // merge searchParams with the root restrictions
76
+ mergedRestrictions = {
77
+ ...restrictions ,
78
+ ...restrictions . searchParams ,
79
+ } ;
80
+
81
+ delete mergedRestrictions . searchParams ;
82
+ }
83
+
84
+ mergedRestrictions = Object . keys ( mergedRestrictions )
85
+ . sort ( )
86
+ . reduce (
87
+ ( acc , key ) => {
88
+ // eslint-disable-next-line no-param-reassign
89
+ acc [ key ] = ( mergedRestrictions as any ) [ key ] ;
90
+ return acc ;
91
+ } ,
92
+ { } as Record < string , unknown >
93
+ ) ;
94
+
95
+ const queryParameters = serializeQueryParameters ( mergedRestrictions ) ;
74
96
return Buffer . from (
75
97
createHmac ( 'sha256' , parentApiKey )
76
98
. update ( queryParameters )
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ import type { SearchParams } from './searchParams';
25
25
import type { SearchParamsObject } from './searchParamsObject' ;
26
26
import type { SearchRulesParams } from './searchRulesParams' ;
27
27
import type { SearchSynonymsParams } from './searchSynonymsParams' ;
28
- import type { SecuredAPIKeyRestrictions } from './securedAPIKeyRestrictions ' ;
28
+ import type { SecuredApiKeyRestrictions } from './securedApiKeyRestrictions ' ;
29
29
import type { Source } from './source' ;
30
30
import type { SynonymHit } from './synonymHit' ;
31
31
@@ -796,7 +796,7 @@ export type GenerateSecuredApiKeyOptions = {
796
796
/**
797
797
* A set of properties defining the restrictions of the secured API key.
798
798
*/
799
- restrictions ?: SecuredAPIKeyRestrictions ;
799
+ restrictions ?: SecuredApiKeyRestrictions ;
800
800
} ;
801
801
802
802
export type GetSecuredApiKeyRemainingValidityOptions = {
Original file line number Diff line number Diff line change @@ -143,7 +143,7 @@ export * from './searchTypeDefault';
143
143
export * from './searchTypeFacet' ;
144
144
export * from './searchUserIdsParams' ;
145
145
export * from './searchUserIdsResponse' ;
146
- export * from './securedAPIKeyRestrictions ' ;
146
+ export * from './securedApiKeyRestrictions ' ;
147
147
export * from './semanticSearch' ;
148
148
export * from './snippetResult' ;
149
149
export * from './snippetResultOption' ;
Original file line number Diff line number Diff line change 2
2
3
3
import type { SearchParamsObject } from './searchParamsObject' ;
4
4
5
- export type SecuredAPIKeyRestrictions = {
5
+ export type SecuredApiKeyRestrictions = {
6
6
searchParams ?: SearchParamsObject ;
7
7
8
8
/**
Original file line number Diff line number Diff line change @@ -647,6 +647,7 @@ export function createSearchClient({
647
647
648
648
return { copyOperationResponse, batchResponses, moveOperationResponse } ;
649
649
} ,
650
+
650
651
/**
651
652
* Creates a new API key with specific permissions and restrictions.
652
653
*
You can’t perform that action at this time.
0 commit comments