docs: update endpoints to use file-routing approach (#5397)
- Move the original guides for creating endpoints and middlewares to sub-sections in the Endpoints category. - Replace existing guides for endpoints and middlewares with the new approach. - Update all endpoints-related snippets across docs to use this new approach.
This commit is contained in:
@@ -80,7 +80,7 @@ const plugins = [
|
||||
|
||||
## Test it Out
|
||||
|
||||
This plugin adds new `POST` and `PUT` endpoints at `/mailchimp/subscribe`. These endpoints require in the body of the request an `email` field. You can also optionally include a `data` object that holds any additional data you want to send to Mailchimp. You can check out [Mailchimp’s subscription documentation](https://mailchimp.com/developer/marketing/api/list-merges/) for more details on the data you can send.
|
||||
This plugin adds new `POST` and `PUT` API Routes at `/mailchimp/subscribe`. These API Routes require in the body of the request an `email` field. You can also optionally include a `data` object that holds any additional data you want to send to Mailchimp. You can check out [Mailchimp’s subscription documentation](https://mailchimp.com/developer/marketing/api/list-merges/) for more details on the data you can send.
|
||||
|
||||
### Without Additional Data
|
||||
|
||||
@@ -92,7 +92,7 @@ Try sending a `POST` or `PUT` request to `/mailchimp/subscribe` with the followi
|
||||
}
|
||||
```
|
||||
|
||||
If the subscription is successful, a `200` response code will be returned with `OK` message. If the same email address is used again in the `POST`, a `400` response will be returned with an error page. If this can occur in your usecase, use the `PUT` endpoint to prevent this.
|
||||
If the subscription is successful, a `200` response code will be returned with `OK` message. If the same email address is used again in the `POST`, a `400` response will be returned with an error page. If this can occur in your usecase, use the `PUT` API Route to prevent this.
|
||||
|
||||

|
||||
|
||||
@@ -119,22 +119,34 @@ All fields inside `data` will be sent to Mailchimp’s API along with the email.
|
||||
|
||||
## Use Mailchimp Service
|
||||
|
||||
If you want to subscribe to users without using this endpoint or at a specific place in your code, you can use Mailchimp’s service `mailchimpService` in your endpoints, services, or subscribers. This service has a method `subscribeNewsletter` which lets you use the subscribe functionality.
|
||||
To subscribe users to the newsletter without using this API Route or at a specific place in your code, you can use Mailchimp’s service `mailchimpService` in your API Routes, services, or subscribers. This service has a method `subscribeNewsletter` which lets you use the subscribe functionality.
|
||||
|
||||
Here’s an example of using the `mailchimpService` inside an endpoint:
|
||||
Here’s an example of using the `mailchimpService` in an API Route:
|
||||
|
||||
```jsx title=src/api/index.ts
|
||||
const mailchimpService = req.scope.resolve("mailchimpService")
|
||||
```ts title=src/api/store/subscribe/route.ts
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
mailchimpService.subscribeNewsletter(
|
||||
"example@gmail.com",
|
||||
{ tags: ["customer"] } // optional
|
||||
)
|
||||
export const POST = async (
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const mailchimpService = req.scope.resolve(
|
||||
"mailchimpService"
|
||||
)
|
||||
|
||||
mailchimpService.subscribeNewsletter(
|
||||
"example@gmail.com",
|
||||
{ tags: ["customer"] } // optional
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
:::tip
|
||||
|
||||
You can learn more about how you can use services in your endpoints, services, and subscribers in the [Services documentation](../../development/services/create-service.mdx#using-your-custom-service).
|
||||
You can learn more about how you can use services in your API Routes, services, and subscribers in the [Services documentation](../../development/services/create-service.mdx#using-your-custom-service).
|
||||
|
||||
:::
|
||||
|
||||
|
||||
@@ -3930,7 +3930,7 @@ Where `<ORDER_PLACED_TEMPLATE_ID` is the ID of your template for order placed em
|
||||
|
||||
Finally, in your `medusa-config.js` file, add the SendGrid plugin into the array of plugins:
|
||||
|
||||
```jsx title=medusa-config.js
|
||||
```js title=medusa-config.js
|
||||
const plugins = [
|
||||
// ...,
|
||||
{
|
||||
@@ -3983,18 +3983,30 @@ You can also track analytics related to emails sent from the SendGrid dashboard.
|
||||
|
||||
## Dynamic usage
|
||||
|
||||
You can resolve the SendGrid service to send emails from your custom services or other resources. For example:
|
||||
You can resolve the SendGrid service to send emails from your custom services or other resources.
|
||||
|
||||
```ts
|
||||
const sendgridService = scope.resolve("sendgridService")
|
||||
const sendOptions = {
|
||||
templateId: "d-123....",
|
||||
from: "ACME <acme@mail.com>",
|
||||
to: "customer@mail.com",
|
||||
dynamic_template_data: { dynamic: "data" },
|
||||
}
|
||||
For example, in an API Route:
|
||||
|
||||
sendgridService.sendEmail(sendOptions)
|
||||
```ts title=src/api/store/email/route.ts
|
||||
import type {
|
||||
MedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
export const POST = async (
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const sendgridService = req.scope.resolve("sendgridService")
|
||||
const sendOptions = {
|
||||
templateId: "d-123....",
|
||||
from: "ACME <acme@mail.com>",
|
||||
to: "customer@mail.com",
|
||||
dynamic_template_data: { dynamic: "data" },
|
||||
}
|
||||
|
||||
sendgridService.sendEmail(sendOptions)
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -78,7 +78,7 @@ npm install medusa-plugin-slack-notification
|
||||
|
||||
After that, open `medusa-config.js` and add the new plugin with its configurations in the `plugins` array:
|
||||
|
||||
```jsx title=medusa-config.js
|
||||
```js title=medusa-config.js
|
||||
const plugins = [
|
||||
// ...,
|
||||
{
|
||||
|
||||
@@ -55,7 +55,7 @@ Make sure to replace `<YOUR_ACCOUNT_SID>`, `<YOUR_AUTH_TOKEN>`, and `<YOUR_TWILI
|
||||
|
||||
Finally, add the plugin and its options in the `medusa-config.js` file to the `plugins` array:
|
||||
|
||||
```jsx title=medusa-config.js
|
||||
```js title=medusa-config.js
|
||||
const plugins = [
|
||||
// ...
|
||||
{
|
||||
@@ -85,7 +85,7 @@ For this example to work, you'll need to have an event bus module installed and
|
||||
|
||||
Create the file `src/services/sms.js` in your Medusa backend with the following content:
|
||||
|
||||
```jsx title=src/services/sms.js
|
||||
```js title=src/services/sms.js
|
||||
class SmsSubscriber {
|
||||
constructor({
|
||||
twilioSmsService,
|
||||
|
||||
Reference in New Issue
Block a user