diff --git a/packages/medusa-js/package.json b/packages/medusa-js/package.json index 7074b68a5a..5eb10679df 100644 --- a/packages/medusa-js/package.json +++ b/packages/medusa-js/package.json @@ -1,6 +1,6 @@ { "name": "@medusajs/medusa-js", - "version": "1.0.3", + "version": "1.0.1-canary.1", "description": "Client for Medusa Commerce Rest API", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -8,9 +8,9 @@ "dist/**/*" ], "scripts": { - "prepare": "cross-env NODE_ENV=production npm run build", "build": "tsc --build", - "test": "jest --config jestconfig.json" + "test": "jest --config jestconfig.json", + "prepare": "cross-env NODE_ENV=production npm run build" }, "author": "Oliver Juhl", "license": "MIT", @@ -33,6 +33,5 @@ "jest": "^26.6.3", "prettier": "^2.2.1", "typescript": "^4.1.3" - }, - "gitHead": "a69b1e85be1da3b1b5bc4c5446471252623c8808" + } } diff --git a/packages/medusa-js/src/index.ts b/packages/medusa-js/src/index.ts index 874a7cc592..e24b219fd7 100644 --- a/packages/medusa-js/src/index.ts +++ b/packages/medusa-js/src/index.ts @@ -12,6 +12,7 @@ import ReturnReasonsResource from "./resources/return-reasons" import ReturnsResource from "./resources/returns" import ShippingOptionsResource from "./resources/shipping-options" import SwapsResource from "./resources/swaps" +import PaymentMethodsResource from "./resources/payment-methods" class Medusa { private client: Client @@ -28,6 +29,7 @@ class Medusa { public swaps: SwapsResource public collections: CollectionsResource public giftCards: GiftCardsResource + public paymentMethods: PaymentMethodsResource constructor(config: Config) { this.client = new Client(config) @@ -45,7 +47,10 @@ class Medusa { this.swaps = new SwapsResource(this.client) this.collections = new CollectionsResource(this.client) this.giftCards = new GiftCardsResource(this.client) + this.paymentMethods = new PaymentMethodsResource(this.client) } } export default Medusa + +export * from "./typings" diff --git a/packages/medusa-js/src/request.ts b/packages/medusa-js/src/request.ts index ab3a72b15b..59f77a4929 100644 --- a/packages/medusa-js/src/request.ts +++ b/packages/medusa-js/src/request.ts @@ -151,22 +151,6 @@ class Client { return client } - /** - * Format the response data as: - * { cart: { id: "some_cart", ... } } - * @param {object} data Axios response data - * @param {number} status Axios response status code - * @return {object} - */ - createRawResponse(data: object, status: number): object { - const res = { status } - Object.entries(data).map(([key, value]) => { - res[key] = value - }) - - return res as any // eslint-disable-line - } - /** * Axios request * @param {Types.RequestMethod} method request method @@ -190,9 +174,11 @@ class Client { headers: this.setHeaders(options, method, path), } - const { data, status } = await this.axiosClient(reqOpts) + // e.g. data = { cart: { ... } }, response = { status, headers, ... } + const { data, ...response } = await this.axiosClient(reqOpts) - return this.createRawResponse(data, status) + // e.g. would return an object like of this shape { cart, response } + return { ...data, response } } } diff --git a/packages/medusa-js/src/resources/addresses.ts b/packages/medusa-js/src/resources/addresses.ts index 40fe1dee1d..ab88a82a20 100644 --- a/packages/medusa-js/src/resources/addresses.ts +++ b/packages/medusa-js/src/resources/addresses.ts @@ -3,18 +3,18 @@ import { StorePostCustomersCustomerAddressesAddressReq, StorePostCustomersCustomerAddressesReq, } from "@medusajs/medusa" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import BaseResource from "./base" class AddressesResource extends BaseResource { /** * Adds an address to a customers saved addresses * @param {StorePostCustomersCustomerAddressesReq} payload contains information to create an address - * @return {AxiosPromise} + * @return {ResponsePromise} */ addAddress( payload: StorePostCustomersCustomerAddressesReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/customers/me/addresses` return this.client.request("POST", path, payload) } @@ -22,9 +22,9 @@ class AddressesResource extends BaseResource { /** * Deletes an address of a customer * @param {string} address_id id of the address to delete - * @return {AxiosPromise} + * @return {ResponsePromise} */ - deleteAddress(address_id: string): AxiosPromise { + deleteAddress(address_id: string): ResponsePromise { const path = `/store/customers/me/addresses/${address_id}` return this.client.request("DELETE", path) } @@ -38,7 +38,7 @@ class AddressesResource extends BaseResource { updateAddress( address_id: string, payload: StorePostCustomersCustomerAddressesAddressReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/customers/me/addresses/${address_id}` return this.client.request("POST", path, payload) } diff --git a/packages/medusa-js/src/resources/auth.ts b/packages/medusa-js/src/resources/auth.ts index 131f21f984..37df601d65 100644 --- a/packages/medusa-js/src/resources/auth.ts +++ b/packages/medusa-js/src/resources/auth.ts @@ -1,18 +1,18 @@ -import { AxiosPromise } from "axios" import { StoreGetAuthEmailRes, StorePostAuthReq, StoreAuthRes, } from "@medusajs/medusa" +import { ResponsePromise } from "../typings" import BaseResource from "./base" class AuthResource extends BaseResource { /** * @description Authenticates a customer using email and password combination * @param {StorePostAuthReq} payload authentication payload - * @return {AxiosPromise} + * @return {ResponsePromise} */ - authenticate(payload: StorePostAuthReq): AxiosPromise { + authenticate(payload: StorePostAuthReq): ResponsePromise { const path = `/store/auth` return this.client.request("POST", path, payload) } @@ -20,9 +20,9 @@ class AuthResource extends BaseResource { /** * @description Retrieves an authenticated session * Usually used to check if authenticated session is alive. - * @return {AxiosPromise} + * @return {ResponsePromise} */ - getSession(): AxiosPromise { + getSession(): ResponsePromise { const path = `/store/auth` return this.client.request("GET", path) } @@ -30,9 +30,9 @@ class AuthResource extends BaseResource { /** * @description Check if email exists * @param {string} email is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - exists(email: string): AxiosPromise { + exists(email: string): ResponsePromise { const path = `/store/auth/${email}` return this.client.request("GET", path) } diff --git a/packages/medusa-js/src/resources/carts.ts b/packages/medusa-js/src/resources/carts.ts index bcb558bbe7..9af25e920a 100644 --- a/packages/medusa-js/src/resources/carts.ts +++ b/packages/medusa-js/src/resources/carts.ts @@ -6,7 +6,7 @@ import { StorePostCartsCartReq, StorePostCartsCartShippingMethodReq, } from "@medusajs/medusa" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import BaseResource from "./base" import LineItemsResource from "./line-items" @@ -17,12 +17,12 @@ class CartsResource extends BaseResource { * Adds a shipping method to cart * @param {string} cart_id Id of cart * @param {StorePostCartsCartShippingMethodReq} payload Containg id of shipping option and optional data - * @return {AxiosPromise} + * @return {ResponsePromise} */ addShippingMethod( cart_id: string, payload: StorePostCartsCartShippingMethodReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/carts/${cart_id}/shipping-methods` return this.client.request("POST", path, payload) } @@ -34,9 +34,9 @@ class CartsResource extends BaseResource { * The completion of a cart can be performed idempotently with a provided header Idempotency-Key. * If not provuided, we will generate one for the request. * @param {string} cart_id is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - complete(cart_id: string): AxiosPromise { + complete(cart_id: string): ResponsePromise { const path = `/store/carts/${cart_id}/complete` return this.client.request("POST", path) } @@ -45,9 +45,9 @@ class CartsResource extends BaseResource { * Creates a cart * @param {StorePostCartReq} payload is optional and can contain a region_id and items. * The cart will contain the payload, if provided. Otherwise it will be empty - * @return {AxiosPromise} + * @return {ResponsePromise} */ - create(payload?: StorePostCartReq): AxiosPromise { + create(payload?: StorePostCartReq): ResponsePromise { const path = `/store/carts` return this.client.request("POST", path, payload) } @@ -57,9 +57,9 @@ class CartsResource extends BaseResource { * Initializes the payment sessions that can be used to pay for the items of the cart. * This is usually called when a customer proceeds to checkout. * @param {string} cart_id is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - createPaymentSessions(cart_id: string): AxiosPromise { + createPaymentSessions(cart_id: string): ResponsePromise { const path = `/store/carts/${cart_id}/payment-sessions` return this.client.request("POST", path) } @@ -68,9 +68,12 @@ class CartsResource extends BaseResource { * Removes a discount from cart. * @param {string} cart_id is required * @param {string} code discount code to remove - * @return {AxiosPromise} + * @return {ResponsePromise} */ - deleteDiscount(cart_id: string, code: string): AxiosPromise { + deleteDiscount( + cart_id: string, + code: string + ): ResponsePromise { const path = `/store/carts/${cart_id}/discounts/${code}` return this.client.request("DELETE", path) } @@ -80,12 +83,12 @@ class CartsResource extends BaseResource { * Can be useful in case a payment has failed * @param {string} cart_id is required * @param {string} provider_id the provider id of the session e.g. "stripe" - * @return {AxiosPromise} + * @return {ResponsePromise} */ deletePaymentSession( cart_id: string, provider_id: string - ): AxiosPromise { + ): ResponsePromise { const path = `/store/carts/${cart_id}/payment-sessions/${provider_id}` return this.client.request("DELETE", path) } @@ -94,12 +97,12 @@ class CartsResource extends BaseResource { * Refreshes a payment session. * @param {string} cart_id is required * @param {string} provider_id the provider id of the session e.g. "stripe" - * @return {AxiosPromise} + * @return {ResponsePromise} */ refreshPaymentSession( cart_id: string, provider_id: string - ): AxiosPromise { + ): ResponsePromise { const path = `/store/carts/${cart_id}/payment-sessions/${provider_id}/refresh` return this.client.request("POST", path) } @@ -107,9 +110,9 @@ class CartsResource extends BaseResource { /** * Retrieves a cart * @param {string} cart_id is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieve(cart_id: string): AxiosPromise { + retrieve(cart_id: string): ResponsePromise { const path = `/store/carts/${cart_id}` return this.client.request("GET", path) } @@ -118,12 +121,12 @@ class CartsResource extends BaseResource { * Refreshes a payment session. * @param {string} cart_id is required * @param {StorePostCartsCartPaymentSessionReq} payload the provider id of the session e.g. "stripe" - * @return {AxiosPromise} + * @return {ResponsePromise} */ setPaymentSession( cart_id: string, payload: StorePostCartsCartPaymentSessionReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/carts/${cart_id}/payment-session` return this.client.request("POST", path, payload) } @@ -132,12 +135,12 @@ class CartsResource extends BaseResource { * Updates a cart * @param {string} cart_id is required * @param {StorePostCartsCartReq} payload is required and can contain region_id, email, billing and shipping address - * @return {AxiosPromise} + * @return {ResponsePromise} */ update( cart_id: string, payload: StorePostCartsCartReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/carts/${cart_id}` return this.client.request("POST", path, payload) } @@ -146,12 +149,12 @@ class CartsResource extends BaseResource { * Updates the payment method * @param {string} cart_id is required * @param {StorePostCartsCartPaymentSessionUpdateReq} payload is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ updatePaymentSession( cart_id: string, payload: StorePostCartsCartPaymentSessionUpdateReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/carts/${cart_id}/payment-session/update` return this.client.request("POST", path, payload) } diff --git a/packages/medusa-js/src/resources/collections.ts b/packages/medusa-js/src/resources/collections.ts index 6e7d3fa45d..de98b59624 100644 --- a/packages/medusa-js/src/resources/collections.ts +++ b/packages/medusa-js/src/resources/collections.ts @@ -1,18 +1,18 @@ -import { AxiosPromise } from "axios" import { StoreCollectionsRes, StoreCollectionsListRes, StoreGetCollectionsParams, } from "@medusajs/medusa" +import { ResponsePromise } from "../typings" import BaseResource from "./base" class CollectionsResource extends BaseResource { /** * @description Retrieves a single collection * @param {string} id id of the collection - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieve(id: string): AxiosPromise { + retrieve(id: string): ResponsePromise { const path = `/store/collections/${id}` return this.client.request("GET", path) } @@ -20,11 +20,11 @@ class CollectionsResource extends BaseResource { /** * @description Retrieves a list of collections * @param {string} query is optional. Can contain a limit and offset for the returned list - * @return {AxiosPromise} + * @return {ResponsePromise} */ list( query?: StoreGetCollectionsParams - ): AxiosPromise { + ): ResponsePromise { let path = `/store/collections` if (query) { diff --git a/packages/medusa-js/src/resources/customers.ts b/packages/medusa-js/src/resources/customers.ts index 8b010eb46d..d9aa1fb650 100644 --- a/packages/medusa-js/src/resources/customers.ts +++ b/packages/medusa-js/src/resources/customers.ts @@ -6,7 +6,7 @@ import { StorePostCustomersCustomerReq, StorePostCustomersReq, } from "@medusajs/medusa" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import AddressesResource from "./addresses" import BaseResource from "./base" import PaymentMethodsResource from "./payment-methods" @@ -18,18 +18,18 @@ class CustomerResource extends BaseResource { /** * Creates a customer * @param {StorePostCustomersReq} payload information of customer - * @return { AxiosPromise} + * @return { ResponsePromise} */ - create(payload: StorePostCustomersReq): AxiosPromise { + create(payload: StorePostCustomersReq): ResponsePromise { const path = `/store/customers` return this.client.request("POST", path, payload) } /** * Retrieves the customer that is currently logged - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieve(): AxiosPromise { + retrieve(): ResponsePromise { const path = `/store/customers/me` return this.client.request("GET", path) } @@ -37,11 +37,11 @@ class CustomerResource extends BaseResource { /** * Updates a customer * @param {StorePostCustomersCustomerReq} payload information to update customer with - * @return {AxiosPromise} + * @return {ResponsePromise} */ update( payload: StorePostCustomersCustomerReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/customers/me` return this.client.request("POST", path, payload) } @@ -49,11 +49,11 @@ class CustomerResource extends BaseResource { /** * Retrieve customer orders * @param {StoreGetCustomersCustomerOrdersParams} params optional params to retrieve orders - * @return {AxiosPromise} + * @return {ResponsePromise} */ listOrders( params?: StoreGetCustomersCustomerOrdersParams - ): AxiosPromise { + ): ResponsePromise { let path = `/store/customers/me/orders` if (params) { let query: string | undefined @@ -76,11 +76,11 @@ class CustomerResource extends BaseResource { /** * Resets customer password * @param {StorePostCustomersCustomerPasswordTokenReq} payload info used to reset customer password - * @return {AxiosPromise} + * @return {ResponsePromise} */ resetPassword( payload: StorePostCustomersCustomerPasswordTokenReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/customers/password-reset` return this.client.request("POST", path, payload) } @@ -89,11 +89,11 @@ class CustomerResource extends BaseResource { * Generates a reset password token, which can be used to reset the password. * The token is not returned but should be sent out to the customer in an email. * @param {StorePostCustomersCustomerPasswordTokenReq} payload info used to generate token - * @return {AxiosPromise} + * @return {ResponsePromise} */ generatePasswordToken( payload: StorePostCustomersCustomerPasswordTokenReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/customers/password-token` return this.client.request("POST", path, payload) } diff --git a/packages/medusa-js/src/resources/gift-cards.ts b/packages/medusa-js/src/resources/gift-cards.ts index f949515287..bf6dd9a7ab 100644 --- a/packages/medusa-js/src/resources/gift-cards.ts +++ b/packages/medusa-js/src/resources/gift-cards.ts @@ -1,14 +1,14 @@ import { StoreGiftCardsRes } from "@medusajs/medusa" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import BaseResource from "./base" class GiftCardsResource extends BaseResource { /** * @description Retrieves a single GiftCard * @param {string} code code of the gift card - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieve(code: string): AxiosPromise { + retrieve(code: string): ResponsePromise { const path = `/store/gift-cards/${code}` return this.client.request("GET", path) } diff --git a/packages/medusa-js/src/resources/line-items.ts b/packages/medusa-js/src/resources/line-items.ts index 36a52535a8..4a096fc741 100644 --- a/packages/medusa-js/src/resources/line-items.ts +++ b/packages/medusa-js/src/resources/line-items.ts @@ -1,10 +1,9 @@ import { StoreCartsRes, - StoreCartsDeleteRes, StorePostCartsCartLineItemsItemReq, StorePostCartsCartLineItemsReq, } from "@medusajs/medusa" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import BaseResource from "./base" class LineItemsResource extends BaseResource { @@ -12,12 +11,12 @@ class LineItemsResource extends BaseResource { * Creates a line-item for a cart * @param {string} cart_id id of cart * @param {StorePostCartsCartLineItemsReq} payload details needed to create a line-item - * @return {AxiosPromise} + * @return {ResponsePromise} */ create( cart_id: string, payload: StorePostCartsCartLineItemsReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/carts/${cart_id}/line-items` return this.client.request("POST", path, payload) } @@ -28,13 +27,13 @@ class LineItemsResource extends BaseResource { * @param {string} cart_id id of cart * @param {string} line_id id of item to update * @param {StorePostCartsCartLineItemsItemReq} payload details needed to update a line-item - * @return {AxiosPromise} + * @return {ResponsePromise} */ update( cart_id: string, line_id: string, payload: StorePostCartsCartLineItemsItemReq - ): AxiosPromise { + ): ResponsePromise { const path = `/store/carts/${cart_id}/line-items/${line_id}` return this.client.request("POST", path, payload) } @@ -43,9 +42,9 @@ class LineItemsResource extends BaseResource { * Remove a line-item from a cart * @param {string} cart_id id of cart * @param {string} line_id id of item to remove - * @return {AxiosPromise} + * @return {ResponsePromise} */ - delete(cart_id: string, line_id: string): AxiosPromise { + delete(cart_id: string, line_id: string): ResponsePromise { const path = `/store/carts/${cart_id}/line-items/${line_id}` return this.client.request("DELETE", path) } diff --git a/packages/medusa-js/src/resources/orders.ts b/packages/medusa-js/src/resources/orders.ts index 1811f1ccd4..318bcaee52 100644 --- a/packages/medusa-js/src/resources/orders.ts +++ b/packages/medusa-js/src/resources/orders.ts @@ -1,14 +1,14 @@ import { StoreGetOrdersParams, StoreOrdersRes } from "@medusajs/medusa" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import BaseResource from "./base" class OrdersResource extends BaseResource { /** * @description Retrieves an order * @param {string} id is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieve(id: string): AxiosPromise { + retrieve(id: string): ResponsePromise { const path = `/store/orders/${id}` return this.client.request("GET", path) } @@ -16,9 +16,9 @@ class OrdersResource extends BaseResource { /** * @description Retrieves an order by cart id * @param {string} cart_id is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieveByCartId(cart_id: string): AxiosPromise { + retrieveByCartId(cart_id: string): ResponsePromise { const path = `/store/orders/cart/${cart_id}` return this.client.request("GET", path) } @@ -26,18 +26,18 @@ class OrdersResource extends BaseResource { /** * @description Look up an order using order details * @param {StoreGetOrdersParams} payload details used to look up the order - * @return {AxiosPromise} + * @return {ResponsePromise} */ - lookupOrder(payload: StoreGetOrdersParams): AxiosPromise { + lookupOrder(payload: StoreGetOrdersParams): ResponsePromise { let path = `/store/orders?` const queryString = Object.entries(payload).map(([key, value]) => { - let val = value + let val = value as string if (Array.isArray(value)) { val = value.join(",") } - return `${key}=${encodeURIComponent(val as string)}` + return `${key}=${encodeURIComponent(val)}` }) path = `/store/orders?${queryString.join("&")}` diff --git a/packages/medusa-js/src/resources/payment-methods.ts b/packages/medusa-js/src/resources/payment-methods.ts index 1d72c2b5b8..d2f2cbaf4b 100644 --- a/packages/medusa-js/src/resources/payment-methods.ts +++ b/packages/medusa-js/src/resources/payment-methods.ts @@ -1,13 +1,14 @@ +import { StoreCustomersListPaymentMethodsRes } from '@medusajs/medusa' +import { ResponsePromise } from "../typings" import BaseResource from "./base" -import { AxiosPromise } from "axios" class PaymentMethodsResource extends BaseResource { /** * Lists customer payment methods * @param {string} id id of cart - * @return {AxiosPromise<{ payment_methods: object[] }>} + * @return {StoreCustomersListPaymentMethodsRes} */ - list(id: string): AxiosPromise<{ payment_methods: object[] }> { + list(id: string): ResponsePromise { const path = `/store/carts/${id}/payment-methods` return this.client.request("GET", path) } diff --git a/packages/medusa-js/src/resources/product-variants.ts b/packages/medusa-js/src/resources/product-variants.ts index 4c7353981a..7e63bf2a3c 100644 --- a/packages/medusa-js/src/resources/product-variants.ts +++ b/packages/medusa-js/src/resources/product-variants.ts @@ -3,16 +3,16 @@ import { StoreVariantsListRes, StoreVariantsRes, } from "@medusajs/medusa" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import BaseResource from "./base" class ProductVariantsResource extends BaseResource { /** * @description Retrieves a single product variant * @param {string} id is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieve(id: string): AxiosPromise { + retrieve(id: string): ResponsePromise { const path = `/store/variants/${id}` return this.client.request("GET", path) } @@ -20,9 +20,9 @@ class ProductVariantsResource extends BaseResource { /** * @description Retrieves a list of of Product Variants * @param {StoreVariantsListParamsObject} query - * @return {AxiosPromise} + * @return {ResponsePromise} */ - list(query?: StoreGetVariantsParams): AxiosPromise { + list(query?: StoreGetVariantsParams): ResponsePromise { const path = `/store/variants` const search = Object.entries(query || {}).map(([key, value]) => { diff --git a/packages/medusa-js/src/resources/products.ts b/packages/medusa-js/src/resources/products.ts index 8a1f967e8c..df4893cf27 100644 --- a/packages/medusa-js/src/resources/products.ts +++ b/packages/medusa-js/src/resources/products.ts @@ -5,7 +5,7 @@ import { StoreProductsListRes, StoreProductsRes, } from "@medusajs/medusa" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import BaseResource from "./base" import ProductVariantsResource from "./product-variants" @@ -15,9 +15,9 @@ class ProductsResource extends BaseResource { /** * @description Retrieves a single Product * @param {string} id is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieve(id: string): AxiosPromise { + retrieve(id: string): ResponsePromise { const path = `/store/products/${id}` return this.client.request("GET", path) } @@ -25,9 +25,11 @@ class ProductsResource extends BaseResource { /** * @description Searches for products * @param {StorePostSearchReq} searchOptions is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - search(searchOptions: StorePostSearchReq): AxiosPromise { + search( + searchOptions: StorePostSearchReq + ): ResponsePromise { const path = `/store/products/search` return this.client.request("POST", path, searchOptions) } @@ -35,9 +37,9 @@ class ProductsResource extends BaseResource { /** * @description Retrieves a list of products * @param {StoreGetProductsParams} query is optional. Can contain a limit and offset for the returned list - * @return {AxiosPromise} + * @return {ResponsePromise} */ - list(query?: StoreGetProductsParams): AxiosPromise { + list(query?: StoreGetProductsParams): ResponsePromise { let path = `/store/products` if (query) { diff --git a/packages/medusa-js/src/resources/regions.ts b/packages/medusa-js/src/resources/regions.ts index 7bd6c6ffb1..551d7881d5 100644 --- a/packages/medusa-js/src/resources/regions.ts +++ b/packages/medusa-js/src/resources/regions.ts @@ -1,13 +1,13 @@ -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import { StoreRegionsListRes, StoreRegionsRes } from "@medusajs/medusa" import BaseResource from "./base" class RegionsResource extends BaseResource { /** * @description Retrieves a list of regions - * @return {AxiosPromise} + * @return {ResponsePromise} */ - list(): AxiosPromise { + list(): ResponsePromise { const path = `/store/regions` return this.client.request("GET", path) } @@ -15,9 +15,9 @@ class RegionsResource extends BaseResource { /** * @description Retrieves a region * @param {string} id is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieve(id: string): AxiosPromise { + retrieve(id: string): ResponsePromise { const path = `/store/regions/${id}` return this.client.request("GET", path) } diff --git a/packages/medusa-js/src/resources/return-reasons.ts b/packages/medusa-js/src/resources/return-reasons.ts index 74142d78db..a06e14b62d 100644 --- a/packages/medusa-js/src/resources/return-reasons.ts +++ b/packages/medusa-js/src/resources/return-reasons.ts @@ -3,24 +3,24 @@ import { StoreReturnReasonsListRes, StoreReturnReasonsRes, } from "@medusajs/medusa" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" class ReturnReasonsResource extends BaseResource { /** * @description Retrieves a single Return Reason * @param {string} id is required - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieve(id: string): AxiosPromise { + retrieve(id: string): ResponsePromise { const path = `/store/return-reasons/${id}` return this.client.request("GET", path) } /** * Lists return reasons defined in Medusa Admin - * @return {AxiosPromise} + * @return {ResponsePromise} */ - list(): AxiosPromise { + list(): ResponsePromise { const path = `/store/return-reasons` return this.client.request("GET", path) } diff --git a/packages/medusa-js/src/resources/returns.ts b/packages/medusa-js/src/resources/returns.ts index 8dec91f50b..c3a71c75eb 100644 --- a/packages/medusa-js/src/resources/returns.ts +++ b/packages/medusa-js/src/resources/returns.ts @@ -1,14 +1,14 @@ import BaseResource from "./base" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import { StoreReturnsRes, StorePostReturnsReq } from "@medusajs/medusa" class ReturnsResource extends BaseResource { /** * Creates a return request * @param {StorePostReturnsReq} payload details needed to create a return - * @return {AxiosPromise} + * @return {ResponsePromise} */ - create(payload: StorePostReturnsReq): AxiosPromise { + create(payload: StorePostReturnsReq): ResponsePromise { const path = `/store/returns` return this.client.request("POST", path, payload) } diff --git a/packages/medusa-js/src/resources/shipping-options.ts b/packages/medusa-js/src/resources/shipping-options.ts index 6d1a29e0f2..12c744b600 100644 --- a/packages/medusa-js/src/resources/shipping-options.ts +++ b/packages/medusa-js/src/resources/shipping-options.ts @@ -2,16 +2,16 @@ import { StoreGetShippingOptionsParams, StoreShippingOptionsListRes, } from "@medusajs/medusa" -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import BaseResource from "./base" class ShippingOptionsResource extends BaseResource { /** * @description Lists shiping options available for a cart * @param {string} cart_id - * @return {AxiosPromise} + * @return {ResponsePromise} */ - listCartOptions(cart_id: string): AxiosPromise { + listCartOptions(cart_id: string): ResponsePromise { const path = `/store/shipping-options/${cart_id}` return this.client.request("GET", path) } @@ -19,11 +19,11 @@ class ShippingOptionsResource extends BaseResource { /** * @description Lists shiping options available * @param {StoreGetShippingOptionsParamsObject} query - * @return {AxiosPromise} + * @return {ResponsePromise} */ list( query?: StoreGetShippingOptionsParams - ): AxiosPromise { + ): ResponsePromise { let path = `/store/shipping-options` const queryString = Object.entries(query || {}).map(([key, value]) => { diff --git a/packages/medusa-js/src/resources/swaps.ts b/packages/medusa-js/src/resources/swaps.ts index 61249060e2..dd3803a3e2 100644 --- a/packages/medusa-js/src/resources/swaps.ts +++ b/packages/medusa-js/src/resources/swaps.ts @@ -1,4 +1,4 @@ -import { AxiosPromise } from "axios" +import { ResponsePromise } from "../typings" import { StoreSwapsRes, StorePostSwapsReq } from "@medusajs/medusa" import BaseResource from "./base" @@ -6,9 +6,9 @@ class SwapsResource extends BaseResource { /** * @description Creates a swap from a cart * @param {StorePostSwapsReq} payload - * @return {AxiosPromise} + * @return {ResponsePromise} */ - create(payload: StorePostSwapsReq): AxiosPromise { + create(payload: StorePostSwapsReq): ResponsePromise { const path = `/store/swaps` return this.client.request("POST", path, payload) } @@ -16,9 +16,9 @@ class SwapsResource extends BaseResource { /** * @description Retrieves a swap by cart id * @param {string} cart_id id of cart - * @return {AxiosPromise} + * @return {ResponsePromise} */ - retrieveByCartId(cart_id: string): AxiosPromise { + retrieveByCartId(cart_id: string): ResponsePromise { const path = `/store/swaps/${cart_id}` return this.client.request("GET", path) } diff --git a/packages/medusa-js/src/typings.ts b/packages/medusa-js/src/typings.ts new file mode 100644 index 0000000000..7e9ec496c6 --- /dev/null +++ b/packages/medusa-js/src/typings.ts @@ -0,0 +1,7 @@ +import { AxiosResponse } from "axios" + +export type Response = T & { + response: Omit, "data"> +} + +export type ResponsePromise = Promise> diff --git a/packages/medusa-js/yarn.lock b/packages/medusa-js/yarn.lock index 93b4ae425d..f3dbd17c5c 100644 --- a/packages/medusa-js/yarn.lock +++ b/packages/medusa-js/yarn.lock @@ -613,7 +613,7 @@ "@types/yargs" "^15.0.0" chalk "^4.0.0" -"@medusajs/medusa-cli@^1.1.23": +"@medusajs/medusa-cli@^1.1.22": version "1.1.23" resolved "https://registry.yarnpkg.com/@medusajs/medusa-cli/-/medusa-cli-1.1.23.tgz#d7272a21c02f9fa3cbe1dc8f9e2840cdd0e1d2f9" integrity sha512-rqjDFd8hM9Q3rvuxwetv28jp3xHzH5Nt5J129JPCk9Sd8Rj6x2lVbKPbULNwgQ6hk8OsXjOQF2hPEyRt+vJL3A== @@ -649,18 +649,20 @@ winston "^3.3.3" yargs "^15.3.1" -"@medusajs/medusa@^1.1.54": - version "1.1.54" - resolved "https://registry.yarnpkg.com/@medusajs/medusa/-/medusa-1.1.54.tgz#1ed0faeb5b699d470df1f8e2b3f84e1278f96986" - integrity sha512-mhvnkN/xUhP6TRUOVPvK6x7TSxXfsEtQ/P1q/ojNAmTaZSaUNXmkITt1cGTftjhDyAIOLk3BxF1q8AGF1Lf/Ig== +"@medusajs/medusa@^1.1.55": + version "1.1.55" + resolved "https://registry.yarnpkg.com/@medusajs/medusa/-/medusa-1.1.55.tgz#c72642d8cd7a6bdf047a9188bcab55bdafdbf419" + integrity sha512-Q0QD2TP7jTpG958G4S8ROILy7lTVzIUbPBpxQ5BrTr13doptSK2LpMYd/yQ73NlSpBLFv+Q/ZQLRHedfoLVXDg== dependencies: "@hapi/joi" "^16.1.8" - "@medusajs/medusa-cli" "^1.1.23" + "@medusajs/medusa-cli" "^1.1.22" "@types/lodash" "^4.14.168" awilix "^4.2.3" body-parser "^1.19.0" bull "^3.12.1" chokidar "^3.4.2" + class-transformer "^0.4.0" + class-validator "^0.13.1" connect-redis "^5.0.0" cookie-parser "^1.4.4" core-js "^3.6.5" @@ -677,8 +679,8 @@ joi "^17.3.0" joi-objectid "^3.0.1" jsonwebtoken "^8.5.1" - medusa-core-utils "^1.1.26" - medusa-test-utils "^1.1.29" + medusa-core-utils "^1.1.28" + medusa-test-utils "^1.1.31" morgan "^1.9.1" multer "^1.4.2" passport "^0.4.0" @@ -718,31 +720,31 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" -"@oclif/command@^1", "@oclif/command@^1.6.0", "@oclif/command@^1.8.3": - version "1.8.3" - resolved "https://registry.yarnpkg.com/@oclif/command/-/command-1.8.3.tgz#1f8bc2c4ecf94b6659a5134d95da179e1dffad9e" - integrity sha512-OGjrhdVgTT2TAAj/2RrdXjwxaDoTm16c2LfAzrta1xIFe6/XhgQIYDmeRN/RptQoZQBX8e9Vv2JoQq+TbghJmw== +"@oclif/command@^1", "@oclif/command@^1.5.20", "@oclif/command@^1.6.0": + version "1.8.0" + resolved "https://registry.yarnpkg.com/@oclif/command/-/command-1.8.0.tgz#c1a499b10d26e9d1a611190a81005589accbb339" + integrity sha512-5vwpq6kbvwkQwKqAoOU3L72GZ3Ta8RRrewKj9OJRolx28KLJJ8Dg9Rf7obRwt5jQA9bkYd8gqzMTrI7H3xLfaw== dependencies: "@oclif/config" "^1.15.1" - "@oclif/errors" "^1.3.5" - "@oclif/parser" "^3.8.5" - "@oclif/plugin-help" "^3.2.4" + "@oclif/errors" "^1.3.3" + "@oclif/parser" "^3.8.3" + "@oclif/plugin-help" "^3" debug "^4.1.1" semver "^7.3.2" -"@oclif/config@^1", "@oclif/config@^1.15.1", "@oclif/config@^1.17.1": - version "1.17.1" - resolved "https://registry.yarnpkg.com/@oclif/config/-/config-1.17.1.tgz#383515f6715b91d8df5db8108214e93bb46e86ca" - integrity sha512-UqV5qsN2np96TNlJspSNlRl7CpFmxYSrB0iLe3XV9NDkbFEE5prGP++h6w6xOR/FL3QV7BoqrbwGuJdJdFbidw== +"@oclif/config@^1", "@oclif/config@^1.15.1": + version "1.17.0" + resolved "https://registry.yarnpkg.com/@oclif/config/-/config-1.17.0.tgz#ba8639118633102a7e481760c50054623d09fcab" + integrity sha512-Lmfuf6ubjQ4ifC/9bz1fSCHc6F6E653oyaRXxg+lgT4+bYf9bk+nqrUpAbrXyABkCqgIBiFr3J4zR/kiFdE1PA== dependencies: "@oclif/errors" "^1.3.3" - "@oclif/parser" "^3.8.6" + "@oclif/parser" "^3.8.0" debug "^4.1.1" globby "^11.0.1" is-wsl "^2.1.1" tslib "^2.0.0" -"@oclif/errors@^1.2.1", "@oclif/errors@^1.2.2", "@oclif/errors@^1.3.3", "@oclif/errors@^1.3.5": +"@oclif/errors@^1.2.1", "@oclif/errors@^1.2.2", "@oclif/errors@^1.3.3": version "1.3.5" resolved "https://registry.yarnpkg.com/@oclif/errors/-/errors-1.3.5.tgz#a1e9694dbeccab10fe2fe15acb7113991bed636c" integrity sha512-OivucXPH/eLLlOT7FkCMoZXiaVYf8I/w1eTAM1+gKzfhALwWTusxEx7wBmW0uzvkSg/9ovWLycPaBgJbM3LOCQ== @@ -758,24 +760,24 @@ resolved "https://registry.yarnpkg.com/@oclif/linewrap/-/linewrap-1.0.0.tgz#aedcb64b479d4db7be24196384897b5000901d91" integrity sha512-Ups2dShK52xXa8w6iBWLgcjPJWjais6KPJQq3gQ/88AY6BXoTX+MIGFPrWQO1KLMiQfoTpcLnUwloN4brrVUHw== -"@oclif/parser@^3.8.5", "@oclif/parser@^3.8.6": - version "3.8.6" - resolved "https://registry.yarnpkg.com/@oclif/parser/-/parser-3.8.6.tgz#d5a108af9c708a051cc6b1d27d47359d75f41236" - integrity sha512-tXb0NKgSgNxmf6baN6naK+CCwOueaFk93FG9u202U7mTBHUKsioOUlw1SG/iPi9aJM3WE4pHLXmty59pci0OEw== +"@oclif/parser@^3.8.0", "@oclif/parser@^3.8.3": + version "3.8.5" + resolved "https://registry.yarnpkg.com/@oclif/parser/-/parser-3.8.5.tgz#c5161766a1efca7343e1f25d769efbefe09f639b" + integrity sha512-yojzeEfmSxjjkAvMRj0KzspXlMjCfBzNRPkWw8ZwOSoNWoJn+OCS/m/S+yfV6BvAM4u2lTzX9Y5rCbrFIgkJLg== dependencies: "@oclif/errors" "^1.2.2" "@oclif/linewrap" "^1.0.0" - chalk "^4.1.0" - tslib "^2.0.0" + chalk "^2.4.2" + tslib "^1.9.3" -"@oclif/plugin-help@^3", "@oclif/plugin-help@^3.2.4": - version "3.2.5" - resolved "https://registry.yarnpkg.com/@oclif/plugin-help/-/plugin-help-3.2.5.tgz#dab0f5e655971b2c49dd80ee472f97b6e69a1297" - integrity sha512-fjkZTstvacCPicF2oaa3Lc+Yw3ocKEaW6x6O7doVqMLuoMUX6wBOQ+f1a3VFzO1fErqNeFPDlUlVUhwq9yMzQg== +"@oclif/plugin-help@^3": + version "3.2.4" + resolved "https://registry.yarnpkg.com/@oclif/plugin-help/-/plugin-help-3.2.4.tgz#100e0e09d806e20595096609f2220d009ee096e2" + integrity sha512-kMSfFbv11S7CKFlbWTKDdAe/gC7P2zCFZEDq6BAHjJdA0htHT8FvBhnyoppR0O2jOTjX80wHjU+ItPpjanfuag== dependencies: - "@oclif/command" "^1.8.3" - "@oclif/config" "^1.17.1" - "@oclif/errors" "^1.3.5" + "@oclif/command" "^1.5.20" + "@oclif/config" "^1.15.1" + "@oclif/errors" "^1.2.2" chalk "^4.1.0" indent-string "^4.0.0" lodash "^4.17.21" @@ -1522,7 +1524,7 @@ caseless@~0.12.0: resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc" integrity sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw= -chalk@^2.0.0: +chalk@^2.0.0, chalk@^2.4.2: version "2.4.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424" integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ== @@ -1584,6 +1586,11 @@ cjs-module-lexer@^0.6.0: resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-0.6.0.tgz#4186fcca0eae175970aee870b9fe2d6cf8d5655f" integrity sha512-uc2Vix1frTfnuzxxu1Hp4ktSvM3QaI4oXl4ZUqL1wjTu/BGki9TrCWoqLTg/drR1KwAEarXuRFCG2Svr1GxPFw== +class-transformer@^0.4.0: + version "0.4.1" + resolved "https://registry.yarnpkg.com/class-transformer/-/class-transformer-0.4.1.tgz#eb86449fb5dc8333acbf880c96214acfa0d8dedf" + integrity sha512-mbBtth1BFa+pN2fmx6/NmMNxxyu9Mw9rx3rzKWBH7yoG+bfSoJOnEJ3qmB6yEKvoO502zUxSV2AqN7EUypC2Tg== + class-utils@^0.3.5: version "0.3.6" resolved "https://registry.yarnpkg.com/class-utils/-/class-utils-0.3.6.tgz#f93369ae8b9a7ce02fd41faad0ca83033190c463" @@ -1594,6 +1601,14 @@ class-utils@^0.3.5: isobject "^3.0.0" static-extend "^0.1.1" +class-validator@^0.13.1: + version "0.13.2" + resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.13.2.tgz#64b031e9f3f81a1e1dcd04a5d604734608b24143" + integrity sha512-yBUcQy07FPlGzUjoLuUfIOXzgynnQPPruyK1Ge2B74k9ROwnle1E+NxLWnUv5OLU8hA/qL5leAE9XnXq3byaBw== + dependencies: + libphonenumber-js "^1.9.43" + validator "^13.7.0" + clean-stack@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/clean-stack/-/clean-stack-3.0.1.tgz#155bf0b2221bf5f4fba89528d24c5953f17fe3a8" @@ -4310,6 +4325,11 @@ levn@~0.3.0: prelude-ls "~1.1.2" type-check "~0.3.2" +libphonenumber-js@^1.9.43: + version "1.9.43" + resolved "https://registry.yarnpkg.com/libphonenumber-js/-/libphonenumber-js-1.9.43.tgz#2371e4383e6780990381d5b900b8c22666221cbb" + integrity sha512-tNB87ZutAiAkl3DE/Bo0Mxqn/XZbNxhPg4v9bYBwQQW4dlhBGqXl1vtmPxeDWbrijzwOA9vRjOOFm5V9SK/W3w== + lines-and-columns@^1.1.6: version "1.1.6" resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00" @@ -4464,7 +4484,7 @@ medusa-core-utils@^0.1.27: "@hapi/joi" "^16.1.8" joi-objectid "^3.0.1" -medusa-core-utils@^1.1.26, medusa-core-utils@^1.1.28: +medusa-core-utils@^1.1.28: version "1.1.28" resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-1.1.28.tgz#dfbec54f50357ed517cd45c136ccb4704317f96c" integrity sha512-2jlDvK/j89hBkK+9vMUBC4IOuwNmydiAo8cncqhuF3hZsC4ahZNxypq7DpFG9bDpWYN/04OupYQjxAUKMfvnDQ== @@ -4487,7 +4507,7 @@ medusa-telemetry@^0.0.10: remove-trailing-slash "^0.1.1" uuid "^8.3.2" -medusa-test-utils@^1.1.29: +medusa-test-utils@^1.1.31: version "1.1.31" resolved "https://registry.yarnpkg.com/medusa-test-utils/-/medusa-test-utils-1.1.31.tgz#6dae478ca9d7c5ff0ea0f1cccb20c2f11ed60e7e" integrity sha512-DmU6xiQJ4v367ULFVZPf7c7USlIsMiUtGrZ6NUKBQOiaw0b+ZIb0Si09Gz3nxnf36elKzMyBKI8sxh2uzjhEqg== @@ -5944,16 +5964,11 @@ side-channel@^1.0.4: get-intrinsic "^1.0.2" object-inspect "^1.9.0" -signal-exit@^3.0.0, signal-exit@^3.0.2: +signal-exit@^3.0.0, signal-exit@^3.0.2, signal-exit@^3.0.3: version "3.0.5" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.5.tgz#9e3e8cc0c75a99472b44321033a7702e7738252f" integrity sha512-KWcOiKeQj6ZyXx7zq4YxSMgHRlod4czeBQZrPb8OKcohcqAXShm7E20kEMle9WBt26hFcAf0qLOcp5zmY7kOqQ== -signal-exit@^3.0.3: - version "3.0.6" - resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.6.tgz#24e630c4b0f03fea446a2bd299e62b4a6ca8d0af" - integrity sha512-sDl4qMFpijcGw22U5w63KmD3cZJfBuFlVNbVMKje2keoKML7X2UzWbc4XrmEbDwg0NXJc3yv4/ox7b+JWb57kQ== - simple-swizzle@^0.2.2: version "0.2.2" resolved "https://registry.yarnpkg.com/simple-swizzle/-/simple-swizzle-0.2.2.tgz#a4da6b635ffcccca33f70d17cb92592de95e557a" @@ -6441,7 +6456,7 @@ triple-beam@^1.2.0, triple-beam@^1.3.0: resolved "https://registry.yarnpkg.com/triple-beam/-/triple-beam-1.3.0.tgz#a595214c7298db8339eeeee083e4d10bd8cb8dd9" integrity sha512-XrHUvV5HpdLmIj4uVMxHggLbFSZYIn7HEWsqePZcI50pco+MPqJ50wMGY794X7AOOhxOBAjbkqfAbEe/QMp2Lw== -tslib@^1: +tslib@^1, tslib@^1.9.3: version "1.14.1" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== @@ -6672,6 +6687,11 @@ validate-npm-package-license@^3.0.1: spdx-correct "^3.0.0" spdx-expression-parse "^3.0.0" +validator@^13.7.0: + version "13.7.0" + resolved "https://registry.yarnpkg.com/validator/-/validator-13.7.0.tgz#4f9658ba13ba8f3d82ee881d3516489ea85c0857" + integrity sha512-nYXQLCBkpJ8X6ltALua9dRrZDHVYxjJ1wgskNt1lH9fzGjs3tgojGSCBjmEPwkWS1y29+DrizMTW19Pr9uB2nw== + vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"