Skip to content

Commit

Permalink
fix(javascript): assert helpers and fix setClientApiKey helper (gener…
Browse files Browse the repository at this point in the history
…ated)

algolia/api-clients-automation#3663

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
  • Loading branch information
algolia-bot and shortcuts committed Sep 9, 2024
1 parent 8b24e5d commit dac3ea5
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 36 deletions.
12 changes: 12 additions & 0 deletions packages/algoliasearch/__tests__/algoliasearch.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ describe('api', () => {

it('provides a `clearCache` method', () => {
expect(client.clearCache).not.toBeUndefined();
expect(() => client.clearCache()).not.toThrow();
});

it('provides a `setClientApiKey` method', () => {
const _client = algoliasearch('foo', 'bar', {
requester: echoRequester(),
});

expect(_client.transporter.baseQueryParameters['x-algolia-api-key']).toEqual('bar');
expect(_client.setClientApiKey).not.toBeUndefined();
_client.setClientApiKey({ apiKey: 'tabac' });
expect(_client.transporter.baseQueryParameters['x-algolia-api-key']).toEqual('tabac');
});

it('sets the user agent', async () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/algoliasearch/__tests__/algoliasearch.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ describe('api', () => {

it('provides a `clearCache` method', () => {
expect(client.clearCache).not.toBeUndefined();
expect(() => client.clearCache()).not.toThrow();
});

it('provides a `setClientApiKey` method', () => {
const _client = algoliasearch('foo', 'bar', {
requester: echoRequester(),
});

expect(_client.transporter.baseHeaders['x-algolia-api-key']).toEqual('bar');
expect(_client.setClientApiKey).not.toBeUndefined();
_client.setClientApiKey({ apiKey: 'tabac' });
expect(_client.transporter.baseHeaders['x-algolia-api-key']).toEqual('tabac');
});

it('sets the user agent', async () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/algoliasearch/lite/src/liteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ export function createLiteClient({
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
if (!authMode || authMode === 'WithinHeaders') {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
} else {
this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
}
},
/**
* Helper: calls the `search` method but with certainty that we will only request Algolia records (hits) and not facets.
Expand Down
8 changes: 6 additions & 2 deletions packages/client-abtesting/src/abtestingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ export function createAbtestingClient({
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
if (!authMode || authMode === 'WithinHeaders') {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
} else {
this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
}
},

/**
Expand Down Expand Up @@ -351,10 +355,10 @@ export function createAbtestingClient({
if (offset !== undefined) {
queryParameters.offset = offset.toString();
}

if (limit !== undefined) {
queryParameters.limit = limit.toString();
}

if (indexPrefix !== undefined) {
queryParameters.indexPrefix = indexPrefix.toString();
}
Expand Down
25 changes: 15 additions & 10 deletions packages/client-analytics/src/analyticsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ export function createAnalyticsClient({
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
if (!authMode || authMode === 'WithinHeaders') {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
} else {
this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
}
},

/**
Expand Down Expand Up @@ -288,9 +292,11 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}
Expand Down Expand Up @@ -335,13 +341,13 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (tags !== undefined) {
queryParameters.tags = tags.toString();
}
Expand Down Expand Up @@ -434,7 +440,6 @@ export function createAnalyticsClient({
if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}
Expand Down Expand Up @@ -485,7 +490,6 @@ export function createAnalyticsClient({
if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (tags !== undefined) {
queryParameters.tags = tags.toString();
}
Expand Down Expand Up @@ -527,6 +531,7 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
Expand Down Expand Up @@ -574,13 +579,13 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (tags !== undefined) {
queryParameters.tags = tags.toString();
}
Expand Down Expand Up @@ -622,7 +627,6 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
Expand Down Expand Up @@ -674,7 +678,6 @@ export function createAnalyticsClient({
if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}
Expand Down Expand Up @@ -768,16 +771,17 @@ export function createAnalyticsClient({
if (index !== undefined) {
queryParameters.index = index.toString();
}

if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}
if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (limit !== undefined) {
queryParameters.limit = limit.toString();
}

if (offset !== undefined) {
queryParameters.offset = offset.toString();
}
Expand Down Expand Up @@ -827,10 +831,10 @@ export function createAnalyticsClient({
if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}

if (limit !== undefined) {
queryParameters.limit = limit.toString();
}
Expand Down Expand Up @@ -916,6 +920,7 @@ export function createAnalyticsClient({
if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}
Expand Down Expand Up @@ -975,7 +980,6 @@ export function createAnalyticsClient({
if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}
Expand Down Expand Up @@ -1040,6 +1044,7 @@ export function createAnalyticsClient({
if (startDate !== undefined) {
queryParameters.startDate = startDate.toString();
}

if (endDate !== undefined) {
queryParameters.endDate = endDate.toString();
}
Expand Down
6 changes: 5 additions & 1 deletion packages/client-insights/src/insightsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export function createInsightsClient({
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
if (!authMode || authMode === 'WithinHeaders') {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
} else {
this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
}
},

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/client-personalization/src/personalizationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ export function createPersonalizationClient({
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
if (!authMode || authMode === 'WithinHeaders') {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
} else {
this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
}
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ export function createQuerySuggestionsClient({
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
if (!authMode || authMode === 'WithinHeaders') {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
} else {
this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
}
},

/**
Expand Down
12 changes: 7 additions & 5 deletions packages/client-search/src/searchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ export function createSearchClient({
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
if (!authMode || authMode === 'WithinHeaders') {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
} else {
this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
}
},

/**
Expand Down Expand Up @@ -1410,7 +1414,6 @@ export function createSearchClient({
.replace('{objectID}', encodeURIComponent(objectID));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}
Expand Down Expand Up @@ -2065,6 +2068,7 @@ export function createSearchClient({
if (page !== undefined) {
queryParameters.page = page.toString();
}

if (hitsPerPage !== undefined) {
queryParameters.hitsPerPage = hitsPerPage.toString();
}
Expand Down Expand Up @@ -2188,6 +2192,7 @@ export function createSearchClient({
.replace('{objectID}', encodeURIComponent(objectID));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (createIfNotExists !== undefined) {
queryParameters.createIfNotExists = createIfNotExists.toString();
}
Expand Down Expand Up @@ -2364,7 +2369,6 @@ export function createSearchClient({
.replace('{objectID}', encodeURIComponent(objectID));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}
Expand Down Expand Up @@ -2468,7 +2472,6 @@ export function createSearchClient({
.replace('{objectID}', encodeURIComponent(objectID));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}
Expand Down Expand Up @@ -2512,7 +2515,6 @@ export function createSearchClient({
const requestPath = '/1/indexes/{indexName}/synonyms/batch'.replace('{indexName}', encodeURIComponent(indexName));
const headers: Headers = {};
const queryParameters: QueryParameters = {};

if (forwardToReplicas !== undefined) {
queryParameters.forwardToReplicas = forwardToReplicas.toString();
}
Expand Down
6 changes: 5 additions & 1 deletion packages/client-usage/src/usageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export function createUsageClient({
* @param params.apiKey - The new API Key to use.
*/
setClientApiKey({ apiKey }: { apiKey: string }): void {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
if (!authMode || authMode === 'WithinHeaders') {
this.transporter.baseHeaders['x-algolia-api-key'] = apiKey;
} else {
this.transporter.baseQueryParameters['x-algolia-api-key'] = apiKey;
}
},

/**
Expand Down
Loading

0 comments on commit dac3ea5

Please sign in to comment.