-
Notifications
You must be signed in to change notification settings - Fork 21
feat(javascript): allow legacy signature for search
method
#665
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6cd8fb8
feat(javascript): allow legacy signature for `search` method
shortcuts 94de623
update playground
shortcuts 0d0450c
`query` in `params`
shortcuts ee821b5
split imports
shortcuts 2fd38d9
Merge branch 'main' into feat/javascript-allow-legacy-search
shortcuts 0856338
Merge branch 'main' into feat/javascript-allow-legacy-search
shortcuts 5d0ce33
oops
shortcuts 52443a4
oops x2
shortcuts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.