Skip to content

Commit

Permalink
chore: types running with v4 locally, but also v4 and v3 (#6271)
Browse files Browse the repository at this point in the history
* chore: change type

* WIP: expand search parameters

doesn't work as there still are v4 v5 differences of course

* do it correctly

* use regular client

* not sure if this should stay

* WIP

* changes rule

* fix dependency

* change order to make script work

* almost all types fixed

* improve test

* fix(types): accepts client with wrong types

will be fixed once algolia/api-clients-automation#3357 is done

* tests

* chore(helper): compatibility

* chore(legacy): correct replacement

* don't remove v5 fully

* fix errors

* safer (or less safe lol)

* correct import

* fix fake type

* v5 only works

* tests

* script for examples too

* script for examples too

* chore(answers): bleh get rid of this!

* stuff

* simplify
  • Loading branch information
Haroenv committed Aug 19, 2024
1 parent 3b06a55 commit 6e3a209
Show file tree
Hide file tree
Showing 93 changed files with 853 additions and 432 deletions.
2 changes: 1 addition & 1 deletion examples/js/getting-started/products.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ <h1 class="header-title">
<div id="related-products"></div>
</div>

<script type="module" src="./src/products.js"></script>
<script type="module" src="./src/products.ts"></script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const searchParams = new URLSearchParams(document.location.search);

const pid = searchParams.get('pid');

if (!pid) {
throw new Error('No product ID provided');
}

const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
Expand Down
4 changes: 2 additions & 2 deletions examples/react/react-native/src/Highlight.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Fragment } from 'react';
import { StyleSheet, Text } from 'react-native';
import { Hit as AlgoliaHit } from '@algolia/client-search';
import { Hit as AlgoliaHit } from 'instantsearch.js';
import {
getHighlightedParts,
getPropertyByPath,
Expand All @@ -26,7 +26,7 @@ type HighlightProps<THit> = {
separator?: string;
};

export function Highlight<THit extends AlgoliaHit<Record<string, unknown>>>({
export function Highlight<THit extends AlgoliaHit>({
hit,
attribute,
separator = ', ',
Expand Down
2 changes: 1 addition & 1 deletion examples/react/react-native/src/InfiniteHits.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { forwardRef } from 'react';
import { StyleSheet, View, FlatList } from 'react-native';
import { Hit as AlgoliaHit } from '@algolia/client-search';
import { Hit as AlgoliaHit } from 'instantsearch.js';
import {
useInfiniteHits,
UseInfiniteHitsProps,
Expand Down
2 changes: 1 addition & 1 deletion examples/react/react-native/types/ProductHit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hit as AlgoliaHit } from '@algolia/client-search';
import { Hit as AlgoliaHit } from 'instantsearch.js';

export type ProductHit = AlgoliaHit<{
brand: string;
Expand Down
4 changes: 2 additions & 2 deletions examples/react/ssr/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = [
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
exclude: /node_modules\/(?!(algoliasearch)\/).*/,
use: [
{
loader: 'babel-loader',
Expand All @@ -48,7 +48,7 @@ module.exports = [
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
exclude: /node_modules\/(?!(algoliasearch)\/).*/,
use: [
{
loader: 'babel-loader',
Expand Down
1 change: 1 addition & 0 deletions examples/vue/default-theme/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
presets: ['@vue/app'],
plugins: ['@babel/plugin-proposal-optional-chaining'],
sourceType: 'unambiguous',
};
1 change: 1 addition & 0 deletions examples/vue/default-theme/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module.exports = {
devServer: {
disableHostCheck: true,
},
transpileDependencies: ['algoliasearch'],
};
1 change: 1 addition & 0 deletions examples/vue/e-commerce/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
presets: ['@vue/app'],
plugins: ['@babel/plugin-proposal-optional-chaining'],
sourceType: 'unambiguous',
};
1 change: 1 addition & 0 deletions examples/vue/e-commerce/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module.exports = {
devServer: {
disableHostCheck: true,
},
transpileDependencies: ['algoliasearch'],
};
5 changes: 0 additions & 5 deletions examples/vue/getting-started/vue.config.js

This file was deleted.

1 change: 1 addition & 0 deletions examples/vue/media/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
presets: ['@vue/app'],
plugins: ['@babel/plugin-proposal-optional-chaining'],
sourceType: 'unambiguous',
};
1 change: 1 addition & 0 deletions examples/vue/media/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module.exports = {
devServer: {
disableHostCheck: true,
},
transpileDependencies: ['algoliasearch'],
};
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"devDependencies": {
"@algolia/cache-common": "4.23.2",
"@algolia/cache-in-memory": "4.23.2",
"@algolia/client-search": "4.23.2",
"@algolia/client-search": "5.0.0",
"@algolia/client-common": "5.0.0",
"@algolia/logger-common": "4.23.2",
"@algolia/requester-node-http": "4.23.2",
"@algolia/transporter": "4.23.2",
Expand Down Expand Up @@ -87,9 +88,10 @@
"@wdio/selenium-standalone-service": "5.16.5",
"@wdio/spec-reporter": "5.16.5",
"@wdio/static-server-service": "5.16.5",
"algoliasearch": "4.23.2",
"algoliasearch-v3": "npm:algoliasearch@3.35.0",
"algoliasearch-v4": "npm:algoliasearch@4.23.2",
"algoliasearch-v5": "npm:algoliasearch@5.0.0",
"algoliasearch": "5.0.0",
"babel-eslint": "10.1.0",
"babel-jest": "27.4.6",
"babel-plugin-inline-replace-variables": "1.3.1",
Expand Down Expand Up @@ -145,6 +147,7 @@
"typescript": "5.5.2"
},
"resolutions": {
"places.js/algoliasearch": "5.0.0",
"brotli-size": "4.0.0",
"webpack": "4.47.0",
"babel-loader": "8.2.2"
Expand Down
48 changes: 14 additions & 34 deletions packages/algoliasearch-helper/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import EventEmitter from '@algolia/events';

// eslint-disable-next-line @typescript-eslint/consistent-type-imports
import type {
FindAnswersResponse,
FrequentlyBoughtTogetherQuery,
HighlightResult,
LookingSimilarQuery,
RankingInfo,
RecommendResponse,
RelatedProductsQuery,
SearchClient,
SearchOptions,
SearchResponse,
SnippetResult,
SupportedLanguage,
TrendingFacetsQuery,
TrendingItemsQuery,
PlainRecommendParameters as ClientPlainRecommendParameters,
} from './types/algoliasearch';
// @ts-ignore
import type {
FrequentlyBoughtTogetherQuery as RecommendFrequentlyBoughtTogetherQuery,
LookingSimilarQuery as RecommendLookingSimilarQuery,
RelatedProductsQuery as RecommendRelatedProductsQuery,
TrendingFacetsQuery as RecommendTrendingFacetsQuery,
TrendingItemsQuery as RecommendTrendingItemsQuery,
RecommendQueriesResponse,
} from '@algolia/recommend';

/**
* The algoliasearchHelper module is the function that will let its
Expand Down Expand Up @@ -539,7 +539,7 @@ declare namespace algoliasearchHelper {
// types missing in @types/algoliasearch, so duplicated from v4
ruleContexts?: string[];
optionalFilters?: Array<string | string[]>;
queryLanguages?: string[];
queryLanguages?: SupportedLanguage[];

/**
* The relevancy threshold to apply to search in a virtual index [0-100]. A bigger
Expand Down Expand Up @@ -1021,7 +1021,7 @@ declare namespace algoliasearchHelper {
* a list of language ISO codes (as a comma-separated string) for which stop words should be enable
* https://www.algolia.com/doc/api-reference/api-parameters/removeStopWords/
*/
removeStopWords?: boolean | string[];
removeStopWords?: boolean | SupportedLanguage[];
/**
* List of attributes on which you want to disable the computation of exact criteria
* default: []
Expand Down Expand Up @@ -1555,23 +1555,7 @@ declare namespace algoliasearchHelper {
}
}

// We remove `indexName` from the Recommend query types as the helper
// will fill in this value before sending the queries
type FrequentlyBoughtTogetherQuery = Omit<
RecommendFrequentlyBoughtTogetherQuery,
'indexName'
>;
type LookingSimilarQuery = Omit<RecommendLookingSimilarQuery, 'indexName'>;
type RelatedProductsQuery = Omit<RecommendRelatedProductsQuery, 'indexName'>;
type TrendingFacetsQuery = Omit<RecommendTrendingFacetsQuery, 'indexName'>;
type TrendingItemsQuery = Omit<RecommendTrendingItemsQuery, 'indexName'>;

export type PlainRecommendParameters =
| FrequentlyBoughtTogetherQuery
| LookingSimilarQuery
| RelatedProductsQuery
| TrendingFacetsQuery
| TrendingItemsQuery;
export type PlainRecommendParameters = ClientPlainRecommendParameters;

export type RecommendParametersWithId<
T extends PlainRecommendParameters = PlainRecommendParameters
Expand Down Expand Up @@ -1605,19 +1589,15 @@ declare namespace algoliasearchHelper {
): RecommendParameters;
}

type RecommendResponse<TObject> =
RecommendQueriesResponse<TObject>['results'];

type RecommendResultItem<TObject = any> = RecommendResponse<TObject>[0];
type RecommendResultMap<T> = { [index: number]: RecommendResultItem<T> };
type RecommendResultMap<T> = { [index: number]: RecommendResponse<T> };

export class RecommendResults<T = any> {
constructor(state: RecommendParameters, results: RecommendResultMap<T>);

_state: RecommendParameters;
_rawResults: RecommendResultMap<T>;

[index: number]: RecommendResultItem<T>;
[index: number]: RecommendResponse<T>;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if (require.main === module) {
path.join(__dirname.replace('datasets', 'spec'), 'getFacetValues')
);
var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var client = algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76');
var helper = new HelperSaver(client, 'instant_search', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if (require.main === module) {
path.join(__dirname.replace('datasets', 'spec'), 'getRefinements')
);
var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var client = algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76');
var helper = new HelperSaver(client, 'instant_search', {
Expand Down
1 change: 1 addition & 0 deletions packages/algoliasearch-helper/test/integration-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

function setup(indexName, fn) {
var appID = process.env.INTEGRATION_TEST_APPID;
Expand Down
1 change: 1 addition & 0 deletions packages/algoliasearch-helper/test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var path = require('path');

var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;
var jest = require('jest');

var staticJestConfig = require('../jest.config');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var algoliaSearchHelper = require('../../../');
var version = require('../../../src/version');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';

var algoliaSearch = require('algoliasearch');
var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var algoliasearchHelper = require('../../../index');

var fakeClient = {};

test('Numeric filters: numeric filters from constructor', function (done) {
var client = algoliaSearch('dsf', 'dsfdf');
var client = algoliasearch('dsf', 'dsfdf');

client.search = function (queries) {
var ps = queries[0].params;
Expand Down Expand Up @@ -42,7 +43,7 @@ test('Numeric filters: numeric filters from constructor', function (done) {
});

test('Numeric filters: numeric filters from setters', function (done) {
var client = algoliaSearch('dsf', 'dsfdf');
var client = algoliasearch('dsf', 'dsfdf');

client.search = function (queries) {
var ps = queries[0].params;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';

var algoliaSearch = require('algoliasearch');
var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var algoliasearchHelper = require('../../../index');

test('When searchOnce with callback, hasPendingRequests is true', function (done) {
var testData = require('../../datasets/SearchParameters/search.dataset')();
var client = algoliaSearch('dsf', 'dsfdf');
var client = algoliasearch('dsf', 'dsfdf');

var triggerCb;
client.search = function () {
Expand Down Expand Up @@ -39,7 +40,7 @@ test('When searchOnce with callback, hasPendingRequests is true', function (done

test('When searchOnce with promises, hasPendingRequests is true', function (done) {
var testData = require('../../datasets/SearchParameters/search.dataset')();
var client = algoliaSearch('dsf', 'dsfdf');
var client = algoliasearch('dsf', 'dsfdf');

var triggerCb;
client.search = function () {
Expand Down Expand Up @@ -71,7 +72,7 @@ test('When searchOnce with promises, hasPendingRequests is true', function (done
});

test('When searchForFacetValues, hasPendingRequests is true', function (done) {
var client = algoliaSearch('dsf', 'dsfdf');
var client = algoliasearch('dsf', 'dsfdf');

var triggerCb;
client.searchForFacetValues = function () {
Expand Down Expand Up @@ -110,7 +111,7 @@ test('When searchForFacetValues, hasPendingRequests is true', function (done) {

test('When helper.search(), hasPendingRequests is true', function (done) {
var testData = require('../../datasets/SearchParameters/search.dataset')();
var client = algoliaSearch('dsf', 'dsfdf');
var client = algoliasearch('dsf', 'dsfdf');

var triggerCb;
client.search = function () {
Expand Down Expand Up @@ -145,7 +146,7 @@ test('When helper.search(), hasPendingRequests is true', function (done) {

test('When helper.search() and one request is discarded, hasPendingRequests is true unless all come back', function (done) {
var testData = require('../../datasets/SearchParameters/search.dataset')();
var client = algoliaSearch('dsf', 'dsfdf');
var client = algoliasearch('dsf', 'dsfdf');

var triggerCbs = [];
client.search = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

test('hierarchical facets: attributes order', function (done) {
var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var algoliasearchHelper = require('../../../');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

test('hierarchical facets: using getHierarchicalFacetBreadcrumb()', function () {
var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var algoliasearchHelper = require('../../../');

Expand Down Expand Up @@ -35,6 +36,7 @@ test('hierarchical facets: using getHierarchicalFacetBreadcrumb()', function ()

test('hierarchical facets: using getHierarchicalFacetBreadcrumb before the first refinement', function () {
var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var algoliasearchHelper = require('../../../');

Expand Down Expand Up @@ -62,6 +64,7 @@ test('hierarchical facets: using getHierarchicalFacetBreadcrumb before the first

test('hierarchical facets: using getHierarchicalFacetBreadcrumb on an undefined facet', function () {
var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var algoliasearchHelper = require('../../../');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

test('hierarchical facets: custom prefix path', function (done) {
var algoliasearch = require('algoliasearch');
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var algoliasearchHelper = require('../../../');

Expand Down
Loading

0 comments on commit 6e3a209

Please sign in to comment.