Skip to content

Commit

Permalink
chore: rest of api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
VariableVic committed Nov 22, 2024
1 parent f0d3741 commit ec97290
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/lib/data/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function retrieveCart() {
method: "GET",
query: {
fields:
"*items, *region, *items.product, *items.variant, +items.thumbnail, +items.metadata, *promotions, *company",
"*items, *region, *items.product, *items.variant, +items.thumbnail, +items.metadata, *promotions",
},
headers,
next,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/data/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const getCollectionByHandle = async (

return sdk.client
.fetch<HttpTypes.StoreCollectionListResponse>(`/store/collections`, {
query: { handle },
query: { handle, fields: "*products" },
next,
})
.then(({ collections }) => collections[0])
Expand Down
2 changes: 1 addition & 1 deletion src/lib/data/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const retrieveCustomer =
.fetch<{ customer: HttpTypes.StoreCustomer }>(`/store/customers/me`, {
method: "GET",
query: {
fields: "*employee, *orders",
fields: "*orders",
},
headers,
next,
Expand Down
30 changes: 24 additions & 6 deletions src/lib/data/fulfillment.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
"use server"

import { sdk } from "@lib/config"
import { cache } from "react"
import { HttpTypes } from "@medusajs/types"
import { getAuthHeaders, getCacheOptions } from "./cookies"

export const listCartShippingMethods = async (cartId: string) => {
const headers = {
...(await getAuthHeaders()),
}

const next = {
...(await getCacheOptions("fulfillment")),
}

// Shipping actions
export const listCartShippingMethods = cache(async function (cartId: string) {
return sdk.store.fulfillment
.listCartOptions({ cart_id: cartId }, { next: { tags: ["shipping"] } })
return sdk.client
.fetch<HttpTypes.StoreShippingOptionListResponse>(
`/store/shipping-options`,
{
method: "GET",
query: { cart_id: cartId },
headers,
next,
}
)
.then(({ shipping_options }) => shipping_options)
.catch(() => {
return null
})
})
}
5 changes: 3 additions & 2 deletions src/lib/data/onboarding.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use server"
import { cookies } from "next/headers"
import { cookies as nextCookies } from "next/headers"
import { redirect } from "next/navigation"

export async function resetOnboardingState(orderId: string) {
(await cookies()).set("_medusa_onboarding", "false", { maxAge: -1 })
const cookies = await nextCookies()
cookies.set("_medusa_onboarding", "false", { maxAge: -1 })
redirect(`http://localhost:7001/a/orders/${orderId}`)
}
64 changes: 48 additions & 16 deletions src/lib/data/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,58 @@

import { sdk } from "@lib/config"
import medusaError from "@lib/util/medusa-error"
import { cache } from "react"
import { getAuthHeaders } from "./cookies"
import { getAuthHeaders, getCacheOptions } from "./cookies"
import { HttpTypes } from "@medusajs/types"

export const retrieveOrder = cache(async function (id: string) {
return sdk.store.order
.retrieve(
id,
{ fields: "*payment_collections.payments" },
{ next: { tags: ["order"] }, ...getAuthHeaders() }
)
export const retrieveOrder = async (id: string) => {
const headers = {
...(await getAuthHeaders()),
}

const next = {
...(await getCacheOptions("orders")),
}

return sdk.client
.fetch<HttpTypes.StoreOrderResponse>(`/store/orders/${id}`, {
method: "GET",
query: {
fields:
"*payment_collections.payments,*items,+items.metadata,*items.variant,*items.product",
},
headers,
next,
})
.then(({ order }) => order)
.catch((err) => medusaError(err))
})
}

export const listOrders = cache(async function (
export const listOrders = async (
limit: number = 10,
offset: number = 0
) {
return sdk.store.order
.list({ limit, offset }, { next: { tags: ["order"] }, ...getAuthHeaders() })
offset: number = 0,
filters?: Record<string, any>
) => {
const headers = {
...(await getAuthHeaders()),
}

const next = {
...(await getCacheOptions("orders")),
}

return sdk.client
.fetch<HttpTypes.StoreOrderListResponse>(`/store/orders`, {
method: "GET",
query: {
limit,
offset,
order: "-created_at",
fields: "*items,+items.metadata,*items.variant,*items.product",
...filters,
},
headers,
next,
})
.then(({ orders }) => orders)
.catch((err) => medusaError(err))
})
}
33 changes: 24 additions & 9 deletions src/lib/data/payment.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
"use server"

import { sdk } from "@lib/config"
import { cache } from "react"

// Shipping actions
export const listCartPaymentMethods = cache(async function (regionId: string) {
return sdk.store.payment
.listPaymentProviders(
{ region_id: regionId },
{ next: { tags: ["payment_providers"] } }
import { getAuthHeaders, getCacheOptions } from "./cookies"
import { HttpTypes } from "@medusajs/types"

export const listCartPaymentMethods = async (regionId: string) => {
const headers = {
...(await getAuthHeaders()),
}

const next = {
...(await getCacheOptions("payment_providers")),
}

return sdk.client
.fetch<HttpTypes.StorePaymentProviderListResponse>(
`/store/payment-providers`,
{
method: "GET",
query: { region_id: regionId },
headers,
next,
}
)
.then(({ payment_providers }) => payment_providers)
.catch(() => {
return null
})
})
}
115 changes: 75 additions & 40 deletions src/lib/data/products.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,67 @@
"use server"

import { sdk } from "@lib/config"
import { sortProducts } from "@lib/util/sort-products"
import { HttpTypes } from "@medusajs/types"
import { cache } from "react"
import { getRegion } from "./regions"
import { SortOptions } from "@modules/store/components/refinement-list/sort-products"
import { sortProducts } from "@lib/util/sort-products"
import { getAuthHeaders, getCacheOptions } from "./cookies"
import { getRegion } from "./regions"

export const getProductsById = cache(async function ({
export const getProductsById = async ({
ids,
regionId,
}: {
ids: string[]
regionId: string
}) {
return sdk.store.product
.list(
{
}) => {
const headers = {
...(await getAuthHeaders()),
}

const next = {
...(await getCacheOptions("products")),
}

return sdk.client
.fetch<{ products: HttpTypes.StoreProduct[] }>(`/store/products`, {
method: "GET",
query: {
id: ids,
region_id: regionId,
fields: "*variants.calculated_price,+variants.inventory_quantity",
fields:
"*variants,*variants.calculated_price,*variants.inventory_quantity",
},
{ next: { tags: ["products"] } }
)
headers,
next,
})
.then(({ products }) => products)
})
}

export const getProductByHandle = cache(async function (
handle: string,
regionId: string
) {
return sdk.store.product
.list(
{
export const getProductByHandle = async (handle: string, regionId: string) => {
const headers = {
...(await getAuthHeaders()),
}

const next = {
...(await getCacheOptions("products")),
}

return sdk.client
.fetch<{ products: HttpTypes.StoreProduct[] }>(`/store/products`, {
method: "GET",
query: {
handle,
region_id: regionId,
fields: "*variants.calculated_price,+variants.inventory_quantity",
fields:
"*variants.calculated_price,+variants.inventory_quantity,+metadata,+tags",
},
{ next: { tags: ["products"] } }
)
headers,
next,
})
.then(({ products }) => products[0])
})
}

export const getProductsList = cache(async function ({
export const listProducts = async ({
pageParam = 1,
queryParams,
countryCode,
Expand All @@ -52,9 +73,10 @@ export const getProductsList = cache(async function ({
response: { products: HttpTypes.StoreProduct[]; count: number }
nextPage: number | null
queryParams?: HttpTypes.FindParams & HttpTypes.StoreProductParams
}> {
}> => {
const limit = queryParams?.limit || 12
const offset = pageParam * limit
const _pageParam = Math.max(pageParam, 1)
const offset = (_pageParam - 1) * limit
const region = await getRegion(countryCode)

if (!region) {
Expand All @@ -64,16 +86,29 @@ export const getProductsList = cache(async function ({
}
}

return sdk.store.product
.list(
const headers = {
...(await getAuthHeaders()),
}

const next = {
...(await getCacheOptions("products")),
}

return sdk.client
.fetch<{ products: HttpTypes.StoreProduct[]; count: number }>(
`/store/products`,
{
limit,
offset,
region_id: region.id,
fields: "*variants.calculated_price",
...queryParams,
},
{ next: { tags: ["products"] } }
method: "GET",
query: {
limit,
offset,
region_id: region.id,
fields: "*variants.calculated_price",
...queryParams,
},
headers,
next,
}
)
.then(({ products, count }) => {
const nextPage = count > offset + limit ? pageParam + 1 : null
Expand All @@ -87,13 +122,13 @@ export const getProductsList = cache(async function ({
queryParams,
}
})
})
}

/**
* This will fetch 100 products to the Next.js cache and sort them based on the sortBy parameter.
* It will then return the paginated products based on the page and limit parameters.
*/
export const getProductsListWithSort = cache(async function ({
export const listProductsWithSort = async ({
page = 0,
queryParams,
sortBy = "created_at",
Expand All @@ -107,12 +142,12 @@ export const getProductsListWithSort = cache(async function ({
response: { products: HttpTypes.StoreProduct[]; count: number }
nextPage: number | null
queryParams?: HttpTypes.FindParams & HttpTypes.StoreProductParams
}> {
}> => {
const limit = queryParams?.limit || 12

const {
response: { products, count },
} = await getProductsList({
} = await listProducts({
pageParam: 0,
queryParams: {
...queryParams,
Expand All @@ -137,4 +172,4 @@ export const getProductsListWithSort = cache(async function ({
nextPage,
queryParams,
}
})
}
Loading

0 comments on commit ec97290

Please sign in to comment.