fix: Merge conflicts with master
This commit is contained in:
commit
ab580066ae
135 files changed
+7501
-1981
No files matched your search
@@ -6,61 +6,6 @@ The Medusa JS Client provides easy access to the Medusa API from a client writte
|
||||
|
||||
## Documentation
|
||||
|
||||
You can learn more about this client and how to use it [our documentation](https://docs.medusajs.com/js-client/overview).
|
||||
You can learn more about how to install and use this client in [our documentation](https://docs.medusajs.com/js-client/overview).
|
||||
|
||||
To learn more about the API endpoints that this client allows you to access check out our [API reference](https://docs.medusajs.com/api/store).
|
||||
|
||||
## Installation
|
||||
|
||||
Install the package with:
|
||||
|
||||
```sh
|
||||
npm install @medusajs/medusa-js
|
||||
# or
|
||||
yarn add @medusajs/medusa-js
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Import Medusa as a default import and initiate it:
|
||||
|
||||
```js
|
||||
import Medusa from "@medusajs/medusa-js"
|
||||
|
||||
const medusa = new Medusa()
|
||||
|
||||
const { cart } = await medusa.carts.create({})
|
||||
```
|
||||
|
||||
### Authentication
|
||||
|
||||
Authentication can be achieved in two ways using the `medusa-js` client, either by utilizing API keys or by using cookie based authentication, each with their own unique use case.
|
||||
|
||||
#### **Using API keys**
|
||||
|
||||
API keys can only be used for admin functionality in Medusa because only users of the admin system have api keys. To use API keys for authentication the key should be used when `medusa-js` is initialized with a config object as described below.
|
||||
|
||||
#### **Using cookies**
|
||||
|
||||
Authentication using cookies is done automatically by Axios when authenticating using the `auth` endpoints. After authentication all subsequent calls will be authenticated.
|
||||
|
||||
_note: Cookie based authentication cannot be used in plain `node.js` applications due to the limitations of axios and `useCredentials` not setting the `Cookie` request header when `set-cookie` is present in the response headers. For pure `node.js` applications use authentication with api keys(see above)_
|
||||
|
||||
## Configuration
|
||||
|
||||
### Initialize with config object
|
||||
|
||||
The package can be initialized with several options:
|
||||
|
||||
```js
|
||||
const medusa = new Medusa({
|
||||
maxRetries: 3,
|
||||
baseUrl: "https://api.example.com",
|
||||
})
|
||||
```
|
||||
|
||||
| Option | Default | Description |
|
||||
| ------------ | ------------------------- | --------------------------------------------------------- |
|
||||
| `maxRetries` | `0` | The amount of times a request is retried. |
|
||||
| `baseUrl` | `'http://localhost:9000'` | The url to which requests are made to. |
|
||||
| `apiKey` | `''` | Optional api key used for authenticating admin requests . |
|
||||
@@ -1,7 +1,12 @@
|
||||
module.exports = {
|
||||
transform: {
|
||||
"^.+\\.(t|j)sx?$": require.resolve("ts-jest"),
|
||||
globals: {
|
||||
"ts-jest": {
|
||||
diagnostics: false,
|
||||
isolatedModules: true,
|
||||
},
|
||||
},
|
||||
testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
|
||||
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
|
||||
transform: {
|
||||
".(ts|tsx)$": require.resolve("ts-jest/dist/"),
|
||||
},
|
||||
testEnvironment: "jsdom",
|
||||
}
|
||||
@@ -3,23 +3,30 @@
|
||||
"version": "1.3.6",
|
||||
"description": "Client for Medusa Commerce Rest API",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.esm.js",
|
||||
"files": [
|
||||
"dist/**/*"
|
||||
],
|
||||
"module": "dist/index.mjs",
|
||||
"types": "dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.js"
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "rollup -c --bundleConfigAsCjs",
|
||||
"build": "tsup src/index.ts",
|
||||
"prepare": "cross-env NODE_ENV=production yarn run build",
|
||||
"test": "jest --passWithNoTests"
|
||||
},
|
||||
"author": "Oliver Juhl",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=12.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "0.26.1",
|
||||
"form-data": "^4.0.0",
|
||||
"axios": "^0.24.0",
|
||||
"qs": "^6.10.3",
|
||||
"retry-axios": "^2.6.0"
|
||||
"retry-axios": "^2.6.0",
|
||||
"uuid": "^9.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@medusajs/medusa": "^1.7.3"
|
||||
@@ -34,15 +41,12 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@medusajs/medusa": "^1.7.3",
|
||||
"@rollup/plugin-commonjs": "^24.0.0",
|
||||
"@rollup/plugin-node-resolve": "^15.0.1",
|
||||
"@types/jest": "^26.0.19",
|
||||
"@types/uuid": "^9.0.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"jest": "^26.6.3",
|
||||
"rollup": "^3.9.1",
|
||||
"rollup-plugin-terser": "^7.0.2",
|
||||
"rollup-plugin-typescript2": "^0.34.1",
|
||||
"typescript": "^4.1.3"
|
||||
"jest": "^27.4.7",
|
||||
"ts-jest": "^27.1.5",
|
||||
"tsup": "^6.5.0",
|
||||
"typescript": "^4.9.4"
|
||||
},
|
||||
"gitHead": "cd1f5afa5aa8c0b15ea957008ee19f1d695cbd2e",
|
||||
"homepage": "https://docs.medusajs.com/js-client/overview"
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
import commonjs from "@rollup/plugin-commonjs"
|
||||
import resolve from "@rollup/plugin-node-resolve"
|
||||
import { defineConfig } from "rollup"
|
||||
import { terser } from "rollup-plugin-terser"
|
||||
import typescript from "rollup-plugin-typescript2"
|
||||
|
||||
const packageJson = require("./package.json")
|
||||
|
||||
export default defineConfig({
|
||||
input: "src/index.ts",
|
||||
external: ["@medusajs/medusa"],
|
||||
output: [
|
||||
{
|
||||
file: packageJson.main,
|
||||
format: "cjs",
|
||||
sourcemap: true,
|
||||
},
|
||||
{
|
||||
file: packageJson.module,
|
||||
format: "esm",
|
||||
sourcemap: true,
|
||||
},
|
||||
],
|
||||
plugins: [
|
||||
terser(),
|
||||
resolve({
|
||||
browser: true,
|
||||
}),
|
||||
commonjs(),
|
||||
typescript({
|
||||
useTsconfigDeclarationDir: true,
|
||||
}),
|
||||
],
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
import MedusaError from "./error"
|
||||
import KeyManager from "./key-manager"
|
||||
import Client, { Config } from "./request"
|
||||
import Admin from "./resources/admin"
|
||||
import AuthResource from "./resources/auth"
|
||||
@@ -6,18 +7,17 @@ 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 OrderEditsResource from "./resources/order-edits"
|
||||
import OrdersResource from "./resources/orders"
|
||||
import PaymentCollectionsResource from "./resources/payment-collections"
|
||||
import PaymentMethodsResource from "./resources/payment-methods"
|
||||
import ProductsResource from "./resources/products"
|
||||
import ProductTypesResource from "./resources/product-types"
|
||||
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"
|
||||
import KeyManager from "./key-manager"
|
||||
|
||||
class Medusa {
|
||||
private client: Client
|
||||
@@ -78,5 +78,4 @@ class Medusa {
|
||||
|
||||
export default Medusa
|
||||
export { default as KeyManager } from "./key-manager"
|
||||
|
||||
export * from "./typings"
|
||||
@@ -1,13 +1,11 @@
|
||||
import {
|
||||
AdminDeleteUploadsReq,
|
||||
IAdminPostUploadsFileReq,
|
||||
AdminDeleteUploadsRes,
|
||||
AdminPostUploadsDownloadUrlReq,
|
||||
AdminUploadsDownloadUrlRes,
|
||||
AdminUploadsRes,
|
||||
} from "@medusajs/medusa"
|
||||
import FormData from "form-data"
|
||||
import { ResponsePromise } from "../../typings"
|
||||
import { AdminCreateUploadPayload, ResponsePromise } from "../../typings"
|
||||
import BaseResource from "../base"
|
||||
|
||||
class AdminUploadsResource extends BaseResource {
|
||||
@@ -15,20 +13,30 @@ class AdminUploadsResource extends BaseResource {
|
||||
"Content-Type": "multipart/form-data",
|
||||
}
|
||||
|
||||
create(file: IAdminPostUploadsFileReq): ResponsePromise<AdminUploadsRes> {
|
||||
/**
|
||||
* @description Uploads at least one file to the specific fileservice that is installed in Medusa.
|
||||
* @param file File or array of files to upload.
|
||||
* @returns Uploaded file or files.
|
||||
*/
|
||||
create(file: AdminCreateUploadPayload): ResponsePromise<AdminUploadsRes> {
|
||||
const path = `/admin/uploads`
|
||||
|
||||
const payload = new FormData()
|
||||
payload.append("files", file)
|
||||
const payload = this._createPayload(file)
|
||||
|
||||
return this.client.request("POST", path, payload, {}, this.headers)
|
||||
}
|
||||
|
||||
createProtected(file: IAdminPostUploadsFileReq): ResponsePromise<AdminUploadsRes> {
|
||||
|
||||
/**
|
||||
* @description Uploads at least one file with ACL or a non-public bucket to the specific fileservice that is installed in Medusa.
|
||||
* @param file File or array of files to upload.
|
||||
* @returns Uploaded file or files.
|
||||
*/
|
||||
createProtected(
|
||||
file: AdminCreateUploadPayload
|
||||
): ResponsePromise<AdminUploadsRes> {
|
||||
const path = `/admin/uploads/protected`
|
||||
|
||||
const payload = new FormData()
|
||||
payload.append("files", file)
|
||||
const payload = this._createPayload(file)
|
||||
|
||||
return this.client.request("POST", path, payload, {}, this.headers)
|
||||
}
|
||||
@@ -50,6 +58,18 @@ class AdminUploadsResource extends BaseResource {
|
||||
|
||||
return this.client.request("POST", path, payload, {}, customHeaders)
|
||||
}
|
||||
|
||||
private _createPayload(file: AdminCreateUploadPayload) {
|
||||
const payload = new FormData()
|
||||
|
||||
if (Array.isArray(file)) {
|
||||
file.forEach((f) => payload.append("files", f))
|
||||
} else {
|
||||
payload.append("files", file)
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
}
|
||||
|
||||
export default AdminUploadsResource
|
||||
@@ -47,3 +47,5 @@ export type InviteUserRolesEnum = `${AdminPostInvitesReq["role"]}`
|
||||
export type AdminPostInvitesPayload = Omit<AdminPostInvitesReq, "role"> & {
|
||||
role: InviteUserRolesEnum
|
||||
}
|
||||
|
||||
export type AdminCreateUploadPayload = File | File[]
|
||||
@@ -2,8 +2,7 @@
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"module": "esnext",
|
||||
"target": "es5",
|
||||
"lib": ["es6", "dom", "es2016", "es2017"],
|
||||
"target": "ES2015",
|
||||
"outDir": "./dist",
|
||||
"declarationDir": "./dist",
|
||||
"esModuleInterop": true,
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { Options } from "tsup"
|
||||
|
||||
const config: Options = {
|
||||
entry: ["src/**/*.ts"],
|
||||
dts: true,
|
||||
clean: true,
|
||||
minify: true,
|
||||
bundle: true,
|
||||
sourcemap: true,
|
||||
format: ["cjs", "esm"],
|
||||
target: "es2020",
|
||||
skipNodeModulesBundle: true,
|
||||
tsconfig: "./tsconfig.json",
|
||||
}
|
||||
|
||||
export default config
|
||||
@@ -1,7 +1,7 @@
|
||||
import Medusa from "@medusajs/medusa-js"
|
||||
import {
|
||||
QueryClientProvider,
|
||||
QueryClientProviderProps,
|
||||
QueryClientProviderProps
|
||||
} from "@tanstack/react-query"
|
||||
import React from "react"
|
||||
|
||||
|
||||
@@ -4,9 +4,8 @@ import {
|
||||
AdminPostUploadsDownloadUrlReq,
|
||||
AdminUploadsDownloadUrlRes,
|
||||
AdminUploadsRes,
|
||||
IAdminPostUploadsFileReq,
|
||||
} from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { AdminCreateUploadPayload, Response } from "@medusajs/medusa-js"
|
||||
import {
|
||||
useMutation,
|
||||
UseMutationOptions,
|
||||
@@ -19,13 +18,13 @@ export const useAdminUploadFile = (
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminUploadsRes>,
|
||||
Error,
|
||||
IAdminPostUploadsFileReq
|
||||
AdminCreateUploadPayload
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation((payload: IAdminPostUploadsFileReq) => {
|
||||
return useMutation((payload: AdminCreateUploadPayload) => {
|
||||
return client.admin.uploads.create(payload)
|
||||
}, buildOptions(queryClient, undefined, options))
|
||||
}
|
||||
@@ -34,13 +33,13 @@ export const useAdminUploadProtectedFile = (
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminUploadsRes>,
|
||||
Error,
|
||||
IAdminPostUploadsFileReq
|
||||
AdminCreateUploadPayload
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation((payload: IAdminPostUploadsFileReq) => {
|
||||
return useMutation((payload: AdminCreateUploadPayload) => {
|
||||
return client.admin.uploads.createProtected(payload)
|
||||
}, buildOptions(queryClient, undefined, options))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user