Skip to content

Commit 4f59d81

Browse files
algolia-botshortcutsTorbjornHoltmon
committed
feat(javascript): add worker build (generated)
algolia/api-clients-automation#4249 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Clément Vannicatte <[email protected]> Co-authored-by: Torbjørn Holtmon <[email protected]>
1 parent 9a18e62 commit 4f59d81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+929
-41
lines changed

base.tsup.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type PKG = {
88

99
const requesters = {
1010
fetch: '@algolia/requester-fetch',
11+
worker: '@algolia/requester-fetch',
1112
http: '@algolia/requester-node-http',
1213
xhr: '@algolia/requester-browser-xhr',
1314
};
@@ -36,6 +37,7 @@ export function getDependencies(pkg: PKG, requester: Requester): string[] {
3637
case 'xhr':
3738
return deps.filter((dep) => dep !== requesters.fetch && dep !== requesters.http);
3839
case 'fetch':
40+
case 'worker':
3941
return deps.filter((dep) => dep !== requesters.xhr && dep !== requesters.http);
4042
default:
4143
throw new Error('unknown requester', requester);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { expect, test, vi } from 'vitest';
2+
3+
import { LogLevelEnum } from '../../client-common/src/types';
4+
import { createConsoleLogger } from '../../logger-console/src/logger';
5+
import { algoliasearch as node_algoliasearch } from '../builds/node';
6+
import { algoliasearch, apiClientVersion } from '../builds/worker';
7+
8+
test('sets the ua', () => {
9+
const client = algoliasearch('APP_ID', 'API_KEY');
10+
expect(client.transporter.algoliaAgent).toEqual({
11+
add: expect.any(Function),
12+
value: expect.stringContaining(
13+
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Worker`,
14+
),
15+
});
16+
});
17+
18+
test('forwards node search helpers', () => {
19+
const client = algoliasearch('APP_ID', 'API_KEY');
20+
expect(client.generateSecuredApiKey).not.toBeUndefined();
21+
expect(client.getSecuredApiKeyRemainingValidity).not.toBeUndefined();
22+
expect(async () => {
23+
const resp = await client.generateSecuredApiKey({ parentApiKey: 'foo', restrictions: { validUntil: 200 } });
24+
client.getSecuredApiKeyRemainingValidity({ securedApiKey: resp });
25+
}).not.toThrow();
26+
});
27+
28+
test('web crypto implementation gives the same result as node crypto', async () => {
29+
const client = algoliasearch('APP_ID', 'API_KEY');
30+
const nodeClient = node_algoliasearch('APP_ID', 'API_KEY');
31+
const resp = await client.generateSecuredApiKey({ parentApiKey: 'foo-bar', restrictions: { validUntil: 200 } });
32+
const nodeResp = await nodeClient.generateSecuredApiKey({
33+
parentApiKey: 'foo-bar',
34+
restrictions: { validUntil: 200 },
35+
});
36+
37+
expect(resp).toEqual(nodeResp);
38+
});
39+
40+
test('with logger', () => {
41+
vi.spyOn(console, 'debug');
42+
vi.spyOn(console, 'info');
43+
vi.spyOn(console, 'error');
44+
45+
const client = algoliasearch('APP_ID', 'API_KEY', {
46+
logger: createConsoleLogger(LogLevelEnum.Debug),
47+
});
48+
49+
expect(async () => {
50+
await client.setSettings({ indexName: 'foo', indexSettings: {} });
51+
expect(console.debug).toHaveBeenCalledTimes(1);
52+
expect(console.info).toHaveBeenCalledTimes(1);
53+
expect(console.error).toHaveBeenCalledTimes(1);
54+
}).not.toThrow();
55+
});
+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.
2+
3+
import type { ClientOptions } from '@algolia/client-common';
4+
5+
import type { AbtestingClient } from '@algolia/client-abtesting';
6+
import { abtestingClient } from '@algolia/client-abtesting';
7+
import type { AnalyticsClient } from '@algolia/client-analytics';
8+
import { analyticsClient } from '@algolia/client-analytics';
9+
import type { InsightsClient } from '@algolia/client-insights';
10+
import { insightsClient } from '@algolia/client-insights';
11+
import type { PersonalizationClient } from '@algolia/client-personalization';
12+
import { personalizationClient } from '@algolia/client-personalization';
13+
import type { QuerySuggestionsClient } from '@algolia/client-query-suggestions';
14+
import { querySuggestionsClient } from '@algolia/client-query-suggestions';
15+
import type { SearchClient } from '@algolia/client-search';
16+
import { searchClient } from '@algolia/client-search';
17+
import type { IngestionClient } from '@algolia/ingestion';
18+
import { ingestionClient } from '@algolia/ingestion';
19+
import type { MonitoringClient } from '@algolia/monitoring';
20+
import { monitoringClient } from '@algolia/monitoring';
21+
import type { RecommendClient } from '@algolia/recommend';
22+
import { recommendClient } from '@algolia/recommend';
23+
24+
import type {
25+
AbtestingRegionOptions,
26+
AnalyticsRegionOptions,
27+
IngestionRegionOptions,
28+
InitClientOptions,
29+
InsightsRegionOptions,
30+
PersonalizationRegionOptions,
31+
QuerySuggestionsRegionOptions,
32+
} from './models';
33+
34+
export * from './models';
35+
36+
export type Algoliasearch = SearchClient & {
37+
initAbtesting: (initOptions: InitClientOptions & AbtestingRegionOptions) => AbtestingClient;
38+
initAnalytics: (initOptions: InitClientOptions & AnalyticsRegionOptions) => AnalyticsClient;
39+
initIngestion: (initOptions: InitClientOptions & IngestionRegionOptions) => IngestionClient;
40+
initInsights: (initOptions: InitClientOptions & InsightsRegionOptions) => InsightsClient;
41+
initMonitoring: (initOptions?: InitClientOptions) => MonitoringClient;
42+
initPersonalization: (initOptions: InitClientOptions & PersonalizationRegionOptions) => PersonalizationClient;
43+
initQuerySuggestions: (initOptions: InitClientOptions & QuerySuggestionsRegionOptions) => QuerySuggestionsClient;
44+
initRecommend: (initOptions?: InitClientOptions) => RecommendClient;
45+
};
46+
47+
export function algoliasearch(appId: string, apiKey: string, options?: ClientOptions): Algoliasearch {
48+
if (!appId || typeof appId !== 'string') {
49+
throw new Error('`appId` is missing.');
50+
}
51+
52+
if (!apiKey || typeof apiKey !== 'string') {
53+
throw new Error('`apiKey` is missing.');
54+
}
55+
56+
const client = searchClient(appId, apiKey, options);
57+
58+
return {
59+
...client,
60+
61+
/**
62+
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
63+
*/
64+
get _ua(): string {
65+
return client.transporter.algoliaAgent.value;
66+
},
67+
68+
initAbtesting: (initOptions: InitClientOptions & AbtestingRegionOptions): AbtestingClient => {
69+
return abtestingClient(
70+
initOptions.appId || appId,
71+
initOptions.apiKey || apiKey,
72+
initOptions.region,
73+
initOptions.options,
74+
);
75+
},
76+
77+
initAnalytics: (initOptions: InitClientOptions & AnalyticsRegionOptions): AnalyticsClient => {
78+
return analyticsClient(
79+
initOptions.appId || appId,
80+
initOptions.apiKey || apiKey,
81+
initOptions.region,
82+
initOptions.options,
83+
);
84+
},
85+
86+
initIngestion: (initOptions: InitClientOptions & IngestionRegionOptions): IngestionClient => {
87+
return ingestionClient(
88+
initOptions.appId || appId,
89+
initOptions.apiKey || apiKey,
90+
initOptions.region,
91+
initOptions.options,
92+
);
93+
},
94+
95+
initInsights: (initOptions: InitClientOptions & InsightsRegionOptions): InsightsClient => {
96+
return insightsClient(
97+
initOptions.appId || appId,
98+
initOptions.apiKey || apiKey,
99+
initOptions.region,
100+
initOptions.options,
101+
);
102+
},
103+
104+
initMonitoring: (initOptions: InitClientOptions = {}): MonitoringClient => {
105+
return monitoringClient(initOptions.appId || appId, initOptions.apiKey || apiKey, initOptions.options);
106+
},
107+
108+
initPersonalization: (initOptions: InitClientOptions & PersonalizationRegionOptions): PersonalizationClient => {
109+
return personalizationClient(
110+
initOptions.appId || appId,
111+
initOptions.apiKey || apiKey,
112+
initOptions.region,
113+
initOptions.options,
114+
);
115+
},
116+
117+
initQuerySuggestions: (initOptions: InitClientOptions & QuerySuggestionsRegionOptions): QuerySuggestionsClient => {
118+
return querySuggestionsClient(
119+
initOptions.appId || appId,
120+
initOptions.apiKey || apiKey,
121+
initOptions.region,
122+
initOptions.options,
123+
);
124+
},
125+
126+
initRecommend: (initOptions: InitClientOptions = {}): RecommendClient => {
127+
return recommendClient(initOptions.appId || appId, initOptions.apiKey || apiKey, initOptions.options);
128+
},
129+
};
130+
}

packages/algoliasearch/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"require": "./dist/node.cjs"
3030
},
3131
"worker": {
32-
"types": "./dist/fetch.d.ts",
33-
"default": "./dist/fetch.js"
32+
"types": "./dist/worker.d.ts",
33+
"default": "./dist/worker.js"
3434
},
3535
"default": {
3636
"types": "./dist/browser.d.ts",

packages/algoliasearch/tsup.config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ const nodeConfigs: Options[] = [
4848
outDir: 'dist',
4949
external: getDependencies(pkg, 'fetch'),
5050
},
51+
{
52+
...nodeOptions,
53+
format: 'esm',
54+
name: 'worker algoliasearch esm',
55+
dts: { entry: { worker: 'builds/worker.ts' } },
56+
entry: ['builds/worker.ts'],
57+
outDir: 'dist',
58+
external: getDependencies(pkg, 'worker'),
59+
},
5160
];
5261

5362
const browserOptions: Options = {

packages/algoliasearch/vitest.workspace.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,19 @@ export default defineWorkspace([
3333
},
3434
test: {
3535
include: ['__tests__/algoliasearch.fetch.test.ts'],
36-
name: 'miniflare',
36+
name: 'miniflare fetch',
37+
environment: 'miniflare',
38+
},
39+
},
40+
{
41+
resolve: {
42+
alias: {
43+
'@algolia/client-search': '../../client-search/builds/worker',
44+
},
45+
},
46+
test: {
47+
include: ['__tests__/algoliasearch.worker.test.ts'],
48+
name: 'miniflare worker',
3749
environment: 'miniflare',
3850
},
3951
},

packages/client-abtesting/builds/fetch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export function abtestingClient(
4747
write: 30000,
4848
},
4949
logger: createNullLogger(),
50-
algoliaAgents: [{ segment: 'Fetch' }],
5150
requester: createFetchRequester(),
51+
algoliaAgents: [{ segment: 'Fetch' }],
5252
responsesCache: createNullCache(),
5353
requestsCache: createNullCache(),
5454
hostsCache: createMemoryCache(),
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.
2+
3+
export type AbtestingClient = ReturnType<typeof createAbtestingClient>;
4+
5+
import { createMemoryCache, createNullCache, createNullLogger } from '@algolia/client-common';
6+
import { createFetchRequester } from '@algolia/requester-fetch';
7+
8+
import type { ClientOptions } from '@algolia/client-common';
9+
10+
import { createAbtestingClient } from '../src/abtestingClient';
11+
12+
import type { Region } from '../src/abtestingClient';
13+
import { REGIONS } from '../src/abtestingClient';
14+
15+
export type { Region, RegionOptions } from '../src/abtestingClient';
16+
17+
export { apiClientVersion } from '../src/abtestingClient';
18+
19+
export * from '../model';
20+
21+
export function abtestingClient(
22+
appId: string,
23+
apiKey: string,
24+
region?: Region,
25+
options?: ClientOptions,
26+
): AbtestingClient {
27+
if (!appId || typeof appId !== 'string') {
28+
throw new Error('`appId` is missing.');
29+
}
30+
31+
if (!apiKey || typeof apiKey !== 'string') {
32+
throw new Error('`apiKey` is missing.');
33+
}
34+
35+
if (region && (typeof region !== 'string' || !REGIONS.includes(region))) {
36+
throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`);
37+
}
38+
39+
return {
40+
...createAbtestingClient({
41+
appId,
42+
apiKey,
43+
region,
44+
timeouts: {
45+
connect: 2000,
46+
read: 5000,
47+
write: 30000,
48+
},
49+
logger: createNullLogger(),
50+
requester: createFetchRequester(),
51+
algoliaAgents: [{ segment: 'Worker' }],
52+
responsesCache: createNullCache(),
53+
requestsCache: createNullCache(),
54+
hostsCache: createMemoryCache(),
55+
...options,
56+
}),
57+
};
58+
}

packages/client-abtesting/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
"require": "./dist/builds/node.cjs"
2929
},
3030
"worker": {
31-
"types": "./dist/fetch.d.ts",
32-
"default": "./dist/builds/fetch.js"
31+
"types": "./dist/worker.d.ts",
32+
"default": "./dist/builds/worker.js"
3333
},
3434
"default": {
3535
"types": "./dist/browser.d.ts",

packages/client-abtesting/tsup.config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ const nodeConfigs: Options[] = [
3030
external: getDependencies(pkg, 'fetch'),
3131
entry: ['builds/fetch.ts', 'src/*.ts'],
3232
},
33+
{
34+
...nodeOptions,
35+
format: 'esm',
36+
name: `worker ${pkg.name} esm`,
37+
dts: { entry: { worker: 'builds/worker.ts' } },
38+
external: getDependencies(pkg, 'worker'),
39+
entry: ['builds/worker.ts', 'src/*.ts'],
40+
},
3341
];
3442

3543
const browserOptions: Options = {

packages/client-analytics/builds/fetch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export function analyticsClient(
4747
write: 30000,
4848
},
4949
logger: createNullLogger(),
50-
algoliaAgents: [{ segment: 'Fetch' }],
5150
requester: createFetchRequester(),
51+
algoliaAgents: [{ segment: 'Fetch' }],
5252
responsesCache: createNullCache(),
5353
requestsCache: createNullCache(),
5454
hostsCache: createMemoryCache(),

0 commit comments

Comments
 (0)