From 20cd6a7b516990d87bd1c13afc1f1996416d7b2f Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Wed, 5 Jun 2024 09:34:18 +0200 Subject: [PATCH] chore: Split up admin domain in js-sdk (#7612) --- packages/core/js-sdk/src/admin/customer.ts | 80 ++++ packages/core/js-sdk/src/admin/index.ts | 391 +----------------- packages/core/js-sdk/src/admin/invite.ts | 87 ++++ .../js-sdk/src/admin/product-collection.ts | 92 +++++ packages/core/js-sdk/src/admin/product.ts | 26 ++ packages/core/js-sdk/src/admin/region.ts | 81 ++++ packages/core/js-sdk/src/admin/upload.ts | 67 +++ 7 files changed, 452 insertions(+), 372 deletions(-) create mode 100644 packages/core/js-sdk/src/admin/customer.ts create mode 100644 packages/core/js-sdk/src/admin/invite.ts create mode 100644 packages/core/js-sdk/src/admin/product-collection.ts create mode 100644 packages/core/js-sdk/src/admin/product.ts create mode 100644 packages/core/js-sdk/src/admin/region.ts create mode 100644 packages/core/js-sdk/src/admin/upload.ts diff --git a/packages/core/js-sdk/src/admin/customer.ts b/packages/core/js-sdk/src/admin/customer.ts new file mode 100644 index 0000000000..bc7da8b45f --- /dev/null +++ b/packages/core/js-sdk/src/admin/customer.ts @@ -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>( + `/admin/customers/${id}`, + { + method: "DELETE", + headers, + } + ) + } +} diff --git a/packages/core/js-sdk/src/admin/index.ts b/packages/core/js-sdk/src/admin/index.ts index 212165322a..47ddd920d6 100644 --- a/packages/core/js-sdk/src/admin/index.ts +++ b/packages/core/js-sdk/src/admin/index.ts @@ -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>( - `/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>( - `/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>( - `/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>( - `/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>(`/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) } } diff --git a/packages/core/js-sdk/src/admin/invite.ts b/packages/core/js-sdk/src/admin/invite.ts new file mode 100644 index 0000000000..43c9d03126 --- /dev/null +++ b/packages/core/js-sdk/src/admin/invite.ts @@ -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>( + `/admin/invites/${id}`, + { + method: "DELETE", + headers, + } + ) + } +} diff --git a/packages/core/js-sdk/src/admin/product-collection.ts b/packages/core/js-sdk/src/admin/product-collection.ts new file mode 100644 index 0000000000..2c1bd9815d --- /dev/null +++ b/packages/core/js-sdk/src/admin/product-collection.ts @@ -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>( + `/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, + } + ) + } +} diff --git a/packages/core/js-sdk/src/admin/product.ts b/packages/core/js-sdk/src/admin/product.ts new file mode 100644 index 0000000000..078e756d66 --- /dev/null +++ b/packages/core/js-sdk/src/admin/product.ts @@ -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, + } + ) + } +} diff --git a/packages/core/js-sdk/src/admin/region.ts b/packages/core/js-sdk/src/admin/region.ts new file mode 100644 index 0000000000..70fd099506 --- /dev/null +++ b/packages/core/js-sdk/src/admin/region.ts @@ -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>( + `/admin/regions/${id}`, + { + method: "DELETE", + headers, + } + ) + } +} diff --git a/packages/core/js-sdk/src/admin/upload.ts b/packages/core/js-sdk/src/admin/upload.ts new file mode 100644 index 0000000000..9568d96f49 --- /dev/null +++ b/packages/core/js-sdk/src/admin/upload.ts @@ -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>(`/admin/uploads/${id}`, { + method: "DELETE", + headers, + }) + } +}