From 25fe224a10842a7ac93ed496a6724ef113b41916 Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Mon, 13 Dec 2021 21:32:47 +0100 Subject: [PATCH] feat: Adds Customer Admin routes to JS client (#918) --- .eslintrc.js | 7 +++ .../src/resources/admin/customers.ts | 63 +++++++++++++++++++ .../medusa-js/src/resources/admin/index.ts | 2 + packages/medusa/src/api/index.js | 1 + 4 files changed, 73 insertions(+) create mode 100644 packages/medusa-js/src/resources/admin/customers.ts diff --git a/.eslintrc.js b/.eslintrc.js index ea84ca099b..038cc6b184 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -41,6 +41,13 @@ module.exports = { "valid-jsdoc": ["off"], }, }, + { + // Medusa JS client + files: ["**/medusa-js/**/resources/**/*.ts"], + rules: { + "valid-jsdoc": ["off"], + }, + }, { files: ["**/api/**/*.ts"], rules: { diff --git a/packages/medusa-js/src/resources/admin/customers.ts b/packages/medusa-js/src/resources/admin/customers.ts new file mode 100644 index 0000000000..631470ab90 --- /dev/null +++ b/packages/medusa-js/src/resources/admin/customers.ts @@ -0,0 +1,63 @@ +import { + AdminCustomersListRes, + AdminCustomersRes, + AdminGetCustomersParams, + AdminPostCustomersReq, +} from "@medusajs/medusa" +import { ResponsePromise } from "../.." +import BaseResource from "../base" + +class AdminCustomersResource extends BaseResource { + /** + * Creates a customer + * @param payload information of customer + */ + create(payload: AdminPostCustomersReq): ResponsePromise { + const path = `/admin/customers` + return this.client.request("POST", path, payload) + } + + /** + * Updates a customer + * @param id customer id + * @param payload data to update customer with + */ + update( + id: string, + payload: AdminPostCustomersReq + ): ResponsePromise { + const path = `/admin/customers/${id}` + return this.client.request("POST", path, payload) + } + + /** + * Retrieves a customer + * @param id customer id + */ + retrieve(id: string): ResponsePromise { + const path = `/admin/customers/${id}` + return this.client.request("GET", path) + } + + /** + * Lists customers + * @param query optional + */ + list( + query?: AdminGetCustomersParams + ): ResponsePromise { + let path = `/admin/customers` + + if (query) { + const queryString = Object.entries(query).map(([key, value]) => { + return `${key}=${value}` + }) + + path = `/admin/customers?${queryString.join("&")}` + } + + return this.client.request("GET", path) + } +} + +export default AdminCustomersResource diff --git a/packages/medusa-js/src/resources/admin/index.ts b/packages/medusa-js/src/resources/admin/index.ts index fbd0ea22ae..2ef6a2295f 100644 --- a/packages/medusa-js/src/resources/admin/index.ts +++ b/packages/medusa-js/src/resources/admin/index.ts @@ -1,8 +1,10 @@ import BaseResource from "../base" import AdminAuthResource from "./auth" +import AdminCustomersResource from "./customers" class Admin extends BaseResource { public auth = new AdminAuthResource(this.client) + public customers = new AdminCustomersResource(this.client) } export default Admin diff --git a/packages/medusa/src/api/index.js b/packages/medusa/src/api/index.js index 15b5ca1f7d..0fd04877de 100644 --- a/packages/medusa/src/api/index.js +++ b/packages/medusa/src/api/index.js @@ -16,6 +16,7 @@ export default (container, config) => { } export * from "./routes/admin/auth" +export * from "./routes/admin/customers" export * from "./routes/admin/notifications" export * from "./routes/admin/store" export * from "./routes/admin/variants"