1 import {
2 FindParams,
3 HttpTypes,
4 SelectParams,
5 } from "@medusajs/types"
6 import { Client } from "../client.js"
7 import { ClientHeaders } from "../types.js"
8
9 export class Invite {
10 /**
11 * @ignore
12 */
13 private client: Client
14 /**
15 * @ignore
16 */
17 constructor(client: Client) {
18 this.client = client
19 }
20
21 /**
22 * This method accepts an invite. It requires sending a previous request to
23 * the {@link Auth.register}.
24 *
25 * This method sends a request to the [Accept Invite]
26 * (https://docs.medusajs.com/api/admin/invites/accept-invite)
27 * API route.
28 *
29 * @param input - The details of the user to create.
30 * @param query - Configure the fields to retrieve in the user.
31 * @param headers - Headers to pass in the request
32 * @returns The user's details.
33 *
34 * @example
35 * await sdk.auth.register("user", "emailpass", {
36 * email: "user@gmail.com",
37 * password: "supersecret"
38 * })
39 *
40 * // all subsequent requests will use the token in the header
41 * const { user } = await sdk.admin.invite.accept(
42 * {
43 * email: "user@gmail.com",
44 * first_name: "John",
45 * last_name: "Smith",
46 * invite_token: "12345..."
47 * },
48 * )
49 */
50 async accept(
51 input: HttpTypes.AdminAcceptInvite & {
52 /**
53 * The invite's token.
54 */
55 invite_token: string
56 },
57 query?: SelectParams,
58 headers?: ClientHeaders
59 ) {
60 const { invite_token, ...rest } = input
61 return await this.client.fetch<HttpTypes.AdminAcceptInviteResponse>(
62 `/admin/invites/accept?token=${input.invite_token}`,
63 {
64 method: "POST",
65 headers,
66 body: rest,
67 query,
68 }
69 )
70 }
71
72 /**
73 * This method creates an invite. It sends a request to the
74 * [Create Invite](https://docs.medusajs.com/api/admin/invites/create-invite)
75 * API route.
76 *
77 * @param body - The invite's details.
78 * @param query - Configure the fields to retrieve in the invite.
79 * @param headers - Headers to pass in the request
80 * @returns The invite's details.
81 *
82 * @example
83 * sdk.admin.invite.create({
84 * email: "user@gmail.com",
85 * })
86 * .then(({ invite }) => {
87 * console.log(invite)
88 * })
89 */
90 async create(
91 body: HttpTypes.AdminCreateInvite,
92 query?: SelectParams,
93 headers?: ClientHeaders
94 ) {
95 return await this.client.fetch<HttpTypes.AdminInviteResponse>(
96 `/admin/invites`,
97 {
98 method: "POST",
99 headers,
100 body,
101 query,
102 }
103 )
104 }
105
106 /**
107 * This method retrieves an invite by its ID. It sends a request to the
108 * [Get Invite](https://docs.medusajs.com/api/admin/invites/get-an-invite)
109 * API route.
110 *
111 * @param id - The invite's ID.
112 * @param query - Configure the fields to retrieve in the invite.
113 * @param headers - Headers to pass in the request
114 * @returns The invite's details.
115 *
116 * @example
117 * To retrieve an invite its ID:
118 *
119 * ```ts
120 * sdk.admin.invite.retrieve("invite_123")
121 * .then(({ invite }) => {
122 * console.log(invite)
123 * })
124 * ```
125 *
126 * To specify the fields and relations to retrieve:
127 *
128 * ```ts
129 * sdk.admin.invite.retrieve("invite_123", {
130 * fields: "id,email"
131 * })
132 * .then(({ invite }) => {
133 * console.log(invite)
134 * })
135 * ```
136 *
137 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
138 */
139 async retrieve(id: string, query?: SelectParams, headers?: ClientHeaders) {
140 return await this.client.fetch<HttpTypes.AdminInviteResponse>(
141 `/admin/invites/${id}`,
142 {
143 headers,
144 query,
145 }
146 )
147 }
148
149 /**
150 * This method retrieves a paginated list of invites. It sends a request to the
151 * [List Invites](https://docs.medusajs.com/api/admin/invites/list-invites)
152 * API route.
153 *
154 * @param queryParams - Filters and pagination configurations.
155 * @param headers - Headers to pass in the request.
156 * @returns The paginated list of invites.
157 *
158 * @example
159 * To retrieve the list of invites:
160 *
161 * ```ts
162 * sdk.admin.invite.list()
163 * .then(({ invites, count, limit, offset }) => {
164 * console.log(invites)
165 * })
166 * ```
167 *
168 * To configure the pagination, pass the `limit` and `offset` query parameters.
169 *
170 * For example, to retrieve only 10 items and skip 10 items:
171 *
172 * ```ts
173 * sdk.admin.invite.list({
174 * limit: 10,
175 * offset: 10
176 * })
177 * .then(({ invites, count, limit, offset }) => {
178 * console.log(invites)
179 * })
180 * ```
181 *
182 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
183 * in each invite:
184 *
185 * ```ts
186 * sdk.admin.invite.list({
187 * fields: "id,email"
188 * })
189 * .then(({ invites, count, limit, offset }) => {
190 * console.log(invites)
191 * })
192 * ```
193 *
194 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
195 */
196 async list(queryParams?: FindParams, headers?: ClientHeaders) {
197 return await this.client.fetch<
198 HttpTypes.AdminInviteListResponse
199 >(`/admin/invites`, {
200 headers,
201 query: queryParams,
202 })
203 }
204
205 /**
206 * This method refreshes the token of an invite. It sends a request to the
207 * [Refresh Invite Token](https://docs.medusajs.com/api/admin/invites/refresh-invite-token)
208 * API route.
209 *
210 * @param id - The invite's ID.
211 * @param headers - Headers to pass in the request.
212 * @returns The invite's details.
213 *
214 * @example
215 * sdk.admin.invite.resend("invite_123")
216 * .then(({ invite }) => {
217 * console.log(invite)
218 * })
219 */
220 async resend(id: string, headers?: ClientHeaders) {
221 return await this.client.fetch<HttpTypes.AdminInviteResponse>(
222 `/admin/invites/${id}/resend`,
223 {
224 method: "POST",
225 headers,
226 }
227 )
228 }
229
230 /**
231 * This method deletes an invite. It sends a request to the
232 * [Delete Invite](https://docs.medusajs.com/api/admin/invites/delete-invite)
233 * API route.
234 *
235 * @param id - The invite's ID.
236 * @param headers - Headers to pass in the request.
237 * @returns The deletion's details.
238 *
239 * @example
240 * sdk.admin.invite.delete("invite_123")
241 * .then(({ deleted }) => {
242 * console.log(deleted)
243 * })
244 */
245 async delete(id: string, headers?: ClientHeaders) {
246 return await this.client.fetch<HttpTypes.AdminInviteDeleteResponse>(
247 `/admin/invites/${id}`,
248 {
249 method: "DELETE",
250 headers,
251 }
252 )
253 }
254 }