Skip to content

fix: simplify model with types and simplify api #15

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 1 commit into from
Nov 25, 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
203 changes: 29 additions & 174 deletions clients/algoliasearch-client-javascript/client-search/searchApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import { SaveObjectResponse } from '../model/saveObjectResponse';
import { SearchParams } from '../model/searchParams';
import { SearchParamsString } from '../model/searchParamsString';
import { SearchResponse } from '../model/searchResponse';

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

export enum SearchApiApiKeys {
apiKey,
Expand All @@ -25,13 +23,10 @@ export class SearchApi {
private transporter: Transporter;

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(appId: string, apiKey: string, requester?: Requester) {
this.setApiKey(SearchApiApiKeys.appId, appId);
this.setApiKey(SearchApiApiKeys.apiKey, apiKey);
Expand Down Expand Up @@ -61,16 +56,21 @@ export class SearchApi {
});
}

public setDefaultAuthentication(auth: Authentication) {
this.authentications.default = auth;
}

public setApiKey(key: SearchApiApiKeys, value: string) {
(this.authentications as any)[SearchApiApiKeys[key]].apiKey = value;
this.authentications[SearchApiApiKeys[key]].apiKey = value;
}

public addInterceptor(interceptor: Interceptor) {
this.interceptors.push(interceptor);
private async sendRequest<TResponse>(
request: Request,
requestOptions: RequestOptions
): Promise<TResponse> {
if (this.authentications.apiKey.apiKey) {
this.authentications.apiKey.applyToRequest(requestOptions);
}
if (this.authentications.appId.apiKey) {
this.authentications.appId.applyToRequest(requestOptions);
}
return this.transporter.request(request, requestOptions);
}

/**
Expand All @@ -79,137 +79,65 @@ export class SearchApi {
* @param indexName The index in which to perform the request
* @param batchObject
*/
public async batch(
indexName: string,
batchObject: BatchObject,
options: { headers: { [name: string]: string } } = { headers: {} }
): Promise<BatchResponse> {
public async batch(indexName: string, batchObject: BatchObject): Promise<BatchResponse> {
const path = '/1/indexes/{indexName}/batch'.replace(
'{' + 'indexName' + '}',
encodeURIComponent(String(indexName))
);
let headers: Headers = {};
let headers: Headers = { Accept: 'application/json' };
let queryParameters: Record<string, string> = {};
const produces = ['application/json'];
// give precedence to 'application/json'
if (produces.indexOf('application/json') >= 0) {
headers.Accept = 'application/json';
} else {
headers.Accept = produces.join(',');
}
let formParams: Record<string, string> = {};

// 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.');
}

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

headers = { ...headers, ...options.headers };

const request: Request = {
method: 'POST',
path,
data: ObjectSerializer.serialize(batchObject, 'BatchObject'),
data: batchObject,
};

const requestOptions: RequestOptions = {
headers,
queryParameters,
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(requestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(requestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(requestOptions)
);

let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions));
}

await interceptorPromise;

return this.transporter.request(request, requestOptions);
return this.sendRequest(request, requestOptions);
}
/**
*
* @summary Get search results for the given requests.
* @param multipleQueriesObject
*/
public async multipleQueries(
multipleQueriesObject: MultipleQueriesObject,
options: { headers: { [name: string]: string } } = { headers: {} }
multipleQueriesObject: MultipleQueriesObject
): Promise<MultipleQueriesResponse> {
const path = '/1/indexes/*/queries';
let headers: Headers = {};
let headers: Headers = { Accept: 'application/json' };
let queryParameters: Record<string, string> = {};
const produces = ['application/json'];
// give precedence to 'application/json'
if (produces.indexOf('application/json') >= 0) {
headers.Accept = 'application/json';
} else {
headers.Accept = produces.join(',');
}
let formParams: Record<string, string> = {};

// 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.'
);
}

headers = { ...headers, ...options.headers };

const request: Request = {
method: 'POST',
path,
data: ObjectSerializer.serialize(multipleQueriesObject, 'MultipleQueriesObject'),
data: multipleQueriesObject,
};

