Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-suits-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/js-sdk": patch
---

chore(js-sdk): add methods to manage promotion codes
63 changes: 63 additions & 0 deletions packages/core/js-sdk/src/__tests__/store.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { http, HttpResponse } from "msw"
import { setupServer } from "msw/node"
import { describe, afterEach, it, beforeEach } from "node:test"
import { Client } from "../client"
import { Store } from "../store"
import { StoreCartResponse } from "@medusajs/types"

const baseUrl = "https://someurl.com"
const cartId = "cart-id"

// This is just a network-layer mocking, it doesn't start an actual server
const server = setupServer(
http.post(`${baseUrl}/store/carts/*/promotions`, ({ request, params, cookies }) => {
return HttpResponse.json({
test: "addPromotions",
});
}),
http.delete(`${baseUrl}/store/carts/*/promotions`, ({ request, params, cookies }) => {
return HttpResponse.json({
test: "removePromotions",
});
}),

http.all("*", ({ request, params, cookies }) => {
return new HttpResponse(null, {
status: 404,
statusText: "Not Found",
})
})
)

describe("Store", () => {
let client: Client
let store: Store
beforeEach(() => {
client = new Client({
baseUrl,
})
store = new Store(client)
server.listen()
})
afterEach(() => {
server.resetHandlers();
server.close();
})

describe("cart", () => {
it("should expose a function to add a promtion to a cart", async () => {
const resp = await store.cart.addPromotions(cartId, { promo_codes: ["promo-1", "promo-2"] });
expect(resp).toEqual({
test: "addPromotions",
})
});

it("should expose a function to remove a promtion from a cart", async () => {
const resp = await store.cart.removePromotions(cartId, { promo_codes: ["promo-1", "promo-2"] });
expect(resp).toEqual({
test: "removePromotions",
})
});

});
});
60 changes: 60 additions & 0 deletions packages/core/js-sdk/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,66 @@ export class Store {
}
)
},


/**
* This method adds a promotion code to the current cart
* The method sends a request to the [Add Promotion Code](https://docs.medusajs.com/api/store#carts_postcartsidpromotions) API route.
*
* @example
* sdk.store.cart.addPromotions("cart_123", {
* promo_codes: ["20OFF"]
* })
* .then(({ cart }) => {
* console.log(cart)
* })
*/
addPromotions: async (
cartId: string,
body: HttpTypes.StoreCartAddPromotion,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<HttpTypes.StoreCartResponse>(
`/store/carts/${cartId}/promotions`,
{
method: "POST",
headers,
body,
query,
}
)
},

/**
* This method removes promotion codes from the current cart
* The method sends a request to the [Remove Promotion Code](https://docs.medusajs.com/api/store#carts_deletecartsidpromotions) API route.
*
* @example
* sdk.store.cart.removePromotions("cart_123", {
* promo_codes: ["20OFF"]
* })
* .then(({ cart }) => {
* console.log(cart)
* })
*/
removePromotions: async (
cartId: string,
body: HttpTypes.StoreCartRemovePromotion,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<HttpTypes.StoreCartResponse>(
`/store/carts/${cartId}/promotions`,
{
method: "DELETE",
headers,
body,
query,
}
)
},

/**
* This method completes a cart and places the order. It's the last step of the checkout flow.
* The method sends a request to the [Complete Cart](https://docs.medusajs.com/api/store#carts_postcartsidcomplete)
Expand Down
Loading