-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): allow legacy signature for
search
method (#665)
- Loading branch information
Showing
16 changed files
with
240 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...ts/algoliasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import type { EchoResponse } from '@experimental-api-clients-automation/client-common'; | ||
import { echoRequester } from '@experimental-api-clients-automation/requester-node-http'; | ||
|
||
import { algoliasearch, apiClientVersion } from '../builds/node'; | ||
|
||
const client = algoliasearch('APP_ID', 'API_KEY', { | ||
requester: echoRequester(), | ||
}); | ||
|
||
describe('api', () => { | ||
it('sets the user agent', async () => { | ||
const req = (await client.post({ | ||
path: '/test', | ||
})) as unknown as EchoResponse; | ||
|
||
expect(req.algoliaAgent).toMatchInlineSnapshot( | ||
`"Algolia%20for%20JavaScript%20(${apiClientVersion});%20Search%20(${apiClientVersion});%20Node.js%20(${process.versions.node})"` | ||
); | ||
}); | ||
|
||
it('throws with undefined API key', () => { | ||
try { | ||
algoliasearch('APP_ID', ''); | ||
} catch (e) { | ||
expect((e as Error).message).toMatch('`apiKey` is missing.'); | ||
} | ||
}); | ||
|
||
it('throws with undefined app ID', () => { | ||
try { | ||
algoliasearch('', 'API_KEY'); | ||
} catch (e) { | ||
expect((e as Error).message).toMatch('`appId` is missing.'); | ||
} | ||
}); | ||
|
||
it('provides the search client at the root of the API', () => { | ||
expect(client.search).not.toBeUndefined(); | ||
}); | ||
|
||
it('provides an init method for the analytics client', () => { | ||
expect(client.initAnalytics).not.toBeUndefined(); | ||
}); | ||
|
||
it('provides an init method for the personalization client', () => { | ||
expect(client.initPersonalization).not.toBeUndefined(); | ||
}); | ||
}); | ||
|
||
/** | ||
* We only test the legacy signature, as `algoliasearch` inherits methods from the `client-search`. | ||
* The new signatures are already tested in the CTS. | ||
*/ | ||
describe('search with legacy signature', () => { | ||
it('allows searching for query', async () => { | ||
const req = (await client.search([ | ||
{ | ||
indexName: 'theIndexName', | ||
}, | ||
])) as unknown as EchoResponse; | ||
|
||
expect(req.path).toEqual('/1/indexes/*/queries'); | ||
expect(req.method).toEqual('POST'); | ||
expect(req.data).toEqual({ requests: [{ indexName: 'theIndexName' }] }); | ||
expect(req.searchParams).toStrictEqual(undefined); | ||
}); | ||
|
||
it('allows searching for facet', async () => { | ||
const req = (await client.search([ | ||
{ | ||
indexName: 'theIndexName', | ||
type: 'facet', | ||
facet: 'theFacet', | ||
}, | ||
])) as unknown as EchoResponse; | ||
|
||
expect(req.path).toEqual('/1/indexes/*/queries'); | ||
expect(req.method).toEqual('POST'); | ||
expect(req.data).toEqual({ | ||
requests: [ | ||
{ indexName: 'theIndexName', type: 'facet', facet: 'theFacet' }, | ||
], | ||
}); | ||
expect(req.searchParams).toStrictEqual(undefined); | ||
}); | ||
|
||
it('accepts a `params` parameter for `searchParams`', async () => { | ||
const req = (await client.search([ | ||
{ | ||
indexName: 'theIndexName', | ||
params: { | ||
hitsPerPage: 42, | ||
}, | ||
}, | ||
])) as unknown as EchoResponse; | ||
|
||
expect(req.path).toEqual('/1/indexes/*/queries'); | ||
expect(req.method).toEqual('POST'); | ||
expect(req.data).toEqual({ | ||
requests: [{ indexName: 'theIndexName', hitsPerPage: 42 }], | ||
}); | ||
expect(req.searchParams).toStrictEqual(undefined); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
clients/algoliasearch-client-javascript/packages/algoliasearch/jest.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { Config } from '@jest/types'; | ||
|
||
const config: Config.InitialOptions = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
roots: ['__tests__'], | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
clients/algoliasearch-client-javascript/packages/algoliasearch/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node", "jest"], | ||
"outDir": "dist" | ||
}, | ||
"include": ["builds/node.ts", "builds/browser.ts"], | ||
"exclude": ["dist", "node_modules"] | ||
"exclude": ["dist", "node_modules", "__tests__"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
templates/javascript/api/operation/legacySearchCompatible/implementation.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
if (searchMethodParams && Array.isArray(searchMethodParams)) { | ||
const newSignatureRequest: SearchMethodParams = { | ||
requests: searchMethodParams.map(({ params, ...legacyRequest }) => { | ||
if (legacyRequest.type === 'facet') { | ||
return { | ||
...legacyRequest, | ||
...params, | ||
type: 'facet', | ||
}; | ||
} | ||
|
||
return { | ||
...legacyRequest, | ||
...params, | ||
facet: undefined, | ||
maxFacetHits: undefined, | ||
facetQuery: undefined, | ||
}; | ||
}), | ||
}; | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
searchMethodParams = newSignatureRequest; | ||
} |
3 changes: 3 additions & 0 deletions
3
templates/javascript/api/operation/legacySearchCompatible/imports.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { SearchForFacetsOptions } from './searchForFacetsOptions'; | ||
import type { SearchForHitsOptions } from './searchForHitsOptions'; | ||
import { SearchParamsObject } from './searchParamsObject'; |
31 changes: 31 additions & 0 deletions
31
templates/javascript/api/operation/legacySearchCompatible/model.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* In v4, the search parameters are wrapped in a `params` parameter. | ||
* | ||
* @deprecated The `search` method now accepts flat `searchParams` at the root of the method. | ||
*/ | ||
type LegacySearchParams = { | ||
params?: SearchParamsObject; | ||
}; | ||
|
||
/** | ||
* In v4, the search parameters are wrapped in a `params` parameter. | ||
* | ||
* @deprecated The `search` method now accepts flat `searchParams` at the root of the method. | ||
*/ | ||
type LegacySearchForFacets = LegacySearchParams & SearchForFacetsOptions; | ||
|
||
/** | ||
* In v4, the search parameters are wrapped in a `params` parameter. | ||
* | ||
* @deprecated The `search` method now accepts flat `searchParams` at the root of the method. | ||
*/ | ||
type LegacySearchForHits = LegacySearchParams & SearchForHitsOptions; | ||
|
||
type LegacySearchQuery = LegacySearchForFacets | LegacySearchForHits; | ||
|
||
/** | ||
* Search method signature compatible with the `algoliasearch` v4 package. When using this signature, extra computation will be required to make it match the new signature. | ||
* | ||
* @deprecated This signature will be removed from the next major version, we recommend using the `SearchMethodParams` type for performances and future proof reasons. | ||
*/ | ||
export type LegacySearchMethodProps = LegacySearchQuery[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.