Skip to content

Commit

Permalink
fix(javascript): type definition and template cleanup (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#3656

Co-authored-by: algolia-bot <[email protected]>
Co-authored-by: Clément Vannicatte <[email protected]>
  • Loading branch information
algolia-bot and shortcuts committed Sep 6, 2024
1 parent d742815 commit f05c7d9
Show file tree
Hide file tree
Showing 37 changed files with 729 additions and 839 deletions.
111 changes: 43 additions & 68 deletions packages/algoliasearch/builds/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,100 +4,75 @@ import type { AbtestingClient, Region as AbtestingRegion } from '@algolia/client
import { abtestingClient } from '@algolia/client-abtesting';
import type { AnalyticsClient, Region as AnalyticsRegion } from '@algolia/client-analytics';
import { analyticsClient } from '@algolia/client-analytics';
import {
DEFAULT_CONNECT_TIMEOUT_BROWSER,
DEFAULT_READ_TIMEOUT_BROWSER,
DEFAULT_WRITE_TIMEOUT_BROWSER,
createBrowserLocalStorageCache,
createFallbackableCache,
createMemoryCache,
} from '@algolia/client-common';
import type { ClientOptions } from '@algolia/client-common';
import type { PersonalizationClient, Region as PersonalizationRegion } from '@algolia/client-personalization';
import { personalizationClient } from '@algolia/client-personalization';
import type { SearchClient } from '@algolia/client-search';
import { searchClient } from '@algolia/client-search';
import type { RecommendClient } from '@algolia/recommend';
import { recommendClient } from '@algolia/recommend';
import { createXhrRequester } from '@algolia/requester-browser-xhr';

import type { InitClientOptions, InitClientRegion } from './models';
import { apiClientVersion } from './models';

export * from './models';

/**
* The client type.
*/
export type Algoliasearch = ReturnType<typeof algoliasearch>;
export type Algoliasearch = SearchClient & {
initRecommend: (initOptions: InitClientOptions) => RecommendClient;
initAnalytics: (initOptions: InitClientOptions & InitClientRegion<AnalyticsRegion>) => AnalyticsClient;
initAbtesting: (initOptions: InitClientOptions & InitClientRegion<AbtestingRegion>) => AbtestingClient;
initPersonalization: (
initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>,
) => PersonalizationClient;
};

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function algoliasearch(appId: string, apiKey: string, options?: ClientOptions) {
export function algoliasearch(appId: string, apiKey: string, options?: ClientOptions): Algoliasearch {
if (!appId || typeof appId !== 'string') {
throw new Error('`appId` is missing.');
}

if (!apiKey || typeof apiKey !== 'string') {
throw new Error('`apiKey` is missing.');
}
function initRecommend(initOptions: InitClientOptions = {}): RecommendClient {
return recommendClient(initOptions.appId || appId, initOptions.apiKey || apiKey, initOptions.options);
}

function initAnalytics(initOptions: InitClientOptions & InitClientRegion<AnalyticsRegion> = {}): AnalyticsClient {
return analyticsClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
}

function initAbtesting(initOptions: InitClientOptions & InitClientRegion<AbtestingRegion> = {}): AbtestingClient {
return abtestingClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
}

function initPersonalization(
initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>,
): PersonalizationClient {
return personalizationClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
}

return {
...searchClient(appId, apiKey, {
timeouts: {
connect: DEFAULT_CONNECT_TIMEOUT_BROWSER,
read: DEFAULT_READ_TIMEOUT_BROWSER,
write: DEFAULT_WRITE_TIMEOUT_BROWSER,
},
requester: createXhrRequester(),
algoliaAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
responsesCache: createMemoryCache(),
requestsCache: createMemoryCache({ serializable: false }),
hostsCache: createFallbackableCache({
caches: [createBrowserLocalStorageCache({ key: `${apiClientVersion}-${appId}` }), createMemoryCache()],
}),
...options,
}),
...searchClient(appId, apiKey, options),
/**
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
*/
get _ua(): string {
return this.transporter.algoliaAgent.value;
},
initAbtesting,
initAnalytics,
initPersonalization,
initRecommend,
initRecommend: (initOptions: InitClientOptions = {}): RecommendClient => {
return recommendClient(initOptions.appId || appId, initOptions.apiKey || apiKey, initOptions.options);
},

initAnalytics: (initOptions: InitClientOptions & InitClientRegion<AnalyticsRegion> = {}): AnalyticsClient => {
return analyticsClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
},

initAbtesting: (initOptions: InitClientOptions & InitClientRegion<AbtestingRegion> = {}): AbtestingClient => {
return abtestingClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
},

initPersonalization: (
initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>,
): PersonalizationClient => {
return personalizationClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
},
};
}
6 changes: 3 additions & 3 deletions packages/algoliasearch/builds/models.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.

import type { Region as ABTestingRegion } from '@algolia/client-abtesting';
import type { Region as AbTestingRegion } from '@algolia/client-abtesting';
import type { Region as AnalyticsRegion } from '@algolia/client-analytics';
import type { ClientOptions } from '@algolia/client-common';
import type {
Expand Down Expand Up @@ -79,7 +79,7 @@ import type {
} from '@algolia/client-search';
import { apiClientVersion } from '@algolia/client-search';

type Region = ABTestingRegion | AnalyticsRegion;
type Region = AbTestingRegion | AnalyticsRegion;

export * from '@algolia/client-search';
export * from '@algolia/recommend';
Expand Down Expand Up @@ -161,7 +161,7 @@ export {
TypoToleranceEnum,
Value,
AnalyticsRegion,
ABTestingRegion,
AbTestingRegion,
Region,
apiClientVersion,
};
Expand Down
161 changes: 39 additions & 122 deletions packages/algoliasearch/builds/node.ts
Original file line number Diff line number Diff line change
@@ -1,161 +1,78 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.

import { createHmac } from 'node:crypto';

import type { AbtestingClient, Region as AbtestingRegion } from '@algolia/client-abtesting';
import { abtestingClient } from '@algolia/client-abtesting';
import type { AnalyticsClient, Region as AnalyticsRegion } from '@algolia/client-analytics';
import { analyticsClient } from '@algolia/client-analytics';
import {
DEFAULT_CONNECT_TIMEOUT_NODE,
DEFAULT_READ_TIMEOUT_NODE,
DEFAULT_WRITE_TIMEOUT_NODE,
createMemoryCache,
createNullCache,
serializeQueryParameters,
} from '@algolia/client-common';
import type { ClientOptions } from '@algolia/client-common';
import type { PersonalizationClient, Region as PersonalizationRegion } from '@algolia/client-personalization';
import { personalizationClient } from '@algolia/client-personalization';
import type { SearchClient } from '@algolia/client-search';
import { searchClient } from '@algolia/client-search';
import type { RecommendClient } from '@algolia/recommend';
import { recommendClient } from '@algolia/recommend';
import { createHttpRequester } from '@algolia/requester-node-http';

import type {
InitClientOptions,
InitClientRegion,
GenerateSecuredApiKeyOptions,
GetSecuredApiKeyRemainingValidityOptions,
} from './models';
import type { InitClientOptions, InitClientRegion } from './models';

export * from './models';

/**
* The client type.
*/
export type Algoliasearch = ReturnType<typeof algoliasearch>;
export type Algoliasearch = SearchClient & {
initRecommend: (initOptions: InitClientOptions) => RecommendClient;
initAnalytics: (initOptions: InitClientOptions & InitClientRegion<AnalyticsRegion>) => AnalyticsClient;
initAbtesting: (initOptions: InitClientOptions & InitClientRegion<AbtestingRegion>) => AbtestingClient;
initPersonalization: (
initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>,
) => PersonalizationClient;
};

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function algoliasearch(appId: string, apiKey: string, options?: ClientOptions) {
export function algoliasearch(appId: string, apiKey: string, options?: ClientOptions): Algoliasearch {
if (!appId || typeof appId !== 'string') {
throw new Error('`appId` is missing.');
}

if (!apiKey || typeof apiKey !== 'string') {
throw new Error('`apiKey` is missing.');
}
function initRecommend(initOptions: InitClientOptions = {}): RecommendClient {
return recommendClient(initOptions.appId || appId, initOptions.apiKey || apiKey, initOptions.options);
}

function initAnalytics(initOptions: InitClientOptions & InitClientRegion<AnalyticsRegion> = {}): AnalyticsClient {
return analyticsClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
}

function initAbtesting(initOptions: InitClientOptions & InitClientRegion<AbtestingRegion> = {}): AbtestingClient {
return abtestingClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
}

function initPersonalization(
initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>,
): PersonalizationClient {
return personalizationClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
}

return {
...searchClient(appId, apiKey, {
timeouts: {
connect: DEFAULT_CONNECT_TIMEOUT_NODE,
read: DEFAULT_READ_TIMEOUT_NODE,
write: DEFAULT_WRITE_TIMEOUT_NODE,
},
requester: createHttpRequester(),
algoliaAgents: [{ segment: 'Node.js', version: process.versions.node }],
responsesCache: createNullCache(),
requestsCache: createNullCache(),
hostsCache: createMemoryCache(),
...options,
}),
...searchClient(appId, apiKey, options),
/**
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
*/
get _ua(): string {
return this.transporter.algoliaAgent.value;
},
initAbtesting,
initAnalytics,
initPersonalization,
initRecommend,
/**
* Helper: Generates a secured API key based on the given `parentApiKey` and given `restrictions`.
*
* @summary Helper: Generates a secured API key based on the given `parentApiKey` and given `restrictions`.
* @param generateSecuredApiKey - The `generateSecuredApiKey` object.
* @param generateSecuredApiKey.parentApiKey - The base API key from which to generate the new secured one.
* @param generateSecuredApiKey.restrictions - A set of properties defining the restrictions of the secured API key.
*/
generateSecuredApiKey({ parentApiKey, restrictions = {} }: GenerateSecuredApiKeyOptions): string {
let mergedRestrictions = restrictions;
if (restrictions.searchParams) {
// merge searchParams with the root restrictions
mergedRestrictions = {
...restrictions,
...restrictions.searchParams,
};

delete mergedRestrictions.searchParams;
}

mergedRestrictions = Object.keys(mergedRestrictions)
.sort()
.reduce(
(acc, key) => {
// eslint-disable-next-line no-param-reassign
acc[key] = (mergedRestrictions as any)[key];
return acc;
},
{} as Record<string, unknown>,
);

const queryParameters = serializeQueryParameters(mergedRestrictions);
return Buffer.from(
createHmac('sha256', parentApiKey).update(queryParameters).digest('hex') + queryParameters,
).toString('base64');
initRecommend: (initOptions: InitClientOptions = {}): RecommendClient => {
return recommendClient(initOptions.appId || appId, initOptions.apiKey || apiKey, initOptions.options);
},

/**
* Helper: Retrieves the remaining validity of the previous generated `securedApiKey`, the `ValidUntil` parameter must have been provided.
*
* @summary Helper: Retrieves the remaining validity of the previous generated `secured_api_key`, the `ValidUntil` parameter must have been provided.
* @param getSecuredApiKeyRemainingValidity - The `getSecuredApiKeyRemainingValidity` object.
* @param getSecuredApiKeyRemainingValidity.securedApiKey - The secured API key generated with the `generateSecuredApiKey` method.
*/
getSecuredApiKeyRemainingValidity({ securedApiKey }: GetSecuredApiKeyRemainingValidityOptions): number {
const decodedString = Buffer.from(securedApiKey, 'base64').toString('ascii');
const regex = /validUntil=(\d+)/;
const match = decodedString.match(regex);
initAnalytics: (initOptions: InitClientOptions & InitClientRegion<AnalyticsRegion> = {}): AnalyticsClient => {
return analyticsClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
},

if (match === null) {
throw new Error('validUntil not found in given secured api key.');
}
initAbtesting: (initOptions: InitClientOptions & InitClientRegion<AbtestingRegion> = {}): AbtestingClient => {
return abtestingClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
},

return parseInt(match[1], 10) - Math.round(new Date().getTime() / 1000);
initPersonalization: (
initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>,
): PersonalizationClient => {
return personalizationClient(
initOptions.appId || appId,
initOptions.apiKey || apiKey,
initOptions.region,
initOptions.options,
);
},
};
}
10 changes: 3 additions & 7 deletions packages/algoliasearch/lite/builds/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ import { createXhrRequester } from '@algolia/requester-browser-xhr';

import { createLiteClient, apiClientVersion } from '../src/liteClient';

export type LiteClient = ReturnType<typeof createLiteClient>;

export { apiClientVersion } from '../src/liteClient';
export * from '../model';

/**
* The client type.
*/
export type LiteClient = ReturnType<typeof liteClient>;

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function liteClient(appId: string, apiKey: string, options?: ClientOptions) {
export function liteClient(appId: string, apiKey: string, options?: ClientOptions): LiteClient {
if (!appId || typeof appId !== 'string') {
throw new Error('`appId` is missing.');
}
Expand Down
Loading

0 comments on commit f05c7d9

Please sign in to comment.