Skip to content

feat(clients): support recommend methods in algoliasearch #2834

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

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ private void setDefaultGeneratorOptions() {
additionalProperties.put("abtestingVersion", Helpers.getPackageJsonVersion("client-abtesting"));
additionalProperties.put("personalizationVersion", Helpers.getPackageJsonVersion("client-personalization"));
additionalProperties.put("searchVersion", Helpers.getPackageJsonVersion("client-search"));
additionalProperties.put("recommendVersion", Helpers.getPackageJsonVersion("recommend"));

// Files used to generate the `lite` client
apiName = "lite" + Helpers.API_SUFFIX;
Expand Down
30 changes: 22 additions & 8 deletions scripts/buildSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { GENERATORS, capitalize, createClientName, exists, run, toAbsolutePath }
import { createSpinner } from './spinners.js';
import type { CodeSamples, Language, SnippetSamples, Spec } from './types.js';

const ALGOLIASEARCH_LITE_OPERATIONS = ['search', 'customPost'];
const ALGOLIASEARCH_LITE_OPERATIONS = ['search', 'customPost', 'getRecommendations'];

function mapLanguageToCodeSampleSupporter(language: Language): CodeSamples['lang'] {
switch (language) {
Expand Down Expand Up @@ -196,10 +196,18 @@ async function buildLiteSpec({
spec: string;
bundledPath: string;
}): Promise<void> {
const parsed = yaml.load(await fsp.readFile(toAbsolutePath(bundledPath), 'utf8')) as Spec;
const base = yaml.load(await fsp.readFile(toAbsolutePath(bundledPath), 'utf8')) as Spec;

await run(`yarn specs:fix recommend`);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware this could be handled better by parallelizing with Promise.all and whatnot, but I feel it's not worth complexifying the code for

const recommendTmpPath = toAbsolutePath('specs/bundled/algoliasearch-recommend.tmp.yml');
await run(`yarn openapi bundle specs/recommend/spec.yml -o ${recommendTmpPath} --ext yml`);
const recommend = yaml.load(await fsp.readFile(recommendTmpPath, 'utf8')) as Spec;
fsp.rm(recommendTmpPath);
Comment on lines +202 to +205
Copy link
Member Author

@aymeric-giraudet aymeric-giraudet Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a way to use the openapi package to load/bundle a YML spec without having to write a temp file or use shell


const paths = [...Object.entries(base.paths), ...Object.entries(recommend.paths)];

// Filter methods.
parsed.paths = Object.entries(parsed.paths).reduce(
base.paths = paths.reduce(
(acc, [path, operations]) => {
for (const [, operation] of Object.entries(operations)) {
if (ALGOLIASEARCH_LITE_OPERATIONS.includes(operation.operationId)) {
Expand All @@ -212,7 +220,12 @@ async function buildLiteSpec({
{} as Spec['paths'],
);

await fsp.writeFile(bundledPath, yaml.dump(parsed));
// Merge both specs
base.components.schemas = { ...base.components.schemas, ...recommend.components.schemas };
base.tags.push(...recommend.tags);
base['x-tagGroups'].push(...recommend['x-tagGroups']);

await fsp.writeFile(bundledPath, yaml.dump(base));

await transformBundle({
bundledPath,
Expand All @@ -239,25 +252,26 @@ async function buildSpec({

// In case of lite we use a the `search` spec as a base because only its bundled form exists.
const specBase = isAlgoliasearch ? 'search' : spec;
const deps = isAlgoliasearch ? ['search', 'recommend'] : [spec];
const logSuffix = docs ? 'doc spec' : 'spec';
const cache = new Cache({
folder: toAbsolutePath('specs/'),
generatedFiles: [docs ? `bundled/${spec}.doc.yml` : `bundled/${spec}.yml`],
filesToCache: [specBase, 'common'],
filesToCache: [...deps, 'common'],
cacheFile: toAbsolutePath(`specs/dist/${spec}.${docs ? 'doc.' : ''}cache`),
});

const spinner = createSpinner(`starting '${spec}' ${logSuffix}`);

if (useCache) {
spinner.text = `checking cache for '${specBase}'`;
spinner.text = `checking cache for '${spec}'`;

if (await cache.isValid()) {
spinner.succeed(`job skipped, cache found for '${specBase}'`);
spinner.succeed(`job skipped, cache found for '${spec}'`);
return;
}

spinner.text = `cache not found for '${specBase}'`;
spinner.text = `cache not found for '${spec}'`;
}

// First linting the base
Expand Down
9 changes: 8 additions & 1 deletion scripts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export type Spec = {
servers: Server[];
tags: Tag[];
paths: Path[];
components: {
schemas: Record<string, any>;
};
};

/**
Expand Down Expand Up @@ -105,5 +108,9 @@ export type SnippetSamples = Record<Language, Record<string, string>>;
*/
type Path = Record<
Method,
Record<string, any> & { operationId: string; 'x-codeSamples': CodeSamples[]; summary: string }
Record<string, any> & {
operationId: string;
'x-codeSamples': CodeSamples[];
summary: string;
}
>;
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
{{> algoliasearch/builds/initClients}}

return {
...createRecommendClient(commonOptions),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to use the init solution like for analytics etc. otherwise both base clients will be merged, I think we can leave search as the base client it would make things clearer for users

...createSearchClient(commonOptions),
/**
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createAbtestingClient, REGIONS as abtestingRegions } from '{{{npmNamesp
import type { Region as PersonalizationRegion } from '{{{npmNamespace}}}/client-personalization/src/personalizationClient';
import { createPersonalizationClient, REGIONS as personalizationRegions } from '{{{npmNamespace}}}/client-personalization/src/personalizationClient';
import { createSearchClient, apiClientVersion as searchClientVersion } from '{{{npmNamespace}}}/client-search/src/searchClient';
import { createRecommendClient } from '{{{npmNamespace}}}/recommend/src/recommendClient';

import type { serializeQueryParameters } from '{{{npmNamespace}}}/client-common';
import type {} from '{{{npmNamespace}}}/client-common';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import {
} from '{{{npmNamespace}}}/client-search/model';

export * from '{{{npmNamespace}}}/client-search/model';
export * from '{{{npmNamespace}}}/recommend/model';
export * from '{{{npmNamespace}}}/client-personalization/model';
export * from '{{{npmNamespace}}}/client-analytics/model';
export * from '{{{npmNamespace}}}/client-abtesting/model';

export { SearchClient } from '{{{npmNamespace}}}/client-search';
export { RecommendClient } from '{{{npmNamespace}}}/recommend';
export { PersonalizationClient } from '{{{npmNamespace}}}/client-personalization';
export { AnalyticsClient } from '{{{npmNamespace}}}/client-analytics';
export { AbtestingClient } from '{{{npmNamespace}}}/client-abtesting';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{{> algoliasearch/builds/initClients}}

return {
...createRecommendClient(commonOptions),
...createSearchClient(commonOptions),
/**
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
Expand Down
1 change: 1 addition & 0 deletions templates/javascript/clients/package.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"{{{npmNamespace}}}/client-personalization": "{{personalizationVersion}}",
"{{{npmNamespace}}}/client-search": "{{searchVersion}}",
"{{{npmNamespace}}}/client-common": "{{utilsPackageVersion}}",
"{{{npmNamespace}}}/recommend": "{{recommendVersion}}",
"{{{npmNamespace}}}/requester-browser-xhr": "{{utilsPackageVersion}}",
"{{{npmNamespace}}}/requester-node-http": "{{utilsPackageVersion}}"
},
Expand Down