feat: Typescript for API layer (#817)
Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Co-authored-by: Zakaria El Asri <33696020+zakariaelas@users.noreply.github.com> Co-authored-by: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Co-authored-by: Philip Korsholm <philip.korsholm@hotmail.com> Co-authored-by: Sebastian Rindom <seb@medusa-commerce.com>
This commit is contained in:
413 files changed
+20957
-10349
No files matched your search
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
/dist
|
||||
@@ -0,0 +1 @@
|
||||
# Medusa Commerce JS Client
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"transform": {
|
||||
"^.+\\.(t|j)sx?$": "ts-jest"
|
||||
},
|
||||
"testRegex": "(/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
|
||||
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "@medusajs/medusa-js",
|
||||
"version": "1.0.1-canary.1",
|
||||
"description": "Client for Medusa Commerce Rest API",
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist/**/*"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc --build",
|
||||
"test": "jest --config jestconfig.json"
|
||||
},
|
||||
"author": "Oliver Juhl",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"axios": "^0.21.0",
|
||||
"retry-axios": "^2.6.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/medusajs/medusa-js.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/medusajs/medusa-js/issues"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^26.0.19",
|
||||
"eslint": "^8.2.0",
|
||||
"jest": "^26.6.3",
|
||||
"prettier": "^2.2.1",
|
||||
"typescript": "^4.1.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
"use strict"
|
||||
/**
|
||||
* MedusaError is the base error for every other MedusaError
|
||||
*/
|
||||
export default class MedusaError extends Error {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
public static factory(type: ErrorType): MedusaError {
|
||||
switch (type) {
|
||||
case ErrorType.INVALID_REQUEST:
|
||||
return new MedusaInvalidRequestError()
|
||||
case ErrorType.AUTHENTICATION:
|
||||
return new MedusaAuthenticationError()
|
||||
case ErrorType.API:
|
||||
return new MedusaAPIError()
|
||||
case ErrorType.PERMISSION:
|
||||
return new MedusaPermissionError()
|
||||
case ErrorType.CONNECTION:
|
||||
return new MedusaConnectionError()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum ErrorType {
|
||||
"INVALID_REQUEST",
|
||||
"API",
|
||||
"AUTHENTICATION",
|
||||
"PERMISSION",
|
||||
"CONNECTION",
|
||||
}
|
||||
|
||||
/**
|
||||
* MedusaInvalidRequestError is raised when a request as invalid parameters.
|
||||
*/
|
||||
export class MedusaInvalidRequestError extends MedusaError {}
|
||||
|
||||
/**
|
||||
* MedusaAPIError is raised in case no other type cover the problem
|
||||
*/
|
||||
export class MedusaAPIError extends MedusaError {}
|
||||
|
||||
/**
|
||||
* MedusaAuthenticationError is raised when invalid credentials is used to connect to Medusa
|
||||
*/
|
||||
export class MedusaAuthenticationError extends MedusaError {}
|
||||
|
||||
/**
|
||||
* MedusaPermissionErorr is raised when attempting to access a resource without permissions
|
||||
*/
|
||||
export class MedusaPermissionError extends MedusaError {}
|
||||
|
||||
/**
|
||||
* MedusaConnectionError is raised when the Medusa servers can't be reached.
|
||||
*/
|
||||
export class MedusaConnectionError extends MedusaError {}
|
||||
@@ -0,0 +1,51 @@
|
||||
import MedusaError from "./error"
|
||||
import Client, { Config } from "./request"
|
||||
import AuthResource from "./resources/auth"
|
||||
import CartsResource from "./resources/carts"
|
||||
import CollectionsResource from "./resources/collections"
|
||||
import CustomersResource from "./resources/customers"
|
||||
import GiftCardsResource from "./resources/gift-cards"
|
||||
import OrdersResource from "./resources/orders"
|
||||
import ProductsResource from "./resources/products"
|
||||
import RegionsResource from "./resources/regions"
|
||||
import ReturnReasonsResource from "./resources/return-reasons"
|
||||
import ReturnsResource from "./resources/returns"
|
||||
import ShippingOptionsResource from "./resources/shipping-options"
|
||||
import SwapsResource from "./resources/swaps"
|
||||
|
||||
class Medusa {
|
||||
private client: Client
|
||||
public auth: AuthResource
|
||||
public carts: CartsResource
|
||||
public customers: CustomersResource
|
||||
public errors: MedusaError
|
||||
public orders: OrdersResource
|
||||
public products: ProductsResource
|
||||
public regions: RegionsResource
|
||||
public returnReasons: ReturnReasonsResource
|
||||
public returns: ReturnsResource
|
||||
public shippingOptions: ShippingOptionsResource
|
||||
public swaps: SwapsResource
|
||||
public collections: CollectionsResource
|
||||
public giftCards: GiftCardsResource
|
||||
|
||||
constructor(config: Config) {
|
||||
this.client = new Client(config)
|
||||
|
||||
this.auth = new AuthResource(this.client)
|
||||
this.carts = new CartsResource(this.client)
|
||||
this.customers = new CustomersResource(this.client)
|
||||
this.errors = new MedusaError()
|
||||
this.orders = new OrdersResource(this.client)
|
||||
this.products = new ProductsResource(this.client)
|
||||
this.regions = new RegionsResource(this.client)
|
||||
this.returnReasons = new ReturnReasonsResource(this.client)
|
||||
this.returns = new ReturnsResource(this.client)
|
||||
this.shippingOptions = new ShippingOptionsResource(this.client)
|
||||
this.swaps = new SwapsResource(this.client)
|
||||
this.collections = new CollectionsResource(this.client)
|
||||
this.giftCards = new GiftCardsResource(this.client)
|
||||
}
|
||||
}
|
||||
|
||||
export default Medusa
|
||||
@@ -0,0 +1,199 @@
|
||||
import axios, { AxiosError, AxiosInstance } from "axios"
|
||||
import * as rax from "retry-axios"
|
||||
import { v4 as uuidv4 } from "uuid"
|
||||
|
||||
export interface Config {
|
||||
baseUrl: string
|
||||
maxRetries: number
|
||||
}
|
||||
export interface RequestOptions {
|
||||
apiKey?: string
|
||||
timeout?: number
|
||||
numberOfRetries?: number
|
||||
}
|
||||
|
||||
export type RequestMethod = "DELETE" | "POST" | "GET"
|
||||
|
||||
const defaultConfig = {
|
||||
maxRetries: 0,
|
||||
baseUrl: "http://localhost:9000",
|
||||
}
|
||||
|
||||
class Client {
|
||||
private axiosClient: AxiosInstance
|
||||
private config: Config
|
||||
|
||||
constructor(config: Config) {
|
||||
/** @private @constant {AxiosInstance} */
|
||||
this.axiosClient = this.createClient({ ...defaultConfig, ...config })
|
||||
|
||||
/** @private @constant {Config} */
|
||||
this.config = { ...defaultConfig, ...config }
|
||||
}
|
||||
|
||||
shouldRetryCondition(
|
||||
err: AxiosError,
|
||||
numRetries: number,
|
||||
maxRetries: number
|
||||
): boolean {
|
||||
// Obviously, if we have reached max. retries we stop
|
||||
if (numRetries >= maxRetries) {
|
||||
return false
|
||||
}
|
||||
|
||||
// If no response, we assume a connection error and retry
|
||||
if (!err.response) {
|
||||
return true
|
||||
}
|
||||
|
||||
// Retry on conflicts
|
||||
if (err.response.status === 409) {
|
||||
return true
|
||||
}
|
||||
|
||||
// All 5xx errors are retried
|
||||
// OBS: We are currently not retrying 500 requests, since our core needs proper error handling.
|
||||
// At the moment, 500 will be returned on all errors, that are not of type MedusaError.
|
||||
if (err.response.status > 500 && err.response.status <= 599) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Stolen from https://github.com/stripe/stripe-node/blob/fd0a597064289b8c82f374f4747d634050739043/lib/utils.js#L282
|
||||
normalizeHeaders(obj: object): object {
|
||||
if (!(obj && typeof obj === "object")) {
|
||||
return obj
|
||||
}
|
||||
|
||||
return Object.keys(obj).reduce((result, header) => {
|
||||
result[this.normalizeHeader(header)] = obj[header]
|
||||
return result
|
||||
}, {})
|
||||
}
|
||||
|
||||
// Stolen from https://github.com/marten-de-vries/header-case-normalizer/blob/master/index.js#L36-L41
|
||||
normalizeHeader(header: string): string {
|
||||
return header
|
||||
.split("-")
|
||||
.map(
|
||||
(text) => text.charAt(0).toUpperCase() + text.substr(1).toLowerCase()
|
||||
)
|
||||
.join("-")
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates all the initial headers.
|
||||
* We add the idempotency key, if the request is configured to retry.
|
||||
* @param {object} userHeaders user supplied headers
|
||||
* @param {Types.RequestMethod} method request method
|
||||
* @param {string} path request path
|
||||
* @return {object}
|
||||
*/
|
||||
setHeaders(
|
||||
userHeaders: RequestOptions,
|
||||
method: RequestMethod,
|
||||
path: string
|
||||
): object {
|
||||
let defaultHeaders: object = {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
// TODO: if route is an authenticated route, add api key
|
||||
if (path.startsWith("/admin")) {
|
||||
defaultHeaders = {
|
||||
...defaultHeaders,
|
||||
}
|
||||
}
|
||||
|
||||
// only add idempotency key, if we want to retry
|
||||
if (this.config.maxRetries > 0 && method === "POST") {
|
||||
defaultHeaders["Idempotency-Key"] = uuidv4()
|
||||
}
|
||||
|
||||
return Object.assign({}, defaultHeaders, this.normalizeHeaders(userHeaders))
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the axios client used for requests
|
||||
* As part of the creation, we configure the retry conditions
|
||||
* and the exponential backoff approach.
|
||||
* @param {Config} config user supplied configurations
|
||||
* @return {AxiosInstance}
|
||||
*/
|
||||
createClient(config: Config): AxiosInstance {
|
||||
const client = axios.create({
|
||||
baseURL: config.baseUrl,
|
||||
})
|
||||
|
||||
rax.attach(client)
|
||||
|
||||
client.defaults.raxConfig = {
|
||||
instance: client,
|
||||
retry: config.maxRetries,
|
||||
backoffType: "exponential",
|
||||
shouldRetry: (err: AxiosError): boolean => {
|
||||
const cfg = rax.getConfig(err)
|
||||
if (cfg) {
|
||||
return this.shouldRetryCondition(
|
||||
err,
|
||||
cfg.currentRetryAttempt || 1,
|
||||
cfg.retry || 3
|
||||
)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
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
|
||||
* @param {string} path request path
|
||||
* @param {object} payload request payload
|
||||
* @param {RequestOptions} options axios configuration
|
||||
* @return {object}
|
||||
*/
|
||||
async request(
|
||||
method: RequestMethod,
|
||||
path: string,
|
||||
payload: object = {},
|
||||
options: RequestOptions = {}
|
||||
): Promise<any> {
|
||||
const reqOpts = {
|
||||
method,
|
||||
withCredentials: true,
|
||||
url: path,
|
||||
data: payload,
|
||||
json: true,
|
||||
headers: this.setHeaders(options, method, path),
|
||||
}
|
||||
|
||||
const { data, status } = await this.axiosClient(reqOpts)
|
||||
|
||||
return this.createRawResponse(data, status)
|
||||
}
|
||||
}
|
||||
|
||||
export default Client
|
||||
@@ -0,0 +1,47 @@
|
||||
import {
|
||||
StoreCustomersRes,
|
||||
StorePostCustomersCustomerAddressesAddressReq,
|
||||
StorePostCustomersCustomerAddressesReq,
|
||||
} from "@medusajs/medusa"
|
||||
import { AxiosPromise } from "axios"
|
||||
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<StoreCustomerResponse>}
|
||||
*/
|
||||
addAddress(
|
||||
payload: StorePostCustomersCustomerAddressesReq
|
||||
): AxiosPromise<StoreCustomersRes> {
|
||||
const path = `/store/customers/me/addresses`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an address of a customer
|
||||
* @param {string} address_id id of the address to delete
|
||||
* @return {AxiosPromise<StoreCustomersResponse>}
|
||||
*/
|
||||
deleteAddress(address_id: string): AxiosPromise<StoreCustomersRes> {
|
||||
const path = `/store/customers/me/addresses/${address_id}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an address of a customer
|
||||
* @param {string} address_id id of customer
|
||||
* @param {StorePostCustomersCustomerAddressesAddressReq} payload address update
|
||||
* @return {StoreCustomersResponse}
|
||||
*/
|
||||
updateAddress(
|
||||
address_id: string,
|
||||
payload: StorePostCustomersCustomerAddressesAddressReq
|
||||
): AxiosPromise<StoreCustomersRes> {
|
||||
const path = `/store/customers/me/addresses/${address_id}`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
}
|
||||
|
||||
export default AddressesResource
|
||||
@@ -0,0 +1,41 @@
|
||||
import { AxiosPromise } from "axios"
|
||||
import {
|
||||
StoreGetAuthEmailRes,
|
||||
StorePostAuthReq,
|
||||
StoreAuthRes,
|
||||
} from "@medusajs/medusa"
|
||||
import BaseResource from "./base"
|
||||
|
||||
class AuthResource extends BaseResource {
|
||||
/**
|
||||
* @description Authenticates a customer using email and password combination
|
||||
* @param {StorePostAuthReq} payload authentication payload
|
||||
* @return {AxiosPromise<StoreAuthRes>}
|
||||
*/
|
||||
authenticate(payload: StorePostAuthReq): AxiosPromise<StoreAuthRes> {
|
||||
const path = `/store/auth`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Retrieves an authenticated session
|
||||
* Usually used to check if authenticated session is alive.
|
||||
* @return {AxiosPromise<StoreAuthRes>}
|
||||
*/
|
||||
getSession(): AxiosPromise<StoreAuthRes> {
|
||||
const path = `/store/auth`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Check if email exists
|
||||
* @param {string} email is required
|
||||
* @return {AxiosPromise<StoreGetAuthEmailRes>}
|
||||
*/
|
||||
exists(email: string): AxiosPromise<StoreGetAuthEmailRes> {
|
||||
const path = `/store/auth/${email}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default AuthResource
|
||||
@@ -0,0 +1,9 @@
|
||||
import Client from "../request"
|
||||
|
||||
export default class BaseResource {
|
||||
public client: Client
|
||||
|
||||
constructor(client: Client) {
|
||||
this.client = client
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
import {
|
||||
StoreCartsRes,
|
||||
StorePostCartReq,
|
||||
StorePostCartsCartPaymentSessionReq,
|
||||
StorePostCartsCartPaymentSessionUpdateReq,
|
||||
StorePostCartsCartReq,
|
||||
StorePostCartsCartShippingMethodReq,
|
||||
} from "@medusajs/medusa"
|
||||
import { AxiosPromise } from "axios"
|
||||
import BaseResource from "./base"
|
||||
import LineItemsResource from "./line-items"
|
||||
|
||||
class CartsResource extends BaseResource {
|
||||
public lineItems = new LineItemsResource(this.client)
|
||||
|
||||
/**
|
||||
* 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<StoreCartsRes>}
|
||||
*/
|
||||
addShippingMethod(
|
||||
cart_id: string,
|
||||
payload: StorePostCartsCartShippingMethodReq
|
||||
): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts/${cart_id}/shipping-methods`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* Completes a cart.
|
||||
* Payment authorization is attempted and if more work is required, we simply return the cart for further updates.
|
||||
* If payment is authorized and order is not yet created, we make sure to do so.
|
||||
* 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<StoreCartsRes>}
|
||||
*/
|
||||
complete(cart_id: string): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts/${cart_id}/complete`
|
||||
return this.client.request("POST", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<StoreCartsRes>}
|
||||
*/
|
||||
create(payload?: StorePostCartReq): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates payment sessions.
|
||||
* 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<StoreCartsRes>}
|
||||
*/
|
||||
createPaymentSessions(cart_id: string): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts/${cart_id}/payment-sessions`
|
||||
return this.client.request("POST", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a discount from cart.
|
||||
* @param {string} cart_id is required
|
||||
* @param {string} code discount code to remove
|
||||
* @return {AxiosPromise<StoreCartsRes>}
|
||||
*/
|
||||
deleteDiscount(cart_id: string, code: string): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts/${cart_id}/discounts/${code}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a payment session from a cart.
|
||||
* 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<StoreCartsRes>}
|
||||
*/
|
||||
deletePaymentSession(
|
||||
cart_id: string,
|
||||
provider_id: string
|
||||
): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts/${cart_id}/payment-sessions/${provider_id}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<StoreCartsRes>}
|
||||
*/
|
||||
refreshPaymentSession(
|
||||
cart_id: string,
|
||||
provider_id: string
|
||||
): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts/${cart_id}/payment-sessions/${provider_id}/refresh`
|
||||
return this.client.request("POST", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a cart
|
||||
* @param {string} cart_id is required
|
||||
* @return {AxiosPromise<StoreCartsRes>}
|
||||
*/
|
||||
retrieve(cart_id: string): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts/${cart_id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Refreshes a payment session.
|
||||
* @param {string} cart_id is required
|
||||
* @param {StorePostCartsCartPaymentSessionReq} payload the provider id of the session e.g. "stripe"
|
||||
* @return {AxiosPromise<StoreCartsRes>}
|
||||
*/
|
||||
setPaymentSession(
|
||||
cart_id: string,
|
||||
payload: StorePostCartsCartPaymentSessionReq
|
||||
): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts/${cart_id}/payment-session`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<StoreCartsRes>}
|
||||
*/
|
||||
update(
|
||||
cart_id: string,
|
||||
payload: StorePostCartsCartReq
|
||||
): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts/${cart_id}`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the payment method
|
||||
* @param {string} cart_id is required
|
||||
* @param {StorePostCartsCartPaymentSessionUpdateReq} payload is required
|
||||
* @return {AxiosPromise<StoreCartsRes>}
|
||||
*/
|
||||
updatePaymentSession(
|
||||
cart_id: string,
|
||||
payload: StorePostCartsCartPaymentSessionUpdateReq
|
||||
): AxiosPromise<StoreCartsRes> {
|
||||
const path = `/store/carts/${cart_id}/payment-session/update`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
}
|
||||
|
||||
export default CartsResource
|
||||
@@ -0,0 +1,42 @@
|
||||
import { AxiosPromise } from "axios"
|
||||
import {
|
||||
StoreCollectionsRes,
|
||||
StoreCollectionsListRes,
|
||||
StoreGetCollectionsParams,
|
||||
} from "@medusajs/medusa"
|
||||
import BaseResource from "./base"
|
||||
|
||||
class CollectionsResource extends BaseResource {
|
||||
/**
|
||||
* @description Retrieves a single collection
|
||||
* @param {string} id id of the collection
|
||||
* @return {AxiosPromise<StoreCollectionsRes>}
|
||||
*/
|
||||
retrieve(id: string): AxiosPromise<StoreCollectionsRes> {
|
||||
const path = `/store/collections/${id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Retrieves a list of collections
|
||||
* @param {string} query is optional. Can contain a limit and offset for the returned list
|
||||
* @return {AxiosPromise<StoreCollectionsListRes>}
|
||||
*/
|
||||
list(
|
||||
query?: StoreGetCollectionsParams
|
||||
): AxiosPromise<StoreCollectionsListRes> {
|
||||
let path = `/store/collections`
|
||||
|
||||
if (query) {
|
||||
const queryString = Object.entries(query).map(([key, value]) => {
|
||||
return `${key}=${value}`
|
||||
})
|
||||
|
||||
path = `/store/collections?${queryString.join("&")}`
|
||||
}
|
||||
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default CollectionsResource
|
||||
@@ -0,0 +1,102 @@
|
||||
import {
|
||||
StoreCustomersListOrdersRes,
|
||||
StoreCustomersRes,
|
||||
StoreGetCustomersCustomerOrdersParams,
|
||||
StorePostCustomersCustomerPasswordTokenReq,
|
||||
StorePostCustomersCustomerReq,
|
||||
StorePostCustomersReq,
|
||||
} from "@medusajs/medusa"
|
||||
import { AxiosPromise } from "axios"
|
||||
import AddressesResource from "./addresses"
|
||||
import BaseResource from "./base"
|
||||
import PaymentMethodsResource from "./payment-methods"
|
||||
|
||||
class CustomerResource extends BaseResource {
|
||||
public paymentMethods = new PaymentMethodsResource(this.client)
|
||||
public addresses = new AddressesResource(this.client)
|
||||
|
||||
/**
|
||||
* Creates a customer
|
||||
* @param {StorePostCustomersReq} payload information of customer
|
||||
* @return { AxiosPromise<StoreCustomersRes>}
|
||||
*/
|
||||
create(payload: StorePostCustomersReq): AxiosPromise<StoreCustomersRes> {
|
||||
const path = `/store/customers`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the customer that is currently logged
|
||||
* @return {AxiosPromise<StoreCustomersRes>}
|
||||
*/
|
||||
retrieve(): AxiosPromise<StoreCustomersRes> {
|
||||
const path = `/store/customers/me`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a customer
|
||||
* @param {StorePostCustomersCustomerReq} payload information to update customer with
|
||||
* @return {AxiosPromise<StoreCustomersRes>}
|
||||
*/
|
||||
update(
|
||||
payload: StorePostCustomersCustomerReq
|
||||
): AxiosPromise<StoreCustomersRes> {
|
||||
const path = `/store/customers/me`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve customer orders
|
||||
* @param {StoreGetCustomersCustomerOrdersParams} params optional params to retrieve orders
|
||||
* @return {AxiosPromise<StoreCustomersListOrdersRes>}
|
||||
*/
|
||||
listOrders(
|
||||
params?: StoreGetCustomersCustomerOrdersParams
|
||||
): AxiosPromise<StoreCustomersListOrdersRes> {
|
||||
let path = `/store/customers/me/orders`
|
||||
if (params) {
|
||||
let query: string | undefined
|
||||
|
||||
for (const key of Object.keys(params)) {
|
||||
if (query) {
|
||||
query += `&${key}=${params[key]}`
|
||||
} else {
|
||||
query = `?${key}=${params[key]}`
|
||||
}
|
||||
}
|
||||
|
||||
if (query) {
|
||||
path += query
|
||||
}
|
||||
}
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets customer password
|
||||
* @param {StorePostCustomersCustomerPasswordTokenReq} payload info used to reset customer password
|
||||
* @return {AxiosPromise<StoreCustomersRes>}
|
||||
*/
|
||||
resetPassword(
|
||||
payload: StorePostCustomersCustomerPasswordTokenReq
|
||||
): AxiosPromise<StoreCustomersRes> {
|
||||
const path = `/store/customers/password-reset`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}
|
||||
*/
|
||||
generatePasswordToken(
|
||||
payload: StorePostCustomersCustomerPasswordTokenReq
|
||||
): AxiosPromise {
|
||||
const path = `/store/customers/password-token`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
}
|
||||
|
||||
export default CustomerResource
|
||||
@@ -0,0 +1,17 @@
|
||||
import { StoreGiftCardsRes } from "@medusajs/medusa"
|
||||
import { AxiosPromise } from "axios"
|
||||
import BaseResource from "./base"
|
||||
|
||||
class GiftCardsResource extends BaseResource {
|
||||
/**
|
||||
* @description Retrieves a single GiftCard
|
||||
* @param {string} code code of the gift card
|
||||
* @return {AxiosPromise<StoreGiftCardsRes>}
|
||||
*/
|
||||
retrieve(code: string): AxiosPromise<StoreGiftCardsRes> {
|
||||
const path = `/store/gift-cards/${code}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default GiftCardsResource
|
||||
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
StoreCartsCartRes,
|
||||
StoreCartsDeleteRes,
|
||||
StorePostCartsCartLineItemsItemReq,
|
||||
StorePostCartsCartLineItemsReq,
|
||||
} from "@medusajs/medusa"
|
||||
import { AxiosPromise } from "axios"
|
||||
import BaseResource from "./base"
|
||||
|
||||
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<StoreCartsCartRes>}
|
||||
*/
|
||||
create(
|
||||
cart_id: string,
|
||||
payload: StorePostCartsCartLineItemsReq
|
||||
): AxiosPromise<StoreCartsCartRes> {
|
||||
const path = `/store/carts/${cart_id}/line-items`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a line-item.
|
||||
* Only quantity updates are allowed
|
||||
* @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<StoreCartsCartRes>}
|
||||
*/
|
||||
update(
|
||||
cart_id: string,
|
||||
line_id: string,
|
||||
payload: StorePostCartsCartLineItemsItemReq
|
||||
): AxiosPromise<StoreCartsCartRes> {
|
||||
const path = `/store/carts/${cart_id}/line-items/${line_id}`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<StoreCartsDeleteRes>}
|
||||
*/
|
||||
delete(cart_id: string, line_id: string): AxiosPromise<StoreCartsDeleteRes> {
|
||||
const path = `/store/carts/${cart_id}/line-items/${line_id}`
|
||||
return this.client.request("DELETE", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default LineItemsResource
|
||||
@@ -0,0 +1,48 @@
|
||||
import { StoreGetOrdersParams, StoreOrdersRes } from "@medusajs/medusa"
|
||||
import { AxiosPromise } from "axios"
|
||||
import BaseResource from "./base"
|
||||
|
||||
class OrdersResource extends BaseResource {
|
||||
/**
|
||||
* @description Retrieves an order
|
||||
* @param {string} id is required
|
||||
* @return {AxiosPromise<StoreOrdersRes>}
|
||||
*/
|
||||
retrieve(id: string): AxiosPromise<StoreOrdersRes> {
|
||||
const path = `/store/orders/${id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Retrieves an order by cart id
|
||||
* @param {string} cart_id is required
|
||||
* @return {AxiosPromise<StoreOrdersRes>}
|
||||
*/
|
||||
retrieveByCartId(cart_id: string): AxiosPromise<StoreOrdersRes> {
|
||||
const path = `/store/orders/cart/${cart_id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Look up an order using order details
|
||||
* @param {StoreGetOrdersParams} payload details used to look up the order
|
||||
* @return {AxiosPromise<StoreOrdersRes>}
|
||||
*/
|
||||
lookupOrder(payload: StoreGetOrdersParams): AxiosPromise<StoreOrdersRes> {
|
||||
let path = `/store/orders?`
|
||||
|
||||
const queryString = Object.entries(payload).map(([key, value]) => {
|
||||
let val = value
|
||||
if (Array.isArray(value)) {
|
||||
val = value.join(",")
|
||||
}
|
||||
|
||||
return `${key}=${encodeURIComponent(val)}`
|
||||
})
|
||||
path = `/store/orders?${queryString.join("&")}`
|
||||
|
||||
return this.client.request("GET", path, payload)
|
||||
}
|
||||
}
|
||||
|
||||
export default OrdersResource
|
||||
@@ -0,0 +1,16 @@
|
||||
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[] }>}
|
||||
*/
|
||||
list(id: string): AxiosPromise<{ payment_methods: object[] }> {
|
||||
const path = `/store/carts/${id}/payment-methods`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default PaymentMethodsResource
|
||||
@@ -0,0 +1,43 @@
|
||||
import {
|
||||
StoreGetVariantsParams,
|
||||
StoreVariantsListRes,
|
||||
StoreVariantsRes,
|
||||
} from "@medusajs/medusa"
|
||||
import { AxiosPromise } from "axios"
|
||||
import BaseResource from "./base"
|
||||
|
||||
class ProductVariantsResource extends BaseResource {
|
||||
/**
|
||||
* @description Retrieves a single product variant
|
||||
* @param {string} id is required
|
||||
* @return {AxiosPromise<StoreVariantsRes>}
|
||||
*/
|
||||
retrieve(id: string): AxiosPromise<StoreVariantsRes> {
|
||||
const path = `/store/variants/${id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Retrieves a list of of Product Variants
|
||||
* @param {StoreVariantsListParamsObject} query
|
||||
* @return {AxiosPromise<StoreVariantsListRes>}
|
||||
*/
|
||||
list(query?: StoreGetVariantsParams): AxiosPromise<StoreVariantsListRes> {
|
||||
const path = `/store/variants`
|
||||
|
||||
const search = Object.entries(query || {}).map(([key, value]) => {
|
||||
if (Array.isArray(value)) {
|
||||
return `${key}=${value.join(",")}`
|
||||
}
|
||||
|
||||
return `${key}=${value}`
|
||||
})
|
||||
|
||||
return this.client.request(
|
||||
"GET",
|
||||
`${path}${search.length > 0 && `?${search.join("&")}`}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default ProductVariantsResource
|
||||
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
StoreGetProductsParams,
|
||||
StorePostSearchReq,
|
||||
StorePostSearchRes,
|
||||
StoreProductsListRes,
|
||||
StoreProductsRes,
|
||||
} from "@medusajs/medusa"
|
||||
import { AxiosPromise } from "axios"
|
||||
import BaseResource from "./base"
|
||||
import ProductVariantsResource from "./product-variants"
|
||||
|
||||
class ProductsResource extends BaseResource {
|
||||
public variants = new ProductVariantsResource(this.client)
|
||||
|
||||
/**
|
||||
* @description Retrieves a single Product
|
||||
* @param {string} id is required
|
||||
* @return {AxiosPromise<StoreProductsRes>}
|
||||
*/
|
||||
retrieve(id: string): AxiosPromise<StoreProductsRes> {
|
||||
const path = `/store/products/${id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Searches for products
|
||||
* @param {StorePostSearchReq} searchOptions is required
|
||||
* @return {AxiosPromise<StorePostSearchRes>}
|
||||
*/
|
||||
search(searchOptions: StorePostSearchReq): AxiosPromise<StorePostSearchRes> {
|
||||
const path = `/store/products/search`
|
||||
return this.client.request("POST", path, searchOptions)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Retrieves a list of products
|
||||
* @param {StoreGetProductsParams} query is optional. Can contain a limit and offset for the returned list
|
||||
* @return {AxiosPromise<StoreProductsListRes>}
|
||||
*/
|
||||
list(query?: StoreGetProductsParams): AxiosPromise<StoreProductsListRes> {
|
||||
let path = `/store/products`
|
||||
|
||||
if (query) {
|
||||
const queryString = Object.entries(query).map(([key, value]) => {
|
||||
return `${key}=${value}`
|
||||
})
|
||||
|
||||
path = `/store/products?${queryString.join("&")}`
|
||||
}
|
||||
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default ProductsResource
|
||||
@@ -0,0 +1,26 @@
|
||||
import { AxiosPromise } from "axios"
|
||||
import { StoreRegionsListRes, StoreRegionsRes } from "@medusajs/medusa"
|
||||
import BaseResource from "./base"
|
||||
|
||||
class RegionsResource extends BaseResource {
|
||||
/**
|
||||
* @description Retrieves a list of regions
|
||||
* @return {AxiosPromise<StoreRegionsListRes>}
|
||||
*/
|
||||
list(): AxiosPromise<StoreRegionsListRes> {
|
||||
const path = `/store/regions`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Retrieves a region
|
||||
* @param {string} id is required
|
||||
* @return {AxiosPromise<StoreRegionsRes>}
|
||||
*/
|
||||
retrieve(id: string): AxiosPromise<StoreRegionsRes> {
|
||||
const path = `/store/regions/${id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default RegionsResource
|
||||
@@ -0,0 +1,29 @@
|
||||
import BaseResource from "./base"
|
||||
import {
|
||||
StoreReturnReasonsListRes,
|
||||
StoreReturnReasonsRes,
|
||||
} from "@medusajs/medusa"
|
||||
import { AxiosPromise } from "axios"
|
||||
|
||||
class ReturnReasonsResource extends BaseResource {
|
||||
/**
|
||||
* @description Retrieves a single Return Reason
|
||||
* @param {string} id is required
|
||||
* @return {AxiosPromise<StoreReturnReasonsRes>}
|
||||
*/
|
||||
retrieve(id: string): AxiosPromise<StoreReturnReasonsRes> {
|
||||
const path = `/store/return-reasons/${id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists return reasons defined in Medusa Admin
|
||||
* @return {AxiosPromise<StoreReturnReasonsListRes>}
|
||||
*/
|
||||
list(): AxiosPromise<StoreReturnReasonsListRes> {
|
||||
const path = `/store/return-reasons`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default ReturnReasonsResource
|
||||
@@ -0,0 +1,17 @@
|
||||
import BaseResource from "./base"
|
||||
import { AxiosPromise } from "axios"
|
||||
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<StoreReturnsRes>}
|
||||
*/
|
||||
create(payload: StorePostReturnsReq): AxiosPromise<StoreReturnsRes> {
|
||||
const path = `/store/returns`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
}
|
||||
|
||||
export default ReturnsResource
|
||||
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
StoreGetShippingOptionsParams,
|
||||
StoreShippingOptionsListRes,
|
||||
} from "@medusajs/medusa"
|
||||
import { AxiosPromise } from "axios"
|
||||
import BaseResource from "./base"
|
||||
|
||||
class ShippingOptionsResource extends BaseResource {
|
||||
/**
|
||||
* @description Lists shiping options available for a cart
|
||||
* @param {string} cart_id
|
||||
* @return {AxiosPromise<StoreShippingOptionsListRes>}
|
||||
*/
|
||||
listCartOptions(cart_id: string): AxiosPromise<StoreShippingOptionsListRes> {
|
||||
const path = `/store/shipping-options/${cart_id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Lists shiping options available
|
||||
* @param {StoreGetShippingOptionsParamsObject} query
|
||||
* @return {AxiosPromise<StoreShippingOptionsListRes>}
|
||||
*/
|
||||
list(
|
||||
query?: StoreGetShippingOptionsParams
|
||||
): AxiosPromise<StoreShippingOptionsListRes> {
|
||||
let path = `/store/shipping-options`
|
||||
|
||||
const queryString = Object.entries(query || {}).map(([key, value]) => {
|
||||
let val = value
|
||||
if (Array.isArray(value)) {
|
||||
val = value.join(",")
|
||||
}
|
||||
|
||||
return `${key}=${val}`
|
||||
})
|
||||
|
||||
path = `/store/shipping-options?${queryString.join("&")}`
|
||||
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default ShippingOptionsResource
|
||||
@@ -0,0 +1,27 @@
|
||||
import { AxiosPromise } from "axios"
|
||||
import { StoreSwapsRes, StorePostSwapsReq } from "@medusajs/medusa"
|
||||
import BaseResource from "./base"
|
||||
|
||||
class SwapsResource extends BaseResource {
|
||||
/**
|
||||
* @description Creates a swap from a cart
|
||||
* @param {StorePostSwapsReq} payload
|
||||
* @return {AxiosPromise<StoreSwapsRes>}
|
||||
*/
|
||||
create(payload: StorePostSwapsReq): AxiosPromise<StoreSwapsRes> {
|
||||
const path = `/store/swaps`
|
||||
return this.client.request("POST", path, payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Retrieves a swap by cart id
|
||||
* @param {string} cart_id id of cart
|
||||
* @return {AxiosPromise<StoreSwapsRes>}
|
||||
*/
|
||||
retrieveByCartId(cart_id: string): AxiosPromise<StoreSwapsRes> {
|
||||
const path = `/store/swaps/${cart_id}`
|
||||
return this.client.request("GET", path)
|
||||
}
|
||||
}
|
||||
|
||||
export default SwapsResource
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["es5", "es6"],
|
||||
"target": "es5",
|
||||
"outDir": "./dist",
|
||||
"esModuleInterop": true,
|
||||
"declaration": true,
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"sourceMap": true,
|
||||
"noImplicitReturns": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", " **/tests/*"]
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user