Skip to content

Commit ec4bf6f

Browse files
committed
feat: use security scheme to pass appId and appKey
1 parent d8d2c9d commit ec4bf6f

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { searchClient } from './searchClient';
22

3-
const appId = process.env.ALGOLIA_APPLICATION_ID_1 || '**** APP_ID *****';
4-
const apiKey = process.env.ALGOLIA_ADMIN_KEY_1 || '**** API_KEY *****';
3+
const appId = process.env.ALGOLIA_APPLICATION_ID_1 || '**** APP_ID *****';
4+
const apiKey = process.env.ALGOLIA_ADMIN_KEY_1 || '**** API_KEY *****';
55
// Init client with appId and apiKey
66
const client = new searchClient(appId, apiKey);
77

openapi_spec/spec.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ servers:
2020
variables:
2121
appId:
2222
default: test
23+
components:
24+
securitySchemes:
25+
appId:
26+
type: apiKey
27+
in: header
28+
name: X-Algolia-Application-Id
29+
apiKey:
30+
type: apiKey
31+
in: header
32+
name: X-Algolia-API-Key
33+
security:
34+
- appId: []
35+
apiKey: []
2336
paths:
2437
# We can add this one later, as it requires the init method
2538
# /1/indexes/{indexName}/query:

output/client-search/searchApi.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { MultipleQueriesResponse } from '../model/multipleQueriesResponse';
88
import { SaveObjectResponse } from '../model/saveObjectResponse';
99

1010
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
11+
import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
1112

1213
import { HttpError, RequestFile } from './apis';
1314

@@ -17,7 +18,10 @@ let defaultBasePath = 'https://test-1.algolianet.com';
1718
// This file is autogenerated - Please do not edit
1819
// ===============================================
1920

20-
export enum SearchApiApiKeys {}
21+
export enum SearchApiApiKeys {
22+
apiKey,
23+
appId,
24+
}
2125

2226
export class SearchApi {
2327
protected _basePath = defaultBasePath;
@@ -26,6 +30,8 @@ export class SearchApi {
2630

2731
protected authentications = {
2832
default: <Authentication>new VoidAuth(),
33+
apiKey: new ApiKeyAuth('header', 'X-Algolia-API-Key'),
34+
appId: new ApiKeyAuth('header', 'X-Algolia-Application-Id'),
2935
};
3036

3137
protected interceptors: Interceptor[] = [];
@@ -128,6 +134,16 @@ export class SearchApi {
128134
};
129135

130136
let authenticationPromise = Promise.resolve();
137+
if (this.authentications.apiKey.apiKey) {
138+
authenticationPromise = authenticationPromise.then(() =>
139+
this.authentications.apiKey.applyToRequest(localVarRequestOptions)
140+
);
141+
}
142+
if (this.authentications.appId.apiKey) {
143+
authenticationPromise = authenticationPromise.then(() =>
144+
this.authentications.appId.applyToRequest(localVarRequestOptions)
145+
);
146+
}
131147
authenticationPromise = authenticationPromise.then(() =>
132148
this.authentications.default.applyToRequest(localVarRequestOptions)
133149
);
@@ -206,6 +222,16 @@ export class SearchApi {
206222
};
207223

208224
let authenticationPromise = Promise.resolve();
225+
if (this.authentications.apiKey.apiKey) {
226+
authenticationPromise = authenticationPromise.then(() =>
227+
this.authentications.apiKey.applyToRequest(localVarRequestOptions)
228+
);
229+
}
230+
if (this.authentications.appId.apiKey) {
231+
authenticationPromise = authenticationPromise.then(() =>
232+
this.authentications.appId.applyToRequest(localVarRequestOptions)
233+
);
234+
}
209235
authenticationPromise = authenticationPromise.then(() =>
210236
this.authentications.default.applyToRequest(localVarRequestOptions)
211237
);
@@ -298,6 +324,16 @@ export class SearchApi {
298324
};
299325

300326
let authenticationPromise = Promise.resolve();
327+
if (this.authentications.apiKey.apiKey) {
328+
authenticationPromise = authenticationPromise.then(() =>
329+
this.authentications.apiKey.applyToRequest(localVarRequestOptions)
330+
);
331+
}
332+
if (this.authentications.appId.apiKey) {
333+
authenticationPromise = authenticationPromise.then(() =>
334+
this.authentications.appId.applyToRequest(localVarRequestOptions)
335+
);
336+
}
301337
authenticationPromise = authenticationPromise.then(() =>
302338
this.authentications.default.applyToRequest(localVarRequestOptions)
303339
);

searchClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { SearchApi } from 'algoliasearch-client-javascript';
1+
import { SearchApi, SearchApiApiKeys } from 'algoliasearch-client-javascript';
22

33
export class searchClient extends SearchApi {
44
public constructor(appId: string, apiKey: string) {
55
super();
66

7-
this.defaultHeaders['X-Algolia-Application-Id'] = appId;
8-
this.defaultHeaders['X-Algolia-API-Key'] = apiKey;
7+
this.setApiKey(SearchApiApiKeys.appId, appId);
8+
this.setApiKey(SearchApiApiKeys.apiKey, apiKey);
99
this.basePath = 'https://' + appId + '-1.algolianet.com';
1010
}
1111
}

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,11 @@ __metadata:
226226

227227
"algoliasearch-client-javascript@file:output/::locator=%40algolia%2Fautomation-javascript-client%40workspace%3A.":
228228
version: 5.0.0
229-
resolution: "algoliasearch-client-javascript@file:output/#output/::hash=5e44d1&locator=%40algolia%2Fautomation-javascript-client%40workspace%3A."
229+
resolution: "algoliasearch-client-javascript@file:output/#output/::hash=d8075e&locator=%40algolia%2Fautomation-javascript-client%40workspace%3A."
230230
dependencies:
231231
"@types/request": ^2.48.7
232232
request: ^2.81.0
233-
checksum: 324b24f6828b13f4d863425274de41987bcd006e3950379d01fb822308ef0cab02ca07e21297697fccd1b6aa5b3784f92f5f4f74550da0f4a61921d1fe18e7b0
233+
checksum: e5b977ffab9bcff3a3d3ef9754c43424b825a47865cff391483674c4d837f579687d4ff5c8243acf3ca7a0d7d8f042dec1a1ed4659f7898ac6cc3851dfb02b40
234234
languageName: node
235235
linkType: hard
236236

0 commit comments

Comments
 (0)