@medusajs/js-sdk 2.22.0-snapshot-20261020204851 → 2.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +89 -0
- package/README.md +102 -0
- package/package.json +6 -2
package/AGENTS.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @medusajs/js-sdk
|
|
2
|
+
|
|
3
|
+
Typed HTTP client for Medusa's Store, Admin, and Auth API routes, used by storefronts, admin customizations, and the Medusa Admin dashboard itself (`packages/admin/dashboard/src/lib/client/client.ts`). Runs in browsers and Node (native `fetch`).
|
|
4
|
+
|
|
5
|
+
## Using this package
|
|
6
|
+
|
|
7
|
+
- Install `@medusajs/js-sdk` (and `@medusajs/types` for `HttpTypes`). Default export is the `Medusa` class.
|
|
8
|
+
- Docs: https://docs.medusajs.com/resources/js-sdk.md, auth: https://docs.medusajs.com/resources/js-sdk/auth/overview.md
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import Medusa from "@medusajs/js-sdk"
|
|
12
|
+
|
|
13
|
+
export const sdk = new Medusa({
|
|
14
|
+
baseUrl: process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL!,
|
|
15
|
+
publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
|
|
16
|
+
auth: { type: "session" },
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const { products } = await sdk.store.product.list({ limit: 10 })
|
|
20
|
+
const { cart } = await sdk.store.cart.create({ region_id: "reg_123" })
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- `Config` options (`src/types.ts`): `baseUrl`, `publishableKey`, `apiKey`, `globalHeaders`, `auth.type` (`"jwt" | "session"`), `auth.jwtTokenStorageKey` (default `medusa_auth_token`), `auth.jwtTokenStorageMethod` (`"local" | "session" | "memory" | "custom" | "nostore"`), `auth.storage` (custom storage with `getItem`/`setItem`/`removeItem`, sync or async), `auth.fetchCredentials`, `logger`, `debug`.
|
|
24
|
+
- `publishableKey` is sent as `x-publishable-api-key` on every request; store routes require it.
|
|
25
|
+
- `apiKey` (admin secret API key) is sent as `Authorization: Basic base64(apiKey + ":")`. Server-side only; never ship it to a browser.
|
|
26
|
+
- Namespaces: `sdk.store.*` (`region`, `collection`, `category`, `productOption`, `product`, `cart`, `fulfillment`, `payment`, `order`, `customer`, `locale`), `sdk.admin.*` (one class per resource in `src/admin/`, e.g. `sdk.admin.product`, `sdk.admin.order`), `sdk.auth.*`.
|
|
27
|
+
|
|
28
|
+
### Auth
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
const result = await sdk.auth.login("customer", "emailpass", {
|
|
32
|
+
email,
|
|
33
|
+
password,
|
|
34
|
+
})
|
|
35
|
+
if (typeof result !== "string") {
|
|
36
|
+
// { location } for OAuth redirect, or mfa_required / verification_required
|
|
37
|
+
}
|
|
38
|
+
const { customer } = await sdk.store.customer.retrieve()
|
|
39
|
+
await sdk.auth.logout()
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
- `login(actor, method, payload)` returns the token string, `{ location }` (OAuth redirect), `{ mfa_required, mfa_challenge, token }`, or `{ verification_required, verification, token }`.
|
|
43
|
+
- `auth.type: "session"`: after login the SDK POSTs the token to `/auth/session` to get a cookie, sends no `Authorization` header, and uses `credentials: "include"` (overridable via `fetchCredentials`). Otherwise the token is stored and sent as `Bearer`.
|
|
44
|
+
- `register()` stores the token but does not start a session: the token has no actor yet. Create the actor (e.g. `sdk.store.customer.create`), then call `sdk.auth.login` again or `sdk.auth.refresh()`.
|
|
45
|
+
- OAuth: `sdk.auth.callback(actor, provider, queryParams)` then `sdk.auth.refresh()` once the actor exists.
|
|
46
|
+
- Also: `sdk.auth.resetPassword`, `updateProvider`, `listProviders`, `createUser`, `sdk.auth.mfa.*`, `sdk.auth.verification.*`.
|
|
47
|
+
|
|
48
|
+
### Custom routes
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const data = await sdk.client.fetch<{ brands: Brand[] }>("/store/brands", {
|
|
52
|
+
method: "POST",
|
|
53
|
+
body: { name: "Acme" },
|
|
54
|
+
query: { fields: "id,name" },
|
|
55
|
+
})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
- `body` objects are JSON-stringified, `query` is serialized with `qs` (`skipNulls`), and auth / publishable-key / locale headers are added.
|
|
59
|
+
- Responses are parsed as JSON only when the request `accept` header contains `application/json` (the default). For files, set `headers: { accept: "application/pdf" }` and read the returned raw `Response`.
|
|
60
|
+
- Status `>= 300` throws `FetchError` with `status`, `statusText`, and `message` (from the JSON body's `message`).
|
|
61
|
+
- `sdk.client.fetchStream(path, init)` returns `{ stream, abort }` for server-sent events.
|
|
62
|
+
- `sdk.setLocale(locale)` stores the locale in `localStorage` and sends `x-medusa-locale`; it logs a warning on the server, where you must pass the header yourself.
|
|
63
|
+
|
|
64
|
+
## Architecture
|
|
65
|
+
|
|
66
|
+
- `src/index.ts`: `Medusa` class wiring `Client`, `Admin`, `Store`, `Auth`.
|
|
67
|
+
- `src/client.ts`: `Client` (`fetch`, `fetchStream`, `setToken`/`getToken`/`clearToken`, header merging, URL building relative to `baseUrl` path).
|
|
68
|
+
- `src/admin/<resource>.ts`: one class per admin resource, registered in `src/admin/index.ts`. `src/store/index.ts`: a single `Store` class with object namespaces. `src/auth/index.ts`: `Auth`.
|
|
69
|
+
- Method convention: `(id?, body?, query?, headers?)` calling `this.client.fetch<HttpTypes.XResponse>(path, { method, headers, body, query })`, with request/response types from `@medusajs/types` `HttpTypes`.
|
|
70
|
+
|
|
71
|
+
## Development
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
yarn workspace @medusajs/js-sdk build # CJS (tsconfig.json) + ESM (tsconfig.esm.json)
|
|
75
|
+
yarn workspace @medusajs/js-sdk test
|
|
76
|
+
cd packages/core/js-sdk && ../../../node_modules/.bin/jest src/__tests__/client.spec.ts
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
- Tests in `src/__tests__/` mock the network with `msw` (`setupServer`), no backend needed.
|
|
80
|
+
- New methods need TSDoc with an `@example` (the JS SDK reference docs are generated from it; use the `writing-tsdocs` skill).
|
|
81
|
+
|
|
82
|
+
## Gotchas
|
|
83
|
+
|
|
84
|
+
- Relative imports in `src/` must end in `.js` (e.g. `from "../client.js"`); `src/__tests__/esm-import-specifiers.spec.ts` fails otherwise (the ESM build in `dist/esm` needs explicit extensions).
|
|
85
|
+
- Header values of `null` delete the header. File uploads rely on this: `admin/upload.ts` and `admin/product.ts` pass `"content-type": null` with a `FormData` body so `fetch` sets the multipart boundary.
|
|
86
|
+
- Header precedence: defaults (`content-type`, `accept`, API key, publishable key) < `globalHeaders` < JWT `Authorization` < per-call `headers`.
|
|
87
|
+
- Without `auth.jwtTokenStorageMethod`, the token goes to `localStorage` if available, else `nostore` (not persisted) on the server. `"local"`/`"session"` throw outside the browser; `"custom"` throws without `auth.storage`.
|
|
88
|
+
- `credentials` is only set when `Request.prototype` supports it (omitted on some server runtimes); for `jwt` it is `"omit"`.
|
|
89
|
+
- The Medusa Admin dashboard depends on this package, so changes to `Client`/`Auth` behavior affect it too.
|
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://www.medusajs.com">
|
|
3
|
+
<picture>
|
|
4
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/59018053/229103275-b5e482bb-4601-46e6-8142-244f531cebdb.svg">
|
|
5
|
+
<source media="(prefers-color-scheme: light)" srcset="https://user-images.githubusercontent.com/59018053/229103726-e5b529a3-9b3f-4970-8a1f-c6af37f087bf.svg">
|
|
6
|
+
<img alt="Medusa logo" src="https://user-images.githubusercontent.com/59018053/229103726-e5b529a3-9b3f-4970-8a1f-c6af37f087bf.svg">
|
|
7
|
+
</picture>
|
|
8
|
+
</a>
|
|
9
|
+
</p>
|
|
10
|
+
<h1 align="center">
|
|
11
|
+
@medusajs/js-sdk
|
|
12
|
+
</h1>
|
|
13
|
+
|
|
14
|
+
<h4 align="center">
|
|
15
|
+
<a href="https://docs.medusajs.com">Documentation</a> |
|
|
16
|
+
<a href="https://www.medusajs.com">Website</a>
|
|
17
|
+
</h4>
|
|
18
|
+
|
|
19
|
+
<p align="center">
|
|
20
|
+
Building blocks for digital commerce
|
|
21
|
+
</p>
|
|
22
|
+
<p align="center">
|
|
23
|
+
<a href="https://github.com/medusajs/medusa/blob/develop/LICENSE">
|
|
24
|
+
<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="Medusa is released under the MIT license." />
|
|
25
|
+
</a>
|
|
26
|
+
<a href="https://github.com/medusajs/medusa/blob/develop/CONTRIBUTING.md">
|
|
27
|
+
<img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" alt="PRs welcome!" />
|
|
28
|
+
</a>
|
|
29
|
+
<a href="https://discord.gg/xpCwq3Kfn8">
|
|
30
|
+
<img src="https://img.shields.io/badge/chat-on%20discord-7289DA.svg" alt="Discord Chat" />
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://twitter.com/intent/follow?screen_name=medusajs">
|
|
33
|
+
<img src="https://img.shields.io/twitter/follow/medusajs.svg?label=Follow%20@medusajs" alt="Follow @medusajs" />
|
|
34
|
+
</a>
|
|
35
|
+
</p>
|
|
36
|
+
|
|
37
|
+
Medusa JS SDK: a JavaScript/TypeScript client for the Medusa Store and Admin API routes. Use it in storefronts, admin customizations, or any JavaScript app that sends requests to a Medusa application, including your custom API routes.
|
|
38
|
+
|
|
39
|
+
## At a glance
|
|
40
|
+
|
|
41
|
+
| | |
|
|
42
|
+
| --- | --- |
|
|
43
|
+
| Package | `@medusajs/js-sdk` |
|
|
44
|
+
| Type | Core library |
|
|
45
|
+
| Import from | `@medusajs/js-sdk` (default export `Medusa`) |
|
|
46
|
+
| Entry points | ESM (`import`) and CommonJS (`require`) builds |
|
|
47
|
+
|
|
48
|
+
## Features
|
|
49
|
+
|
|
50
|
+
- `sdk.store` methods for the Store API routes, such as products, carts, and customers.
|
|
51
|
+
- `sdk.admin` methods for the Admin API routes.
|
|
52
|
+
- `sdk.auth` methods to register, log in, log out, and reset passwords for any actor type, such as `customer` or `user`.
|
|
53
|
+
- `sdk.client.fetch` to send requests to custom API routes with the configured headers and authentication.
|
|
54
|
+
- `sdk.setLocale` to send the `x-medusa-locale` header with every request.
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm install @medusajs/js-sdk @medusajs/types
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`@medusajs/types` is optional and provides the request and response types.
|
|
63
|
+
|
|
64
|
+
## Usage
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import Medusa from "@medusajs/js-sdk"
|
|
68
|
+
|
|
69
|
+
export const sdk = new Medusa({
|
|
70
|
+
baseUrl: "http://localhost:9000",
|
|
71
|
+
debug: process.env.NODE_ENV === "development",
|
|
72
|
+
publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const { products } = await sdk.store.product.list()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Configuration
|
|
79
|
+
|
|
80
|
+
| Option | Type | Description | Default |
|
|
81
|
+
| --- | --- | --- | --- |
|
|
82
|
+
| `baseUrl` | `string` | URL of the Medusa application. Required. | - |
|
|
83
|
+
| `publishableKey` | `string` | Publishable API key, sent in the `x-publishable-api-key` header. Required for Store API routes. | - |
|
|
84
|
+
| `apiKey` | `string` | Secret API key, sent as Basic authentication. Use only on the server for Admin API routes. | - |
|
|
85
|
+
| `globalHeaders` | `ClientHeaders` | Headers to send with every request. | - |
|
|
86
|
+
| `auth.type` | `"jwt" \| "session"` | How to authenticate. See [Authentication types](#authentication-types). | `"jwt"` |
|
|
87
|
+
| `auth.jwtTokenStorageMethod` | `"local" \| "session" \| "memory" \| "custom" \| "nostore"` | Where to store the JWT token. | `"local"` in browsers, `"nostore"` otherwise |
|
|
88
|
+
| `auth.jwtTokenStorageKey` | `string` | Storage key of the JWT token. | `"medusa_auth_token"` |
|
|
89
|
+
| `auth.storage` | `CustomStorage` | Object with `getItem`, `setItem`, and `removeItem` methods. Required when `jwtTokenStorageMethod` is `"custom"`. | - |
|
|
90
|
+
| `auth.fetchCredentials` | `"include" \| "omit" \| "same-origin"` | `credentials` of fetch requests when `auth.type` is `"session"`. | `"include"` |
|
|
91
|
+
| `logger` | `Logger` | Logger with `error`, `warn`, `info`, and `debug` methods. | `console` |
|
|
92
|
+
| `debug` | `boolean` | Whether to log debug messages. | `false` |
|
|
93
|
+
|
|
94
|
+
### Authentication types
|
|
95
|
+
|
|
96
|
+
- `"jwt"` (default): after login, the SDK stores the JWT token based on `auth.jwtTokenStorageMethod` and sends it in the `Authorization: Bearer` header. Fetch requests use `credentials: "omit"`.
|
|
97
|
+
- `"session"`: after login, the SDK exchanges the JWT token for a cookie session using the `/auth/session` API route, and sends requests with `auth.fetchCredentials`. Logging out deletes the session. Use it for the Medusa Admin and admin customizations.
|
|
98
|
+
|
|
99
|
+
## Resources
|
|
100
|
+
|
|
101
|
+
- [JS SDK](https://docs.medusajs.com/resources/js-sdk)
|
|
102
|
+
- [JS SDK authentication](https://docs.medusajs.com/resources/js-sdk/auth/overview)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medusajs/js-sdk",
|
|
3
|
-
"version": "2.22.0
|
|
3
|
+
"version": "2.22.0",
|
|
4
4
|
"description": "SDK for the Medusa API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -25,14 +25,18 @@
|
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
27
|
"dist",
|
|
28
|
+
"AGENTS.md",
|
|
28
29
|
"!dist/**/__tests__",
|
|
29
30
|
"!dist/**/__mocks__",
|
|
30
31
|
"!dist/**/__fixtures__"
|
|
31
32
|
],
|
|
33
|
+
"llms": "https://docs.medusajs.com/llms.txt",
|
|
34
|
+
"llmsFull": "https://docs.medusajs.com/llms-full.txt",
|
|
35
|
+
"mcpServer": "https://docs.medusajs.com/mcp",
|
|
32
36
|
"author": "Medusa",
|
|
33
37
|
"license": "SEE LICENSE IN LICENSE",
|
|
34
38
|
"devDependencies": {
|
|
35
|
-
"@medusajs/types": "2.22.0
|
|
39
|
+
"@medusajs/types": "2.22.0"
|
|
36
40
|
},
|
|
37
41
|
"dependencies": {
|
|
38
42
|
"fetch-event-stream": "^0.1.5",
|