1 import { HttpTypes } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 /**
6 * This class is used to send requests to Admin Search API routes.
7 *
8 * @since 2.19.0
9 */
10 export class Search {
11 /**
12 * @ignore
13 */
14 private client: Client
15
16 /**
17 * @ignore
18 */
19 constructor(client: Client) {
20 this.client = client
21 }
22
23 /**
24 * This method searches across entities. Per entity, results come from the
25 * Search Module when that entity is indexed; otherwise each entity is queried
26 * with the same free-text `q` filter the list endpoints support.
27 *
28 * Results are grouped per entity and paginated independently.
29 *
30 * @param {HttpTypes.AdminSearchParams} query - Search query and pagination parameters.
31 * @param {ClientHeaders} headers - Headers to pass in the request.
32 * @returns {Promise<HttpTypes.AdminSearchResponse>} The grouped search results.
33 *
34 * @example
35 * sdk.admin.search.list({ q: "shirt", limit: 5 })
36 * .then(({ results }) => {
37 * console.log(results)
38 * })
39 *
40 * @tags search
41 * @since 2.19.0
42 */
43 async list(query?: HttpTypes.AdminSearchParams, headers?: ClientHeaders) {
44 return await this.client.fetch<HttpTypes.AdminSearchResponse>(
45 `/admin/search`,
46 {
47 headers,
48 query,
49 }
50 )
51 }
52
53 /**
54 * This method retrieves the registered search indexes, their status, and the
55 * fields each stores. `enabled` is `false` when the Search Module is not
56 * configured.
57 *
58 * @param {ClientHeaders} headers - Headers to pass in the request.
59 * @returns {Promise<HttpTypes.AdminSearchIndexListResponse>} The registered search indexes.
60 *
61 * @example
62 * sdk.admin.search.listIndexes()
63 * .then(({ search_indexes, enabled }) => {
64 * console.log(search_indexes, enabled)
65 * })
66 *
67 * @tags search
68 */
69 async listIndexes(headers?: ClientHeaders) {
70 return await this.client.fetch<HttpTypes.AdminSearchIndexListResponse>(
71 `/admin/search-indexes`,
72 {
73 headers,
74 }
75 )
76 }
77
78 /**
79 * This method deletes a search index, dropping every physical index ever built
80 * for it along with the versions and sync history behind them. The next
81 * migration rebuilds it from scratch, starting at version 1. Until it runs,
82 * nothing serves reads for this index.
83 *
84 * @param {string} id - The name of the index to delete.
85 * @param {ClientHeaders} headers - Headers to pass in the request.
86 * @returns {Promise<HttpTypes.AdminSearchIndexDeleteResponse>} The deletion's details.
87 *
88 * @example
89 * sdk.admin.search.deleteIndex("product")
90 * .then(({ deleted, deleted_versions }) => {
91 * console.log(deleted, deleted_versions)
92 * })
93 *
94 * @tags search
95 */
96 async deleteIndex(id: string, headers?: ClientHeaders) {
97 return await this.client.fetch<HttpTypes.AdminSearchIndexDeleteResponse>(
98 `/admin/search-indexes/${id}`,
99 {
100 method: "DELETE",
101 headers,
102 }
103 )
104 }
105
106 /**
107 * This method triggers rebuilding a search index from its seed. The rebuild runs in
108 * the background - check the index's `status` through {@link listIndexes} to know
109 * when it's done.
110 *
111 * @param {string} id - The name of the index to reindex.
112 * @param {HttpTypes.AdminReindexSearchIndex} body - Options to scope the reindex, such as `since` or `filters`.
113 * @param {ClientHeaders} headers - Headers to pass in the request.
114 * @returns {Promise<HttpTypes.AdminSearchIndexReindexResponse>} The triggered reindex job.
115 *
116 * @example
117 * sdk.admin.search.reindex("product")
118 * .then(({ job_id, indexes }) => {
119 * console.log(job_id, indexes)
120 * })
121 *
122 * @tags search
123 */
124 async reindex(
125 id: string,
126 body?: HttpTypes.AdminReindexSearchIndex,
127 headers?: ClientHeaders
128 ) {
129 return await this.client.fetch<HttpTypes.AdminSearchIndexReindexResponse>(
130 `/admin/search-indexes/${id}/reindex`,
131 {
132 method: "POST",
133 headers,
134 body,
135 }
136 )
137 }
138 }