docs: migrated from javascript to typescript (#2398)

This commit is contained in:
Shahed Nasser
2022-10-11 16:36:08 +03:00
committed by GitHub
parent 2ac8d8e979
commit f1605ae2a1
7 changed files with 256 additions and 179 deletions
+27 -27
View File
@@ -4,13 +4,13 @@ In this document, youll learn how to create endpoints in your Medusa server.
## Overview
Custom endpoints reside under the `src/api` directory in your Medusa Backend. To define a new endpoint, you can add the file `index.js` under the `src/api` directory. This file should export a function that returns an Express router.
Custom endpoints reside under the `src/api` directory in your Medusa Backend. They're defined in a TypeScript or JavaScript file that is named `index` (for example, `index.ts`). This file should export a function that returns an Express router.
## Implementation
To create a new endpoint, start by creating a new file in `src/api` called `index.js`. At its basic format, `index.js` should look something like this:
To create a new endpoint, start by creating a new file in `src/api` called `index.ts`. At its basic format, `index.ts` should look something like this:
```jsx
```ts
import { Router } from "express"
export default (rootDirectory, pluginOptions) => {
@@ -26,21 +26,21 @@ export default (rootDirectory, pluginOptions) => {
}
```
This exports a function that returns an Express router. The function receives two parameters:
This exports a function that returns an Express router. The function receives two parameters:
- `rootDirectory` is the absolute path to the root directory that your server is running from.
- `pluginOptions` is an object that has your plugin's options. If your API route is not implemented in a plugin, then it will be an empty object.
### Endpoints Path
Your endpoint can be under any path you wish.
Your endpoint can be under any path you wish.
By Medusas conventions:
- All Storefront REST APIs are prefixed by `/store`. For example, the `/store/products` endpoint lets you retrieve the products to display them on your storefront.
- All Admin REST APIs are prefixed by `/admin`. For example, the `/admin/products` endpoint lets you retrieve the products to display them on your Admin.
You can also create endpoints that do not reside under these two prefixes, similar to the `hello` endpoint in the previous example.
You can also create endpoints that don't reside under these two prefixes, similar to the `hello` endpoint in the previous example.
## CORS Configuration
@@ -48,14 +48,14 @@ If youre adding a storefront or admin endpoint and you want to access these e
First, you need to import your Medusa configurations along with the `cors` library:
```jsx
```ts
import cors from "cors"
import { projectConfig } from "../../medusa-config"
```
Then, create an object that will hold the Cross-Origin Resource Sharing (CORS) configurations. If its a storefront endpoint, pass the `origin` property storefront options:
```jsx
```ts
const corsOptions = {
origin: projectConfig.store_cors.split(","),
credentials: true,
@@ -64,7 +64,7 @@ const corsOptions = {
If its an admin endpoint, pass the `origin` property admin options:
```jsx
```ts
const corsOptions = {
origin: projectConfig.admin_cors.split(","),
credentials: true,
@@ -73,7 +73,7 @@ const corsOptions = {
Finally, for each route you add, create an `OPTIONS` request and add `cors` as a middleware for the route:
```jsx
```ts
router.options("/admin/hello", cors(corsOptions))
router.get("/admin/hello", cors(corsOptions), (req, res) => {
//...
@@ -84,9 +84,9 @@ router.get("/admin/hello", cors(corsOptions), (req, res) => {
### Same File
You can add more than one endpoint in `src/api/index.js`:
You can add more than one endpoint in `src/api/index.ts`:
```jsx
```ts
router.options("/store/hello", cors(storeCorsOptions))
router.get("/store/hello", cors(storeCorsOptions), (req, res) => {
res.json({
@@ -106,11 +106,11 @@ router.get("/admin/hello", cors(adminCorsOptions), (req, res) => {
Alternatively, you can add multiple files for each endpoint or set of endpoints for readability and easy maintenance.
To do that with the previous example, first, create the file `src/api/store.js` with the following content:
To do that with the previous example, first, create the file `src/api/store.ts` with the following content:
```jsx
```ts
import cors from "cors"
import { projectConfig } from "../../../medusa-config"
import { projectConfig } from "../../medusa-config"
export default (router) => {
const storeCorsOptions = {
@@ -128,11 +128,11 @@ export default (router) => {
You export a function that receives an Express router as a parameter and adds the endpoint `store/hello` to it.
Next, create the file `src/api/admin.js` with the following content:
Next, create the file `src/api/admin.ts` with the following content:
```jsx
```ts
import cors from "cors"
import { projectConfig } from "../../../medusa-config"
import { projectConfig } from "../../medusa-config"
export default (router) => {
const adminCorsOptions = {
@@ -150,16 +150,16 @@ export default (router) => {
Again, you export a function that receives an Express router as a parameter and adds the endpoint `admin/hello` to it.
Finally, in `src/api/index.js` import the two functions at the beginning of the file:
Finally, in `src/api/index.ts` import the two functions at the beginning of the file:
```jsx
```ts
import storeRoutes from "./store"
import adminRoutes from "./admin"
```
and in the exported function, call each of the functions passing them the Express router:
```jsx
```ts
export default () => {
const router = Router()
@@ -178,13 +178,13 @@ Protected routes are routes that should be accessible by logged-in customers or
To make a storefront route protected, first, import the `authenticate-customer` middleware:
```jsx
```ts
import authenticate from "@medusajs/medusa/dist/api/middlewares/authenticate-customer"
```
Then, add the middleware to your route:
```jsx
```ts
router.options("/store/hello", cors(corsOptions))
router.get("/store/hello", cors(corsOptions), authenticate(), async (req, res) => {
if (req.user) {
@@ -203,15 +203,15 @@ To disallow guest customers from accessing the endpoint, you can throw an error
To make an admin route protected, first, import the `authenticate` middleware:
```jsx
```ts
import authenticate from "@medusajs/medusa/dist/api/middlewares/authenticate"
```
Then, add the middleware to your route:
```jsx
```ts
router.options("/admin/products/count", cors(corsOptions))
router.get("/admin/products/count", cors(corsOptions), authenticate(), (req, res) => {
router.get("/admin/products/count", cors(corsOptions), authenticate(), async (req, res) => {
//access current user
const id = req.user.userId
const userService = req.scope.resolve("userService")
@@ -231,7 +231,7 @@ You can retrieve any registered service in your endpoint using `req.scope.resol
Heres an example of an endpoint that retrieves the count of products in your store:
```jsx
```ts
router.get("/admin/products/count", cors(corsOptions), authenticate(), (req, res) => {
const productService = req.scope.resolve("productService")