Skip to content

Commit

Permalink
feat: use security scheme to pass appId and appKey
Browse files Browse the repository at this point in the history
  • Loading branch information
damcou committed Nov 15, 2021
1 parent d8d2c9d commit ec4bf6f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { searchClient } from './searchClient';

const appId = process.env.ALGOLIA_APPLICATION_ID_1 || '**** APP_ID *****';
const apiKey = process.env.ALGOLIA_ADMIN_KEY_1 || '**** API_KEY *****';
const appId = process.env.ALGOLIA_APPLICATION_ID_1 || '**** APP_ID *****';
const apiKey = process.env.ALGOLIA_ADMIN_KEY_1 || '**** API_KEY *****';
// Init client with appId and apiKey
const client = new searchClient(appId, apiKey);

Expand Down
13 changes: 13 additions & 0 deletions openapi_spec/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ servers:
variables:
appId:
default: test
components:
securitySchemes:
appId:
type: apiKey
in: header
name: X-Algolia-Application-Id
apiKey:
type: apiKey
in: header
name: X-Algolia-API-Key
security:
- appId: []
apiKey: []
paths:
# We can add this one later, as it requires the init method
# /1/indexes/{indexName}/query:
Expand Down
38 changes: 37 additions & 1 deletion output/client-search/searchApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { MultipleQueriesResponse } from '../model/multipleQueriesResponse';
import { SaveObjectResponse } from '../model/saveObjectResponse';

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

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

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

export enum SearchApiApiKeys {}
export enum SearchApiApiKeys {
apiKey,
appId,
}

export class SearchApi {
protected _basePath = defaultBasePath;
Expand All @@ -26,6 +30,8 @@ export class SearchApi {

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

protected interceptors: Interceptor[] = [];
Expand Down Expand Up @@ -128,6 +134,16 @@ export class SearchApi {
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(localVarRequestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(localVarRequestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(localVarRequestOptions)
);
Expand Down Expand Up @@ -206,6 +222,16 @@ export class SearchApi {
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(localVarRequestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(localVarRequestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(localVarRequestOptions)
);
Expand Down Expand Up @@ -298,6 +324,16 @@ export class SearchApi {
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(localVarRequestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(localVarRequestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(localVarRequestOptions)
);
Expand Down
6 changes: 3 additions & 3 deletions searchClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { SearchApi } from 'algoliasearch-client-javascript';
import { SearchApi, SearchApiApiKeys } from 'algoliasearch-client-javascript';

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

this.defaultHeaders['X-Algolia-Application-Id'] = appId;
this.defaultHeaders['X-Algolia-API-Key'] = apiKey;
this.setApiKey(SearchApiApiKeys.appId, appId);
this.setApiKey(SearchApiApiKeys.apiKey, apiKey);
this.basePath = 'https://' + appId + '-1.algolianet.com';
}
}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ __metadata:

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

Expand Down

0 comments on commit ec4bf6f

Please sign in to comment.