|
| 1 | +import { |
| 2 | + createAuth, |
| 3 | + createTransporter, |
| 4 | + getUserAgent, |
| 5 | + shuffle, |
| 6 | +} from '@experimental-api-clients-automation/client-common'; |
| 7 | +import type { |
| 8 | + CreateClientOptions, |
| 9 | + Headers, |
| 10 | + Host, |
| 11 | + Request, |
| 12 | + RequestOptions, |
| 13 | + QueryParameters, |
| 14 | +} from '@experimental-api-clients-automation/client-common'; |
| 15 | + |
| 16 | +import type { MultipleQueriesParams } from '../model/multipleQueriesParams'; |
| 17 | +import type { MultipleQueriesResponse } from '../model/multipleQueriesResponse'; |
| 18 | +import type { SearchForFacetValuesRequest } from '../model/searchForFacetValuesRequest'; |
| 19 | +import type { SearchForFacetValuesResponse } from '../model/searchForFacetValuesResponse'; |
| 20 | +import type { SearchParams } from '../model/searchParams'; |
| 21 | +import type { SearchResponse } from '../model/searchResponse'; |
| 22 | + |
| 23 | +export * from '../model'; |
| 24 | +export const apiClientVersion = '0.0.5'; |
| 25 | + |
| 26 | +function getDefaultHosts(appId: string): Host[] { |
| 27 | + return ( |
| 28 | + [ |
| 29 | + { |
| 30 | + url: `${appId}-dsn.algolia.net`, |
| 31 | + accept: 'read', |
| 32 | + protocol: 'https', |
| 33 | + }, |
| 34 | + { |
| 35 | + url: `${appId}.algolia.net`, |
| 36 | + accept: 'write', |
| 37 | + protocol: 'https', |
| 38 | + }, |
| 39 | + ] as Host[] |
| 40 | + ).concat( |
| 41 | + shuffle([ |
| 42 | + { |
| 43 | + url: `${appId}-1.algolianet.com`, |
| 44 | + accept: 'readWrite', |
| 45 | + protocol: 'https', |
| 46 | + }, |
| 47 | + { |
| 48 | + url: `${appId}-2.algolianet.com`, |
| 49 | + accept: 'readWrite', |
| 50 | + protocol: 'https', |
| 51 | + }, |
| 52 | + { |
| 53 | + url: `${appId}-3.algolianet.com`, |
| 54 | + accept: 'readWrite', |
| 55 | + protocol: 'https', |
| 56 | + }, |
| 57 | + ]) |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +// eslint-disable-next-line @typescript-eslint/explicit-function-return-type |
| 62 | +export function createAlgoliasearchLiteClient(options: CreateClientOptions) { |
| 63 | + const auth = createAuth(options.appId, options.apiKey, options.authMode); |
| 64 | + const transporter = createTransporter({ |
| 65 | + hosts: options?.hosts ?? getDefaultHosts(options.appId), |
| 66 | + hostsCache: options.hostsCache, |
| 67 | + requestsCache: options.requestsCache, |
| 68 | + responsesCache: options.responsesCache, |
| 69 | + baseHeaders: { |
| 70 | + 'content-type': 'application/x-www-form-urlencoded', |
| 71 | + ...auth.headers(), |
| 72 | + }, |
| 73 | + baseQueryParameters: auth.queryParameters(), |
| 74 | + userAgent: getUserAgent({ |
| 75 | + userAgents: options.userAgents, |
| 76 | + client: 'AlgoliasearchLite', |
| 77 | + version: apiClientVersion, |
| 78 | + }), |
| 79 | + timeouts: options.timeouts, |
| 80 | + requester: options.requester, |
| 81 | + }); |
| 82 | + |
| 83 | + function addUserAgent(segment: string, version?: string): void { |
| 84 | + transporter.userAgent.add({ segment, version }); |
| 85 | + } |
| 86 | + |
| 87 | + return { |
| 88 | + addUserAgent, |
| 89 | + /** |
| 90 | + * Get search results for the given requests. |
| 91 | + * |
| 92 | + * @summary Get search results for the given requests. |
| 93 | + * @param multipleQueriesParams - The multipleQueriesParams object. |
| 94 | + */ |
| 95 | + multipleQueries( |
| 96 | + multipleQueriesParams: MultipleQueriesParams, |
| 97 | + requestOptions?: RequestOptions |
| 98 | + ): Promise<MultipleQueriesResponse> { |
| 99 | + if (!multipleQueriesParams) { |
| 100 | + throw new Error( |
| 101 | + 'Parameter `multipleQueriesParams` is required when calling `multipleQueries`.' |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + if (!multipleQueriesParams.requests) { |
| 106 | + throw new Error( |
| 107 | + 'Parameter `multipleQueriesParams.requests` is required when calling `multipleQueries`.' |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + const requestPath = '/1/indexes/*/queries'; |
| 112 | + const headers: Headers = {}; |
| 113 | + const queryParameters: QueryParameters = {}; |
| 114 | + |
| 115 | + const request: Request = { |
| 116 | + method: 'POST', |
| 117 | + path: requestPath, |
| 118 | + data: multipleQueriesParams, |
| 119 | + }; |
| 120 | + |
| 121 | + return transporter.request( |
| 122 | + request, |
| 123 | + { |
| 124 | + queryParameters, |
| 125 | + headers, |
| 126 | + }, |
| 127 | + requestOptions |
| 128 | + ); |
| 129 | + }, |
| 130 | + |
| 131 | + /** |
| 132 | + * This method allow you to send requests to the Algolia REST API. |
| 133 | + * |
| 134 | + * @summary Send requests to the Algolia REST API. |
| 135 | + * @param post - The post object. |
| 136 | + * @param post.path - The path of the API endpoint to target, anything after the /1 needs to be specified. |
| 137 | + * @param post.parameters - Query parameters to be applied to the current query. |
| 138 | + * @param post.body - The parameters to send with the custom request. |
| 139 | + */ |
| 140 | + post( |
| 141 | + { path, parameters, body }: PostProps, |
| 142 | + requestOptions?: RequestOptions |
| 143 | + ): Promise<Record<string, any>> { |
| 144 | + if (!path) { |
| 145 | + throw new Error('Parameter `path` is required when calling `post`.'); |
| 146 | + } |
| 147 | + |
| 148 | + const requestPath = '/1{path}'.replace('{path}', path); |
| 149 | + const headers: Headers = {}; |
| 150 | + const queryParameters: QueryParameters = parameters || {}; |
| 151 | + |
| 152 | + const request: Request = { |
| 153 | + method: 'POST', |
| 154 | + path: requestPath, |
| 155 | + data: body, |
| 156 | + }; |
| 157 | + |
| 158 | + return transporter.request( |
| 159 | + request, |
| 160 | + { |
| 161 | + queryParameters, |
| 162 | + headers, |
| 163 | + }, |
| 164 | + requestOptions |
| 165 | + ); |
| 166 | + }, |
| 167 | + |
| 168 | + /** |
| 169 | + * Get search results. |
| 170 | + * |
| 171 | + * @summary Get search results. |
| 172 | + * @param search - The search object. |
| 173 | + * @param search.indexName - The index in which to perform the request. |
| 174 | + * @param search.searchParams - The searchParams object. |
| 175 | + */ |
| 176 | + search( |
| 177 | + { indexName, searchParams }: SearchProps, |
| 178 | + requestOptions?: RequestOptions |
| 179 | + ): Promise<SearchResponse> { |
| 180 | + if (!indexName) { |
| 181 | + throw new Error( |
| 182 | + 'Parameter `indexName` is required when calling `search`.' |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + if (!searchParams) { |
| 187 | + throw new Error( |
| 188 | + 'Parameter `searchParams` is required when calling `search`.' |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + const requestPath = '/1/indexes/{indexName}/query'.replace( |
| 193 | + '{indexName}', |
| 194 | + encodeURIComponent(indexName) |
| 195 | + ); |
| 196 | + const headers: Headers = {}; |
| 197 | + const queryParameters: QueryParameters = {}; |
| 198 | + |
| 199 | + const request: Request = { |
| 200 | + method: 'POST', |
| 201 | + path: requestPath, |
| 202 | + data: searchParams, |
| 203 | + }; |
| 204 | + |
| 205 | + return transporter.request( |
| 206 | + request, |
| 207 | + { |
| 208 | + queryParameters, |
| 209 | + headers, |
| 210 | + }, |
| 211 | + requestOptions |
| 212 | + ); |
| 213 | + }, |
| 214 | + |
| 215 | + /** |
| 216 | + * Search for values of a given facet, optionally restricting the returned values to those contained in objects matching other search criteria. |
| 217 | + * |
| 218 | + * @summary Search for values of a given facet. |
| 219 | + * @param searchForFacetValues - The searchForFacetValues object. |
| 220 | + * @param searchForFacetValues.indexName - The index in which to perform the request. |
| 221 | + * @param searchForFacetValues.facetName - The facet name. |
| 222 | + * @param searchForFacetValues.searchForFacetValuesRequest - The searchForFacetValuesRequest object. |
| 223 | + */ |
| 224 | + searchForFacetValues( |
| 225 | + { |
| 226 | + indexName, |
| 227 | + facetName, |
| 228 | + searchForFacetValuesRequest, |
| 229 | + }: SearchForFacetValuesProps, |
| 230 | + requestOptions?: RequestOptions |
| 231 | + ): Promise<SearchForFacetValuesResponse> { |
| 232 | + if (!indexName) { |
| 233 | + throw new Error( |
| 234 | + 'Parameter `indexName` is required when calling `searchForFacetValues`.' |
| 235 | + ); |
| 236 | + } |
| 237 | + |
| 238 | + if (!facetName) { |
| 239 | + throw new Error( |
| 240 | + 'Parameter `facetName` is required when calling `searchForFacetValues`.' |
| 241 | + ); |
| 242 | + } |
| 243 | + |
| 244 | + const requestPath = '/1/indexes/{indexName}/facets/{facetName}/query' |
| 245 | + .replace('{indexName}', encodeURIComponent(indexName)) |
| 246 | + .replace('{facetName}', encodeURIComponent(facetName)); |
| 247 | + const headers: Headers = {}; |
| 248 | + const queryParameters: QueryParameters = {}; |
| 249 | + |
| 250 | + const request: Request = { |
| 251 | + method: 'POST', |
| 252 | + path: requestPath, |
| 253 | + data: searchForFacetValuesRequest, |
| 254 | + }; |
| 255 | + |
| 256 | + return transporter.request( |
| 257 | + request, |
| 258 | + { |
| 259 | + queryParameters, |
| 260 | + headers, |
| 261 | + }, |
| 262 | + requestOptions |
| 263 | + ); |
| 264 | + }, |
| 265 | + }; |
| 266 | +} |
| 267 | + |
| 268 | +export type AlgoliasearchLiteClient = ReturnType< |
| 269 | + typeof createAlgoliasearchLiteClient |
| 270 | +>; |
| 271 | + |
| 272 | +export type PostProps = { |
| 273 | + /** |
| 274 | + * The path of the API endpoint to target, anything after the /1 needs to be specified. |
| 275 | + */ |
| 276 | + path: string; |
| 277 | + /** |
| 278 | + * Query parameters to be applied to the current query. |
| 279 | + */ |
| 280 | + parameters?: Record<string, any>; |
| 281 | + /** |
| 282 | + * The parameters to send with the custom request. |
| 283 | + */ |
| 284 | + body?: Record<string, any>; |
| 285 | +}; |
| 286 | + |
| 287 | +export type SearchProps = { |
| 288 | + /** |
| 289 | + * The index in which to perform the request. |
| 290 | + */ |
| 291 | + indexName: string; |
| 292 | + searchParams: SearchParams; |
| 293 | +}; |
| 294 | + |
| 295 | +export type SearchForFacetValuesProps = { |
| 296 | + /** |
| 297 | + * The index in which to perform the request. |
| 298 | + */ |
| 299 | + indexName: string; |
| 300 | + /** |
| 301 | + * The facet name. |
| 302 | + */ |
| 303 | + facetName: string; |
| 304 | + searchForFacetValuesRequest?: SearchForFacetValuesRequest; |
| 305 | +}; |
0 commit comments