docs: update docs across projects following publishable API key change in store routes (#9098)
* docs: update docs across projects following publishable API key change in store routes * rename env variable * move subscribe route out of store
This commit is contained in:
1 parent
ef8dc4087e
commit
58f06be44d
66 files changed
+577
-298
No files matched your search
@@ -12,9 +12,9 @@ An API route is created for every HTTP method you export a handler function for
|
||||
|
||||
Allowed HTTP methods are: `GET`, `POST`, `DELETE`, `PUT`, `PATCH`, `OPTIONS`, and `HEAD`.
|
||||
|
||||
For example, create the file `src/api/store/hello-world/route.ts` with the following content:
|
||||
For example, create the file `src/api/hello-world/route.ts` with the following content:
|
||||
|
||||
```ts title="src/api/store/hello-world/route.ts"
|
||||
```ts title="src/api/hello-world/route.ts"
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -41,5 +41,5 @@ export const POST = async (
|
||||
|
||||
This adds two API Routes:
|
||||
|
||||
- A `GET` route at `http://localhost:9000/store/hello-world`.
|
||||
- A `POST` route at `http://localhost:9000/store/hello-world`.
|
||||
- A `GET` route at `http://localhost:9000/hello-world`.
|
||||
- A `POST` route at `http://localhost:9000/hello-world`.
|
||||
@@ -37,7 +37,7 @@ import type {
|
||||
export default defineMiddlewares({
|
||||
routes: [
|
||||
{
|
||||
matcher: "/store*",
|
||||
matcher: "/custom*",
|
||||
middlewares: [
|
||||
(
|
||||
req: MedusaRequest,
|
||||
@@ -59,7 +59,7 @@ The `defineMiddlewares` function accepts a middleware configurations object that
|
||||
- `matcher`: a string or regular expression indicating the API route path to apply the middleware on. The regular expression must be compatible with [path-to-regexp](https://github.com/pillarjs/path-to-regexp).
|
||||
- `middlewares`: An array of middleware functions.
|
||||
|
||||
In the example above, you define a middleware that logs the message `Received a request!` whenever a request is sent to an API route path starting with `/store`.
|
||||
In the example above, you define a middleware that logs the message `Received a request!` whenever a request is sent to an API route path starting with `/custom`.
|
||||
|
||||
---
|
||||
|
||||
@@ -73,7 +73,7 @@ To test the middleware:
|
||||
npm run dev
|
||||
```
|
||||
|
||||
2. Send a request to any API route starting with `/store`.
|
||||
2. Send a request to any API route starting with `/custom`.
|
||||
3. See the following message in the terminal:
|
||||
|
||||
```bash
|
||||
@@ -128,7 +128,7 @@ import type {
|
||||
export default defineMiddlewares({
|
||||
routes: [
|
||||
{
|
||||
matcher: "/store/custom/:id",
|
||||
matcher: "/custom/:id",
|
||||
middlewares: [
|
||||
// ...
|
||||
],
|
||||
@@ -137,7 +137,7 @@ export default defineMiddlewares({
|
||||
})
|
||||
```
|
||||
|
||||
This applies a middleware to the routes defined in the file `src/api/store/custom/[id]/route.ts`.
|
||||
This applies a middleware to the routes defined in the file `src/api/custom/[id]/route.ts`.
|
||||
|
||||
---
|
||||
|
||||
@@ -160,7 +160,7 @@ import type {
|
||||
export default defineMiddlewares({
|
||||
routes: [
|
||||
{
|
||||
matcher: "/store*",
|
||||
matcher: "/custom*",
|
||||
method: ["POST", "PUT"],
|
||||
middlewares: [
|
||||
// ...
|
||||
@@ -172,7 +172,7 @@ export default defineMiddlewares({
|
||||
|
||||
`method`'s value is one or more HTTP methods to apply the middleware to.
|
||||
|
||||
This example applies the middleware only when a `POST` or `PUT` request is sent to an API route path starting with `/store`.
|
||||
This example applies the middleware only when a `POST` or `PUT` request is sent to an API route path starting with `/custom`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -10,13 +10,13 @@ In this chapter, you’ll learn about path, query, and request body parameters.
|
||||
|
||||
To create an API route that accepts a path parameter, create a directory within the route file's path whose name is of the format `[param]`.
|
||||
|
||||
For example, to create an API Route at the path `/hello-world/:id`, where `:id` is a path parameter, create the file `src/api/store/hello-world/[id]/route.ts` with the following content:
|
||||
For example, to create an API Route at the path `/hello-world/:id`, where `:id` is a path parameter, create the file `src/api/hello-world/[id]/route.ts` with the following content:
|
||||
|
||||
export const singlePathHighlights = [
|
||||
["11", "req.params.id", "Access the path parameter `id`"]
|
||||
]
|
||||
|
||||
```ts title="src/api/store/hello-world/[id]/route.ts" highlights={singlePathHighlights} apiTesting testApiUrl="http://localhost:9000/store/hello-world/{id}" testApiMethod="GET" testPathParams={{ "id": "" }}
|
||||
```ts title="src/api/hello-world/[id]/route.ts" highlights={singlePathHighlights}
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -38,14 +38,14 @@ The `MedusaRequest` object has a `params` property. `params` holds the path para
|
||||
|
||||
To create an API route that accepts multiple path parameters, create within the file's path multiple directories whose names are of the format `[param]`.
|
||||
|
||||
For example, to create an API route at `/store/hello-world/:id/name/:name`, create the file `src/api/store/hello-world/[id]/name/[name]/route.ts` with the following content:
|
||||
For example, to create an API route at `/hello-world/:id/name/:name`, create the file `src/api/hello-world/[id]/name/[name]/route.ts` with the following content:
|
||||
|
||||
export const multiplePathHighlights = [
|
||||
["12", "req.params.id", "Access the path parameter `id`"],
|
||||
["13", "req.params.name", "Access the path parameter `name`"]
|
||||
]
|
||||
|
||||
```ts title="src/api/store/hello-world/[id]/name/[name]/route.ts" highlights={multiplePathHighlights} apiTesting testApiUrl="http://localhost:9000/store/hello-world/{id}/name/{name}" testApiMethod="GET" testPathParams={{ "id": "", "name": "" }}
|
||||
```ts title="src/api/hello-world/[id]/name/[name]/route.ts" highlights={multiplePathHighlights}
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -77,7 +77,7 @@ export const queryHighlights = [
|
||||
["11", "req.query.name", "Access the query parameter `name`"],
|
||||
]
|
||||
|
||||
```ts title="src/api/store/hello-world/route.ts" highlights={queryHighlights} apiTesting testApiUrl="http://localhost:9000/store/hello-world" testApiMethod="GET" testQueryParams={{ "name": "" }}
|
||||
```ts title="src/api/hello-world/route.ts" highlights={queryHighlights}
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -108,7 +108,7 @@ export const bodyHighlights = [
|
||||
["15", "req.body.name", "Access the request body parameter `name`"],
|
||||
]
|
||||
|
||||
```ts title="src/api/store/hello-world/route.ts" highlights={bodyHighlights}
|
||||
```ts title="src/api/hello-world/route.ts" highlights={bodyHighlights}
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -138,8 +138,8 @@ The `MedusaRequest` type accepts a type argument that indicates the type of the
|
||||
|
||||
To test it out, send the following request to your Medusa application:
|
||||
|
||||
```bash apiTesting testApiUrl="http://localhost:9000/store/hello-world" testApiMethod="POST" testBodyParams={{ "name": "" }}
|
||||
curl -X POST 'http://localhost:9000/store/hello-world' \
|
||||
```bash
|
||||
curl -X POST 'http://localhost:9000/hello-world' \
|
||||
-H 'Content-Type: application/json' \
|
||||
--data-raw '{
|
||||
"name": "John"
|
||||
|
||||
@@ -20,7 +20,7 @@ Requests to `/admin` must be user-authenticated to access the route.
|
||||
|
||||
<Note title="Tip">
|
||||
|
||||
Refer to the API Reference for [Admin](https://docs.medusajs.com/api/admin#authentication) and [Store](https://docs.medusajs.com/api/store#authentication) authentication methods.
|
||||
Refer to the API Reference for [Admin](!api!/admin#authentication) and [Store](!api!/store#authentication) authentication methods.
|
||||
|
||||
</Note>
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ export const jsonHighlights = [
|
||||
["7", "json", "Return a JSON object."]
|
||||
]
|
||||
|
||||
```ts title="src/api/store/custom/route.ts" highlights={jsonHighlights} apiTesting testApiUrl="http://localhost:9000/store/custom" testApiMethod="GET"
|
||||
```ts title="src/api/custom/route.ts" highlights={jsonHighlights}
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
|
||||
export const GET = async (
|
||||
@@ -51,7 +51,7 @@ export const statusHighlight = [
|
||||
["7", "status", "Set the response code to `201`."]
|
||||
]
|
||||
|
||||
```ts title="src/api/store/custom/route.ts" highlights={statusHighlight} apiTesting testApiUrl="http://localhost:9000/store/custom" testApiMethod="GET"
|
||||
```ts title="src/api/custom/route.ts" highlights={statusHighlight}
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
|
||||
export const GET = async (
|
||||
|
||||
@@ -8,7 +8,7 @@ In this chapter, you'll learn how to validate request body parameters in your cu
|
||||
|
||||
## Example Scenario
|
||||
|
||||
Consider you're creating a `POST` API route at `/store/custom`. It accepts two paramters `a` and `b` that are required numbers, and returns their sum.
|
||||
Consider you're creating a `POST` API route at `/custom`. It accepts two paramters `a` and `b` that are required numbers, and returns their sum.
|
||||
|
||||
The next steps explain how to add validation to this API route, as an example.
|
||||
|
||||
@@ -20,9 +20,9 @@ Medusa uses [Zod](https://zod.dev/) to validate the body parameters of an incomi
|
||||
|
||||
To use Zod to validate your custom schemas, create a `validators.ts` file in any `src/api` subfolder. This file holds Zod schemas for each of your API routes.
|
||||
|
||||
For example, create the file `src/api/store/custom/validators.ts` with the following content:
|
||||
For example, create the file `src/api/custom/validators.ts` with the following content:
|
||||
|
||||
```ts title="src/api/store/custom/validators.ts"
|
||||
```ts title="src/api/custom/validators.ts"
|
||||
import { z } from "zod"
|
||||
|
||||
export const PostStoreCustomSchema = z.object({
|
||||
@@ -41,7 +41,7 @@ The `PostStoreCustomSchema` variable is a Zod schema that indicates the request
|
||||
|
||||
## Step 2: Add Validation Middleware
|
||||
|
||||
To use this schema for validating the body parameters of requests to `/store/custom`, use the `validateAndTransformBody` middleware provided by `@medusajs/medusa`. It accepts the Zod schema as a parameter.
|
||||
To use this schema for validating the body parameters of requests to `/custom`, use the `validateAndTransformBody` middleware provided by `@medusajs/medusa`. It accepts the Zod schema as a parameter.
|
||||
|
||||
For example, create the file `src/api/middlewares.ts` with the following content:
|
||||
|
||||
@@ -50,12 +50,12 @@ import { defineMiddlewares } from "@medusajs/medusa"
|
||||
import {
|
||||
validateAndTransformBody,
|
||||
} from "@medusajs/medusa/dist/api/utils/validate-body"
|
||||
import { PostStoreCustomSchema } from "./store/custom/validators"
|
||||
import { PostStoreCustomSchema } from "./custom/validators"
|
||||
|
||||
export default defineMiddlewares({
|
||||
routes: [
|
||||
{
|
||||
matcher: "/store/custom",
|
||||
matcher: "/custom",
|
||||
method: "POST",
|
||||
middlewares: [
|
||||
validateAndTransformBody(PostStoreCustomSchema),
|
||||
@@ -65,7 +65,7 @@ export default defineMiddlewares({
|
||||
})
|
||||
```
|
||||
|
||||
This applies the `validateAndTransformBody` middleware on `POST` requests to `/store/custom`. It uses the `PostStoreCustomSchema` as the validation schema.
|
||||
This applies the `validateAndTransformBody` middleware on `POST` requests to `/custom`. It uses the `PostStoreCustomSchema` as the validation schema.
|
||||
|
||||
### How the Validation Works
|
||||
|
||||
@@ -79,14 +79,14 @@ If a request's body parameters are validated successfully, the middleware sets t
|
||||
|
||||
In your API route, consume the validated body using the `validatedBody` property of `MedusaRequest`.
|
||||
|
||||
For example, create the file `src/api/store/custom/route.ts` with the following content:
|
||||
For example, create the file `src/api/custom/route.ts` with the following content:
|
||||
|
||||
export const routeHighlights = [
|
||||
["5", "PostStoreCustomSchemaType", "Infer the request body type from the schema to pass it as a type parameter to `MedusaRequest`."],
|
||||
["14", "", "Access the body parameters using `validatedBody`."]
|
||||
]
|
||||
|
||||
```ts title="src/api/store/custom/route.ts" highlights={routeHighlights}
|
||||
```ts title="src/api/custom/route.ts" highlights={routeHighlights}
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import { z } from "zod"
|
||||
import { PostStoreCustomSchema } from "./validators"
|
||||
@@ -117,7 +117,7 @@ To pass the request body's type as a type parameter to `MedusaRequest`, use Zod'
|
||||
|
||||
## Test it Out
|
||||
|
||||
To test out the validation, send a `POST` request to `/store/custom`. You can try sending incorrect request body parameters.
|
||||
To test out the validation, send a `POST` request to `/custom`. You can try sending incorrect request body parameters.
|
||||
|
||||
For example, if you omit the `a` parameter, you'll receive a `400` response code with the following response data:
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ In your resources, such as API routes or workflows, you can resolve Query to fet
|
||||
|
||||
## Query Example
|
||||
|
||||
For example, create the route `src/api/store/query/route.ts` with the following content:
|
||||
For example, create the route `src/api/query/route.ts` with the following content:
|
||||
|
||||
export const exampleHighlights = [
|
||||
["13", "", "Resolve Query from the Medusa container."],
|
||||
@@ -33,7 +33,7 @@ export const exampleHighlights = [
|
||||
["17", "fields", "An array of the data model’s properties to retrieve in the result."],
|
||||
]
|
||||
|
||||
```ts title="src/api/store/query/route.ts" highlights={exampleHighlights} apiTesting testApiMethod="GET" testApiUrl="http://localhost:9000/store/query" collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
```ts title="src/api/query/route.ts" highlights={exampleHighlights} collapsibleLines="1-8" expandButtonLabel="Show Imports"
|
||||
import {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
|
||||
@@ -19,7 +19,7 @@ export const highlights = [
|
||||
["14", "throwOnError", "Specify that errors occuring during the workflow's execution should be returned, not thrown."],
|
||||
]
|
||||
|
||||
```ts title="src/api/store/workflows/route.ts" highlights={highlights} collapsibleLines="1-6" expandButtonLabel="Show Imports"
|
||||
```ts title="src/api/workflows/route.ts" highlights={highlights} collapsibleLines="1-6" expandButtonLabel="Show Imports"
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
|
||||
@@ -239,9 +239,9 @@ In the workflow construction function, you first run the `updateProduct` step, t
|
||||
|
||||
You can now use and execute your workflow in your Medusa application.
|
||||
|
||||
To execute the workflow in an API route, create the file `src/api/store/products/[id]/erp/route.ts` with the following content:
|
||||
To execute the workflow in an API route, create the file `src/api/products/[id]/erp/route.ts` with the following content:
|
||||
|
||||
```ts title="src/api/store/products/[id]/erp/route.ts"
|
||||
```ts title="src/api/products/[id]/erp/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import updateProductAndErpWorkflow, {
|
||||
UpdateProductAndErpWorkflowInput,
|
||||
|
||||
@@ -79,7 +79,7 @@ export default myWorkflow
|
||||
|
||||
3. Execute the workflow from a resource, such as an API route:
|
||||
|
||||
```ts title="src/api/store/workflow/route.ts" collapsibleLines="1-6" expandButtonLabel="Show Imports"
|
||||
```ts title="src/api/workflow/route.ts" collapsibleLines="1-6" expandButtonLabel="Show Imports"
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -103,10 +103,10 @@ export async function GET(
|
||||
npm run dev
|
||||
```
|
||||
|
||||
5. Send a `GET` request to `/store/workflow`:
|
||||
5. Send a `GET` request to `/workflow`:
|
||||
|
||||
```bash apiTesting testApiMethod="GET" testApiUrl="http://localhost:9000/store/workflow"
|
||||
curl http://localhost:9000/store/workflow
|
||||
```bash
|
||||
curl http://localhost:9000/workflow
|
||||
```
|
||||
|
||||
In the console, you'll see:
|
||||
|
||||
@@ -276,7 +276,7 @@ export const highlights = [
|
||||
["30", "subscribe", "Subscribe to status changes of the workflow execution."],
|
||||
]
|
||||
|
||||
```ts title="src/api/store/workflows/route.ts" highlights={highlights} collapsibleLines="1-11" expandButtonLabel="Show Imports"
|
||||
```ts title="src/api/workflows/route.ts" highlights={highlights} collapsibleLines="1-11" expandButtonLabel="Show Imports"
|
||||
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import myWorkflow from "../../../workflows/hello-world"
|
||||
import {
|
||||
|
||||
@@ -20,9 +20,9 @@ An API Route is created in a TypeScript or JavaScript file under the `src/api` d
|
||||
|
||||
Each file exports API Route handler functions for at least one HTTP method (`GET`, `POST`, `DELETE`, etc…).
|
||||
|
||||
For example, to create a `GET` API Route at `/store/hello-world`, create the file `src/api/store/hello-world/route.ts` with the following content:
|
||||
For example, to create a `GET` API Route at `/hello-world`, create the file `src/api/hello-world/route.ts` with the following content:
|
||||
|
||||
```ts title="src/api/store/hello-world/route.ts"
|
||||
```ts title="src/api/hello-world/route.ts"
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -46,10 +46,10 @@ To test the API route above, start the Medusa application:
|
||||
npm run dev
|
||||
```
|
||||
|
||||
Then, send a `GET` request to the `/store/hello-world` API Route:
|
||||
Then, send a `GET` request to the `/hello-world` API Route:
|
||||
|
||||
```bash apiTesting testApiUrl="http://localhost:9000/store/hello-world" testApiMethod="GET"
|
||||
curl http://localhost:9000/store/hello-world
|
||||
```bash
|
||||
curl http://localhost:9000/hello-world
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -105,9 +105,9 @@ Resolving a module's service is essential to use its methods that perform action
|
||||
|
||||
</Note>
|
||||
|
||||
For example, create the API route `src/api/store/custom/route.ts` with the following content:
|
||||
For example, create the API route `src/api/custom/route.ts` with the following content:
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
```ts title="src/api/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import HelloModuleService from "../../../modules/hello/service"
|
||||
import { HELLO_MODULE } from "../../../modules/hello"
|
||||
@@ -132,10 +132,10 @@ Then, start the Medusa application:
|
||||
npm run dev
|
||||
```
|
||||
|
||||
Finally, send a `GET` request to `/store/custom`:
|
||||
Finally, send a `GET` request to `/custom`:
|
||||
|
||||
```bash apiTesting testApiUrl="http://localhost:9000/store/custom" testApiMethod="GET"
|
||||
curl http://localhost:9000/store/custom
|
||||
```bash
|
||||
curl http://localhost:9000/custom
|
||||
```
|
||||
|
||||
You’ll receive the following response:
|
||||
|
||||
@@ -96,7 +96,7 @@ To execute the workflow, invoke it passing the Medusa container as a parameter.
|
||||
<CodeTabs group="resource-types">
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/workflow/route.ts" highlights={[["11"], ["12"], ["13"], ["14"], ["15"], ["16"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
|
||||
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"], ["13"], ["14"], ["15"], ["16"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -193,10 +193,10 @@ To test out your workflow, start your Medusa application:
|
||||
npm run dev
|
||||
```
|
||||
|
||||
Then, send a `GET` request to `/store/workflow`:
|
||||
Then, send a `GET` request to `/workflow`:
|
||||
|
||||
```bash apiTesting testApiMethod="GET" testApiUrl="http://localhost:9000/store/workflow"
|
||||
curl http://localhost:9000/store/workflow
|
||||
```bash
|
||||
curl http://localhost:9000/workflow
|
||||
```
|
||||
|
||||
You’ll receive the following response:
|
||||
|
||||
+21
-21
@@ -19,9 +19,9 @@ In this chapter, you'll learn how to write integration tests for API routes usin
|
||||
|
||||
## Test a GET API Route
|
||||
|
||||
Consider the following API route created at `src/api/store/custom/route.ts`:
|
||||
Consider the following API route created at `src/api/custom/route.ts`:
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
```ts title="src/api/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
|
||||
export async function GET(
|
||||
@@ -37,7 +37,7 @@ export async function GET(
|
||||
To write an integration test that tests this API route, create the file `integration-tests/http/custom-routes.spec.ts` with the following content:
|
||||
|
||||
export const getHighlights = [
|
||||
["8", "api.get", "Send a GET request to the `/store/custom` API route."]
|
||||
["8", "api.get", "Send a GET request to the `/custom` API route."]
|
||||
]
|
||||
|
||||
```ts title="integration-tests/http/custom-routes.spec.ts" highlights={getHighlights}
|
||||
@@ -46,10 +46,10 @@ import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
medusaIntegrationTestRunner({
|
||||
testSuite: ({ api, getContainer }) => {
|
||||
describe("Custom endpoints", () => {
|
||||
describe("GET /store/custom", () => {
|
||||
describe("GET /custom", () => {
|
||||
it("returns correct message", async () => {
|
||||
const response = await api.get(
|
||||
`/store/custom`
|
||||
`/custom`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
@@ -64,7 +64,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
You use the `medusaIntegrationTestRunner` to write your tests.
|
||||
|
||||
You add a single test that sends a `GET` request to `/store/custom` using the `api.get` method. For the test to pass, the response is expected to:
|
||||
You add a single test that sends a `GET` request to `/custom` using the `api.get` method. For the test to pass, the response is expected to:
|
||||
|
||||
- Have a code status `200`,
|
||||
- Have a `message` property in the returned data.
|
||||
@@ -103,9 +103,9 @@ const MyCustom = model.define("my_custom", {
|
||||
export default MyCustom
|
||||
```
|
||||
|
||||
And consider that the file `src/api/store/custom/route.ts` defines another route handler for `POST` requests:
|
||||
And consider that the file `src/api/custom/route.ts` defines another route handler for `POST` requests:
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
```ts title="src/api/custom/route.ts"
|
||||
// other imports...
|
||||
import HelloModuleService from "../../../modules/hello/service"
|
||||
|
||||
@@ -134,7 +134,7 @@ This API route creates a new record of `MyCustom`.
|
||||
To write tests for this API route, add the following at the end of the `testSuite` function in `integration-tests/http/custom-routes.spec.ts`:
|
||||
|
||||
export const postHighlights = [
|
||||
["14", "api.post", "Send a POST request to the `/store/custom` API route."]
|
||||
["14", "api.post", "Send a POST request to the `/custom` API route."]
|
||||
]
|
||||
|
||||
```ts title="integration-tests/http/custom-routes.spec.ts" highlights={postHighlights}
|
||||
@@ -146,13 +146,13 @@ medusaIntegrationTestRunner({
|
||||
describe("Custom endpoints", () => {
|
||||
// other tests...
|
||||
|
||||
describe("POST /store/custom", () => {
|
||||
describe("POST /custom", () => {
|
||||
const id = "1"
|
||||
|
||||
it("Creates my custom", async () => {
|
||||
|
||||
const response = await api.post(
|
||||
`/store/custom`,
|
||||
`/custom`,
|
||||
{
|
||||
id,
|
||||
name: "Test",
|
||||
@@ -174,7 +174,7 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
```
|
||||
|
||||
This adds a test for the `POST /store/custom` API route. It uses `api.post` to send the POST request. The `api.post` method accepts as a second parameter the data to pass in the request body.
|
||||
This adds a test for the `POST /custom` API route. It uses `api.post` to send the POST request. The `api.post` method accepts as a second parameter the data to pass in the request body.
|
||||
|
||||
The test passes if the response has:
|
||||
|
||||
@@ -188,7 +188,7 @@ To ensure consistency in the database for the rest of the tests after the above
|
||||
|
||||
Use the `getContainer` function passed as a parameter to the `testSuite` function to resolve a service and use it for setup or teardown purposes
|
||||
|
||||
So, add an `afterAll` hook in the `describe` block for `POST /store/custom`:
|
||||
So, add an `afterAll` hook in the `describe` block for `POST /custom`:
|
||||
|
||||
```ts title="integration-tests/http/custom-routes.spec.ts"
|
||||
// other imports...
|
||||
@@ -199,7 +199,7 @@ medusaIntegrationTestRunner({
|
||||
describe("Custom endpoints", () => {
|
||||
// other tests...
|
||||
|
||||
describe("POST /store/custom", () => {
|
||||
describe("POST /custom", () => {
|
||||
// ...
|
||||
afterAll(() => async () => {
|
||||
const helloModuleService: HelloModuleService = getContainer().resolve(
|
||||
@@ -220,9 +220,9 @@ The `afterAll` hook resolves the `HelloModuleService` and use its `deleteMyCusto
|
||||
|
||||
## Test a DELETE API Route
|
||||
|
||||
Consider a `/store/custom/:id` API route created at `src/api/store/custom/[id]/route.ts`:
|
||||
Consider a `/custom/:id` API route created at `src/api/custom/[id]/route.ts`:
|
||||
|
||||
```ts title="src/api/store/custom/[id]/route.ts"
|
||||
```ts title="src/api/custom/[id]/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||||
import HelloModuleService from "../../../modules/hello/service"
|
||||
|
||||
@@ -247,7 +247,7 @@ This API route accepts an ID path parameter, and uses the `HelloModuleService` t
|
||||
To add tests for this API route, add the following to `integration-tests/http/custom-routes.spec.ts`:
|
||||
|
||||
export const deleteHighlights = [
|
||||
["21", "api.delete", "Send a DELETE request to the `/store/custom/:id` API route."]
|
||||
["21", "api.delete", "Send a DELETE request to the `/custom/:id` API route."]
|
||||
]
|
||||
|
||||
```ts title="integration-tests/http/custom-routes.spec.ts" highlights={deleteHighlights}
|
||||
@@ -256,7 +256,7 @@ medusaIntegrationTestRunner({
|
||||
describe("Custom endpoints", () => {
|
||||
// ...
|
||||
|
||||
describe("DELETE /store/custom/:id", () => {
|
||||
describe("DELETE /custom/:id", () => {
|
||||
const id = "1"
|
||||
|
||||
beforeAll(() => async () => {
|
||||
@@ -272,7 +272,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
it("Deletes my custom", async () => {
|
||||
const response = await api.delete(
|
||||
`/store/custom/${id}`
|
||||
`/custom/${id}`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
@@ -285,9 +285,9 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
```
|
||||
|
||||
This adds a new test for the `DELETE /store/custom/:id` API route. You use the `beforeAll` hook to create a `MyCustom` record using the `HelloModuleService`.
|
||||
This adds a new test for the `DELETE /custom/:id` API route. You use the `beforeAll` hook to create a `MyCustom` record using the `HelloModuleService`.
|
||||
|
||||
In the test, you use the `api.delete` method to send a `DELETE` request to `/store/custom/:id`. The test passes if the response:
|
||||
In the test, you use the `api.delete` method to send a `DELETE` request to `/custom/:id`. The test passes if the response:
|
||||
|
||||
- Has a `200` status code.
|
||||
- Has a `success` property in its data.
|
||||
|
||||
@@ -10,9 +10,9 @@ In this chapter, you’ll build your first customizations with Medusa.
|
||||
|
||||
API Routes expose business logic through REST APIs. You can create custom API routes to expose custom business logic.
|
||||
|
||||
To create an API route, create the file `src/api/store/hello-world/route.ts` with the following content:
|
||||
To create an API route, create the file `src/api/hello-world/route.ts` with the following content:
|
||||
|
||||
```ts title="src/api/store/hello-world/route.ts"
|
||||
```ts title="src/api/hello-world/route.ts"
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -40,10 +40,10 @@ To test out your customizations:
|
||||
npm run dev
|
||||
```
|
||||
|
||||
2. Send a `GET` request to `/store/hello-world`:
|
||||
2. Send a `GET` request to `/hello-world`:
|
||||
|
||||
```bash apiTesting testApiMethod="GET" testApiUrl="http://localhost:9000/store/hello-world"
|
||||
curl http://localhost:9000/store/hello-world
|
||||
```bash
|
||||
curl http://localhost:9000/hello-world
|
||||
```
|
||||
|
||||
In the response, you’ll receive the following object:
|
||||
|
||||
@@ -13,3 +13,15 @@ You're free to choose how to build your storefront. You can start with our Next.
|
||||
To learn how to build a storefront from scratch, check out the [Storefront Development guides](!resources!/storefront-development) in the Learning Resources documentation.
|
||||
|
||||
</Note>
|
||||
|
||||
---
|
||||
|
||||
## Passing a Publishable API Key in Storefront Requests
|
||||
|
||||
When sending a request to an API route starting with `/store`, you must include a publishable API key in the header of your request.
|
||||
|
||||
A publishable API key sets the scope of your request to one or more sales channels.
|
||||
|
||||
Then, when you retrieve products, only products of those sales channels are retrieved. This also ensures you retrieve correct inventory data, and associate created orders with the scoped sales channel.
|
||||
|
||||
Learn more about passing the publishable API key in [this storefront development guide](!resources!/storefront-development/publishable-api-keys).
|
||||
@@ -1,30 +1,30 @@
|
||||
export const generatedEditDates = {
|
||||
"app/basics/scheduled-jobs/page.mdx": "2024-08-05T07:24:27+00:00",
|
||||
"app/basics/workflows/page.mdx": "2024-09-03T08:07:16.276Z",
|
||||
"app/basics/workflows/page.mdx": "2024-09-11T10:48:38.853Z",
|
||||
"app/deployment/page.mdx": "2024-08-05T07:24:05+00:00",
|
||||
"app/page.mdx": "2024-09-03T07:09:09.034Z",
|
||||
"app/basics/modules-and-services/page.mdx": "2024-09-04T15:37:27.695Z",
|
||||
"app/basics/modules-and-services/page.mdx": "2024-09-11T10:48:35.195Z",
|
||||
"app/basics/commerce-modules/page.mdx": "2024-09-03T07:48:48.148Z",
|
||||
"app/advanced-development/workflows/retry-failed-steps/page.mdx": "2024-07-31T17:01:33+03:00",
|
||||
"app/advanced-development/workflows/workflow-hooks/page.mdx": "2024-09-10T11:39:51.168Z",
|
||||
"app/cheatsheet/page.mdx": "2024-07-11T13:53:40+03:00",
|
||||
"app/debugging-and-testing/logging/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/more-resources/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/storefront-development/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/storefront-development/page.mdx": "2024-09-11T10:58:59.290Z",
|
||||
"app/storefront-development/nextjs-starter/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/basics/page.mdx": "2024-09-03T07:11:06.879Z",
|
||||
"app/basics/admin-customizations/page.mdx": "2024-09-03T08:07:35.584Z",
|
||||
"app/advanced-development/workflows/workflow-timeout/page.mdx": "2024-07-31T17:01:33+03:00",
|
||||
"app/advanced-development/workflows/parallel-steps/page.mdx": "2024-07-31T17:01:33+03:00",
|
||||
"app/advanced-development/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/first-customizations/page.mdx": "2024-05-07T18:00:28+02:00",
|
||||
"app/first-customizations/page.mdx": "2024-09-11T10:48:42.374Z",
|
||||
"app/debugging-and-testing/page.mdx": "2024-05-03T17:36:38+03:00",
|
||||
"app/basics/medusa-container/page.mdx": "2024-09-03T07:31:40.214Z",
|
||||
"app/architectural-modules/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/basics/project-directories-files/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/basics/api-routes/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/basics/api-routes/page.mdx": "2024-09-11T10:48:31.777Z",
|
||||
"app/basics/modules-directory-structure/page.mdx": "2024-05-07T18:00:28+02:00",
|
||||
"app/advanced-development/workflows/access-workflow-errors/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/advanced-development/workflows/access-workflow-errors/page.mdx": "2024-09-11T10:46:54.913Z",
|
||||
"app/basics/events-and-subscribers/page.mdx": "2024-09-03T08:01:30.986Z",
|
||||
"app/advanced-development/modules/container/page.mdx": "2024-08-05T07:23:49+00:00",
|
||||
"app/basics/data-models/page.mdx": "2024-09-03T07:58:42.761Z",
|
||||
@@ -33,11 +33,11 @@ export const generatedEditDates = {
|
||||
"app/advanced-development/admin/widgets/page.mdx": "2024-08-06T09:44:22+02:00",
|
||||
"app/advanced-development/data-models/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/advanced-development/modules/remote-link/page.mdx": "2024-07-24T09:16:01+02:00",
|
||||
"app/advanced-development/api-routes/protected-routes/page.mdx": "2024-09-10T11:39:51.166Z",
|
||||
"app/advanced-development/api-routes/protected-routes/page.mdx": "2024-09-11T10:45:44.293Z",
|
||||
"app/advanced-development/workflows/add-workflow-hook/page.mdx": "2024-08-13T09:55:37+03:00",
|
||||
"app/advanced-development/events-and-subscribers/data-payload/page.mdx": "2024-07-16T17:12:05+01:00",
|
||||
"app/advanced-development/data-models/default-properties/page.mdx": "2024-07-02T12:34:44+03:00",
|
||||
"app/advanced-development/workflows/advanced-example/page.mdx": "2024-07-31T17:01:33+03:00",
|
||||
"app/advanced-development/workflows/advanced-example/page.mdx": "2024-09-11T10:46:59.975Z",
|
||||
"app/advanced-development/events-and-subscribers/emit-event/page.mdx": "2024-09-10T11:39:51.168Z",
|
||||
"app/advanced-development/workflows/conditions/page.mdx": "2024-07-31T17:01:33+03:00",
|
||||
"app/advanced-development/modules/module-link-directions/page.mdx": "2024-07-24T09:16:01+02:00",
|
||||
@@ -55,18 +55,18 @@ export const generatedEditDates = {
|
||||
"app/advanced-development/modules/module-links/page.mdx": "2024-07-24T09:16:01+02:00",
|
||||
"app/advanced-development/data-models/searchable-property/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/advanced-development/scheduled-jobs/execution-number/page.mdx": "2024-07-02T09:41:15+00:00",
|
||||
"app/advanced-development/api-routes/parameters/page.mdx": "2024-09-04T08:17:50.071Z",
|
||||
"app/advanced-development/api-routes/http-methods/page.mdx": "2024-09-04T08:15:11.609Z",
|
||||
"app/advanced-development/api-routes/parameters/page.mdx": "2024-09-11T10:44:13.491Z",
|
||||
"app/advanced-development/api-routes/http-methods/page.mdx": "2024-09-11T10:43:33.169Z",
|
||||
"app/advanced-development/admin/tips/page.mdx": "2024-09-10T11:39:51.165Z",
|
||||
"app/advanced-development/api-routes/cors/page.mdx": "2024-09-04T08:24:47.068Z",
|
||||
"app/advanced-development/admin/ui-routes/page.mdx": "2024-08-06T09:44:22+02:00",
|
||||
"app/advanced-development/api-routes/middlewares/page.mdx": "2024-09-04T09:45:12.441Z",
|
||||
"app/advanced-development/api-routes/middlewares/page.mdx": "2024-09-11T10:45:31.861Z",
|
||||
"app/advanced-development/modules/isolation/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/advanced-development/data-models/configure-properties/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/advanced-development/data-models/index/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/advanced-development/custom-cli-scripts/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/advanced-development/data-models/property-types/page.mdx": "2024-07-04T17:26:03+03:00",
|
||||
"app/debugging-and-testing/testing-tools/integration-tests/api-routes/page.mdx": "2024-09-10T11:39:51.170Z",
|
||||
"app/debugging-and-testing/testing-tools/integration-tests/api-routes/page.mdx": "2024-09-11T10:48:09.593Z",
|
||||
"app/debugging-and-testing/testing-tools/integration-tests/page.mdx": "2024-09-10T11:39:51.170Z",
|
||||
"app/debugging-and-testing/testing-tools/integration-tests/workflows/page.mdx": "2024-09-10T11:39:51.171Z",
|
||||
"app/debugging-and-testing/testing-tools/page.mdx": "2024-09-10T11:39:51.172Z",
|
||||
@@ -74,11 +74,11 @@ export const generatedEditDates = {
|
||||
"app/debugging-and-testing/testing-tools/unit-tests/page.mdx": "2024-09-02T11:03:26.997Z",
|
||||
"app/advanced-development/modules/service-constraints/page.mdx": "2024-09-04T15:37:04.166Z",
|
||||
"app/advanced-development/api-routes/page.mdx": "2024-09-04T09:36:33.961Z",
|
||||
"app/advanced-development/api-routes/responses/page.mdx": "2024-09-10T11:39:51.167Z",
|
||||
"app/advanced-development/api-routes/validation/page.mdx": "2024-09-10T11:39:51.167Z",
|
||||
"app/advanced-development/api-routes/responses/page.mdx": "2024-09-11T10:44:37.016Z",
|
||||
"app/advanced-development/api-routes/validation/page.mdx": "2024-09-11T10:46:31.476Z",
|
||||
"app/advanced-development/api-routes/errors/page.mdx": "2024-09-10T11:39:51.166Z",
|
||||
"app/advanced-development/admin/constraints/page.mdx": "2024-09-10T11:39:51.165Z",
|
||||
"app/advanced-development/modules/query/page.mdx": "2024-09-10T11:39:51.168Z",
|
||||
"app/advanced-development/modules/query/page.mdx": "2024-09-11T10:46:49.512Z",
|
||||
"app/debugging-and-testing/testing-tools/modules-tests/module-example/page.mdx": "2024-09-10T11:39:51.171Z",
|
||||
"app/debugging-and-testing/testing-tools/modules-tests/page.mdx": "2024-09-10T11:39:51.171Z"
|
||||
}
|
||||
Reference in new issue
Block a user