From 40f6e888750dd43e3cecbd3f2df1648af76ada5a Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Tue, 14 Dec 2021 17:33:55 +0100 Subject: [PATCH] Feat/medusa js admin collections (#916) * feat: Add Auth Admin routes to JS client * include api key verification for admin * add admin/collections to medusa-js * undo file changes * jsdoc Co-authored-by: olivermrbl --- .../src/resources/admin/collections.ts | 80 +++++++++++++++++++ .../medusa-js/src/resources/admin/index.ts | 2 + packages/medusa/src/api/index.js | 1 + 3 files changed, 83 insertions(+) create mode 100644 packages/medusa-js/src/resources/admin/collections.ts diff --git a/packages/medusa-js/src/resources/admin/collections.ts b/packages/medusa-js/src/resources/admin/collections.ts new file mode 100644 index 0000000000..10207f9023 --- /dev/null +++ b/packages/medusa-js/src/resources/admin/collections.ts @@ -0,0 +1,80 @@ +import { + AdminPostCollectionsReq, + AdminCollectionsRes, + AdminPostCollectionsCollectionReq, + AdminCollectionsDeleteRes, + AdminCollectionsListRes, + AdminGetCollectionsParams, +} from "@medusajs/medusa" +import { ResponsePromise } from "../../typings" +import BaseResource from "../base" + +class AdminCollectionsResource extends BaseResource { + /** + * @description Creates a collection. + * @param payload + * @returns Created collection. + */ + create( + payload: AdminPostCollectionsReq + ): ResponsePromise { + const path = `/admin/collections` + return this.client.request("POST", path, payload) + } + + /** + * @description Updates a collection + * @param id id of the collection to update. + * @param payload update to apply to collection. + * @returns the updated collection. + */ + update( + id: string, + payload: AdminPostCollectionsCollectionReq + ): ResponsePromise { + const path = `/admin/collections/${id}` + return this.client.request("POST", path, payload) + } + + /** + * @description deletes a collection + * @param id id of collection to delete. + * @returns Deleted response + */ + delete(id: string): ResponsePromise { + const path = `/admin/collections/${id}` + return this.client.request("DELETE", path) + } + + /** + * @description get a collection + * @param id id of the collection to retrieve. + * @returns the collection with the given id + */ + retrieve(id: string): ResponsePromise { + const path = `/admin/collections/${id}` + return this.client.request("GET", path) + } + + /** + * @description Lists collections matching a query + * @param query Query for searching collections + * @returns a list of colllections matching the query. + */ + list( + query?: AdminGetCollectionsParams + ): ResponsePromise { + let path = `/admin/collections` + + if (query) { + const queryString = Object.entries(query).map(([key, value]) => { + return typeof value !== "undefined" ? `${key}=${value}` : "" + }) + path = `/admin/collections?${queryString.join("&")}` + } + + return this.client.request("GET", path) + } +} + +export default AdminCollectionsResource diff --git a/packages/medusa-js/src/resources/admin/index.ts b/packages/medusa-js/src/resources/admin/index.ts index 8c8be33b67..425bf1c5e5 100644 --- a/packages/medusa-js/src/resources/admin/index.ts +++ b/packages/medusa-js/src/resources/admin/index.ts @@ -2,6 +2,7 @@ import BaseResource from "../base" import AdminAuthResource from "./auth" import AdminCustomersResource from "./customers" import AdminDiscountsResource from "./discounts" +import CollectionsResource from "./collections" import AdminDraftOrdersResource from "./draft-orders" import AdminGiftCardsResource from "./gift-cards" import AdminInvitesResource from "./invites" @@ -11,6 +12,7 @@ class Admin extends BaseResource { public auth = new AdminAuthResource(this.client) public customers = new AdminCustomersResource(this.client) public discounts = new AdminDiscountsResource(this.client) + public collections = new CollectionsResource(this.client) public draftOrders = new AdminDraftOrdersResource(this.client) public giftCards = new AdminGiftCardsResource(this.client) public invites = new AdminInvitesResource(this.client) diff --git a/packages/medusa/src/api/index.js b/packages/medusa/src/api/index.js index 091f59623b..7aa7150dd4 100644 --- a/packages/medusa/src/api/index.js +++ b/packages/medusa/src/api/index.js @@ -16,6 +16,7 @@ export default (container, config) => { } // Admin +export * from "./routes/admin/collections" export * from "./routes/admin/auth" export * from "./routes/admin/customers" export * from "./routes/admin/discounts"