Skip to content
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

fix(javascript): remove factory from build #639

Merged
merged 2 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}}

Comment on lines +26 to +35
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import * is not enough here ?

Copy link
Member Author

@shortcuts shortcuts Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we give it an alias we would have to change all methods to use Alias.TypeName so I thought it was less verbose to do it at the import level, wdyt?

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