Skip to content

Commit

Permalink
fix(javascript): remove factory from build (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Jun 6, 2022
1 parent 6acaa20 commit 2a1f30c
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 49 deletions.
20 changes: 10 additions & 10 deletions clients/algoliasearch-client-javascript/bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@
"files": [
{
"path": "packages/algoliasearch/dist/algoliasearch.umd.browser.js",
"maxSize": "7.40KB"
"maxSize": "7.50KB"
},
{
"path": "packages/algoliasearch-lite/dist/algoliasearch-lite.umd.browser.js",
"maxSize": "3.70KB"
"maxSize": "3.75KB"
},
{
"path": "packages/client-abtesting/dist/client-abtesting.umd.browser.js",
"maxSize": "3.85KB"
"maxSize": "3.90KB"
},
{
"path": "packages/client-analytics/dist/client-analytics.umd.browser.js",
"maxSize": "4.45KB"
"maxSize": "4.50KB"
},
{
"path": "packages/client-insights/dist/client-insights.umd.browser.js",
"maxSize": "3.70KB"
"maxSize": "3.75KB"
},
{
"path": "packages/client-personalization/dist/client-personalization.umd.browser.js",
"maxSize": "3.85KB"
"maxSize": "3.95KB"
},
{
"path": "packages/client-query-suggestions/dist/client-query-suggestions.umd.browser.js",
"maxSize": "3.90KB"
"maxSize": "4.00KB"
},
{
"path": "packages/client-search/dist/client-search.umd.browser.js",
"maxSize": "6.30KB"
"maxSize": "6.25KB"
},
{
"path": "packages/client-sources/dist/client-sources.umd.browser.js",
"maxSize": "3.70KB"
"maxSize": "3.85KB"
},
{
"path": "packages/recommend/dist/recommend.umd.browser.js",
"maxSize": "3.80KB"
"maxSize": "3.85KB"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import type {
import { createPersonalizationClient } from '@experimental-api-clients-automation/client-personalization/src/personalizationClient';
import {
createSearchClient,
apiClientVersion,
apiClientVersion as searchClientVersion,
} from '@experimental-api-clients-automation/client-search/src/searchClient';
import { createXhrRequester } from '@experimental-api-clients-automation/requester-browser-xhr';

export const apiClientVersion = searchClientVersion;

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function algoliasearch(
appId: string,
Expand Down Expand Up @@ -98,3 +100,5 @@ export function algoliasearch(
initPersonalization,
};
}

export type Algoliasearch = ReturnType<typeof algoliasearch>;
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ import type {
Region as PersonalizationRegion,
} from '@experimental-api-clients-automation/client-personalization/src/personalizationClient';
import { createPersonalizationClient } from '@experimental-api-clients-automation/client-personalization/src/personalizationClient';
import { createSearchClient } from '@experimental-api-clients-automation/client-search/src/searchClient';
import {
createSearchClient,
apiClientVersion as searchClientVersion,
} from '@experimental-api-clients-automation/client-search/src/searchClient';
import { createHttpRequester } from '@experimental-api-clients-automation/requester-node-http';

export const apiClientVersion = searchClientVersion;

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function algoliasearch(
appId: string,
Expand Down Expand Up @@ -88,3 +93,5 @@ export function algoliasearch(
initPersonalization,
};
}

export type Algoliasearch = ReturnType<typeof algoliasearch>;
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void processOpts() {
// clear all supported files to avoid unwanted ones
supportingFiles.clear();

supportingFiles.add(new SupportingFile("clientMethodProps.mustache", "model", "clientMethodProps.ts"));
supportingFiles.add(new SupportingFile("modelBarrel.mustache", "model", "index.ts"));
supportingFiles.add(new SupportingFile("browser.mustache", "builds", "browser.ts"));
supportingFiles.add(new SupportingFile("node.mustache", "builds", "node.ts"));
Expand Down
13 changes: 9 additions & 4 deletions playground/javascript/node/algoliasearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { algoliasearch } from '@experimental-api-clients-automation/algoliasearc
import { ApiError } from '@experimental-api-clients-automation/client-common';
import dotenv from 'dotenv';

import type { SearchResponse } from '@experimental-api-clients-automation/client-search';
import type { SearchResponses } from '@experimental-api-clients-automation/client-search';

dotenv.config({ path: '../../.env' });

Expand Down Expand Up @@ -31,9 +31,14 @@ client.addAlgoliaAgent('algoliasearch node playground', '0.0.1');

async function testAlgoliasearch() {
try {
const res: SearchResponse = await client.search({
indexName: searchIndex,
searchParams: { query: searchQuery },
const res: SearchResponses = await client.search({
requests: [
{
indexName: searchIndex,
query: searchQuery,
hitsPerPage: 50,
},
],
});

console.log(`[OK search]`, res);
Expand Down
33 changes: 10 additions & 23 deletions templates/javascript/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ import type {
import { {{classname}} } from '{{filename}}';
{{/imports}}

export * from '../model';
{{#operations}}
import type {
{{#operation}}
{{#vendorExtensions.x-create-wrapping-object}}
{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props,
{{/vendorExtensions.x-create-wrapping-object}}
{{/operation}}
} from '../model/clientMethodProps';
{{/operations}}

export const apiClientVersion = '{{packageVersion}}';

{{#operations}}
{{#description}}
/**
* {{&description}}
*/
{{/description}}

{{#hasRegionalHost}}
export const REGIONS = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}] as const;
export type Region = typeof REGIONS[number];
Expand Down Expand Up @@ -244,20 +247,4 @@ export function create{{capitalizedApiName}}({

export type {{capitalizedApiName}} = ReturnType<typeof create{{capitalizedApiName}}>;

{{#operation}}
{{#vendorExtensions.x-create-wrapping-object}}
export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
{{#allParams}}
{{#description}}
/**
* {{{description}}}
*/
{{/description}}
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
{{/allParams}}
}
{{/vendorExtensions.x-create-wrapping-object}}

{{/operation}}

{{/operations}}
14 changes: 9 additions & 5 deletions templates/javascript/browser.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import type { {{capitalizedApiName}} } from '../src/{{apiName}}';
import { Region, REGIONS } from '../src/{{apiName}}';
{{/hasRegionalHost}}

export * from '../src/{{apiName}}';
{{! We don't use `export *` to prevent exposing the factory, to avoid confusion for the user }}
export {
apiClientVersion,
{{capitalizedApiName}},
} from '../src/{{apiName}}';
export * from '../model';

export function {{apiName}}(
appId: string,
Expand All @@ -24,16 +29,15 @@ export function {{apiName}}(
throw new Error("`apiKey` is missing.");
}

{{#hasRegionalHost}}
{{^fallbackToAliasHost}}
{{#hasRegionalHost}}{{^fallbackToAliasHost}}
if (!region) {
throw new Error("`region` is missing.");
}

if (typeof region !== 'string' || !REGIONS.includes(region)) {
throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`);
}
{{/fallbackToAliasHost}}
{{/hasRegionalHost}}
{{/fallbackToAliasHost}}{{/hasRegionalHost}}

return create{{capitalizedApiName}}({
appId,
Expand Down
27 changes: 27 additions & 0 deletions templates/javascript/clientMethodProps.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{#apiInfo.apis.0}}

{{#imports}}
import { {{classname}} } from '{{filename}}';
{{/imports}}

{{#operations}}
{{#operation}}
{{#vendorExtensions.x-create-wrapping-object}}
/**
* Properties for the `{{nickname}}` method.
*/
export type {{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Props = {
{{#allParams}}
{{#description}}
/**
* {{{description}}}
*/
{{/description}}
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
{{/allParams}}
}
{{/vendorExtensions.x-create-wrapping-object}}

{{/operation}}
{{/operations}}
{{/apiInfo.apis.0}}
1 change: 1 addition & 0 deletions templates/javascript/modelBarrel.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
export * from '{{{ classFilename }}}';
{{/model}}
{{/models}}
export * from './clientMethodProps'
14 changes: 9 additions & 5 deletions templates/javascript/node.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import type { {{capitalizedApiName}} } from '../src/{{apiName}}';
import { Region, REGIONS } from '../src/{{apiName}}';
{{/hasRegionalHost}}

export * from '../src/{{apiName}}';
{{! We don't use `export *` to prevent exposing the factory, to avoid confusion for the user }}
export {
apiClientVersion,
{{capitalizedApiName}},
} from '../src/{{apiName}}';
export * from '../model';

export function {{apiName}}(
appId: string,
Expand All @@ -24,16 +29,15 @@ export function {{apiName}}(
throw new Error("`apiKey` is missing.");
}

{{#hasRegionalHost}}
{{^fallbackToAliasHost}}
{{#hasRegionalHost}}{{^fallbackToAliasHost}}
if (!region) {
throw new Error("`region` is missing.");
}

if (typeof region !== 'string' || !REGIONS.includes(region)) {
throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`);
}
{{/fallbackToAliasHost}}
{{/hasRegionalHost}}
{{/fallbackToAliasHost}}{{/hasRegionalHost}}

return create{{capitalizedApiName}}({
appId,
Expand Down

0 comments on commit 2a1f30c

Please sign in to comment.