chore(docs): added eslint to lint documentation code blocks (#2920)

* docs: added rule for code length

* chore: fixes based on vale errors

* changed to use eslint

* fixes using eslint

* added github action for documentation eslint

* changed allowed max-length

* fixed incorrect heading level

* removed comment
This commit is contained in:
Shahed Nasser
2022-12-30 18:44:46 +02:00
committed by GitHub
parent 99add15fc3
commit d1b4b11ff6
67 changed files with 2611 additions and 1735 deletions
+38 -27
View File
@@ -62,12 +62,13 @@ Next, in the exported function, retrieve the CORS configurations of your server
```ts
export default (rootDirectory) => {
//...
// ...
const { configModule } = getConfigFile<ConfigModule>(rootDirectory, "medusa-config")
const { configModule } =
getConfigFile<ConfigModule>(rootDirectory, "medusa-config")
const { projectConfig } = configModule
//....
// ....
}
```
@@ -94,7 +95,7 @@ Finally, for each route you add, create an `OPTIONS` request and add `cors` 
```ts
router.options("/admin/hello", cors(corsOptions))
router.get("/admin/hello", cors(corsOptions), (req, res) => {
//...
// ...
})
```
@@ -202,20 +203,24 @@ 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:
```ts
import authenticate from "@medusajs/medusa/dist/api/middlewares/authenticate-customer"
import
authenticate
from "@medusajs/medusa/dist/api/middlewares/authenticate-customer"
```
Then, add the middleware to your route:
```ts
router.options("/store/hello", cors(corsOptions))
router.get("/store/hello", cors(corsOptions), authenticate(), async (req, res) => {
if (req.user) {
//user is logged in
//to get customer id: req.user.customer_id
router.get("/store/hello", cors(corsOptions), authenticate(),
async (req, res) => {
if (req.user) {
// user is logged in
// to get customer id: req.user.customer_id
}
// ...
}
//...
})
)
```
Please note that the endpoint is still accessible by all users, however, youll be able to access the current logged-in customer if theres any.
@@ -227,21 +232,25 @@ To disallow guest customers from accessing the endpoint, you can throw an error
To make an admin route protected, first, import the `authenticate` middleware:
```ts
import authenticate from "@medusajs/medusa/dist/api/middlewares/authenticate"
import
authenticate
from "@medusajs/medusa/dist/api/middlewares/authenticate"
```
Then, add the middleware to your route:
```ts
router.options("/admin/products/count", cors(corsOptions))
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")
const user = await userService.retrieve(id)
//...
})
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")
const user = await userService.retrieve(id)
// ...
}
)
```
Now, only authenticated users can access this endpoint.
@@ -257,15 +266,17 @@ 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:
```ts
router.get("/admin/products/count", cors(corsOptions), authenticate(), (req, res) => {
const productService = req.scope.resolve("productService")
router.get("/admin/products/count", cors(corsOptions), authenticate(),
(req, res) => {
const productService = req.scope.resolve("productService")
productService.count().then((count) => {
res.json({
count,
productService.count().then((count) => {
res.json({
count,
})
})
})
})
}
)
```
The `productService` has a `count` method that returns a Promise. This Promise resolves to the count of the products. You return a JSON of the product count.