|
1 | 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 | 2 |
|
3 | | -import { createHmac } from 'node:crypto'; |
4 | | - |
5 | 3 | import type { AbtestingClient, Region as AbtestingRegion } from '@algolia/client-abtesting'; |
6 | 4 | import { abtestingClient } from '@algolia/client-abtesting'; |
7 | 5 | import type { AnalyticsClient, Region as AnalyticsRegion } from '@algolia/client-analytics'; |
8 | 6 | import { analyticsClient } from '@algolia/client-analytics'; |
9 | | -import { |
10 | | - DEFAULT_CONNECT_TIMEOUT_NODE, |
11 | | - DEFAULT_READ_TIMEOUT_NODE, |
12 | | - DEFAULT_WRITE_TIMEOUT_NODE, |
13 | | - createMemoryCache, |
14 | | - createNullCache, |
15 | | - serializeQueryParameters, |
16 | | -} from '@algolia/client-common'; |
17 | 7 | import type { ClientOptions } from '@algolia/client-common'; |
18 | 8 | import type { PersonalizationClient, Region as PersonalizationRegion } from '@algolia/client-personalization'; |
19 | 9 | import { personalizationClient } from '@algolia/client-personalization'; |
| 10 | +import type { SearchClient } from '@algolia/client-search'; |
20 | 11 | import { searchClient } from '@algolia/client-search'; |
21 | 12 | import type { RecommendClient } from '@algolia/recommend'; |
22 | 13 | import { recommendClient } from '@algolia/recommend'; |
23 | | -import { createHttpRequester } from '@algolia/requester-node-http'; |
24 | 14 |
|
25 | | -import type { |
26 | | - InitClientOptions, |
27 | | - InitClientRegion, |
28 | | - GenerateSecuredApiKeyOptions, |
29 | | - GetSecuredApiKeyRemainingValidityOptions, |
30 | | -} from './models'; |
| 15 | +import type { InitClientOptions, InitClientRegion } from './models'; |
31 | 16 |
|
32 | 17 | export * from './models'; |
33 | 18 |
|
34 | | -/** |
35 | | - * The client type. |
36 | | - */ |
37 | | -export type Algoliasearch = ReturnType<typeof algoliasearch>; |
| 19 | +export type Algoliasearch = SearchClient & { |
| 20 | + initRecommend: (initOptions: InitClientOptions) => RecommendClient; |
| 21 | + initAnalytics: (initOptions: InitClientOptions & InitClientRegion<AnalyticsRegion>) => AnalyticsClient; |
| 22 | + initAbtesting: (initOptions: InitClientOptions & InitClientRegion<AbtestingRegion>) => AbtestingClient; |
| 23 | + initPersonalization: ( |
| 24 | + initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>, |
| 25 | + ) => PersonalizationClient; |
| 26 | +}; |
38 | 27 |
|
39 | | -// eslint-disable-next-line @typescript-eslint/explicit-function-return-type |
40 | | -export function algoliasearch(appId: string, apiKey: string, options?: ClientOptions) { |
| 28 | +export function algoliasearch(appId: string, apiKey: string, options?: ClientOptions): Algoliasearch { |
41 | 29 | if (!appId || typeof appId !== 'string') { |
42 | 30 | throw new Error('`appId` is missing.'); |
43 | 31 | } |
44 | 32 |
|
45 | 33 | if (!apiKey || typeof apiKey !== 'string') { |
46 | 34 | throw new Error('`apiKey` is missing.'); |
47 | 35 | } |
48 | | - function initRecommend(initOptions: InitClientOptions = {}): RecommendClient { |
49 | | - return recommendClient(initOptions.appId || appId, initOptions.apiKey || apiKey, initOptions.options); |
50 | | - } |
51 | | - |
52 | | - function initAnalytics(initOptions: InitClientOptions & InitClientRegion<AnalyticsRegion> = {}): AnalyticsClient { |
53 | | - return analyticsClient( |
54 | | - initOptions.appId || appId, |
55 | | - initOptions.apiKey || apiKey, |
56 | | - initOptions.region, |
57 | | - initOptions.options, |
58 | | - ); |
59 | | - } |
60 | | - |
61 | | - function initAbtesting(initOptions: InitClientOptions & InitClientRegion<AbtestingRegion> = {}): AbtestingClient { |
62 | | - return abtestingClient( |
63 | | - initOptions.appId || appId, |
64 | | - initOptions.apiKey || apiKey, |
65 | | - initOptions.region, |
66 | | - initOptions.options, |
67 | | - ); |
68 | | - } |
69 | | - |
70 | | - function initPersonalization( |
71 | | - initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>, |
72 | | - ): PersonalizationClient { |
73 | | - return personalizationClient( |
74 | | - initOptions.appId || appId, |
75 | | - initOptions.apiKey || apiKey, |
76 | | - initOptions.region, |
77 | | - initOptions.options, |
78 | | - ); |
79 | | - } |
80 | 36 |
|
81 | 37 | return { |
82 | | - ...searchClient(appId, apiKey, { |
83 | | - timeouts: { |
84 | | - connect: DEFAULT_CONNECT_TIMEOUT_NODE, |
85 | | - read: DEFAULT_READ_TIMEOUT_NODE, |
86 | | - write: DEFAULT_WRITE_TIMEOUT_NODE, |
87 | | - }, |
88 | | - requester: createHttpRequester(), |
89 | | - algoliaAgents: [{ segment: 'Node.js', version: process.versions.node }], |
90 | | - responsesCache: createNullCache(), |
91 | | - requestsCache: createNullCache(), |
92 | | - hostsCache: createMemoryCache(), |
93 | | - ...options, |
94 | | - }), |
| 38 | + ...searchClient(appId, apiKey, options), |
95 | 39 | /** |
96 | 40 | * Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system. |
97 | 41 | */ |
98 | 42 | get _ua(): string { |
99 | 43 | return this.transporter.algoliaAgent.value; |
100 | 44 | }, |
101 | | - initAbtesting, |
102 | | - initAnalytics, |
103 | | - initPersonalization, |
104 | | - initRecommend, |
105 | | - /** |
106 | | - * Helper: Generates a secured API key based on the given `parentApiKey` and given `restrictions`. |
107 | | - * |
108 | | - * @summary Helper: Generates a secured API key based on the given `parentApiKey` and given `restrictions`. |
109 | | - * @param generateSecuredApiKey - The `generateSecuredApiKey` object. |
110 | | - * @param generateSecuredApiKey.parentApiKey - The base API key from which to generate the new secured one. |
111 | | - * @param generateSecuredApiKey.restrictions - A set of properties defining the restrictions of the secured API key. |
112 | | - */ |
113 | | - generateSecuredApiKey({ parentApiKey, restrictions = {} }: GenerateSecuredApiKeyOptions): string { |
114 | | - let mergedRestrictions = restrictions; |
115 | | - if (restrictions.searchParams) { |
116 | | - // merge searchParams with the root restrictions |
117 | | - mergedRestrictions = { |
118 | | - ...restrictions, |
119 | | - ...restrictions.searchParams, |
120 | | - }; |
121 | | - |
122 | | - delete mergedRestrictions.searchParams; |
123 | | - } |
124 | | - |
125 | | - mergedRestrictions = Object.keys(mergedRestrictions) |
126 | | - .sort() |
127 | | - .reduce( |
128 | | - (acc, key) => { |
129 | | - // eslint-disable-next-line no-param-reassign |
130 | | - acc[key] = (mergedRestrictions as any)[key]; |
131 | | - return acc; |
132 | | - }, |
133 | | - {} as Record<string, unknown>, |
134 | | - ); |
135 | | - |
136 | | - const queryParameters = serializeQueryParameters(mergedRestrictions); |
137 | | - return Buffer.from( |
138 | | - createHmac('sha256', parentApiKey).update(queryParameters).digest('hex') + queryParameters, |
139 | | - ).toString('base64'); |
| 45 | + initRecommend: (initOptions: InitClientOptions = {}): RecommendClient => { |
| 46 | + return recommendClient(initOptions.appId || appId, initOptions.apiKey || apiKey, initOptions.options); |
140 | 47 | }, |
141 | 48 |
|
142 | | - /** |
143 | | - * Helper: Retrieves the remaining validity of the previous generated `securedApiKey`, the `ValidUntil` parameter must have been provided. |
144 | | - * |
145 | | - * @summary Helper: Retrieves the remaining validity of the previous generated `secured_api_key`, the `ValidUntil` parameter must have been provided. |
146 | | - * @param getSecuredApiKeyRemainingValidity - The `getSecuredApiKeyRemainingValidity` object. |
147 | | - * @param getSecuredApiKeyRemainingValidity.securedApiKey - The secured API key generated with the `generateSecuredApiKey` method. |
148 | | - */ |
149 | | - getSecuredApiKeyRemainingValidity({ securedApiKey }: GetSecuredApiKeyRemainingValidityOptions): number { |
150 | | - const decodedString = Buffer.from(securedApiKey, 'base64').toString('ascii'); |
151 | | - const regex = /validUntil=(\d+)/; |
152 | | - const match = decodedString.match(regex); |
| 49 | + initAnalytics: (initOptions: InitClientOptions & InitClientRegion<AnalyticsRegion> = {}): AnalyticsClient => { |
| 50 | + return analyticsClient( |
| 51 | + initOptions.appId || appId, |
| 52 | + initOptions.apiKey || apiKey, |
| 53 | + initOptions.region, |
| 54 | + initOptions.options, |
| 55 | + ); |
| 56 | + }, |
153 | 57 |
|
154 | | - if (match === null) { |
155 | | - throw new Error('validUntil not found in given secured api key.'); |
156 | | - } |
| 58 | + initAbtesting: (initOptions: InitClientOptions & InitClientRegion<AbtestingRegion> = {}): AbtestingClient => { |
| 59 | + return abtestingClient( |
| 60 | + initOptions.appId || appId, |
| 61 | + initOptions.apiKey || apiKey, |
| 62 | + initOptions.region, |
| 63 | + initOptions.options, |
| 64 | + ); |
| 65 | + }, |
157 | 66 |
|
158 | | - return parseInt(match[1], 10) - Math.round(new Date().getTime() / 1000); |
| 67 | + initPersonalization: ( |
| 68 | + initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>, |
| 69 | + ): PersonalizationClient => { |
| 70 | + return personalizationClient( |
| 71 | + initOptions.appId || appId, |
| 72 | + initOptions.apiKey || apiKey, |
| 73 | + initOptions.region, |
| 74 | + initOptions.options, |
| 75 | + ); |
159 | 76 | }, |
160 | 77 | }; |
161 | 78 | } |
0 commit comments