|
| 1 | +import type Medusa from "@medusajs/js-sdk" |
| 2 | +import type { HttpTypes } from "@medusajs/types" |
| 3 | +import type { RegionService, RegionListResponse } from "./types" |
| 4 | + |
| 5 | +export type MedusaRegionListInput = { |
| 6 | + enabled?: boolean |
| 7 | +} |
| 8 | + |
| 9 | +export type MedusaRegionDetailInput = { |
| 10 | + id?: string |
| 11 | + enabled?: boolean |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Creates a RegionService for Medusa SDK |
| 16 | + * |
| 17 | + * @example |
| 18 | + * ```typescript |
| 19 | + * import { createRegionHooks, createMedusaRegionService } from "@techsio/storefront-data" |
| 20 | + * import { sdk } from "@/lib/medusa-client" |
| 21 | + * |
| 22 | + * const regionHooks = createRegionHooks({ |
| 23 | + * service: createMedusaRegionService(sdk), |
| 24 | + * queryKeys: regionQueryKeys, |
| 25 | + * }) |
| 26 | + * ``` |
| 27 | + */ |
| 28 | +export function createMedusaRegionService( |
| 29 | + sdk: Medusa |
| 30 | +): RegionService< |
| 31 | + HttpTypes.StoreRegion, |
| 32 | + MedusaRegionListInput, |
| 33 | + MedusaRegionDetailInput |
| 34 | +> { |
| 35 | + return { |
| 36 | + async getRegions( |
| 37 | + _params: MedusaRegionListInput, |
| 38 | + _signal?: AbortSignal |
| 39 | + ): Promise<RegionListResponse<HttpTypes.StoreRegion>> { |
| 40 | + const response = await sdk.store.region.list() |
| 41 | + return { |
| 42 | + regions: response.regions, |
| 43 | + count: response.regions.length, |
| 44 | + } |
| 45 | + }, |
| 46 | + |
| 47 | + async getRegion( |
| 48 | + params: MedusaRegionDetailInput, |
| 49 | + _signal?: AbortSignal |
| 50 | + ): Promise<HttpTypes.StoreRegion | null> { |
| 51 | + if (!params.id) { |
| 52 | + return null |
| 53 | + } |
| 54 | + const response = await sdk.store.region.retrieve(params.id) |
| 55 | + return response.region ?? null |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
0 commit comments