feat(admin, admin-ui, medusa-js, medusa-react, medusa): Support Admin Extensions (#4761)
Co-authored-by: Rares Stefan <948623+StephixOne@users.noreply.github.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Rares Stefan
Oli Juhl
parent
26c78bbc03
commit
f1a05f4725
@@ -1,6 +1,6 @@
|
||||
import MedusaError from "./error"
|
||||
import KeyManager from "./key-manager"
|
||||
import Client, { Config } from "./request"
|
||||
import Client, { type Config, type RequestOptions } from "./request"
|
||||
import {
|
||||
Admin,
|
||||
AuthResource,
|
||||
@@ -85,9 +85,7 @@ class Medusa {
|
||||
}
|
||||
|
||||
export default Medusa
|
||||
export { default as MedusaError } from "./error"
|
||||
export { default as KeyManager } from "./key-manager"
|
||||
export { default as Client, Config } from "./request"
|
||||
export * from "./resources"
|
||||
export * from "./typings"
|
||||
|
||||
export type { Config, RequestOptions }
|
||||
export { MedusaError, KeyManager, Client }
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import qs from "qs"
|
||||
import { RequestOptions } from "../../request"
|
||||
import { ResponsePromise } from "../../typings"
|
||||
import { createAdminPath } from "../../utils"
|
||||
import BaseResource from "../base"
|
||||
|
||||
class AdminCustomResource extends BaseResource {
|
||||
get<TQuery extends Record<string, any>, TResponse = any>(
|
||||
path: string,
|
||||
query?: TQuery,
|
||||
options?: RequestOptions,
|
||||
customHeaders?: Record<string, any>
|
||||
): ResponsePromise<TResponse> {
|
||||
let formattedPath = createAdminPath(path)
|
||||
|
||||
if (query) {
|
||||
const queryString = qs.stringify(query)
|
||||
formattedPath += `?${queryString}`
|
||||
}
|
||||
|
||||
return this.client.request(
|
||||
"GET",
|
||||
formattedPath,
|
||||
undefined,
|
||||
options,
|
||||
customHeaders
|
||||
)
|
||||
}
|
||||
|
||||
post<TPayload extends Record<string, any>, TResponse = any>(
|
||||
path: string,
|
||||
payload?: TPayload,
|
||||
options?: RequestOptions,
|
||||
customHeaders?: Record<string, any>
|
||||
): ResponsePromise<TResponse> {
|
||||
const formattedPath = createAdminPath(path)
|
||||
|
||||
return this.client.request(
|
||||
"POST",
|
||||
formattedPath,
|
||||
payload,
|
||||
options,
|
||||
customHeaders
|
||||
)
|
||||
}
|
||||
|
||||
delete<TResponse = any>(
|
||||
path: string,
|
||||
options?: RequestOptions,
|
||||
customHeaders?: Record<string, any>
|
||||
): ResponsePromise<TResponse> {
|
||||
const formattedPath = createAdminPath(path)
|
||||
|
||||
return this.client.request(
|
||||
"DELETE",
|
||||
formattedPath,
|
||||
undefined,
|
||||
options,
|
||||
customHeaders
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default AdminCustomResource
|
||||
@@ -3,6 +3,7 @@ import AdminAuthResource from "./auth"
|
||||
import AdminBatchJobsResource from "./batch-jobs"
|
||||
import AdminCollectionsResource from "./collections"
|
||||
import AdminCurrenciesResource from "./currencies"
|
||||
import AdminCustomResource from "./custom"
|
||||
import AdminCustomerGroupsResource from "./customer-groups"
|
||||
import AdminCustomersResource from "./customers"
|
||||
import AdminDiscountsResource from "./discounts"
|
||||
@@ -75,10 +76,12 @@ class Admin extends BaseResource {
|
||||
public paymentCollections = new AdminPaymentCollectionsResource(this.client)
|
||||
public payments = new AdminPaymentsResource(this.client)
|
||||
public productCategories = new AdminProductCategoriesResource(this.client)
|
||||
public custom = new AdminCustomResource(this.client)
|
||||
}
|
||||
|
||||
export {
|
||||
Admin,
|
||||
AdminCustomResource,
|
||||
AdminAuthResource,
|
||||
AdminBatchJobsResource,
|
||||
AdminCollectionsResource,
|
||||
|
||||
@@ -19,3 +19,17 @@ export function stringifyNullProperties<T extends object>(input: T): T {
|
||||
|
||||
return convertProperties(input)
|
||||
}
|
||||
|
||||
export function createAdminPath(path: string) {
|
||||
let formattedPath = path
|
||||
|
||||
if (!formattedPath.startsWith("/")) {
|
||||
formattedPath = `/${formattedPath}`
|
||||
}
|
||||
|
||||
if (!formattedPath.startsWith("/admin")) {
|
||||
formattedPath = `/admin${formattedPath}`
|
||||
}
|
||||
|
||||
return formattedPath
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user