chore: Split up admin domain in js-sdk (#7612)

This commit is contained in:
Oli Juhl
2024-06-05 09:34:18 +02:00
committed by GitHub
parent 4a6327e497
commit 20cd6a7b51
7 changed files with 452 additions and 372 deletions

View File

@@ -0,0 +1,80 @@
import {
DeleteResponse,
FindParams,
HttpTypes,
PaginatedResponse,
SelectParams,
} from "@medusajs/types"
import { Client } from "../client"
import { ClientHeaders } from "../types"
export class Customer {
private client: Client
constructor(client: Client) {
this.client = client
}
async create(
body: HttpTypes.AdminCreateCustomer,
query?: SelectParams,
headers?: ClientHeaders
) {
return this.client.fetch<{
customer: HttpTypes.AdminCustomer
}>(`/admin/customers`, {
method: "POST",
headers,
body,
query,
})
}
async update(
id: string,
body: HttpTypes.AdminUpdateCustomer,
query?: SelectParams,
headers?: ClientHeaders
) {
return this.client.fetch<{ customer: HttpTypes.AdminCustomer }>(
`/admin/customers/${id}`,
{
method: "POST",
headers,
body,
query,
}
)
}
async list(
queryParams?: FindParams & HttpTypes.AdminCollectionFilters,
headers?: ClientHeaders
) {
return this.client.fetch<
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>
>(`/admin/customers`, {
headers,
query: queryParams,
})
}
async retrieve(id: string, query?: SelectParams, headers?: ClientHeaders) {
return this.client.fetch<{ customer: HttpTypes.AdminCustomer }>(
`/admin/customers/${id}`,
{
query,
headers,
}
)
}
async delete(id: string, headers?: ClientHeaders) {
return this.client.fetch<DeleteResponse<"customer">>(
`/admin/customers/${id}`,
{
method: "DELETE",
headers,
}
)
}
}

View File

@@ -1,378 +1,25 @@
import {
DeleteResponse,
FindParams,
HttpTypes,
PaginatedResponse,
SelectParams,
} from "@medusajs/types"
import { Client } from "../client"
import { ClientHeaders } from "../types"
import { Customer } from "./customer"
import { Invite } from "./invite"
import { Product } from "./product"
import { ProductCollection } from "./product-collection"
import { Region } from "./region"
import { Upload } from "./upload"
export class Admin {
private client: Client
public invite: Invite
public customer: Customer
public productCollection: ProductCollection
public product: Product
public upload: Upload
public region: Region
constructor(client: Client) {
this.client = client
}
public region = {
create: async (
body: HttpTypes.AdminCreateRegion,
query?: SelectParams,
headers?: ClientHeaders
) => {
return await this.client.fetch<{ region: HttpTypes.AdminRegion }>(
`/admin/regions`,
{
method: "POST",
headers,
body,
query,
}
)
},
update: async (
id: string,
body: HttpTypes.AdminUpdateRegion,
query?: SelectParams,
headers?: ClientHeaders
) => {
return await this.client.fetch<{ region: HttpTypes.AdminRegion }>(
`/admin/regions/${id}`,
{
method: "POST",
headers,
body,
query,
}
)
},
list: async (
queryParams?: FindParams & HttpTypes.AdminRegionFilters,
headers?: ClientHeaders
) => {
return await this.client.fetch<
PaginatedResponse<{ regions: HttpTypes.AdminRegion[] }>
>(`/admin/regions`, {
query: queryParams,
headers,
})
},
retrieve: async (
id: string,
query?: SelectParams,
headers?: ClientHeaders
) => {
return await this.client.fetch<{ region: HttpTypes.AdminRegion }>(
`/admin/regions/${id}`,
{
query,
headers,
}
)
},
delete: async (id: string, headers?: ClientHeaders) => {
return await this.client.fetch<DeleteResponse<"region">>(
`/admin/regions/${id}`,
{
method: "DELETE",
headers,
}
)
},
}
public invites = {
accept: async (
input: HttpTypes.AdminAcceptInvite & { invite_token: string },
query?: SelectParams,
headers?: ClientHeaders
) => {
const { invite_token, ...rest } = input
return await this.client.fetch<{ user: HttpTypes.AdminUserResponse }>(
`/admin/invites/accept?token=${input.invite_token}`,
{
method: "POST",
headers,
body: rest,
query,
}
)
},
create: async (
body: HttpTypes.AdminCreateInvite,
query?: SelectParams,
headers?: ClientHeaders
) => {
return await this.client.fetch<{ invite: HttpTypes.AdminInviteResponse }>(
`/admin/invites`,
{
method: "POST",
headers,
body,
query,
}
)
},
retrieve: async (
id: string,
query?: SelectParams,
headers?: ClientHeaders
) => {
return await this.client.fetch<{ invite: HttpTypes.AdminInviteResponse }>(
`/admin/invites/${id}`,
{
headers,
query,
}
)
},
list: async (queryParams?: FindParams, headers?: ClientHeaders) => {
return await this.client.fetch<
PaginatedResponse<{ invites: HttpTypes.AdminInviteResponse[] }>
>(`/admin/invites`, {
headers,
query: queryParams,
})
},
resend: async (id: string, headers?: ClientHeaders) => {
return await this.client.fetch<{ invite: HttpTypes.AdminInviteResponse }>(
`/admin/invites/${id}/resend`,
{
headers,
}
)
},
delete: async (id: string, headers?: ClientHeaders) => {
return await this.client.fetch<DeleteResponse<"invite">>(
`/admin/invites/${id}`,
{
method: "DELETE",
headers,
}
)
},
}
public products = {
create: async (
body: HttpTypes.AdminCreateProduct,
query?: SelectParams,
headers?: ClientHeaders
) => {
return await this.client.fetch<{ product: HttpTypes.AdminProduct }>(
`/admin/products`,
{
method: "POST",
headers,
body,
query,
}
)
},
}
public customer = {
create: async (
body: HttpTypes.AdminCreateCustomer,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{
customer: HttpTypes.AdminCustomer
}>(`/admin/customers`, {
method: "POST",
headers,
body,
query,
})
},
update: async (
id: string,
body: HttpTypes.AdminUpdateCustomer,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{ customer: HttpTypes.AdminCustomer }>(
`/admin/customers/${id}`,
{
method: "POST",
headers,
body,
query,
}
)
},
list: async (
queryParams?: FindParams & HttpTypes.AdminCollectionFilters,
headers?: ClientHeaders
) => {
return this.client.fetch<
PaginatedResponse<{ customers: HttpTypes.AdminCustomer[] }>
>(`/admin/customers`, {
headers,
query: queryParams,
})
},
retrieve: async (
id: string,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{ customer: HttpTypes.AdminCustomer }>(
`/admin/customers/${id}`,
{
query,
headers,
}
)
},
delete: async (id: string, headers?: ClientHeaders) => {
return this.client.fetch<DeleteResponse<"customer">>(
`/admin/customers/${id}`,
{
method: "DELETE",
headers,
}
)
},
}
public collection = {
create: async (
body: HttpTypes.AdminCreateCollection,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{ collection: HttpTypes.AdminCollection }>(
`/admin/collections`,
{
method: "POST",
headers,
body,
query,
}
)
},
update: async (
id: string,
body: HttpTypes.AdminUpdateCollection,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{ collection: HttpTypes.AdminCollection }>(
`/admin/collections/${id}`,
{
method: "POST",
headers,
body,
query,
}
)
},
list: async (queryParams?: FindParams, headers?: ClientHeaders) => {
return this.client.fetch<
PaginatedResponse<{ collections: HttpTypes.AdminCollection[] }>
>(`/admin/collections`, {
headers,
query: queryParams,
})
},
retrieve: async (
id: string,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{ collection: HttpTypes.AdminCollection }>(
`/admin/collections/${id}`,
{
query,
headers,
}
)
},
delete: async (id: string, headers?: ClientHeaders) => {
return this.client.fetch<DeleteResponse<"collection">>(
`/admin/collections/${id}`,
{
method: "DELETE",
headers,
}
)
},
updateProducts: async (
id: string,
body: HttpTypes.AdminUpdateCollectionProducts,
headers?: ClientHeaders
) => {
return this.client.fetch<{ collection: HttpTypes.AdminCollection }>(
`/admin/collections/${id}/products`,
{
method: "POST",
headers,
body,
}
)
},
}
public uploads = {
// Note: The creation/upload flow be made more advanced, with support for streaming and progress, but for now we keep it simple
create: async (
body: HttpTypes.AdminUploadFile,
query?: SelectParams,
headers?: ClientHeaders
) => {
const form = new FormData()
if (body instanceof FileList) {
Array.from(body).forEach((file) => {
form.append("files", file)
})
} else {
body.files.forEach((file) => {
form.append(
"files",
"content" in file
? new Blob([file.content], {
type: "text/plain",
})
: file,
file.name
)
})
}
return this.client.fetch<{ files: HttpTypes.AdminFile[] }>(
`/admin/uploads`,
{
method: "POST",
headers: {
...headers,
// Let the browser determine the content type.
"content-type": null,
},
body: form,
query,
}
)
},
retrieve: async (
id: string,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{ file: HttpTypes.AdminFile }>(
`/admin/uploads/${id}`,
{
query,
headers,
}
)
},
delete: async (id: string, headers?: ClientHeaders) => {
return this.client.fetch<DeleteResponse<"file">>(`/admin/uploads/${id}`, {
method: "DELETE",
headers,
})
},
this.invite = new Invite(client)
this.customer = new Customer(client)
this.productCollection = new ProductCollection(client)
this.product = new Product(client)
this.upload = new Upload(client)
this.region = new Region(client)
}
}

View File

@@ -0,0 +1,87 @@
import {
DeleteResponse,
FindParams,
HttpTypes,
PaginatedResponse,
SelectParams,
} from "@medusajs/types"
import { Client } from "../client"
import { ClientHeaders } from "../types"
export class Invite {
private client: Client
constructor(client: Client) {
this.client = client
}
async accept(
input: HttpTypes.AdminAcceptInvite & { invite_token: string },
query?: SelectParams,
headers?: ClientHeaders
) {
const { invite_token, ...rest } = input
return await this.client.fetch<{ user: HttpTypes.AdminUserResponse }>(
`/admin/invites/accept?token=${input.invite_token}`,
{
method: "POST",
headers,
body: rest,
query,
}
)
}
async create(
body: HttpTypes.AdminCreateInvite,
query?: SelectParams,
headers?: ClientHeaders
) {
return await this.client.fetch<{ invite: HttpTypes.AdminInviteResponse }>(
`/admin/invites`,
{
method: "POST",
headers,
body,
query,
}
)
}
async retrieve(id: string, query?: SelectParams, headers?: ClientHeaders) {
return await this.client.fetch<{ invite: HttpTypes.AdminInviteResponse }>(
`/admin/invites/${id}`,
{
headers,
query,
}
)
}
async list(queryParams?: FindParams, headers?: ClientHeaders) {
return await this.client.fetch<
PaginatedResponse<{ invites: HttpTypes.AdminInviteResponse[] }>
>(`/admin/invites`, {
headers,
query: queryParams,
})
}
async resend(id: string, headers?: ClientHeaders) {
return await this.client.fetch<{ invite: HttpTypes.AdminInviteResponse }>(
`/admin/invites/${id}/resend`,
{
headers,
}
)
}
async delete(id: string, headers?: ClientHeaders) {
return await this.client.fetch<DeleteResponse<"invite">>(
`/admin/invites/${id}`,
{
method: "DELETE",
headers,
}
)
}
}

View File

@@ -0,0 +1,92 @@
import {
DeleteResponse,
FindParams,
HttpTypes,
PaginatedResponse,
SelectParams,
} from "@medusajs/types"
import { Client } from "../client"
import { ClientHeaders } from "../types"
export class ProductCollection {
private client: Client
constructor(client: Client) {
this.client = client
}
async create(
body: HttpTypes.AdminCreateCollection,
query?: SelectParams,
headers?: ClientHeaders
) {
return this.client.fetch<{ collection: HttpTypes.AdminCollection }>(
`/admin/collections`,
{
method: "POST",
headers,
body,
query,
}
)
}
async update(
id: string,
body: HttpTypes.AdminUpdateCollection,
query?: SelectParams,
headers?: ClientHeaders
) {
return this.client.fetch<{ collection: HttpTypes.AdminCollection }>(
`/admin/collections/${id}`,
{
method: "POST",
headers,
body,
query,
}
)
}
async list(queryParams?: FindParams, headers?: ClientHeaders) {
return this.client.fetch<
PaginatedResponse<{ collections: HttpTypes.AdminCollection[] }>
>(`/admin/collections`, {
headers,
query: queryParams,
})
}
async retrieve(id: string, query?: SelectParams, headers?: ClientHeaders) {
return this.client.fetch<{ collection: HttpTypes.AdminCollection }>(
`/admin/collections/${id}`,
{
query,
headers,
}
)
}
async delete(id: string, headers?: ClientHeaders) {
return this.client.fetch<DeleteResponse<"collection">>(
`/admin/collections/${id}`,
{
method: "DELETE",
headers,
}
)
}
async updateProducts(
id: string,
body: HttpTypes.AdminUpdateCollectionProducts,
headers?: ClientHeaders
) {
return this.client.fetch<{ collection: HttpTypes.AdminCollection }>(
`/admin/collections/${id}/products`,
{
method: "POST",
headers,
body,
}
)
}
}

View File

@@ -0,0 +1,26 @@
import { HttpTypes, SelectParams } from "@medusajs/types"
import { Client } from "../client"
import { ClientHeaders } from "../types"
export class Product {
private client: Client
constructor(client: Client) {
this.client = client
}
async create(
body: HttpTypes.AdminCreateProduct,
query?: SelectParams,
headers?: ClientHeaders
) {
return await this.client.fetch<{ product: HttpTypes.AdminProduct }>(
`/admin/products`,
{
method: "POST",
headers,
body,
query,
}
)
}
}

View File

@@ -0,0 +1,81 @@
import {
DeleteResponse,
FindParams,
HttpTypes,
PaginatedResponse,
SelectParams,
} from "@medusajs/types"
import { Client } from "../client"
import { ClientHeaders } from "../types"
export class Region {
private client: Client
constructor(client: Client) {
this.client = client
}
async create(
body: HttpTypes.AdminCreateRegion,
query?: SelectParams,
headers?: ClientHeaders
) {
return await this.client.fetch<{ region: HttpTypes.AdminRegion }>(
`/admin/regions`,
{
method: "POST",
headers,
body,
query,
}
)
}
async update(
id: string,
body: HttpTypes.AdminUpdateRegion,
query?: SelectParams,
headers?: ClientHeaders
) {
return await this.client.fetch<{ region: HttpTypes.AdminRegion }>(
`/admin/regions/${id}`,
{
method: "POST",
headers,
body,
query,
}
)
}
async list(
queryParams?: FindParams & HttpTypes.AdminRegionFilters,
headers?: ClientHeaders
) {
return await this.client.fetch<
PaginatedResponse<{ regions: HttpTypes.AdminRegion[] }>
>(`/admin/regions`, {
query: queryParams,
headers,
})
}
async retrieve(id: string, query?: SelectParams, headers?: ClientHeaders) {
return await this.client.fetch<{ region: HttpTypes.AdminRegion }>(
`/admin/regions/${id}`,
{
query,
headers,
}
)
}
async delete(id: string, headers?: ClientHeaders) {
return await this.client.fetch<DeleteResponse<"region">>(
`/admin/regions/${id}`,
{
method: "DELETE",
headers,
}
)
}
}

View File

@@ -0,0 +1,67 @@
import { DeleteResponse, HttpTypes, SelectParams } from "@medusajs/types"
import { Client } from "../client"
import { ClientHeaders } from "../types"
export class Upload {
private client: Client
constructor(client: Client) {
this.client = client
}
// Note: The creation/upload flow be made more advanced, with support for streaming and progress, but for now we keep it simple
async create(
body: HttpTypes.AdminUploadFile,
query?: SelectParams,
headers?: ClientHeaders
) {
const form = new FormData()
if (body instanceof FileList) {
Array.from(body).forEach((file) => {
form.append("files", file)
})
} else {
body.files.forEach((file) => {
form.append(
"files",
"content" in file
? new Blob([file.content], {
type: "text/plain",
})
: file,
file.name
)
})
}
return this.client.fetch<{ files: HttpTypes.AdminFile[] }>(
`/admin/uploads`,
{
method: "POST",
headers: {
...headers,
// Let the browser determine the content type.
"content-type": null,
},
body: form,
query,
}
)
}
async retrieve(id: string, query?: SelectParams, headers?: ClientHeaders) {
return this.client.fetch<{ file: HttpTypes.AdminFile }>(
`/admin/uploads/${id}`,
{
query,
headers,
}
)
}
async delete(id: string, headers?: ClientHeaders) {
return this.client.fetch<DeleteResponse<"file">>(`/admin/uploads/${id}`, {
method: "DELETE",
headers,
})
}
}