Skip to content

Commit 3b49978

Browse files
fix(medusa,js-sdk,types): Add basic draft order operations to js-sdk (medusajs#11514)
**What** - Exposes `sdk.admin.draftOrder.create/update/retrieve/list` functions from the js-sdk - Implements the necessary types in the types package. - Adds missing endpoints to admin API.
1 parent 120e6f9 commit 3b49978

File tree

17 files changed

+612
-52
lines changed

17 files changed

+612
-52
lines changed

.changeset/shaggy-donkeys-pay.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@medusajs/js-sdk": patch
3+
"@medusajs/types": patch
4+
"@medusajs/medusa": patch
5+
---
6+
7+
fix(medusa,js-sdk,types): Add basic draft order operations to js-sdk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2+
import { HttpTypes } from "@medusajs/types"
3+
import { ModuleRegistrationName } from "@medusajs/utils"
4+
import {
5+
adminHeaders,
6+
createAdminUser,
7+
} from "../../../../helpers/create-admin-user"
8+
import { setupTaxStructure } from "../../../../modules/__tests__/fixtures"
9+
10+
jest.setTimeout(300000)
11+
12+
medusaIntegrationTestRunner({
13+
testSuite: ({ dbConnection, getContainer, api }) => {
14+
let region: HttpTypes.AdminRegion
15+
let testDraftOrder: HttpTypes.AdminDraftOrder
16+
17+
beforeEach(async () => {
18+
const container = getContainer()
19+
20+
await setupTaxStructure(container.resolve(ModuleRegistrationName.TAX))
21+
await createAdminUser(dbConnection, adminHeaders, container)
22+
23+
region = (
24+
await api.post(
25+
`/admin/regions`,
26+
{
27+
name: "USA",
28+
currency_code: "usd",
29+
countries: ["US"],
30+
},
31+
adminHeaders
32+
)
33+
).data.region
34+
35+
testDraftOrder = (
36+
await api.post(
37+
"/admin/draft-orders",
38+
{ email: "[email protected]", region_id: region.id },
39+
adminHeaders
40+
)
41+
).data.draft_order
42+
})
43+
44+
describe("GET /draft-orders", () => {
45+
it("should get a list of draft orders", async () => {
46+
const response = await api.get("/admin/draft-orders", adminHeaders)
47+
48+
expect(response.status).toBe(200)
49+
expect(response.data.draft_orders).toBeDefined()
50+
expect(response.data.draft_orders.length).toBe(1)
51+
expect(response.data.draft_orders).toEqual(
52+
expect.arrayContaining([
53+
expect.objectContaining({
54+
55+
}),
56+
])
57+
)
58+
})
59+
})
60+
61+
describe("POST /draft-orders", () => {
62+
it("should create a draft order", async () => {
63+
const response = await api.post(
64+
"/admin/draft-orders",
65+
{
66+
67+
region_id: region.id,
68+
},
69+
adminHeaders
70+
)
71+
72+
expect(response.status).toBe(200)
73+
expect(response.data.draft_order.email).toBe("[email protected]")
74+
expect(response.data.draft_order.region_id).toBe(region.id)
75+
})
76+
})
77+
78+
describe("GET /draft-orders/:id", () => {
79+
it("should get a draft order", async () => {
80+
const response = await api.get(
81+
`/admin/draft-orders/${testDraftOrder.id}`,
82+
adminHeaders
83+
)
84+
85+
expect(response.status).toBe(200)
86+
expect(response.data.draft_order.email).toBe("[email protected]")
87+
expect(response.data.draft_order.region_id).toBe(region.id)
88+
})
89+
})
90+
91+
describe("POST /draft-orders/:id", () => {
92+
it("should update a draft order", async () => {
93+
const response = await api.post(
94+
`/admin/draft-orders/${testDraftOrder.id}`,
95+
{
96+
97+
},
98+
adminHeaders
99+
)
100+
101+
expect(response.status).toBe(200)
102+
expect(response.data.draft_order.email).toBe("[email protected]")
103+
})
104+
})
105+
},
106+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import { HttpTypes } from "@medusajs/types"
2+
3+
import { Client } from "../client"
4+
import { ClientHeaders } from "../types"
5+
6+
export class DraftOrder {
7+
/**
8+
* @ignore
9+
*/
10+
private client: Client
11+
/**
12+
* @ignore
13+
*/
14+
constructor(client: Client) {
15+
this.client = client
16+
}
17+
18+
/**
19+
* This method retrieves a draft order by its ID. It sends a request to the
20+
* [Get Draft Order](https://docs.medusajs.com/api/admin#draft-orders_getdraftordersid)
21+
* API route.
22+
*
23+
* @param id - The draft order's ID.
24+
* @param query - Configure the fields to retrieve in the draft order.
25+
* @param headers - Headers to pass in the request
26+
* @returns The draft order's details.
27+
*
28+
* @example
29+
* To retrieve a draft order by its ID:
30+
*
31+
* ```ts
32+
* sdk.admin.draftOrder.retrieve("draft_order_123")
33+
* .then(({ draft_order }) => {
34+
* console.log(draft_order)
35+
* })
36+
* ```
37+
*
38+
* To specify the fields and relations to retrieve:
39+
*
40+
* ```ts
41+
* sdk.admin.draftOrder.retrieve("draft_order_123", {
42+
* fields: "id,*items"
43+
* })
44+
* .then(({ draft_order }) => {
45+
* console.log(draft_order)
46+
* })
47+
* ```
48+
*
49+
* Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store#select-fields-and-relations).
50+
*/
51+
async retrieve(
52+
id: string,
53+
query?: HttpTypes.AdminDraftOrderParams,
54+
headers?: ClientHeaders
55+
) {
56+
return await this.client.fetch<HttpTypes.AdminDraftOrderResponse>(
57+
`/admin/draft-orders/${id}`,
58+
{
59+
query,
60+
headers,
61+
}
62+
)
63+
}
64+
65+
/**
66+
* This method retrieves a paginated list of draft orders. It sends a request to the
67+
* [List Draft Orders](https://docs.medusajs.com/api/admin#draft-orders_getdraftorders) API route.
68+
*
69+
* @param queryParams - Filters and pagination configurations.
70+
* @param headers - Headers to pass in the request.
71+
* @returns The paginated list of draft orders.
72+
*
73+
* @example
74+
* To retrieve the list of draft orders:
75+
*
76+
* ```ts
77+
* sdk.admin.draftOrder.list()
78+
* .then(({ draft_orders, count, limit, offset }) => {
79+
* console.log(draft_orders)
80+
* })
81+
* ```
82+
*
83+
* To configure the pagination, pass the `limit` and `offset` query parameters.
84+
*
85+
* For example, to retrieve only 10 items and skip 10 items:
86+
*
87+
* ```ts
88+
* sdk.admin.draftOrder.list({
89+
* limit: 10,
90+
* offset: 10
91+
* })
92+
* .then(({ draft_orders, count, limit, offset }) => {
93+
* console.log(draft_orders)
94+
* })
95+
* ```
96+
*
97+
* Using the `fields` query parameter, you can specify the fields and relations to retrieve
98+
* in each draft order:
99+
*
100+
* ```ts
101+
* sdk.admin.draftOrder.list({
102+
* fields: "id,*items"
103+
* })
104+
* .then(({ draft_orders, count, limit, offset }) => {
105+
* console.log(draft_orders)
106+
* })
107+
* ```
108+
*
109+
* Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store#select-fields-and-relations).
110+
*/
111+
async list(
112+
queryParams?: HttpTypes.AdminDraftOrderListParams,
113+
headers?: ClientHeaders
114+
) {
115+
return await this.client.fetch<HttpTypes.AdminDraftOrderListResponse>(
116+
`/admin/draft-orders`,
117+
{
118+
query: queryParams,
119+
headers,
120+
}
121+
)
122+
}
123+
124+
/**
125+
* This method creates a draft order. It sends a request to the
126+
* [Create Draft Order](https://docs.medusajs.com/api/admin#draft-orders_postdraftorders) API route.
127+
*
128+
* @param body - The data to create the draft order.
129+
* @param query - Configure the fields to retrieve in the draft order.
130+
* @param headers - Headers to pass in the request.
131+
*
132+
* @example
133+
* To create a draft order:
134+
*
135+
* ```ts
136+
* sdk.admin.draftOrder.create({
137+
* email: "test@test.com",
138+
* items: [
139+
* {
140+
* variant_id: "variant_123",
141+
* quantity: 1,
142+
* },
143+
* ],
144+
* region_id: "region_123",
145+
* sales_channel_id: "sales_channel_123",
146+
* })
147+
* .then(({ draft_order }) => {
148+
* console.log(draft_order)
149+
* })
150+
* ```
151+
*/
152+
async create(
153+
body: HttpTypes.AdminCreateDraftOrder,
154+
query?: HttpTypes.AdminDraftOrderParams,
155+
headers?: ClientHeaders
156+
) {
157+
return await this.client.fetch<HttpTypes.AdminDraftOrderResponse>(
158+
`/admin/draft-orders`,
159+
{
160+
method: "POST",
161+
body,
162+
query,
163+
headers,
164+
}
165+
)
166+
}
167+
168+
/**
169+
* This method updates a draft order. It sends a request to the
170+
* [Update Draft Order](https://docs.medusajs.com/api/admin#draft-orders_postdraftordersid) API route.
171+
*
172+
* @param id - The draft order's ID.
173+
* @param body - The data to update the draft order.
174+
* @param query - Configure the fields to retrieve in the draft order.
175+
* @param headers - Headers to pass in the request.
176+
*
177+
* @example
178+
* To update a draft order:
179+
*
180+
* ```ts
181+
* sdk.admin.draftOrder.update("draft_order_123", {
182+
* email: "test@test.com",
183+
* })
184+
* .then(({ draft_order }) => {
185+
* console.log(draft_order)
186+
* })
187+
* ```
188+
*/
189+
async update(
190+
id: string,
191+
body: HttpTypes.AdminUpdateDraftOrder,
192+
query?: HttpTypes.AdminDraftOrderParams,
193+
headers?: ClientHeaders
194+
) {
195+
return await this.client.fetch<HttpTypes.AdminDraftOrderResponse>(
196+
`/admin/draft-orders/${id}`,
197+
{
198+
method: "POST",
199+
body,
200+
query,
201+
headers,
202+
}
203+
)
204+
}
205+
}

packages/core/js-sdk/src/admin/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Claim } from "./claim"
55
import { Currency } from "./currency"
66
import { Customer } from "./customer"
77
import { CustomerGroup } from "./customer-group"
8+
import { DraftOrder } from "./draft-order"
89
import { Exchange } from "./exchange"
910
import { Fulfillment } from "./fulfillment"
1011
import { FulfillmentProvider } from "./fulfillment-provider"
@@ -126,6 +127,10 @@ export class Admin {
126127
* @tags order
127128
*/
128129
public order: Order
130+
/**
131+
* @tags draft order
132+
*/
133+
public draftOrder: DraftOrder
129134
/**
130135
* @tags order
131136
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { AdminOrder } from "../../order"
2+
3+
export interface AdminDraftOrder extends AdminOrder {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./entities"
2+
export * from "./payloads"
3+
export * from "./queries"
4+
export * from "./responses"

0 commit comments

Comments
 (0)