Skip to content

feat: straightforward client init with apiKey and appId #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions algolia-typescript-template/api-all.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ export class HttpError extends Error {

export { RequestFile } from '../model/models';

export class searchClient extends SearchApi{}

export const APIS = [{{#apis}}{{#operations}}{{ classname }}{{/operations}}{{^-last}}, {{/-last}}{{/apis}}];
{{/apiInfo}}
29 changes: 8 additions & 21 deletions algolia-typescript-template/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,14 @@ export class {{classname}} {

protected interceptors: Interceptor[] = [];

constructor(basePath?: string);
{{#authMethods}}
{{#isBasicBasic}}
constructor(username: string, password: string, basePath?: string);
{{/isBasicBasic}}
{{/authMethods}}
constructor(basePathOrUsername: string, password?: string, basePath?: string) {
if (password) {
{{#authMethods}}
{{#isBasicBasic}}
this.username = basePathOrUsername;
this.password = password
{{/isBasicBasic}}
{{/authMethods}}
if (basePath) {
this.basePath = basePath;
}
} else {
if (basePathOrUsername) {
this.basePath = basePathOrUsername
}
constructor(appId: string, apiKey: string, basePath?: string) {

this.setApiKey(SearchApiApiKeys.appId, appId);
this.setApiKey(SearchApiApiKeys.apiKey, apiKey);
this.basePath = 'https://' + appId + '-1.algolianet.com';

if (basePath) {
this.basePath = basePath;
}
}

Expand Down
9 changes: 6 additions & 3 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { SearchApi } from 'algoliasearch-client-javascript';
import { searchClient } from 'algoliasearch-client-javascript';

const client = new SearchApi();
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);

async function testClient() {
// test openapi gen
try {
const res = await client.multipleQueries('R2IYF7ETH7', 'e1e920e59f457ec70473486171c1d3b6', {
const res = await client.multipleQueries({
requests: [
{
indexName: 'docsearch',
Expand Down
2 changes: 0 additions & 2 deletions openapi_spec/paths/indexes/batch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ post:
operationId: batch
summary: Performs multiple write operations in a single API call
parameters:
- $ref: '../../parameters.yml#/AppId'
- $ref: '../../parameters.yml#/ApiKey'
- $ref: '../../parameters.yml#/IndexName'
requestBody:
required: true
Expand Down
3 changes: 0 additions & 3 deletions openapi_spec/paths/indexes/multipleQueries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ post:
- search
operationId: multipleQueries
summary: Get search results for the given requests.
parameters:
- $ref: '../../parameters.yml#/AppId'
- $ref: '../../parameters.yml#/ApiKey'
requestBody:
required: true
content:
Expand Down
2 changes: 0 additions & 2 deletions openapi_spec/paths/indexes/saveObject.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ post:
summary: Save object
description: Add an object to the index, automatically assigning it an object ID
parameters:
- $ref: '../../parameters.yml#/AppId'
- $ref: '../../parameters.yml#/ApiKey'
- $ref: '../../parameters.yml#/IndexName'
requestBody:
required: true
Expand Down
2 changes: 0 additions & 2 deletions openapi_spec/paths/indexes/search.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ post:
operationId: search
summary: Get search results
parameters:
- $ref: '../../parameters.yml#/AppId'
- $ref: '../../parameters.yml#/ApiKey'
- $ref: '../../parameters.yml#/IndexName'
requestBody:
$ref: '../../schemas/SearchParams.yml'
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
2 changes: 2 additions & 0 deletions output/client-search/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export class HttpError extends Error {

export { RequestFile } from '../model/models';

export class searchClient extends SearchApi {}

export const APIS = [SearchApi];
133 changes: 44 additions & 89 deletions 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,20 +30,19 @@ 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[] = [];

constructor(basePath?: string);
constructor(basePathOrUsername: string, password?: string, basePath?: string) {
if (password) {
if (basePath) {
this.basePath = basePath;
}
} else {
if (basePathOrUsername) {
this.basePath = basePathOrUsername;
}
constructor(appId: string, apiKey: string, basePath?: string) {
this.setApiKey(SearchApiApiKeys.appId, appId);
this.setApiKey(SearchApiApiKeys.apiKey, apiKey);
this.basePath = 'https://' + appId + '-1.algolianet.com';

if (basePath) {
this.basePath = basePath;
}
}

Expand Down Expand Up @@ -78,14 +81,10 @@ export class SearchApi {
/**
*
* @summary Performs multiple write operations in a single API call
* @param xAlgoliaApplicationId Algolia appID
* @param xAlgoliaAPIKey Algolia API key
* @param indexName The index in which to perform the request
* @param batchObject
*/
public async batch(
xAlgoliaApplicationId: string,
xAlgoliaAPIKey: string,
indexName: string,
batchObject: BatchObject,
options: { headers: { [name: string]: string } } = { headers: {} }
Expand All @@ -107,20 +106,6 @@ export class SearchApi {
}
let localVarFormParams: any = {};

// verify required parameter 'xAlgoliaApplicationId' is not null or undefined
if (xAlgoliaApplicationId === null || xAlgoliaApplicationId === undefined) {
throw new Error(
'Required parameter xAlgoliaApplicationId was null or undefined when calling batch.'
);
}

// verify required parameter 'xAlgoliaAPIKey' is not null or undefined
if (xAlgoliaAPIKey === null || xAlgoliaAPIKey === undefined) {
throw new Error(
'Required parameter xAlgoliaAPIKey was null or undefined when calling batch.'
);
}

// verify required parameter 'indexName' is not null or undefined
if (indexName === null || indexName === undefined) {
throw new Error('Required parameter indexName was null or undefined when calling batch.');
Expand All @@ -131,14 +116,6 @@ export class SearchApi {
throw new Error('Required parameter batchObject was null or undefined when calling batch.');
}

localVarHeaderParams['X-Algolia-Application-Id'] = ObjectSerializer.serialize(
xAlgoliaApplicationId,
'string'
);
localVarHeaderParams['X-Algolia-API-Key'] = ObjectSerializer.serialize(
xAlgoliaAPIKey,
'string'
);
(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;
Expand All @@ -154,6 +131,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 @@ -192,13 +179,9 @@ export class SearchApi {
/**
*
* @summary Get search results for the given requests.
* @param xAlgoliaApplicationId Algolia appID
* @param xAlgoliaAPIKey Algolia API key
* @param multipleQueriesObject
*/
public async multipleQueries(
xAlgoliaApplicationId: string,
xAlgoliaAPIKey: string,
multipleQueriesObject: MultipleQueriesObject,
options: { headers: { [name: string]: string } } = { headers: {} }
): Promise<{ response: http.IncomingMessage; body: MultipleQueriesResponse }> {
Expand All @@ -214,35 +197,13 @@ export class SearchApi {
}
let localVarFormParams: any = {};

// verify required parameter 'xAlgoliaApplicationId' is not null or undefined
if (xAlgoliaApplicationId === null || xAlgoliaApplicationId === undefined) {
throw new Error(
'Required parameter xAlgoliaApplicationId was null or undefined when calling multipleQueries.'
);
}

// verify required parameter 'xAlgoliaAPIKey' is not null or undefined
if (xAlgoliaAPIKey === null || xAlgoliaAPIKey === undefined) {
throw new Error(
'Required parameter xAlgoliaAPIKey was null or undefined when calling multipleQueries.'
);
}

// verify required parameter 'multipleQueriesObject' is not null or undefined
if (multipleQueriesObject === null || multipleQueriesObject === undefined) {
throw new Error(
'Required parameter multipleQueriesObject was null or undefined when calling multipleQueries.'
);
}

localVarHeaderParams['X-Algolia-Application-Id'] = ObjectSerializer.serialize(
xAlgoliaApplicationId,
'string'
);
localVarHeaderParams['X-Algolia-API-Key'] = ObjectSerializer.serialize(
xAlgoliaAPIKey,
'string'
);
(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;
Expand All @@ -258,6 +219,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 @@ -296,14 +267,10 @@ export class SearchApi {
/**
* Add an object to the index, automatically assigning it an object ID
* @summary Save object
* @param xAlgoliaApplicationId Algolia appID
* @param xAlgoliaAPIKey Algolia API key
* @param indexName The index in which to perform the request
* @param requestBody
*/
public async saveObject(
xAlgoliaApplicationId: string,
xAlgoliaAPIKey: string,
indexName: string,
requestBody: { [key: string]: object },
options: { headers: { [name: string]: string } } = { headers: {} }
Expand All @@ -325,20 +292,6 @@ export class SearchApi {
}
let localVarFormParams: any = {};

// verify required parameter 'xAlgoliaApplicationId' is not null or undefined
if (xAlgoliaApplicationId === null || xAlgoliaApplicationId === undefined) {
throw new Error(
'Required parameter xAlgoliaApplicationId was null or undefined when calling saveObject.'
);
}

// verify required parameter 'xAlgoliaAPIKey' is not null or undefined
if (xAlgoliaAPIKey === null || xAlgoliaAPIKey === undefined) {
throw new Error(
'Required parameter xAlgoliaAPIKey was null or undefined when calling saveObject.'
);
}

// verify required parameter 'indexName' is not null or undefined
if (indexName === null || indexName === undefined) {
throw new Error(
Expand All @@ -353,14 +306,6 @@ export class SearchApi {
);
}

localVarHeaderParams['X-Algolia-Application-Id'] = ObjectSerializer.serialize(
xAlgoliaApplicationId,
'string'
);
localVarHeaderParams['X-Algolia-API-Key'] = ObjectSerializer.serialize(
xAlgoliaAPIKey,
'string'
);
(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;
Expand All @@ -376,6 +321,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
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=9f2500&locator=%40algolia%2Fautomation-javascript-client%40workspace%3A."
resolution: "algoliasearch-client-javascript@file:output/#output/::hash=342434&locator=%40algolia%2Fautomation-javascript-client%40workspace%3A."
dependencies:
"@types/request": ^2.48.7
request: ^2.81.0
checksum: 4a413d1bbbeeaf41a9e4f75a2e9f8be6b827439a80099c57ae0d2b2387da9563ff416133c028989fe17eaa95025e248f8f586d78a08f903819eacae536f73b0b
checksum: 79ae2881a4d93698d4600f7710ea07512df105e9faf61982acc2cc8327d1666fe39f7e83c4a4d471b1876c321b932850a49923a49d22d474bacdf16dfaa81e71
languageName: node
linkType: hard

Expand Down