Skip to content

Commit

Permalink
feat(clients): retrieve hosts from spec file (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Feb 2, 2022
1 parent a29c4d5 commit bf5203e
Show file tree
Hide file tree
Showing 24 changed files with 307 additions and 164 deletions.
21 changes: 21 additions & 0 deletions base.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"target": "ES6",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"strict": true,
"moduleResolution": "node",
"removeComments": true,
"sourceMap": true,
"noLib": false,
"declaration": true,
"lib": ["ESNext"],
"outDir": "dist",
"typeRoots": ["node_modules/@types"],
"types": ["node"],
"resolveJsonModule": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,16 @@ export class AbtestingApi {
constructor(
appId: string,
apiKey: string,
region: 'de' | 'us',
region?: 'de' | 'us',
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}
if (!region) {
throw new Error('`region` is missing.');
}

this.setAuthentication({ appId, apiKey });

Expand All @@ -82,10 +80,12 @@ export class AbtestingApi {
});
}

getDefaultHosts(region: 'de' | 'us'): Host[] {
getDefaultHosts(region?: 'de' | 'us'): Host[] {
const regionHost = region ? `.${region}.` : '.';

return [
{
url: `analytics.${region}.algolia.com`,
url: `analytics${regionHost}algolia.com`,
accept: 'readWrite',
protocol: 'https',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ export class AnalyticsApi {
constructor(
appId: string,
apiKey: string,
region: 'de' | 'us',
region?: 'de' | 'us',
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}
Expand All @@ -94,10 +95,12 @@ export class AnalyticsApi {
});
}

getDefaultHosts(region: 'de' | 'us' = 'us'): Host[] {
getDefaultHosts(region?: 'de' | 'us'): Host[] {
const regionHost = region ? `.${region}.` : '.';

return [
{
url: `analytics.${region}.algolia.com`,
url: `analytics${regionHost}algolia.com`,
accept: 'readWrite',
protocol: 'https',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,21 @@ export class InsightsApi {
constructor(
appId: string,
apiKey: string,
region?: 'de' | 'us',
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
hosts: options?.hosts ?? this.getDefaultHosts(),
hosts: options?.hosts ?? this.getDefaultHosts(region),
baseHeaders: {
'content-type': 'application/x-www-form-urlencoded',
},
Expand All @@ -76,9 +78,15 @@ export class InsightsApi {
});
}

getDefaultHosts(): Host[] {
getDefaultHosts(region?: 'de' | 'us'): Host[] {
const regionHost = region ? `.${region}.` : '.';

return [
{ url: 'insights.algolia.io', accept: 'readWrite', protocol: 'https' },
{
url: `insights${regionHost}algolia.io`,
accept: 'readWrite',
protocol: 'https',
},
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ export class PersonalizationApi {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

if (!region) {
throw new Error('`region` is missing.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ export class QuerySuggestionsApi {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

if (!region) {
throw new Error('`region` is missing.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export class SearchApi {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ export class SourcesApi {
region: 'de' | 'us',
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}

if (!region) {
throw new Error('`region` is missing.');
}

this.setAuthentication({ appId, apiKey });

this.transporter = new Transporter({
Expand All @@ -70,7 +82,7 @@ export class SourcesApi {
});
}

getDefaultHosts(region: 'de' | 'us' = 'us'): Host[] {
getDefaultHosts(region: 'de' | 'us'): Host[] {
return [
{
url: `data.${region}.algolia.com`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class RecommendApi {
if (!appId) {
throw new Error('`appId` is missing.');
}

if (!apiKey) {
throw new Error('`apiKey` is missing.');
}
Expand Down
43 changes: 21 additions & 22 deletions openapitools.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"generator-cli": {
"version": "5.3.0",
"version": "5.4.0",
"generators": {
"javascript-search": {
"generatorName": "typescript-node",
Expand All @@ -19,9 +19,7 @@
"supportsES6": true,
"npmName": "@algolia/client-search",
"packageVersion": "5.0.0",

"packageName": "@algolia/client-search",
"isSearchHost": true
"packageName": "@algolia/client-search"
}
},
"javascript-recommend": {
Expand All @@ -40,9 +38,7 @@
"supportsES6": true,
"npmName": "@algolia/recommend",
"packageVersion": "5.0.0",

"packageName": "@algolia/recommend",
"isSearchHost": true
"packageName": "@algolia/recommend"
}
},
"javascript-personalization": {
Expand All @@ -60,11 +56,11 @@
"supportsES6": true,
"npmName": "@algolia/client-personalization",
"packageVersion": "5.0.0",

"packageName": "@algolia/client-personalization",
"hasRegionalHost": true,
"isEuHost": true,
"host": "personalization"
"host": "personalization",
"topLevelDomain": "com"
}
},
"javascript-analytics": {
Expand All @@ -82,12 +78,12 @@
"supportsES6": true,
"npmName": "@algolia/client-analytics",
"packageVersion": "5.0.0",

"packageName": "@algolia/client-analytics",
"fallbackToAliasHost": true,
"hasRegionalHost": true,
"isDeHost": true,
"fallbackToUS": true,
"host": "analytics"
"host": "analytics",
"topLevelDomain": "com"
}
},
"javascript-insights": {
Expand All @@ -105,9 +101,12 @@
"supportsES6": true,
"npmName": "@algolia/client-insights",
"packageVersion": "5.0.0",

"packageName": "@algolia/client-insights",
"host": "insights"
"fallbackToAliasHost": true,
"hasRegionalHost": true,
"isDeHost": true,
"host": "insights",
"topLevelDomain": "io"
}
},
"javascript-abtesting": {
Expand All @@ -125,11 +124,12 @@
"supportsES6": true,
"npmName": "@algolia/client-abtesting",
"packageVersion": "5.0.0",

"packageName": "@algolia/client-abtesting",
"hasRegionalHost": true,
"fallbackToAliasHost": true,
"isDeHost": true,
"host": "analytics"
"host": "analytics",
"topLevelDomain": "com"
}
},
"javascript-query-suggestions": {
Expand All @@ -147,11 +147,11 @@
"supportsES6": true,
"npmName": "@algolia/client-query-suggestions",
"packageVersion": "5.0.0",

"packageName": "@algolia/client-query-suggestions",
"hasRegionalHost": true,
"isEuHost": true,
"host": "query-suggestions"
"host": "query-suggestions",
"topLevelDomain": "com"
}
},
"javascript-sources": {
Expand All @@ -169,11 +169,11 @@
"supportsES6": true,
"npmName": "@algolia/client-sources",
"packageVersion": "0.0.1",

"packageName": "@algolia/client-sources",
"hasRegionalHost": true,
"isDeHost": true,
"host": "data"
"host": "data",
"topLevelDomain": "com"
}
},
"java-search": {
Expand All @@ -195,10 +195,9 @@
"sourceFolder": "algoliasearch-core",
"java8": true,
"dateLibrary": "java8",

"packageName": "algoliasearch-client-java-2"
}
}
}
}
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"workspaces": [
"clients/algoliasearch-client-javascript/*",
"playground/javascript/",
"tests/"
"tests/",
"scripts/"
],
"scripts": {
"build:clients": "./scripts/multiplexer.sh ${2:-nonverbose} ./scripts/builds/clients.sh ${0:-all} ${1:-all}",
Expand All @@ -29,7 +30,7 @@
"github-actions:lint": "eslint --ext=yml .github/"
},
"devDependencies": {
"@openapitools/openapi-generator-cli": "2.4.18",
"@openapitools/openapi-generator-cli": "2.4.26",
"@redocly/openapi-cli": "1.0.0-beta.79",
"@typescript-eslint/eslint-plugin": "5.5.0",
"@typescript-eslint/parser": "5.5.0",
Expand Down
17 changes: 2 additions & 15 deletions playground/javascript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
{
"extends": "../base.tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"target": "ES6",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"strict": true,
"moduleResolution": "node",
"removeComments": true,
"sourceMap": true,
"noLib": false,
"declaration": true,
"lib": ["dom", "es6", "es5", "dom.iterable", "scripthost"],
"outDir": "dist",
"typeRoots": ["node_modules/@types"]
"outDir": "dist"
},
"include": ["*.ts"]
}
5 changes: 5 additions & 0 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ run_pre_gen() {
echo "> Running pre-gen script for $GENERATOR..."
$pregen $CLIENT
fi

# Sets the hosts option to the `openapitools.json` config file
yarn workspace scripts setHostsOptions $LANGUAGE $CLIENT
}

generate_client() {
echo "> Generating code for $GENERATOR..."

CMD="yarn openapi-generator-cli generate --generator-key $GENERATOR"

if [[ $VERBOSE == "true" ]]; then
$CMD
else
Expand Down
Loading

0 comments on commit bf5203e

Please sign in to comment.