feat: medusa js admin regions (#939)
This commit is contained in:
@@ -10,6 +10,7 @@ import AdminNotesResource from "./notes"
|
||||
import AdminVariantsResource from "./variants"
|
||||
import AdminSwapsResource from "./swaps"
|
||||
import AdminShippingProfilesResource from "./shipping-profiles"
|
||||
import AdminRegionsResource from "./regions"
|
||||
|
||||
class Admin extends BaseResource {
|
||||
public auth = new AdminAuthResource(this.client)
|
||||
@@ -23,6 +24,7 @@ class Admin extends BaseResource {
|
||||
public variants = new AdminVariantsResource(this.client)
|
||||
public swaps = new AdminSwapsResource(this.client)
|
||||
public shippingProfiles = new AdminShippingProfilesResource(this.client)
|
||||
public regions = new AdminRegionsResource(this.client)
|
||||
}
|
||||
|
||||
export default Admin
|
||||
|
||||
202
packages/medusa-js/src/resources/admin/regions.ts
Normal file
202
packages/medusa-js/src/resources/admin/regions.ts
Normal file
@@ -0,0 +1,202 @@
|
||||
import {
|
||||
AdminPostRegionsReq,
|
||||
AdminRegionsRes,
|
||||
AdminPostRegionsRegionReq,
|
||||
AdminRegionsDeleteRes,
|
||||
AdminRegionsListRes,
|
||||
AdminGetRegionsParams,
|
||||
AdminPostRegionsRegionCountriesReq,
|
||||
AdminPostRegionsRegionFulfillmentProvidersReq,
|
||||
AdminPostRegionsRegionPaymentProvidersReq,
|
||||
AdminPostRegionsRegionMetadata,
|
||||
AdminGetRegionsRegionFulfillmentOptionsRes,
|
||||
} from "@medusajs/medusa"
|
||||
import { ResponsePromise } from "../../typings"
|
||||
import BaseResource from "../base"
|
||||
|
||||
class AdminRegionsResource extends BaseResource {
|
||||
/**
|
||||
* @description creates a region.
|
||||
* @param payload
|
||||
* @returns created region.
|
||||
*/
|
||||
create(payload: AdminPostRegionsReq): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description updates a region
|
||||
* @param id id of the region to update.
|
||||
* @param payload update to apply to region.
|
||||
* @returns the updated region.
|
||||
*/
|
||||
update(
|
||||
id: string,
|
||||
payload: AdminPostRegionsRegionReq
|
||||
): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions/${id}`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description deletes a region
|
||||
* @param id id of region to delete.
|
||||
* @returns Deleted response
|
||||
*/
|
||||
delete(id: string): ResponsePromise<AdminRegionsDeleteRes> {
|
||||
const path = `/admin/regions/${id}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get a region
|
||||
* @param id id of the region to retrieve.
|
||||
* @returns the region with the given id
|
||||
*/
|
||||
retrieve(id: string): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions/${id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description lists regions matching a query
|
||||
* @param query query for searching regions
|
||||
* @returns a list of regions matching the query.
|
||||
*/
|
||||
list(query?: AdminGetRegionsParams): ResponsePromise<AdminRegionsListRes> {
|
||||
let path = `/admin/regions`
|
||||
|
||||
if (query) {
|
||||
const queryString = Object.entries(query).map(([key, value]) => {
|
||||
return typeof value !== "undefined" ? `${key}=${value}` : ""
|
||||
})
|
||||
path = `/admin/regions?${queryString.join("&")}`
|
||||
}
|
||||
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description adds metadata to a region
|
||||
* @param id region id
|
||||
* @param payload metadata
|
||||
* @returns updated region
|
||||
*/
|
||||
setMetadata(
|
||||
id: string,
|
||||
payload: AdminPostRegionsRegionMetadata
|
||||
): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions/${id}/metadata`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description delete a region's metadata key value pair
|
||||
* @param id region id
|
||||
* @param key metadata key
|
||||
* @returns updated region
|
||||
*/
|
||||
deleteMetadata(id: string, key: string): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions/${id}/metadata/${key}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description adds a country to the list of countries in a region
|
||||
* @param id region id
|
||||
* @param payload country data
|
||||
* @returns updated region
|
||||
*/
|
||||
addCountry(
|
||||
id: string,
|
||||
payload: AdminPostRegionsRegionCountriesReq
|
||||
): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions/${id}/countries`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description remove a country from a region's list of coutnries
|
||||
* @param id region id
|
||||
* @param country_code the 2 character ISO code for the Country.
|
||||
* @returns updated region
|
||||
*/
|
||||
deleteCountry(
|
||||
id: string,
|
||||
country_code: string
|
||||
): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions/${id}/countries/${country_code}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description adds a fulfillment provider to a region
|
||||
* @param id region id
|
||||
* @param payload fulfillment provider data
|
||||
* @returns updated region
|
||||
*/
|
||||
addFulfillmentProvider(
|
||||
id: string,
|
||||
payload: AdminPostRegionsRegionFulfillmentProvidersReq
|
||||
): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions/${id}/fulfillment-providers`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description remove a fulfillment provider from a region
|
||||
* @param id region id
|
||||
* @param provider_id the if of the fulfillment provider
|
||||
* @returns updated region
|
||||
*/
|
||||
deleteFulfillmentProvider(
|
||||
id: string,
|
||||
provider_id: string
|
||||
): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions/${id}/fulfillment-providers/${provider_id}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description retrieves the list of fulfillment options available in a region
|
||||
* @param id region id
|
||||
* @returns list of fulfillment options
|
||||
*/
|
||||
retrieveFulfillmentOptions(
|
||||
id: string
|
||||
): ResponsePromise<AdminGetRegionsRegionFulfillmentOptionsRes> {
|
||||
const path = `/admin/regions/${id}/fulfillment-options`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description adds a payment provider to a region
|
||||
* @param id region id
|
||||
* @param payload payment provider data
|
||||
* @returns updated region
|
||||
*/
|
||||
addPaymentProvider(
|
||||
id: string,
|
||||
payload: AdminPostRegionsRegionPaymentProvidersReq
|
||||
): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions/${id}/payment-providers`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description removes a payment provider from a region
|
||||
* @param id region id
|
||||
* @param provider_id the id of the payment provider
|
||||
* @returns updated region
|
||||
*/
|
||||
deletePaymentProvider(
|
||||
id: string,
|
||||
provider_id: string
|
||||
): ResponsePromise<AdminRegionsRes> {
|
||||
const path = `/admin/regions/${id}/payment-providers/${provider_id}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default AdminRegionsResource
|
||||
@@ -29,6 +29,7 @@ export * from "./routes/admin/shipping-profiles"
|
||||
export * from "./routes/admin/store"
|
||||
export * from "./routes/admin/variants"
|
||||
export * from "./routes/admin/swaps"
|
||||
export * from "./routes/admin/regions"
|
||||
// Store
|
||||
export * from "./routes/store/auth"
|
||||
export * from "./routes/store/carts"
|
||||
|
||||
@@ -110,3 +110,11 @@ export class FulfillmentOption {
|
||||
export class AdminGetRegionsRegionFulfillmentOptionsRes {
|
||||
fulfillment_options: FulfillmentOption[]
|
||||
}
|
||||
|
||||
export * from "./list-regions"
|
||||
export * from "./update-region"
|
||||
export * from "./create-region"
|
||||
export * from "./add-country"
|
||||
export * from "./add-payment-provider"
|
||||
export * from "./add-fulfillment-provider"
|
||||
export * from "./set-metadata"
|
||||
|
||||
@@ -4,8 +4,8 @@ import RegionService from "../../../../services/region"
|
||||
import { defaultAdminRegionFields, defaultAdminRegionRelations } from "."
|
||||
|
||||
/**
|
||||
* @oas [delete] /regions/{id}/payment-providers/{provider_id}
|
||||
* operationId: "PostRegionsRegionPaymentProvidersProvider"
|
||||
* @oas [post] /regions/{id}/metadata
|
||||
* operationId: "PostRegionsRegionMetadata"
|
||||
* summary: "Set the metadata of a Region"
|
||||
* description: "Sets the metadata of a Region"
|
||||
* x-authenticated: true
|
||||
@@ -28,10 +28,7 @@ import { defaultAdminRegionFields, defaultAdminRegionRelations } from "."
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const validated = await validator(
|
||||
AdminPostRegionsRegionPaymentProvidersProviderReq,
|
||||
req.body
|
||||
)
|
||||
const validated = await validator(AdminPostRegionsRegionMetadata, req.body)
|
||||
|
||||
const regionService: RegionService = req.scope.resolve("regionService")
|
||||
await regionService.setMetadata(id, validated.key, validated.value)
|
||||
@@ -44,7 +41,7 @@ export default async (req, res) => {
|
||||
res.status(200).json({ region })
|
||||
}
|
||||
|
||||
export class AdminPostRegionsRegionPaymentProvidersProviderReq {
|
||||
export class AdminPostRegionsRegionMetadata {
|
||||
@IsString()
|
||||
key: string
|
||||
|
||||
|
||||
Reference in New Issue
Block a user