chore: Remove last trace of client in dashboard (#8873)

This commit is contained in:
Oli Juhl
2024-08-29 17:48:01 +02:00
committed by GitHub
parent bd20d66968
commit 9cf1a5d709
23 changed files with 524 additions and 424 deletions
@@ -1,48 +0,0 @@
import {
AdminCampaignListResponse,
AdminCampaignResponse,
AdminCreateCampaign,
AdminUpdateCampaign,
LinkMethodRequest,
} from "@medusajs/types"
import { CampaignDeleteRes } from "../../types/api-responses"
import { deleteRequest, getRequest, postRequest } from "./common"
async function retrieveCampaign(id: string, query?: Record<string, any>) {
return getRequest<AdminCampaignResponse>(`/admin/campaigns/${id}`, query)
}
async function listCampaigns(query?: Record<string, any>) {
return getRequest<AdminCampaignListResponse>(`/admin/campaigns`, query)
}
async function createCampaign(payload: AdminCreateCampaign) {
return postRequest<AdminCampaignResponse>(`/admin/campaigns`, payload)
}
async function updateCampaign(id: string, payload: AdminUpdateCampaign) {
return postRequest<AdminCampaignResponse>(`/admin/campaigns/${id}`, payload)
}
async function deleteCampaign(id: string) {
return deleteRequest<CampaignDeleteRes>(`/admin/campaigns/${id}`)
}
async function addOrRemoveCampaignPromotions(
id: string,
payload: LinkMethodRequest
) {
return postRequest<AdminCampaignResponse>(
`/admin/campaigns/${id}/promotions`,
payload
)
}
export const campaigns = {
retrieve: retrieveCampaign,
list: listCampaigns,
create: createCampaign,
update: updateCampaign,
delete: deleteCampaign,
addOrRemovePromotions: addOrRemoveCampaignPromotions,
}
@@ -1,27 +0,0 @@
import {
AdminProductCategoryListResponse,
AdminProductCategoryResponse,
} from "@medusajs/types"
import { getRequest } from "./common"
async function listProductCategories(query?: Record<string, any>) {
return getRequest<AdminProductCategoryListResponse>(
`/admin/product-categories`,
query
)
}
async function retrieveProductCategory(
id: string,
query?: Record<string, any>
) {
return getRequest<AdminProductCategoryResponse>(
`/admin/product-categories/${id}`,
query
)
}
export const categories = {
list: listProductCategories,
retrieve: retrieveProductCategory,
}
@@ -1,16 +1,7 @@
import Medusa from "@medusajs/js-sdk"
import { campaigns } from "./campaigns"
import { categories } from "./categories"
import { promotions } from "./promotions"
export const backendUrl = __BACKEND_URL__ ?? "http://localhost:9000"
export const client = {
campaigns: campaigns,
categories: categories,
promotions: promotions,
}
export const sdk = new Medusa({
baseUrl: backendUrl,
auth: {
@@ -1,108 +0,0 @@
import { stringify } from "qs"
const baseUrl = __BACKEND_URL__ ?? "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",
})
}
@@ -1,126 +0,0 @@
import {
AdminGetPromotionsParams,
AdminGetPromotionsRuleValueParams,
} from "@medusajs/medusa"
import { AdminRuleValueOptionsListResponse } from "@medusajs/types"
import {
BatchAddPromotionRulesReq,
BatchRemovePromotionRulesReq,
BatchUpdatePromotionRulesReq,
CreatePromotionReq,
UpdatePromotionReq,
} from "../../types/api-payloads"
import {
PromotionDeleteRes,
PromotionListRes,
PromotionRes,
PromotionRuleAttributesListRes,
} from "../../types/api-responses"
import { deleteRequest, getRequest, postRequest } from "./common"
async function retrievePromotion(id: string, query?: AdminGetPromotionsParams) {
return getRequest<PromotionRes, AdminGetPromotionsParams>(
`/admin/promotions/${id}`,
query
)
}
async function listPromotions(query?: AdminGetPromotionsParams) {
return getRequest<PromotionListRes>(`/admin/promotions`, query)
}
async function deletePromotion(id: string) {
return deleteRequest<PromotionDeleteRes>(`/admin/promotions/${id}`)
}
async function updatePromotion(id: string, payload: UpdatePromotionReq) {
return postRequest<PromotionRes>(`/admin/promotions/${id}`, payload)
}
async function createPromotion(payload: CreatePromotionReq) {
return postRequest<PromotionRes>(`/admin/promotions`, payload)
}
async function addPromotionRules(
id: string,
ruleType: string,
payload: BatchAddPromotionRulesReq
) {
return postRequest<PromotionRes>(
`/admin/promotions/${id}/${ruleType}/batch`,
{
create: payload.rules,
}
)
}
async function updatePromotionRules(
id: string,
ruleType: string,
payload: BatchUpdatePromotionRulesReq
) {
return postRequest<PromotionRes>(
`/admin/promotions/${id}/${ruleType}/batch`,
{
update: payload.rules,
}
)
}
async function removePromotionRules(
id: string,
ruleType: string,
payload: BatchRemovePromotionRulesReq
) {
return postRequest<PromotionRes>(
`/admin/promotions/${id}/${ruleType}/batch`,
{
delete: payload.rule_ids,
}
)
}
async function listPromotionRules(
id: string | null,
ruleType: string,
query?: Record<string, string>
) {
return getRequest<PromotionRuleAttributesListRes>(
`/admin/promotions/${id}/${ruleType}`,
query
)
}
async function listPromotionRuleAttributes(
ruleType: string,
promotionType?: string
) {
return getRequest<PromotionRuleAttributesListRes>(
`/admin/promotions/rule-attribute-options/${ruleType}?promotion_type=${promotionType}`
)
}
async function listPromotionRuleValues(
ruleType: string,
ruleValue: string,
query?: AdminGetPromotionsRuleValueParams
) {
return getRequest<AdminRuleValueOptionsListResponse>(
`/admin/promotions/rule-value-options/${ruleType}/${ruleValue}`,
query
)
}
export const promotions = {
retrieve: retrievePromotion,
list: listPromotions,
delete: deletePromotion,
update: updatePromotion,
create: createPromotion,
addRules: addPromotionRules,
removeRules: removePromotionRules,
updateRules: updatePromotionRules,
listRules: listPromotionRules,
listRuleAttributes: listPromotionRuleAttributes,
listRuleValues: listPromotionRuleValues,
}