feat(dashboard): Migrate to new hooks and API client (#6963)
This commit is contained in:
committed by
GitHub
parent
5ba74ec5fc
commit
8a5c6928f7
@@ -0,0 +1,40 @@
|
||||
import { CreateApiKeyReq, UpdateApiKeyReq } from "../../types/api-payloads"
|
||||
import {
|
||||
ApiKeyDeleteRes,
|
||||
ApiKeyListRes,
|
||||
ApiKeyRes,
|
||||
} from "../../types/api-responses"
|
||||
import { deleteRequest, getRequest, postRequest } from "./common"
|
||||
|
||||
const retrieveApiKey = async (id: string, query?: Record<string, any>) => {
|
||||
return getRequest<ApiKeyRes>(`/admin/api-keys/${id}`, query)
|
||||
}
|
||||
|
||||
const listApiKeys = async (query?: Record<string, any>) => {
|
||||
return getRequest<ApiKeyListRes>(`/admin/api-keys`, query)
|
||||
}
|
||||
|
||||
const deleteApiKey = async (id: string) => {
|
||||
return deleteRequest<ApiKeyDeleteRes>(`/admin/api-keys/${id}`)
|
||||
}
|
||||
|
||||
const revokeApiKey = async (id: string) => {
|
||||
return postRequest<ApiKeyRes>(`/admin/api-keys/${id}/revoke`)
|
||||
}
|
||||
|
||||
const createApiKey = async (payload: CreateApiKeyReq) => {
|
||||
return postRequest<ApiKeyRes>(`/admin/api-keys`, payload)
|
||||
}
|
||||
|
||||
const updateApiKey = async (id: string, payload: UpdateApiKeyReq) => {
|
||||
return postRequest<ApiKeyRes>(`/admin/api-keys/${id}`, payload)
|
||||
}
|
||||
|
||||
export const apiKeys = {
|
||||
retrieve: retrieveApiKey,
|
||||
list: listApiKeys,
|
||||
delete: deleteApiKey,
|
||||
create: createApiKey,
|
||||
update: updateApiKey,
|
||||
revoke: revokeApiKey,
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { EmailPassReq } from "../../types/api-payloads"
|
||||
import { EmailPassRes } from "../../types/api-responses"
|
||||
import { postRequest } from "./common"
|
||||
|
||||
async function emailPass(payload: EmailPassReq) {
|
||||
return postRequest<EmailPassRes>("/auth/admin/emailpass", payload)
|
||||
}
|
||||
|
||||
async function login(token: string) {
|
||||
return postRequest<void>("/auth/session", undefined, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const auth = {
|
||||
authenticate: {
|
||||
emailPass,
|
||||
},
|
||||
login,
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { apiKeys } from "./api-keys"
|
||||
import { auth } from "./auth"
|
||||
import { collections } from "./collections"
|
||||
import { currencies } from "./currencies"
|
||||
import { customers } from "./customers"
|
||||
import { invites } from "./invites"
|
||||
import { productTypes } from "./product-types"
|
||||
import { products } from "./products"
|
||||
import { promotions } from "./promotions"
|
||||
import { regions } from "./regions"
|
||||
import { salesChannels } from "./sales-channels"
|
||||
import { stockLocations } from "./stock-locations"
|
||||
import { stores } from "./stores"
|
||||
import { users } from "./users"
|
||||
import { workflowExecutions } from "./workflow-executions"
|
||||
|
||||
export const client = {
|
||||
auth: auth,
|
||||
apiKeys: apiKeys,
|
||||
customers: customers,
|
||||
currencies: currencies,
|
||||
collections: collections,
|
||||
promotions: promotions,
|
||||
stores: stores,
|
||||
salesChannels: salesChannels,
|
||||
users: users,
|
||||
regions: regions,
|
||||
invites: invites,
|
||||
products: products,
|
||||
productTypes: productTypes,
|
||||
stockLocations: stockLocations,
|
||||
workflowExecutions: workflowExecutions,
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
CreateProductCollectionReq,
|
||||
UpdateProductCollectionReq,
|
||||
} from "../../types/api-payloads"
|
||||
import {
|
||||
ProductCollectionDeleteRes,
|
||||
ProductCollectionListRes,
|
||||
ProductCollectionRes,
|
||||
} from "../../types/api-responses"
|
||||
import { deleteRequest, getRequest, postRequest } from "./common"
|
||||
|
||||
async function listProductCollections(query?: Record<string, any>) {
|
||||
return getRequest<ProductCollectionListRes>(`/admin/collections`, query)
|
||||
}
|
||||
|
||||
async function retrieveProductCollection(
|
||||
id: string,
|
||||
query?: Record<string, any>
|
||||
) {
|
||||
return getRequest<ProductCollectionRes>(`/admin/collections/${id}`, query)
|
||||
}
|
||||
|
||||
async function updateProductCollection(
|
||||
id: string,
|
||||
payload: UpdateProductCollectionReq
|
||||
) {
|
||||
return postRequest<ProductCollectionRes>(`/admin/collections/${id}`, payload)
|
||||
}
|
||||
|
||||
async function createProductCollection(payload: CreateProductCollectionReq) {
|
||||
return postRequest<ProductCollectionRes>(`/admin/collections`, payload)
|
||||
}
|
||||
|
||||
async function deleteProductCollection(id: string) {
|
||||
return deleteRequest<ProductCollectionDeleteRes>(`/admin/collections/${id}`)
|
||||
}
|
||||
|
||||
export const collections = {
|
||||
list: listProductCollections,
|
||||
retrieve: retrieveProductCollection,
|
||||
update: updateProductCollection,
|
||||
create: createProductCollection,
|
||||
delete: deleteProductCollection,
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import { stringify } from "qs"
|
||||
|
||||
const baseUrl = "http://localhost:9000"
|
||||
|
||||
const commonHeaders: HeadersInit = {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
function getUrl(path: string, query?: Record<string, any>) {
|
||||
const params = query ? stringify(query) : null
|
||||
|
||||
return `${baseUrl}${path}${params ? `?${params}` : ""}`
|
||||
}
|
||||
|
||||
function getBody(payload?: Record<string, any>) {
|
||||
return payload ? JSON.stringify(payload) : undefined
|
||||
}
|
||||
|
||||
function getOptions(
|
||||
options?: Omit<RequestInit, "body">,
|
||||
payload?: Record<string, any>
|
||||
): RequestInit {
|
||||
const body = getBody(payload)
|
||||
|
||||
return {
|
||||
...options,
|
||||
headers: {
|
||||
...commonHeaders,
|
||||
...options?.headers,
|
||||
},
|
||||
body,
|
||||
credentials: "include",
|
||||
}
|
||||
}
|
||||
|
||||
async function makeRequest<
|
||||
TRes,
|
||||
TPayload extends Record<string, any> | undefined,
|
||||
TQuery extends Record<string, any> | undefined = undefined,
|
||||
>(
|
||||
path: string,
|
||||
payload?: TPayload,
|
||||
query?: TQuery,
|
||||
options?: Omit<RequestInit, "body">
|
||||
): Promise<TRes> {
|
||||
const url = getUrl(path, query)
|
||||
const requestOptions = getOptions(options, payload)
|
||||
|
||||
const response = await fetch(url, requestOptions)
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json()
|
||||
// Temp: Add a better error type
|
||||
throw new Error(`API error ${response.status}: ${errorData.message}`)
|
||||
}
|
||||
|
||||
return response.json()
|
||||
}
|
||||
|
||||
export async function getRequest<
|
||||
TRes,
|
||||
TQuery extends Record<string, any> | undefined = {},
|
||||
>(
|
||||
path: string,
|
||||
query?: TQuery,
|
||||
options?: Omit<RequestInit, "body" | "method">
|
||||
): Promise<TRes> {
|
||||
return makeRequest<TRes, undefined, Record<string, any>>(
|
||||
path,
|
||||
undefined,
|
||||
query,
|
||||
{
|
||||
...options,
|
||||
method: "GET",
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export async function postRequest<
|
||||
TRes,
|
||||
TPayload extends Record<string, any> | undefined = {},
|
||||
>(
|
||||
path: string,
|
||||
payload?: TPayload,
|
||||
options?: Omit<RequestInit, "body" | "method">
|
||||
): Promise<TRes> {
|
||||
return makeRequest<TRes, Record<string, any>, undefined>(
|
||||
path,
|
||||
payload,
|
||||
undefined,
|
||||
{
|
||||
...options,
|
||||
method: "POST",
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export async function deleteRequest<TRes>(
|
||||
path: string,
|
||||
options?: Omit<RequestInit, "body" | "method">
|
||||
): Promise<TRes> {
|
||||
return makeRequest<TRes, undefined, undefined>(path, undefined, undefined, {
|
||||
...options,
|
||||
method: "DELETE",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { CurrencyListRes, CurrencyRes } from "../../types/api-responses"
|
||||
import { getRequest } from "./common"
|
||||
|
||||
async function retrieveCurrency(id: string, query?: Record<string, any>) {
|
||||
return getRequest<CurrencyRes>(`/admin/currencies/${id}`, query)
|
||||
}
|
||||
|
||||
async function listCurrencies(query?: Record<string, any>) {
|
||||
return getRequest<CurrencyListRes>("/admin/currencies", query)
|
||||
}
|
||||
|
||||
export const currencies = {
|
||||
retrieve: retrieveCurrency,
|
||||
list: listCurrencies,
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { CreateCustomerReq, UpdateCustomerReq } from "../../types/api-payloads"
|
||||
import { CustomerListRes, CustomerRes } from "../../types/api-responses"
|
||||
import { getRequest, postRequest } from "./common"
|
||||
|
||||
async function retrieveCustomer(id: string, query?: Record<string, any>) {
|
||||
return getRequest<CustomerRes>(`/admin/customers/${id}`, query)
|
||||
}
|
||||
|
||||
async function listCustomers(query?: Record<string, any>) {
|
||||
return getRequest<CustomerListRes>(`/admin/customers`, query)
|
||||
}
|
||||
|
||||
async function createCustomer(payload: CreateCustomerReq) {
|
||||
return postRequest<CustomerRes>(`/admin/customers`, payload)
|
||||
}
|
||||
|
||||
async function updateCustomer(id: string, payload: UpdateCustomerReq) {
|
||||
return postRequest<CustomerRes>(`/admin/customers/${id}`, payload)
|
||||
}
|
||||
|
||||
export const customers = {
|
||||
retrieve: retrieveCustomer,
|
||||
list: listCustomers,
|
||||
create: createCustomer,
|
||||
update: updateCustomer,
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./client"
|
||||
@@ -0,0 +1,38 @@
|
||||
import { CreateInviteReq } from "../../types/api-payloads"
|
||||
import {
|
||||
InviteDeleteRes,
|
||||
InviteListRes,
|
||||
InviteRes,
|
||||
} from "../../types/api-responses"
|
||||
import { deleteRequest, getRequest, postRequest } from "./common"
|
||||
|
||||
async function retrieveInvite(id: string, query?: Record<string, any>) {
|
||||
return getRequest<InviteRes, Record<string, any>>(
|
||||
`/admin/invites/${id}`,
|
||||
query
|
||||
)
|
||||
}
|
||||
|
||||
async function listInvites(query?: Record<string, any>) {
|
||||
return getRequest<InviteListRes, Record<string, any>>(`/admin/invites`, query)
|
||||
}
|
||||
|
||||
async function createInvite(payload: CreateInviteReq) {
|
||||
return postRequest<InviteRes>(`/admin/invites`, payload)
|
||||
}
|
||||
|
||||
async function resendInvite(id: string) {
|
||||
return postRequest<InviteRes>(`/admin/invites/${id}/resend`)
|
||||
}
|
||||
|
||||
async function deleteInvite(id: string) {
|
||||
return deleteRequest<InviteDeleteRes>(`/admin/invites/${id}`)
|
||||
}
|
||||
|
||||
export const invites = {
|
||||
retrieve: retrieveInvite,
|
||||
list: listInvites,
|
||||
create: createInvite,
|
||||
resend: resendInvite,
|
||||
delete: deleteInvite,
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ProductTypeListRes, ProductTypeRes } from "../../types/api-responses"
|
||||
import { getRequest } from "./common"
|
||||
|
||||
async function listProductTypes(query?: Record<string, any>) {
|
||||
return getRequest<ProductTypeListRes>(`/admin/product-types`, query)
|
||||
}
|
||||
|
||||
async function retrieveProductType(id: string, query?: Record<string, any>) {
|
||||
return getRequest<ProductTypeRes>(`/admin/product-types/${id}`, query)
|
||||
}
|
||||
|
||||
export const productTypes = {
|
||||
list: listProductTypes,
|
||||
retrieve: retrieveProductType,
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ProductListRes, ProductRes } from "../../types/api-responses"
|
||||
import { getRequest } from "./common"
|
||||
|
||||
async function retrieveProduct(id: string, query?: Record<string, any>) {
|
||||
return getRequest<ProductRes>(`/admin/products/${id}`, query)
|
||||
}
|
||||
|
||||
async function listProducts(query?: Record<string, any>) {
|
||||
return getRequest<ProductListRes>(`/admin/products`, query)
|
||||
}
|
||||
|
||||
export const products = {
|
||||
retrieve: retrieveProduct,
|
||||
list: listProducts,
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { AdminGetPromotionsParams } from "@medusajs/medusa"
|
||||
|
||||
import { PromotionListRes, PromotionRes } from "../../types/api-responses"
|
||||
import { getRequest } from "./common"
|
||||
|
||||
const retrievePromotion = async (
|
||||
id: string,
|
||||
query?: AdminGetPromotionsParams
|
||||
) => {
|
||||
return getRequest<PromotionRes, AdminGetPromotionsParams>(
|
||||
`/admin/promotions/${id}`,
|
||||
query
|
||||
)
|
||||
}
|
||||
|
||||
const listPromotions = async (query?: AdminGetPromotionsParams) => {
|
||||
return getRequest<PromotionListRes>(`/admin/promotions`, query)
|
||||
}
|
||||
|
||||
export const promotions = {
|
||||
retrieve: retrievePromotion,
|
||||
list: listPromotions,
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { CreateRegionDTO, UpdateRegionDTO } from "@medusajs/types"
|
||||
import {
|
||||
RegionDeleteRes,
|
||||
RegionListRes,
|
||||
RegionRes,
|
||||
} from "../../types/api-responses"
|
||||
import { deleteRequest, getRequest, postRequest } from "./common"
|
||||
|
||||
async function retrieveRegion(id: string, query?: Record<string, any>) {
|
||||
return getRequest<RegionRes>(`/admin/regions/${id}`, query)
|
||||
}
|
||||
|
||||
async function listRegions(query?: Record<string, any>) {
|
||||
return getRequest<RegionListRes>(`/admin/regions`, query)
|
||||
}
|
||||
|
||||
async function createRegion(payload: CreateRegionDTO) {
|
||||
return postRequest<RegionRes>(`/admin/regions`, payload)
|
||||
}
|
||||
|
||||
async function updateRegion(id: string, payload: UpdateRegionDTO) {
|
||||
return postRequest<RegionRes>(`/admin/regions/${id}`, payload)
|
||||
}
|
||||
|
||||
async function deleteRegion(id: string) {
|
||||
return deleteRequest<RegionDeleteRes>(`/admin/regions/${id}`)
|
||||
}
|
||||
|
||||
export const regions = {
|
||||
retrieve: retrieveRegion,
|
||||
list: listRegions,
|
||||
create: createRegion,
|
||||
update: updateRegion,
|
||||
delete: deleteRegion,
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import {
|
||||
AddProductsSalesChannelReq,
|
||||
CreateSalesChannelReq,
|
||||
RemoveProductsSalesChannelReq,
|
||||
UpdateSalesChannelReq,
|
||||
} from "../../types/api-payloads"
|
||||
import {
|
||||
SalesChannelDeleteRes,
|
||||
SalesChannelListRes,
|
||||
SalesChannelRes,
|
||||
} from "../../types/api-responses"
|
||||
import { deleteRequest, getRequest, postRequest } from "./common"
|
||||
|
||||
async function retrieveSalesChannel(id: string, query?: Record<string, any>) {
|
||||
return getRequest<SalesChannelRes, Record<string, any>>(
|
||||
`/admin/sales-channels/${id}`,
|
||||
query
|
||||
)
|
||||
}
|
||||
|
||||
async function listSalesChannels(query?: Record<string, any>) {
|
||||
return getRequest<SalesChannelListRes, Record<string, any>>(
|
||||
`/admin/sales-channels`,
|
||||
query
|
||||
)
|
||||
}
|
||||
|
||||
async function createSalesChannel(payload: CreateSalesChannelReq) {
|
||||
return postRequest<SalesChannelRes>(`/admin/sales-channels`, payload)
|
||||
}
|
||||
|
||||
async function updateSalesChannel(id: string, payload: UpdateSalesChannelReq) {
|
||||
return postRequest<SalesChannelRes>(`/admin/sales-channels/${id}`, payload)
|
||||
}
|
||||
|
||||
async function deleteSalesChannel(id: string) {
|
||||
return deleteRequest<SalesChannelDeleteRes>(`/admin/sales-channels/${id}`)
|
||||
}
|
||||
|
||||
async function batchRemoveProducts(
|
||||
id: string,
|
||||
payload: RemoveProductsSalesChannelReq
|
||||
) {
|
||||
return postRequest<SalesChannelRes>(
|
||||
`/admin/sales-channels/${id}/products/batch/remove`,
|
||||
payload
|
||||
)
|
||||
}
|
||||
|
||||
async function batchAddProducts(
|
||||
id: string,
|
||||
payload: AddProductsSalesChannelReq
|
||||
) {
|
||||
return postRequest<SalesChannelRes>(
|
||||
`/admin/sales-channels/${id}/products/batch/add`,
|
||||
payload
|
||||
)
|
||||
}
|
||||
|
||||
export const salesChannels = {
|
||||
retrieve: retrieveSalesChannel,
|
||||
list: listSalesChannels,
|
||||
create: createSalesChannel,
|
||||
update: updateSalesChannel,
|
||||
delete: deleteSalesChannel,
|
||||
removeProducts: batchRemoveProducts,
|
||||
addProducts: batchAddProducts,
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import {
|
||||
CreateStockLocationReq,
|
||||
UpdateStockLocationReq,
|
||||
} from "../../types/api-payloads"
|
||||
import {
|
||||
StockLocationDeleteRes,
|
||||
StockLocationListRes,
|
||||
StockLocationRes,
|
||||
} from "../../types/api-responses"
|
||||
import { deleteRequest, getRequest, postRequest } from "./common"
|
||||
|
||||
async function listStockLocations(query?: Record<string, any>) {
|
||||
return getRequest<StockLocationListRes>(`/admin/stock-locations`, query)
|
||||
}
|
||||
|
||||
async function retrieveStockLocation(id: string, query?: Record<string, any>) {
|
||||
return getRequest<StockLocationRes>(`/admin/stock-locations/${id}`, query)
|
||||
}
|
||||
|
||||
async function createStockLocation(payload: CreateStockLocationReq) {
|
||||
return postRequest<StockLocationRes>(`/admin/stock-locations`, payload)
|
||||
}
|
||||
|
||||
async function updateStockLocation(
|
||||
id: string,
|
||||
payload: UpdateStockLocationReq
|
||||
) {
|
||||
return postRequest<StockLocationRes>(`/admin/stock-locations/${id}`, payload)
|
||||
}
|
||||
|
||||
async function deleteStockLocation(id: string) {
|
||||
return deleteRequest<StockLocationDeleteRes>(`/admin/stock-locations/${id}`)
|
||||
}
|
||||
|
||||
export const stockLocations = {
|
||||
list: listStockLocations,
|
||||
retrieve: retrieveStockLocation,
|
||||
create: createStockLocation,
|
||||
update: updateStockLocation,
|
||||
delete: deleteStockLocation,
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { UpdateStoreReq } from "../../types/api-payloads"
|
||||
import { StoreListRes, StoreRes } from "../../types/api-responses"
|
||||
import { getRequest, postRequest } from "./common"
|
||||
|
||||
async function retrieveStore(query?: Record<string, any>): Promise<StoreRes> {
|
||||
const response = await getRequest<StoreListRes>("/admin/stores", query)
|
||||
|
||||
const activeStore = response.stores?.[0]
|
||||
|
||||
if (!activeStore) {
|
||||
// Temp: Add proper error handling
|
||||
throw new Error("No active store found")
|
||||
}
|
||||
|
||||
return { store: activeStore }
|
||||
}
|
||||
|
||||
async function updateStore(id: string, payload: UpdateStoreReq) {
|
||||
return postRequest<StoreRes>(`/admin/stores/${id}`, payload)
|
||||
}
|
||||
|
||||
export const stores = {
|
||||
retrieve: retrieveStore,
|
||||
update: updateStore,
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { UpdateUserReq } from "../../types/api-payloads"
|
||||
import { UserDeleteRes, UserListRes, UserRes } from "../../types/api-responses"
|
||||
import { deleteRequest, getRequest, postRequest } from "./common"
|
||||
|
||||
async function me() {
|
||||
return getRequest<UserRes>("/admin/users/me")
|
||||
}
|
||||
|
||||
async function retrieveUser(id: string, query?: Record<string, any>) {
|
||||
return getRequest<UserRes>(`/admin/users/${id}`, query)
|
||||
}
|
||||
|
||||
async function listUsers(query?: Record<string, any>) {
|
||||
return getRequest<UserListRes>(`/admin/users`, query)
|
||||
}
|
||||
|
||||
async function updateUser(id: string, payload: UpdateUserReq) {
|
||||
return postRequest<UserRes>(`/admin/users/${id}`, payload)
|
||||
}
|
||||
|
||||
async function deleteUser(id: string) {
|
||||
return deleteRequest<UserDeleteRes>(`/admin/users/${id}`)
|
||||
}
|
||||
|
||||
export const users = {
|
||||
me,
|
||||
retrieve: retrieveUser,
|
||||
list: listUsers,
|
||||
update: updateUser,
|
||||
delete: deleteUser,
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import {
|
||||
WorkflowExecutionListRes,
|
||||
WorkflowExecutionRes,
|
||||
} from "../../types/api-responses"
|
||||
import { getRequest } from "./common"
|
||||
|
||||
async function retrieveWorkflowExecution(
|
||||
id: string,
|
||||
query?: Record<string, any>
|
||||
) {
|
||||
return getRequest<WorkflowExecutionRes>(
|
||||
`/admin/workflows-executions/${id}`,
|
||||
query
|
||||
)
|
||||
}
|
||||
|
||||
async function listWorkflowExecutions(query?: Record<string, any>) {
|
||||
return getRequest<WorkflowExecutionListRes>(
|
||||
`/admin/workflows-executions`,
|
||||
query
|
||||
)
|
||||
}
|
||||
|
||||
export const workflowExecutions = {
|
||||
retrieve: retrieveWorkflowExecution,
|
||||
list: listWorkflowExecutions,
|
||||
}
|
||||
Reference in New Issue
Block a user