Skip to content

Commit d8d2c9d

Browse files
committed
feat: straightforward client init with apiKey and appId
1 parent 238c677 commit d8d2c9d

File tree

9 files changed

+20
-93
lines changed

9 files changed

+20
-93
lines changed

app.ts

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

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

58
async function testClient() {
69
// test openapi gen
710
try {
8-
const res = await client.multipleQueries('R2IYF7ETH7', 'e1e920e59f457ec70473486171c1d3b6', {
11+
const res = await client.multipleQueries({
912
requests: [
1013
{
1114
indexName: 'docsearch',

openapi_spec/paths/indexes/batch.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ post:
44
operationId: batch
55
summary: Performs multiple write operations in a single API call
66
parameters:
7-
- $ref: '../../parameters.yml#/AppId'
8-
- $ref: '../../parameters.yml#/ApiKey'
97
- $ref: '../../parameters.yml#/IndexName'
108
requestBody:
119
required: true

openapi_spec/paths/indexes/multipleQueries.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ post:
33
- search
44
operationId: multipleQueries
55
summary: Get search results for the given requests.
6-
parameters:
7-
- $ref: '../../parameters.yml#/AppId'
8-
- $ref: '../../parameters.yml#/ApiKey'
96
requestBody:
107
required: true
118
content:

openapi_spec/paths/indexes/saveObject.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ post:
55
summary: Save object
66
description: Add an object to the index, automatically assigning it an object ID
77
parameters:
8-
- $ref: '../../parameters.yml#/AppId'
9-
- $ref: '../../parameters.yml#/ApiKey'
108
- $ref: '../../parameters.yml#/IndexName'
119
requestBody:
1210
required: true

openapi_spec/paths/indexes/search.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ post:
44
operationId: search
55
summary: Get search results
66
parameters:
7-
- $ref: '../../parameters.yml#/AppId'
8-
- $ref: '../../parameters.yml#/ApiKey'
97
- $ref: '../../parameters.yml#/IndexName'
108
requestBody:
119
$ref: '../../schemas/SearchParams.yml'

output/client-search/searchApi.ts

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,10 @@ export class SearchApi {
7878
/**
7979
*
8080
* @summary Performs multiple write operations in a single API call
81-
* @param xAlgoliaApplicationId Algolia appID
82-
* @param xAlgoliaAPIKey Algolia API key
8381
* @param indexName The index in which to perform the request
8482
* @param batchObject
8583
*/
8684
public async batch(
87-
xAlgoliaApplicationId: string,
88-
xAlgoliaAPIKey: string,
8985
indexName: string,
9086
batchObject: BatchObject,
9187
options: { headers: { [name: string]: string } } = { headers: {} }
@@ -107,20 +103,6 @@ export class SearchApi {
107103
}
108104
let localVarFormParams: any = {};
109105

110-
// verify required parameter 'xAlgoliaApplicationId' is not null or undefined
111-
if (xAlgoliaApplicationId === null || xAlgoliaApplicationId === undefined) {
112-
throw new Error(
113-
'Required parameter xAlgoliaApplicationId was null or undefined when calling batch.'
114-
);
115-
}
116-
117-
// verify required parameter 'xAlgoliaAPIKey' is not null or undefined
118-
if (xAlgoliaAPIKey === null || xAlgoliaAPIKey === undefined) {
119-
throw new Error(
120-
'Required parameter xAlgoliaAPIKey was null or undefined when calling batch.'
121-
);
122-
}
123-
124106
// verify required parameter 'indexName' is not null or undefined
125107
if (indexName === null || indexName === undefined) {
126108
throw new Error('Required parameter indexName was null or undefined when calling batch.');
@@ -131,14 +113,6 @@ export class SearchApi {
131113
throw new Error('Required parameter batchObject was null or undefined when calling batch.');
132114
}
133115

134-
localVarHeaderParams['X-Algolia-Application-Id'] = ObjectSerializer.serialize(
135-
xAlgoliaApplicationId,
136-
'string'
137-
);
138-
localVarHeaderParams['X-Algolia-API-Key'] = ObjectSerializer.serialize(
139-
xAlgoliaAPIKey,
140-
'string'
141-
);
142116
(<any>Object).assign(localVarHeaderParams, options.headers);
143117

144118
let localVarUseFormData = false;
@@ -192,13 +166,9 @@ export class SearchApi {
192166
/**
193167
*
194168
* @summary Get search results for the given requests.
195-
* @param xAlgoliaApplicationId Algolia appID
196-
* @param xAlgoliaAPIKey Algolia API key
197169
* @param multipleQueriesObject
198170
*/
199171
public async multipleQueries(
200-
xAlgoliaApplicationId: string,
201-
xAlgoliaAPIKey: string,
202172
multipleQueriesObject: MultipleQueriesObject,
203173
options: { headers: { [name: string]: string } } = { headers: {} }
204174
): Promise<{ response: http.IncomingMessage; body: MultipleQueriesResponse }> {
@@ -214,35 +184,13 @@ export class SearchApi {
214184
}
215185
let localVarFormParams: any = {};
216186

217-
// verify required parameter 'xAlgoliaApplicationId' is not null or undefined
218-
if (xAlgoliaApplicationId === null || xAlgoliaApplicationId === undefined) {
219-
throw new Error(
220-
'Required parameter xAlgoliaApplicationId was null or undefined when calling multipleQueries.'
221-
);
222-
}
223-
224-
// verify required parameter 'xAlgoliaAPIKey' is not null or undefined
225-
if (xAlgoliaAPIKey === null || xAlgoliaAPIKey === undefined) {
226-
throw new Error(
227-
'Required parameter xAlgoliaAPIKey was null or undefined when calling multipleQueries.'
228-
);
229-
}
230-
231187
// verify required parameter 'multipleQueriesObject' is not null or undefined
232188
if (multipleQueriesObject === null || multipleQueriesObject === undefined) {
233189
throw new Error(
234190
'Required parameter multipleQueriesObject was null or undefined when calling multipleQueries.'
235191
);
236192
}
237193

238-
localVarHeaderParams['X-Algolia-Application-Id'] = ObjectSerializer.serialize(
239-
xAlgoliaApplicationId,
240-
'string'
241-
);
242-
localVarHeaderParams['X-Algolia-API-Key'] = ObjectSerializer.serialize(
243-
xAlgoliaAPIKey,
244-
'string'
245-
);
246194
(<any>Object).assign(localVarHeaderParams, options.headers);
247195

248196
let localVarUseFormData = false;
@@ -296,14 +244,10 @@ export class SearchApi {
296244
/**
297245
* Add an object to the index, automatically assigning it an object ID
298246
* @summary Save object
299-
* @param xAlgoliaApplicationId Algolia appID
300-
* @param xAlgoliaAPIKey Algolia API key
301247
* @param indexName The index in which to perform the request
302248
* @param requestBody
303249
*/
304250
public async saveObject(
305-
xAlgoliaApplicationId: string,
306-
xAlgoliaAPIKey: string,
307251
indexName: string,
308252
requestBody: { [key: string]: object },
309253
options: { headers: { [name: string]: string } } = { headers: {} }
@@ -325,20 +269,6 @@ export class SearchApi {
325269
}
326270
let localVarFormParams: any = {};
327271

328-
// verify required parameter 'xAlgoliaApplicationId' is not null or undefined
329-
if (xAlgoliaApplicationId === null || xAlgoliaApplicationId === undefined) {
330-
throw new Error(
331-
'Required parameter xAlgoliaApplicationId was null or undefined when calling saveObject.'
332-
);
333-
}
334-
335-
// verify required parameter 'xAlgoliaAPIKey' is not null or undefined
336-
if (xAlgoliaAPIKey === null || xAlgoliaAPIKey === undefined) {
337-
throw new Error(
338-
'Required parameter xAlgoliaAPIKey was null or undefined when calling saveObject.'
339-
);
340-
}
341-
342272
// verify required parameter 'indexName' is not null or undefined
343273
if (indexName === null || indexName === undefined) {
344274
throw new Error(
@@ -353,14 +283,6 @@ export class SearchApi {
353283
);
354284
}
355285

356-
localVarHeaderParams['X-Algolia-Application-Id'] = ObjectSerializer.serialize(
357-
xAlgoliaApplicationId,
358-
'string'
359-
);
360-
localVarHeaderParams['X-Algolia-API-Key'] = ObjectSerializer.serialize(
361-
xAlgoliaAPIKey,
362-
'string'
363-
);
364286
(<any>Object).assign(localVarHeaderParams, options.headers);
365287

366288
let localVarUseFormData = false;

searchClient.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { SearchApi } from 'algoliasearch-client-javascript';
2+
3+
export class searchClient extends SearchApi {
4+
public constructor(appId: string, apiKey: string) {
5+
super();
6+
7+
this.defaultHeaders['X-Algolia-Application-Id'] = appId;
8+
this.defaultHeaders['X-Algolia-API-Key'] = apiKey;
9+
this.basePath = 'https://' + appId + '-1.algolianet.com';
10+
}
11+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
"outDir": "dist",
1717
"typeRoots": ["node_modules/@types"]
1818
},
19-
"include": ["app.ts"]
19+
"include": ["searchClient.ts", "app.ts"]
2020
}

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=9f2500&locator=%40algolia%2Fautomation-javascript-client%40workspace%3A."
229+
resolution: "algoliasearch-client-javascript@file:output/#output/::hash=5e44d1&locator=%40algolia%2Fautomation-javascript-client%40workspace%3A."
230230
dependencies:
231231
"@types/request": ^2.48.7
232232
request: ^2.81.0
233-
checksum: 4a413d1bbbeeaf41a9e4f75a2e9f8be6b827439a80099c57ae0d2b2387da9563ff416133c028989fe17eaa95025e248f8f586d78a08f903819eacae536f73b0b
233+
checksum: 324b24f6828b13f4d863425274de41987bcd006e3950379d01fb822308ef0cab02ca07e21297697fccd1b6aa5b3784f92f5f4f74550da0f4a61921d1fe18e7b0
234234
languageName: node
235235
linkType: hard
236236

0 commit comments

Comments
 (0)