Skip to content

Commit

Permalink
feat(javascript): provide algoliasearch package (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Feb 23, 2022
1 parent d5c6bf8 commit c06f890
Show file tree
Hide file tree
Showing 14 changed files with 357 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ runs:
path: /home/runner/work/api-clients-automation/api-clients-automation/clients/algoliasearch-client-javascript/packages/requester-browser-xhr/dist
key: ${{ runner.os }}-${{ env.CACHE_VERSION }}-js-requester-browser-xhr-${{ hashFiles('clients/algoliasearch-client-javascript/packages/requester-browser-xhr/**') }}

- name: Restore built JavaScript algoliasearch client
if: ${{ inputs.job == 'cts' }}
uses: actions/cache@v2
with:
path: /home/runner/work/api-clients-automation/api-clients-automation/clients/algoliasearch-client-javascript/packages/algoliasearch/dist
key: ${{ runner.os }}-${{ env.CACHE_VERSION }}-js-algoliasearch-${{ hashFiles('clients/algoliasearch-client-javascript/packages/algoliasearch/**') }}

- name: Restore built JavaScript search client
if: ${{ inputs.job == 'cts' }}
uses: actions/cache@v2
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ jobs:
- client-common
- requester-browser-xhr
- requester-node-http
- algoliasearch
steps:
- uses: actions/checkout@v2

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"files": [
{
"path": "packages/algoliasearch/dist/algoliasearch.umd.browser.js",
"maxSize": "6.25KB"
},
{
"path": "packages/client-abtesting/dist/client-abtesting.umd.browser.js",
"maxSize": "3.25KB"
Expand All @@ -14,7 +18,7 @@
},
{
"path": "packages/client-personalization/dist/client-personalization.umd.browser.js",
"maxSize": "3.00KB"
"maxSize": "3.25KB"
},
{
"path": "packages/client-query-suggestions/dist/client-query-suggestions.umd.browser.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import type {
AnalyticsApi,
Region as AnalyticsRegion,
} from '@algolia/client-analytics/src/analyticsApi';
import { createAnalyticsApi } from '@algolia/client-analytics/src/analyticsApi';
import type {
CreateClientOptions,
Host,
Requester,
} from '@algolia/client-common';
import type {
PersonalizationApi,
Region as PersonalizationRegion,
} from '@algolia/client-personalization/src/personalizationApi';
import { createPersonalizationApi } from '@algolia/client-personalization/src/personalizationApi';
import { createSearchApi } from '@algolia/client-search/src/searchApi';
import { createXhrRequester } from '@algolia/requester-browser-xhr';

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function algoliasearch(
appId: string,
apiKey: string,
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}

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

const commonOptions: Omit<CreateClientOptions, 'apiKey' | 'appId'> = {
timeouts: {
connect: 1,
read: 2,
write: 30,
},
requester: options?.requester ?? createXhrRequester(),
userAgents: [{ segment: 'Browser' }],
authMode: 'WithinQueryParameters',
...options,
};

function initAnalytics(
analyticsAppId: string,
analyticsApiKey: string,
region?: AnalyticsRegion,
analyticsOptions?: { requester?: Requester; hosts?: Host[] }
): AnalyticsApi {
return createAnalyticsApi({
appId: analyticsAppId,
apiKey: analyticsApiKey,
region,
...analyticsOptions,
...commonOptions,
});
}

function initPersonalization(
personalizationAppId: string,
personalizationApiKey: string,
region: PersonalizationRegion,
personalizationOptions?: { requester?: Requester; hosts?: Host[] }
): PersonalizationApi {
if (!region) {
throw new Error('`region` is missing.');
}

return createPersonalizationApi({
appId: personalizationAppId,
apiKey: personalizationApiKey,
region,
...personalizationOptions,
...commonOptions,
});
}

return {
...createSearchApi({ appId, apiKey, ...commonOptions }),
initAnalytics,
initPersonalization,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type {
AnalyticsApi,
Region as AnalyticsRegion,
} from '@algolia/client-analytics/src/analyticsApi';
import { createAnalyticsApi } from '@algolia/client-analytics/src/analyticsApi';
import type {
CreateClientOptions,
Host,
Requester,
} from '@algolia/client-common';
import type {
PersonalizationApi,
Region as PersonalizationRegion,
} from '@algolia/client-personalization/src/personalizationApi';
import { createPersonalizationApi } from '@algolia/client-personalization/src/personalizationApi';
import { createSearchApi } from '@algolia/client-search/src/searchApi';
import { createHttpRequester } from '@algolia/requester-node-http';

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function algoliasearch(
appId: string,
apiKey: string,
options?: { requester?: Requester; hosts?: Host[] }
) {
if (!appId) {
throw new Error('`appId` is missing.');
}

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

const commonOptions: Omit<CreateClientOptions, 'apiKey' | 'appId'> = {
timeouts: {
connect: 2,
read: 5,
write: 30,
},
requester: options?.requester ?? createHttpRequester(),
userAgents: [{ segment: 'Node.js', version: process.versions.node }],
...options,
};

function initAnalytics(
analyticsAppId: string,
analyticsApiKey: string,
region?: AnalyticsRegion,
analyticsOptions?: { requester?: Requester; hosts?: Host[] }
): AnalyticsApi {
return createAnalyticsApi({
appId: analyticsAppId,
apiKey: analyticsApiKey,
region,
...analyticsOptions,
...commonOptions,
});
}

function initPersonalization(
personalizationAppId: string,
personalizationApiKey: string,
region: PersonalizationRegion,
personalizationOptions?: { requester?: Requester; hosts?: Host[] }
): PersonalizationApi {
if (!region) {
throw new Error('`region` is missing.');
}

return createPersonalizationApi({
appId: personalizationAppId,
apiKey: personalizationApiKey,
region,
...personalizationOptions,
...commonOptions,
});
}

return {
...createSearchApi({ appId, apiKey, ...commonOptions }),
initAnalytics,
initPersonalization,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/no-unresolved
export * from './dist/algoliasearch/builds/node';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/no-commonjs,import/extensions
module.exports = require('./dist/algoliasearch.cjs.node.js');
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "algoliasearch",
"version": "5.0.0",
"description": "A fully-featured and blazing-fast JavaScript API client to interact with Algolia API.",
"repository": "algolia/algoliasearch-client-javascript",
"author": "Algolia",
"license": "MIT",
"main": "index.js",
"module": "dist/algoliasearch.esm.node.js",
"types": "index.d.ts",
"jsdelivr": "dist/algoliasearch.umd.browser.js",
"unpkg": "dist/algoliasearch.umd.browser.js",
"browser": {
"./index.js": "./dist/algoliasearch.cjs.browser.js",
"./lite.js": "./dist/algoliasearch-lite.cjs.browser.js"
},
"scripts": {
"clean": "rm -rf ./dist"
},
"engines": {
"node": "^14.0.0"
},
"dependencies": {
"@algolia/client-analytics": "5.0.0",
"@algolia/client-common": "5.0.0",
"@algolia/client-personalization": "5.0.0",
"@algolia/client-search": "5.0.0",
"@algolia/requester-browser-xhr": "5.0.0",
"@algolia/requester-node-http": "5.0.0"
},
"devDependencies": {
"@types/node": "16.11.11",
"typescript": "4.5.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["builds/node.ts", "builds/browser.ts"],
"exclude": ["dist", "node_modules"]
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { Host } from './Host';
import type { Requester } from './Requester';
import type { Timeouts, UserAgentOptions } from './Transporter';

export type AuthMode = 'WithinHeaders' | 'WithinQueryParameters';

export type CreateClientOptions = {
appId: string;
apiKey: string;
requester: any;
requester: Requester;
timeouts: Timeouts;
userAgents: UserAgentOptions[];
hosts?: Host[];
Expand Down
28 changes: 21 additions & 7 deletions clients/algoliasearch-client-javascript/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ function createBundlers({ output, clientPath }) {
}

function getAvailableClients() {
const availableClients = [];
// We default `algoliasearch` as it's not a generated client, but an aggregation of
// multiple clients.
const availableClients = ['algoliasearch'];
const generators = Object.entries(
generatorConfig['generator-cli'].generators
);
Expand Down Expand Up @@ -112,15 +114,19 @@ function initPackagesConfig() {
}

return availableClients.flatMap((packageName) => {
const isAlgoliasearchClient = packageName.startsWith('algoliasearch');
const commonConfig = {
package: packageName,
name: `@algolia/${packageName}`,
name: isAlgoliasearchClient ? packageName : `@algolia/${packageName}`,
output: packageName,
dependencies: [
'@algolia/client-common',
'@algolia/requester-browser-xhr',
'@algolia/requester-node-http',
],
dependencies: isAlgoliasearchClient
? [
'@algolia/client-analytics',
'@algolia/client-common',
'@algolia/client-personalization',
'@algolia/client-search',
]
: ['@algolia/client-common'],
external: [],
};
const browserFormats = ['umd-browser', 'esm-browser', 'cjs-browser'];
Expand All @@ -132,13 +138,21 @@ function initPackagesConfig() {
input: 'builds/browser.ts',
formats: browserFormats,
external: ['dom'],
dependencies: [
...commonConfig.dependencies,
'@algolia/requester-browser-xhr',
],
globals: {
[packageName]: packageName,
},
},
{
...commonConfig,
input: 'builds/node.ts',
dependencies: [
...commonConfig.dependencies,
'@algolia/requester-node-http',
],
formats: nodeFormats,
},
];
Expand Down
Loading

0 comments on commit c06f890

Please sign in to comment.