Skip to content

Commit 9281ee9

Browse files
committed
feat(auth): introduce WithinBody option for AuthMode
fixes #1035 Implementation is done by: - adding a new auth mode - adding data as the return type for createAuth - expose data from transporter - serialize transporter data - map api key back to query parameter if GET
1 parent 7c8e967 commit 9281ee9

File tree

13 files changed

+61
-17
lines changed

13 files changed

+61
-17
lines changed

packages/client-analytics/src/createAnalyticsClient.ts

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export const createAnalyticsClient: CreateClient<
2929
...auth.queryParameters(),
3030
...options.queryParameters,
3131
},
32+
33+
data: auth.data(),
3234
});
3335

3436
const appId = options.appId;

packages/client-common/src/createAuth.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,23 @@ export function createAuth(authMode: AuthModeType, appId: string, apiKey: string
88

99
return {
1010
headers(): Readonly<Record<string, string>> {
11-
return authMode === AuthMode.WithinHeaders ? credentials : {};
11+
if (authMode === AuthMode.WithinHeaders) {
12+
return credentials;
13+
} else if (authMode === AuthMode.WithinBody) {
14+
return {
15+
'x-algolia-application-id': appId,
16+
};
17+
}
18+
19+
return {};
1220
},
1321

1422
queryParameters(): Readonly<Record<string, string>> {
1523
return authMode === AuthMode.WithinQueryParameters ? credentials : {};
1624
},
25+
26+
data(): Readonly<Record<string, string>> {
27+
return authMode === AuthMode.WithinBody ? { apiKey } : {};
28+
},
1729
};
1830
}
+8-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
export type Auth = {
22
/**
3-
* Returns the headers related to auth. Should be
4-
* merged to the transporter headers.
3+
* Returns the headers related to auth. Should be merged into the headers.
54
*/
65
readonly headers: () => Readonly<Record<string, string>>;
76

87
/**
9-
* Returns the query parameters related to auth. Should be
10-
* merged to the query parameters headers.
8+
* Returns the query parameters related to auth. Should be merged into the
9+
* query parameters.
1110
*/
1211
readonly queryParameters: () => Readonly<Record<string, string>>;
12+
13+
/**
14+
* Returns the data related to auth. Should be merged into the body.
15+
*/
16+
readonly data: () => Readonly<Record<string, string>>;
1317
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
export const AuthMode: Readonly<Record<string, AuthModeType>> = {
22
/**
3-
* If auth credentials should be in query parameters.
3+
* Algolia credentials are sent as query parameters
44
*/
55
WithinQueryParameters: 0,
66

77
/**
8-
* If auth credentials should be in headers.
8+
* Algolia credentials are sent as headers
99
*/
1010
WithinHeaders: 1,
11+
12+
/**
13+
* Algolia credentials are sent as part of the body
14+
*/
15+
WithinBody: 2,
1116
};
1217

13-
export type AuthModeType = 0 | 1;
18+
export type AuthModeType = 0 | 1 | 2;

packages/client-common/src/types/ClientTransporterOptions.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { Headers, HostOptions, QueryParameters, TransporterOptions } from '@algo
22

33
export type ClientTransporterOptions = Pick<
44
TransporterOptions,
5-
Exclude<keyof TransporterOptions, 'headers'> &
6-
Exclude<keyof TransporterOptions, 'queryParameters'> &
7-
Exclude<keyof TransporterOptions, 'hosts'>
5+
Exclude<keyof TransporterOptions, 'hosts' | 'headers' | 'queryParameters' | 'data'>
86
> & {
97
/**
108
* The hosts used by the requester.

packages/client-personalization/src/createPersonalizationClient.ts

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export const createPersonalizationClient: CreateClient<
2929
...auth.queryParameters(),
3030
...options.queryParameters,
3131
},
32+
33+
data: auth.data(),
3234
});
3335

3436
return addMethods({ appId: options.appId, transporter }, options.methods);

packages/client-search/src/createSearchClient.ts

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export const createSearchClient: CreateClient<
4444
...auth.queryParameters(),
4545
...options.queryParameters,
4646
},
47+
48+
data: auth.data(),
4749
});
4850

4951
const base = {

packages/recommend/src/createRecommendClient.ts

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export const createRecommendClient: CreateClient<
4444
...auth.queryParameters(),
4545
...options.queryParameters,
4646
},
47+
48+
data: auth.data(),
4749
});
4850

4951
const base = {

packages/transporter/src/concerns/retryableRequest.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function retryableRequest<TResponse>(
3131
/**
3232
* First we prepare the payload that do not depend from hosts.
3333
*/
34-
const data = serializeData(request, requestOptions);
34+
const data = serializeData(transporter, request, requestOptions);
3535
const headers = serializeHeaders(transporter, requestOptions);
3636
const method = request.method;
3737

@@ -40,6 +40,8 @@ export function retryableRequest<TResponse>(
4040
request.method !== MethodEnum.Get
4141
? {}
4242
: {
43+
// if AuthMode.WithinData, we forcibly map apiKey back to a query param
44+
'x-algolia-api-key': transporter.data?.apiKey,
4345
...request.data,
4446
...requestOptions.data,
4547
};

packages/transporter/src/createTransporter.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function createTransporter(options: TransporterOptions): Transporter {
2121
hosts,
2222
queryParameters,
2323
headers,
24+
data,
2425
} = options;
2526

2627
const transporter: Transporter = {
@@ -33,14 +34,15 @@ export function createTransporter(options: TransporterOptions): Transporter {
3334
userAgent,
3435
headers,
3536
queryParameters,
37+
data,
3638
hosts: hosts.map(host => createStatelessHost(host)),
3739
read<TResponse>(
3840
request: Request,
3941
requestOptions?: RequestOptions
4042
): Readonly<Promise<TResponse>> {
4143
/**
4244
* First, we compute the user request options. Now, keep in mind,
43-
* that using request options the user is able to modified the intire
45+
* that using request options the user is able to modified the entire
4446
* payload of the request. Such as headers, query parameters, and others.
4547
*/
4648
const mappedRequestOptions = createMappedRequestOptions(
@@ -73,7 +75,7 @@ export function createTransporter(options: TransporterOptions): Transporter {
7375
: request.cacheable;
7476

7577
/**
76-
* If is not "cacheable", we immediatly trigger the retryable request, no
78+
* If is not "cacheable", we immediately trigger the retryable request, no
7779
* need to check cache implementations.
7880
*/
7981
if (cacheable !== true) {
@@ -96,7 +98,7 @@ export function createTransporter(options: TransporterOptions): Transporter {
9698

9799
/**
98100
* With the computed key, we first ask the responses cache
99-
* implemention if this request was been resolved before.
101+
* implementation if this request was been resolved before.
100102
*/
101103
return transporter.responsesCache.get(
102104
key,

packages/transporter/src/serializer.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,22 @@ export function serializeQueryParameters(parameters: Readonly<Record<string, any
3636
}
3737

3838
export function serializeData(
39+
transporter: Transporter,
3940
request: Request,
4041
requestOptions: RequestOptions
4142
): string | undefined {
4243
if (
4344
request.method === MethodEnum.Get ||
44-
(request.data === undefined && requestOptions.data === undefined)
45+
(transporter.data === undefined &&
46+
request.data === undefined &&
47+
requestOptions.data === undefined)
4548
) {
4649
return undefined;
4750
}
4851

4952
const data = Array.isArray(request.data)
5053
? request.data
51-
: { ...request.data, ...requestOptions.data };
54+
: { ...transporter.data, ...request.data, ...requestOptions.data };
5255

5356
return JSON.stringify(data);
5457
}

packages/transporter/src/types/Transporter.ts

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export type Transporter = {
6868
*/
6969
readonly queryParameters: QueryParameters;
7070

71+
/**
72+
* data sent on each request
73+
*/
74+
readonly data: Record<string, string> | undefined;
75+
7176
/**
7277
* The hosts used by the retry strategy.
7378
*

packages/transporter/src/types/TransporterOptions.ts

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ export type TransporterOptions = {
6464
*/
6565
readonly queryParameters: QueryParameters;
6666

67+
/**
68+
* The data set by the requester (credentials)
69+
*/
70+
readonly data: Record<string, string>;
71+
6772
/**
6873
* The user agent used. Sent on query parameters.
6974
*/

0 commit comments

Comments
 (0)