const requestOptions: RequestOptions = {
headers,
queryParameters,
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(requestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(requestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(requestOptions)
);

let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions));
}

await interceptorPromise;

return this.transporter.request(request, requestOptions);
return this.sendRequest(request, requestOptions);
}
/**
* Add an object to the index, automatically assigning it an object ID
Expand All @@ -219,74 +147,39 @@ export class SearchApi {
*/
public async saveObject(
indexName: string,
requestBody: { [key: string]: object },
options: { headers: { [name: string]: string } } = { headers: {} }
requestBody: { [key: string]: object }
): Promise<SaveObjectResponse> {
const path = '/1/indexes/{indexName}'.replace(
'{' + 'indexName' + '}',
encodeURIComponent(String(indexName))
);
let headers: Headers = {};
let headers: Headers = { Accept: 'application/json' };
let queryParameters: Record<string, string> = {};
const produces = ['application/json'];
// give precedence to 'application/json'
if (produces.indexOf('application/json') >= 0) {
headers.Accept = 'application/json';
} else {
headers.Accept = produces.join(',');
}
let formParams: Record<string, string> = {};

// 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 saveObject.'
);
}

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

headers = { ...headers, ...options.headers };

const request: Request = {
method: 'POST',
path,
data: ObjectSerializer.serialize(requestBody, '{ [key: string]: object; }'),
data: requestBody,
};

const requestOptions: RequestOptions = {
headers,
queryParameters,
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(requestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(requestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(requestOptions)
);

let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions));
}

await interceptorPromise;

return this.transporter.request(request, requestOptions);
return this.sendRequest(request, requestOptions);
}
/**
*
Expand All @@ -296,74 +189,36 @@ export class SearchApi {
*/
public async search(
indexName: string,
searchParamsSearchParamsString: SearchParams | SearchParamsString,
options: { headers: { [name: string]: string } } = { headers: {} }
searchParamsSearchParamsString: SearchParams | SearchParamsString
): Promise<SearchResponse> {
const path = '/1/indexes/{indexName}/query'.replace(
'{' + 'indexName' + '}',
encodeURIComponent(String(indexName))
);
let headers: Headers = {};
let headers: Headers = { Accept: 'application/json' };
let queryParameters: Record<string, string> = {};
const produces = ['application/json'];
// give precedence to 'application/json'
if (produces.indexOf('application/json') >= 0) {
headers.Accept = 'application/json';
} else {
headers.Accept = produces.join(',');
}
let formParams: Record<string, string> = {};

// 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 search.');
}

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

headers = { ...headers, ...options.headers };

const request: Request = {
method: 'POST',
path,
data: ObjectSerializer.serialize(
searchParamsSearchParamsString,
'SearchParams | SearchParamsString'
),
data: searchParamsSearchParamsString,
};

const requestOptions: RequestOptions = {
headers,
queryParameters,
};

let authenticationPromise = Promise.resolve();
if (this.authentications.apiKey.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.apiKey.applyToRequest(requestOptions)
);
}
if (this.authentications.appId.apiKey) {
authenticationPromise = authenticationPromise.then(() =>
this.authentications.appId.applyToRequest(requestOptions)
);
}
authenticationPromise = authenticationPromise.then(() =>
this.authentications.default.applyToRequest(requestOptions)
);

let interceptorPromise = authenticationPromise;
for (const interceptor of this.interceptors) {
interceptorPromise = interceptorPromise.then(() => interceptor(requestOptions));
}

await interceptorPromise;

return this.transporter.request(request, requestOptions);
return this.sendRequest(request, requestOptions);
}
}
20 changes: 3 additions & 17 deletions clients/algoliasearch-client-javascript/model/batchObject.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import { Operation } from './operation';

export class BatchObject {
'requests'?: Array<Operation>;

static discriminator: string | undefined = undefined;

static attributeTypeMap: Array<{ name: string; baseName: string; type: string }> = [
{
name: 'requests',
baseName: 'requests',
type: 'Array<Operation>',
},
];

static getAttributeTypeMap() {
return BatchObject.attributeTypeMap;
}
}
export type BatchObject = {
requests?: Array<Operation>;
};
Loading