Skip to content

Commit 2ca2aef

Browse files
authored
[CSL-3049] Add support for cioJsClientOptions (#108)
1 parent afe386b commit 2ca2aef

File tree

7 files changed

+42
-6
lines changed

7 files changed

+42
-6
lines changed

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export const apiKeyDescription = `Pass an \`apiKey\` to request results from con
111111
export const cioJsClientDescription = `If you are already using an instance of the \`ConstructorIOClient\`, you can pass a \`cioJsClient\` instead of an \`apiKey\` to request results from constructor's servers
112112
113113
> Note: when we say \`cioJsClient\`, we are referring to an instance of the [constructorio-client-javascript](https://www.npmjs.com/package/@constructor-io/constructorio-client-javascript)`;
114+
export const cioJsClientOptionsDescription = `If you don't want to create an instance of the \`ConstructorIOClient\` but still want to customize some of the options, you can pass a \`cioJsClientOptions\` object. You can learn more about the possible values [here under the parameters section](https://constructor-io.github.io/constructorio-client-javascript/ConstructorIO.html).`;
114115
export const placeholderDescription = `Pass a \`placeholder\` to override the default input field placeholder text shown to users before they start typing their query`;
115116
export const searchSuggestionsDescription = `Override default \`sections\` to only suggest search terms`;
116117
export const productsDescription = `Override default \`sections\` to only suggested products`;

src/hooks/useCioAutocomplete.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
2828
openOnFocus,
2929
apiKey,
3030
cioJsClient,
31+
cioJsClientOptions,
3132
placeholder = 'What can we help you find today?',
3233
sections = defaultSections,
3334
zeroStateSections,
@@ -37,7 +38,7 @@ const useCioAutocomplete = (options: UseCioAutocompleteOptions) => {
3738

3839
const [query, setQuery] = useState('');
3940
const previousQuery = usePrevious(query);
40-
const cioClient = useCioClient({ apiKey, cioJsClient } as CioClientConfig);
41+
const cioClient = useCioClient({ apiKey, cioJsClient, cioJsClientOptions } as CioClientConfig);
4142

4243
// Get autocomplete sections (autocomplete + recommendations + custom)
4344
const { activeSections, activeSectionsWithData, zeroStateActiveSections, request } = useSections(

src/hooks/useCioClient.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import { CioClientConfig } from '../types';
66

77
type UseCioClient = (cioClientConfig: CioClientConfig) => Nullable<ConstructorIOClient>;
88

9-
const useCioClient: UseCioClient = ({ apiKey, cioJsClient }) => {
9+
const useCioClient: UseCioClient = ({ apiKey, cioJsClient, cioJsClientOptions }) => {
1010
if (!apiKey && !cioJsClient) {
1111
// eslint-disable-next-line no-console
1212
console.error('Either apiKey or cioJsClient is required');
1313
}
1414

15-
return useMemo(() => cioJsClient || getCioClient(apiKey), [apiKey, cioJsClient]);
15+
return useMemo(
16+
() => cioJsClient || getCioClient(apiKey, cioJsClientOptions),
17+
[apiKey, cioJsClient, cioJsClientOptions]
18+
);
1619
};
1720

1821
export default useCioClient;

src/stories/Autocomplete/Component/index.stories.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
customStylesDescription,
1313
apiKey,
1414
onSubmitDefault as onSubmit,
15+
cioJsClientOptionsDescription,
1516
} from '../../../constants';
1617

1718
export default {
@@ -91,6 +92,16 @@ const args = { cioJsClient, onSubmit: ${functionStrings.onSubmit} };`,
9192
cioJsClientDescription
9293
);
9394

95+
const cioJsClientOptions = { serviceUrl: 'https://ac.cnstrc.com' };
96+
97+
export const ProvideCIOClientOptions = ComponentTemplate.bind({});
98+
ProvideCIOClientOptions.args = { apiKey, cioJsClientOptions, onSubmit };
99+
addComponentStoryDescription(
100+
ProvideCIOClientOptions,
101+
`const args = ${stringifyWithDefaults(ProvideCIOClientOptions.args)}`,
102+
cioJsClientOptionsDescription
103+
);
104+
94105
const placeholder = 'Custom placeholder';
95106

96107
export const ProvideCustomPlaceHolder = ComponentTemplate.bind({});

src/stories/Autocomplete/Hook/index.stories.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
customStylesDescription,
1212
apiKey,
1313
onSubmitDefault as onSubmit,
14+
cioJsClientOptionsDescription,
1415
} from '../../../constants';
1516

1617
export default {
@@ -47,6 +48,16 @@ const args = { cioJsClient, onSubmit: ${functionStrings.onSubmit} };`,
4748
cioJsClientDescription
4849
);
4950

51+
const cioJsClientOptions = { serviceUrl: 'https://ac.cnstrc.com' };
52+
53+
export const ProvideCIOClientOptions = HooksTemplate.bind({});
54+
ProvideCIOClientOptions.args = { apiKey, cioJsClientOptions, onSubmit };
55+
addHookStoryCode(
56+
ProvideCIOClientOptions,
57+
`const args = ${stringifyWithDefaults(ProvideCIOClientOptions.args)}`,
58+
cioJsClientOptionsDescription
59+
);
60+
5061
const placeholder = 'Custom placeholder';
5162

5263
export const ProvideCustomPlaceHolder = HooksTemplate.bind({});

src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ import {
88
Product as ProductFromClient,
99
Item as ItemBase,
1010
AutocompleteRequestType,
11+
ConstructorClientOptions,
1112
} from '@constructor-io/constructorio-client-javascript/lib/types';
1213

1314
export type { IAutocompleteParameters } from '@constructor-io/constructorio-client-javascript/lib/types';
1415

15-
export type CioClientConfig = { apiKey?: string; cioJsClient?: ConstructorIOClient };
16+
export type CioClientConfig = {
17+
apiKey?: string;
18+
cioJsClient?: ConstructorIOClient;
19+
cioJsClientOptions?: ConstructorClientOptions;
20+
};
1621

1722
export interface AdvancedParametersBase {
1823
numTermsWithGroupSuggestions?: number;

src/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import ConstructorIOClient from '@constructor-io/constructorio-client-javascript';
2-
import { AutocompleteRequestType } from '@constructor-io/constructorio-client-javascript/lib/types';
2+
import {
3+
AutocompleteRequestType,
4+
ConstructorClientOptions,
5+
} from '@constructor-io/constructorio-client-javascript/lib/types';
36
import { isCustomSection } from './typeGuards';
47
import { OnSubmit, Item, Section, UserDefinedSection, SectionsData } from './types';
58
import version from './version';
@@ -123,12 +126,13 @@ export const disableStoryActions = (story) => {
123126
story.parameters.actions = { argTypesRegex: null };
124127
};
125128

126-
export const getCioClient = (apiKey?: string) => {
129+
export const getCioClient = (apiKey?: string, cioJsClientOptions?: ConstructorClientOptions) => {
127130
if (apiKey) {
128131
const cioClient = new ConstructorIOClient({
129132
apiKey,
130133
sendTrackingEvents: true,
131134
version: `cio-ui-autocomplete-${version}`,
135+
...cioJsClientOptions,
132136
});
133137

134138
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)