Skip to content

Commit

Permalink
feat(javascript): add every APIs (#3942)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Oct 14, 2024
1 parent 542b24e commit 21d09d3
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 120 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ composer.json
tsconfig.json
vendor
builddir
foo
10 changes: 5 additions & 5 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ jobs:
type: specs

- name: Remove generated clients
if: ${{ startsWith(env.head_ref, 'chore/prepare-release-') }}
if: ${{ startsWith(github.head_ref, 'chore/prepare-release-') }}
run: |
cd clients/algoliasearch-client-javascript/packages
ls | grep -v -E "(client-common|requester-*|algoliasearch)" | xargs rm -rf
ls | grep -v -E "(logger-console|client-common|requester-*|algoliasearch)" | xargs rm -rf
cd algoliasearch
ls | grep -v -E "__tests__" | xargs rm -rf
Expand All @@ -228,11 +228,11 @@ jobs:
run: cd clients/algoliasearch-client-javascript && yarn test ${{ !contains(fromJSON(needs.setup.outputs.JAVASCRIPT_DATA).toRun, 'algoliasearch') && '--ignore algoliasearch' || '' }}

- name: Test JavaScript bundle size
if: ${{ startsWith(env.head_ref, 'chore/prepare-release-') }}
if: ${{ startsWith(github.head_ref, 'chore/prepare-release-') }}
run: cd clients/algoliasearch-client-javascript && yarn test:size

- name: Test JavaScript bundle and types
if: ${{ startsWith(env.head_ref, 'chore/prepare-release-') }}
if: ${{ startsWith(github.head_ref, 'chore/prepare-release-') }}
run: cd clients/algoliasearch-client-javascript && yarn test:bundle

- name: Remove previous CTS output
Expand Down Expand Up @@ -335,7 +335,7 @@ jobs:
run: yarn cli generate ${{ matrix.client.language }} ${{ matrix.client.toRun }}

- name: Update composer.lock
if: ${{ matrix.client.language == 'php' && startsWith(env.head_ref, 'chore/renovateBaseBranch') && matrix.client.isMainVersion }}
if: ${{ matrix.client.language == 'php' && startsWith(github.head_ref, 'chore/renovateBaseBranch') && matrix.client.isMainVersion }}
run: cd ${{ matrix.client.path }} && composer update

- name: Check for file duplicates in Swift
Expand Down
10 changes: 5 additions & 5 deletions clients/algoliasearch-client-javascript/bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
"files": [
{
"path": "packages/algoliasearch/dist/algoliasearch.umd.js",
"maxSize": "9.50KB"
"maxSize": "12.60KB"
},
{
"path": "packages/algoliasearch/dist/lite/builds/browser.umd.js",
"maxSize": "3.95KB"
},
{
"path": "packages/client-abtesting/dist/builds/browser.umd.js",
"maxSize": "4.10KB"
"maxSize": "4.15KB"
},
{
"path": "packages/client-analytics/dist/builds/browser.umd.js",
"maxSize": "4.75KB"
"maxSize": "4.85KB"
},
{
"path": "packages/client-insights/dist/builds/browser.umd.js",
Expand All @@ -30,7 +30,7 @@
},
{
"path": "packages/client-search/dist/builds/browser.umd.js",
"maxSize": "7.15KB"
"maxSize": "7.25KB"
},
{
"path": "packages/ingestion/dist/builds/browser.umd.js",
Expand All @@ -42,7 +42,7 @@
},
{
"path": "packages/recommend/dist/builds/browser.umd.js",
"maxSize": "4.10KB"
"maxSize": "4.15KB"
}
]
}
2 changes: 1 addition & 1 deletion clients/algoliasearch-client-javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"scripts": {
"build:all": "lerna run build --include-dependencies",
"build:many": "lerna run build --scope '@algolia/requester-testing' --scope ${0:-'{@algolia/*,algoliasearch}'} --include-dependencies",
"build:many": "lerna run build --scope '@algolia/requester-testing' --scope '@algolia/logger-console' --scope ${0:-'{@algolia/*,algoliasearch}'} --include-dependencies",
"clean": "lerna run clean",
"release:bump": "lerna version ${0:-patch} --no-changelog --no-git-tag-version --no-push --exact --force-publish --yes",
"release:publish": "tsc --project scripts/tsconfig.json && node scripts/dist/scripts/publish.js",
Expand Down
27 changes: 15 additions & 12 deletions clients/algoliasearch-client-javascript/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ __metadata:
languageName: unknown
linkType: soft

"@algolia/client-insights@workspace:packages/client-insights":
"@algolia/client-insights@npm:5.8.1, @algolia/client-insights@workspace:packages/client-insights":
version: 0.0.0-use.local
resolution: "@algolia/client-insights@workspace:packages/client-insights"
dependencies:
Expand Down Expand Up @@ -88,7 +88,7 @@ __metadata:
languageName: unknown
linkType: soft

"@algolia/client-query-suggestions@workspace:packages/client-query-suggestions":
"@algolia/client-query-suggestions@npm:5.8.1, @algolia/client-query-suggestions@workspace:packages/client-query-suggestions":
version: 0.0.0-use.local
resolution: "@algolia/client-query-suggestions@workspace:packages/client-query-suggestions"
dependencies:
Expand All @@ -105,7 +105,7 @@ __metadata:
languageName: unknown
linkType: soft

"@algolia/client-search@npm:5.8.1, @algolia/client-search@workspace:packages/client-search":
"@algolia/client-search@workspace:packages/client-search":
version: 0.0.0-use.local
resolution: "@algolia/client-search@workspace:packages/client-search"
dependencies:
Expand All @@ -122,7 +122,7 @@ __metadata:
languageName: unknown
linkType: soft

"@algolia/ingestion@workspace:packages/ingestion":
"@algolia/ingestion@npm:1.8.1, @algolia/ingestion@workspace:packages/ingestion":
version: 0.0.0-use.local
resolution: "@algolia/ingestion@workspace:packages/ingestion"
dependencies:
Expand Down Expand Up @@ -155,7 +155,7 @@ __metadata:
languageName: unknown
linkType: soft

"@algolia/monitoring@workspace:packages/monitoring":
"@algolia/monitoring@npm:1.8.1, @algolia/monitoring@workspace:packages/monitoring":
version: 0.0.0-use.local
resolution: "@algolia/monitoring@workspace:packages/monitoring"
dependencies:
Expand Down Expand Up @@ -2088,8 +2088,11 @@ __metadata:
"@algolia/client-abtesting": "npm:5.8.1"
"@algolia/client-analytics": "npm:5.8.1"
"@algolia/client-common": "npm:5.8.1"
"@algolia/client-insights": "npm:5.8.1"
"@algolia/client-personalization": "npm:5.8.1"
"@algolia/client-search": "npm:5.8.1"
"@algolia/client-query-suggestions": "npm:5.8.1"
"@algolia/ingestion": "npm:1.8.1"
"@algolia/monitoring": "npm:1.8.1"
"@algolia/recommend": "npm:5.8.1"
"@algolia/requester-browser-xhr": "npm:5.8.1"
"@algolia/requester-fetch": "npm:5.8.1"
Expand Down Expand Up @@ -3938,13 +3941,13 @@ __metadata:
linkType: hard

"form-data@npm:^4.0.0":
version: 4.0.0
resolution: "form-data@npm:4.0.0"
version: 4.0.1
resolution: "form-data@npm:4.0.1"
dependencies:
asynckit: "npm:^0.4.0"
combined-stream: "npm:^1.0.8"
mime-types: "npm:^2.1.12"
checksum: 10/7264aa760a8cf09482816d8300f1b6e2423de1b02bba612a136857413fdc96d7178298ced106817655facc6b89036c6e12ae31c9eb5bdc16aabf502ae8a5d805
checksum: 10/6adb1cff557328bc6eb8a68da205f9ae44ab0e88d4d9237aaf91eed591ffc64f77411efb9016af7d87f23d0a038c45a788aa1c6634e51175c4efa36c2bc53774
languageName: node
linkType: hard

Expand Down Expand Up @@ -5819,11 +5822,11 @@ __metadata:
linkType: hard

"nan@npm:^2.14.0":
version: 2.20.0
resolution: "nan@npm:2.20.0"
version: 2.21.0
resolution: "nan@npm:2.21.0"
dependencies:
node-gyp: "npm:latest"
checksum: 10/5f16e4c9953075d9920229c703c1d781c0b74118ce3d9e926b448a4eef92b7d8be5ac6adc748a13a5fafb594436cbfe63250e3471aefdd78e3a0cd14603b9ba7
checksum: 10/4517d316776b509a961c65241b72863e70f63aa5b0c354ca19fbbbf7c78254e54aa1f0de142fac1df986a04c27e74a1fb7e4aa85e9e3834daa172b8d9d1bbb07
languageName: node
linkType: hard

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.servers.Server;
import java.util.List;
import java.util.Map;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.*;
import java.util.stream.StreamSupport;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.SupportingFile;
Expand Down Expand Up @@ -158,26 +155,32 @@ private void setDefaultGeneratorOptions() {
additionalProperties.put("isSearchClient", CLIENT.equals("search") || isAlgoliasearchClient);
additionalProperties.put("isIngestionClient", CLIENT.equals("ingestion"));
additionalProperties.put("isAlgoliasearchClient", isAlgoliasearchClient);
additionalProperties.put(
"isAvailableInAlgoliasearch",
CLIENT.equals("search") ||
CLIENT.equals("recommend") ||
CLIENT.equals("personalization") ||
CLIENT.equals("analytics") ||
CLIENT.equals("abtesting")
);
additionalProperties.put("packageVersion", Helpers.getPackageJsonVersion(packageName));
additionalProperties.put("packageName", packageName);
additionalProperties.put("npmPackageName", isAlgoliasearchClient ? packageName : "@algolia/" + packageName);
additionalProperties.put("nodeSearchHelpers", CLIENT.equals("search") || isAlgoliasearchClient);

if (isAlgoliasearchClient) {
// Files used to create the package.json of the algoliasearch package
additionalProperties.put("analyticsVersion", Helpers.getPackageJsonVersion("client-analytics"));
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"));
var dependencies = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> packages = Helpers.getClientConfigList("javascript", "clients");
for (Map<String, Object> pkg : packages) {
String name = ((String) pkg.get("output")).replace("clients/algoliasearch-client-javascript/packages/", "");
if (name.contains("search")) {
continue;
}

var dependency = new HashMap<String, Object>();
dependency.put("dependencyName", Helpers.createClientName((String) pkg.get("name"), "javascript"));
dependency.put("dependencyPackage", "@algolia/" + name);
dependency.put("dependencyVersion", Helpers.getPackageJsonVersion(name));
dependency.put(
"dependencyHasRegionalHosts",
!name.contains("search") && !name.contains("recommend") && !name.contains("monitoring")
);

dependencies.add(dependency);
}
additionalProperties.put("dependencies", dependencies);

// Files used to generate the `lite` client
clientName = "lite" + Helpers.API_SUFFIX;
Expand Down
16 changes: 16 additions & 0 deletions generators/src/main/java/com/algolia/codegen/utils/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,22 @@ public static String getClientConfigField(String language, String... fields) thr
return value.asText();
}

/** Get the `field` value in the `config/clients.config.json` file for the given language */
public static List<Map<String, Object>> getClientConfigList(String language, String... fields) throws ConfigException {
if (fields.length == 0) {
throw new ConfigException("getClientConfigList requires at least one field");
}
JsonNode value = getClientConfig(language);
for (String field : fields) {
value = value.get(field);
}
try {
return new ObjectMapper().readerForListOf(Map.class).readValue(value);
} catch (IOException e) {
throw new ConfigException("Cannot convert value", e);
}
}

public static List<String> getClientListForLanguage(String language) throws ConfigException {
JsonNode value = getClientConfig(language);
value = value.get("clients");
Expand Down
4 changes: 2 additions & 2 deletions playground/javascript/node/algoliasearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function testAlgoliasearch() {
}

try {
const analyticsClient = client.initAnalytics();
const analyticsClient = client.initAnalytics({region: 'de'});

const res = await analyticsClient.getTopFilterForAttribute({
attribute: 'myAttribute1,myAttribute2',
Expand All @@ -97,7 +97,7 @@ async function testAlgoliasearch() {
}

try {
const abtestingClient = client.initAbtesting();
const abtestingClient = client.initAbtesting({region: 'us'});

const res = await abtestingClient.getABTest({
id: 42,
Expand Down
4 changes: 2 additions & 2 deletions templates/javascript/clients/README.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@

## 💡 Getting Started

{{#isAvailableInAlgoliasearch}}
{{^isAlgoliasearchClient}}
> [!TIP]
> This API client is already a dependency of [the algoliasearch client](https://www.npmjs.com/package/algoliasearch), you don't need to manually install `{{{npmPackageName}}}` if you already have `algoliasearch` installed.
{{/isAvailableInAlgoliasearch}}
{{/isAlgoliasearchClient}}

To get started, you first need to install {{npmPackageName}} (or any other available API client package).
All of our clients comes with type definition, and are available for both browser and node environments.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// {{{generationBanner}}}

import type { ClientOptions } from '@algolia/client-common';
import type { AbtestingClient, Region as AbtestingRegion } from "@algolia/client-abtesting"
import type { AnalyticsClient, Region as AnalyticsRegion } from "@algolia/client-analytics"
import type { PersonalizationClient, Region as PersonalizationRegion } from "@algolia/client-personalization"
import type { RecommendClient } from "@algolia/recommend"
import type { SearchClient } from "@algolia/client-search"

import { abtestingClient } from '@algolia/client-abtesting';
import { analyticsClient } from '@algolia/client-analytics';
import { personalizationClient } from '@algolia/client-personalization';
import type { SearchClient } from '@algolia/client-search';
import { searchClient } from '@algolia/client-search';
import { recommendClient } from '@algolia/recommend';

{{#dependencies}}
import { {{{dependencyName}}}Client } from '{{{dependencyPackage}}}';
import type { {{#lambda.titlecase}}{{{dependencyName}}}{{/lambda.titlecase}}Client } from '{{{dependencyPackage}}}';
{{/dependencies}}

import type {
InitClientOptions,
InitClientRegion,
{{#dependencies}}
{{#dependencyHasRegionalHosts}}
{{#lambda.titlecase}}{{{dependencyName}}}Region{{/lambda.titlecase}},
{{#lambda.titlecase}}{{{dependencyName}}}RegionOptions{{/lambda.titlecase}},
{{/dependencyHasRegionalHosts}}
{{/dependencies}}
} from './models';

export * from './models';

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;
{{#dependencies}}
init{{#lambda.titlecase}}{{{dependencyName}}}{{/lambda.titlecase}}: (initOptions: InitClientOptions {{#dependencyHasRegionalHosts}}& {{#lambda.titlecase}}{{{dependencyName}}}RegionOptions{{/lambda.titlecase}}{{/dependencyHasRegionalHosts}}) => {{#lambda.titlecase}}{{{dependencyName}}}{{/lambda.titlecase}}Client;
{{/dependencies}}
};

export function algoliasearch(appId: string, apiKey: string, options?: ClientOptions): Algoliasearch {
Expand All @@ -40,43 +40,19 @@ export function algoliasearch(appId: string, apiKey: string, options?: ClientOpt

return {
...client,
/**
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
*/
get _ua(): string {
return client.transporter.algoliaAgent.value;
},
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,
);
{{#dependencies}}
init{{#lambda.titlecase}}{{{dependencyName}}}{{/lambda.titlecase}}: (initOptions: InitClientOptions {{#dependencyHasRegionalHosts}}& {{#lambda.titlecase}}{{{dependencyName}}}RegionOptions{{/lambda.titlecase}}{{/dependencyHasRegionalHosts}}): {{#lambda.titlecase}}{{{dependencyName}}}{{/lambda.titlecase}}Client => {
return {{{dependencyName}}}Client(initOptions.appId || appId, initOptions.apiKey || apiKey, {{#dependencyHasRegionalHosts}}initOptions.region,{{/dependencyHasRegionalHosts}}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,
);
},
{{/dependencies}}
}
}
Loading

0 comments on commit 21d09d3

Please sign in to comment.