docs: fix code block titles (#5733)

* docs: fix code block titles

* remove console

* fix build error
This commit is contained in:
Shahed Nasser
2023-11-27 16:08:10 +00:00
committed by GitHub
parent de8f748674
commit 547b16ead5
110 changed files with 483 additions and 456 deletions
@@ -48,7 +48,7 @@ You can learn more about Middlewares and their capabilities in [Expresss docu
Here's an example of a middleware:
```ts title=src/api/middlewares/custom-middleware.ts
```ts title="src/api/middlewares/custom-middleware.ts"
export function customMiddleware(req, res, next) {
// TODO perform an action
@@ -66,7 +66,7 @@ The examples used here don't apply Cross-Origin Resource Origin (CORS) options f
:::
```ts title=src/api/index.ts
```ts title="src/api/index.ts"
import { Router } from "express"
import {
customMiddleware,
@@ -118,7 +118,7 @@ If you want to register a logged-in user and access it in your resources, you ca
To register a new resource in the dependency container, use the `req.scope.register` method:
```ts title=src/api/middlewares/custom-middleware.ts
```ts title="src/api/middlewares/custom-middleware.ts"
export function customMiddleware(req, res, next) {
// TODO perform an action
@@ -136,7 +136,7 @@ You can then load this new resource within other resources. For example, to load
<!-- eslint-disable prefer-rest-params -->
```ts title=src/services/custom-service.ts
```ts title="src/services/custom-service.ts"
import { TransactionBaseService } from "@medusajs/medusa"
class CustomService extends TransactionBaseService {
@@ -18,7 +18,7 @@ v1.17.2 of `@medusajs/medusa` introduced a new approach to creating middlewares
## Basic Implementation
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import type { MiddlewaresConfig } from "@medusajs/medusa"
import type {
MedusaNextFunction,
@@ -99,7 +99,7 @@ The `resolve`'s value is a function that returns the resource to be registered i
For example:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import type { MiddlewaresConfig } from "@medusajs/medusa"
import type {
MedusaNextFunction,
@@ -134,7 +134,7 @@ You can then load this new resource within other resources. For example, to load
<!-- eslint-disable prefer-rest-params -->
```ts title=src/services/custom-service.ts
```ts title="src/services/custom-service.ts"
import { TransactionBaseService } from "@medusajs/medusa"
class CustomService extends TransactionBaseService {
@@ -19,7 +19,7 @@ Following v1.17.2 of `@medusajs/medusa`, it's highly recommended to use the [API
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:
```ts title=src/api/index.ts
```ts title="src/api/index.ts"
import { Router } from "express"
export default (rootDirectory, options) => {
@@ -62,7 +62,7 @@ Instead of returning an Express router in the function, you can return an array
For example:
```ts title=src/api/index.ts
```ts title="src/api/index.ts"
import { Router } from "express"
export default (rootDirectory, options) => {
@@ -199,7 +199,7 @@ If you want to accept request body parameters, you need to pass express middlewa
For example:
```ts title=src/api/index.ts
```ts title="src/api/index.ts"
import bodyParser from "body-parser"
import express, { Router } from "express"
@@ -356,7 +356,7 @@ The function returns an object with the following properties:
Here's an example of retrieving the configurations within an endpoint using `getConfigFile`:
```ts title=src/api/index.ts
```ts title="src/api/index.ts"
import { Router } from "express"
import { ConfigModule } from "@medusajs/medusa"
import { getConfigFile } from "medusa-core-utils"
@@ -382,7 +382,7 @@ Notice that `getConfigFile` is a generic function. So, if you're using TypeScrip
If you're accessing custom configurations, you'll need to create a new type that defines these configurations. For example:
```ts title=src/api/index.ts
```ts title="src/api/index.ts"
import { Router } from "express"
import { ConfigModule } from "@medusajs/medusa"
import { getConfigFile } from "medusa-core-utils"
@@ -426,7 +426,7 @@ Code snippets are taken from the [full example available at the end of this docu
To handle errors using Medusa's middlewares, first, import the `errorHandler` middleware from `@medusajs/medusa` and apply it on your routers. Make sure it's applied after all other middlewares and routes:
```ts title=src/api/index.ts
```ts title="src/api/index.ts"
import express, { Router } from "express"
import adminRoutes from "./admin"
import storeRoutes from "./store"
@@ -449,7 +449,7 @@ export default (rootDirectory, options) => {
Then, wrap the function handler of every route with the `wrapHandler` middleware imported from `@medusajs/medusa`. For example:
```ts title=src/api/admin.ts
```ts title="src/api/admin.ts"
import { wrapHandler } from "@medusajs/medusa"
// ...
@@ -477,7 +477,7 @@ Alternatively, you can define the endpoints in different files, and import and u
<!-- eslint-disable @typescript-eslint/no-var-requires -->
```ts title=src/api/admin.ts
```ts title="src/api/admin.ts"
import { wrapHandler } from "@medusajs/medusa"
// ...
@@ -17,7 +17,7 @@ v1.17.2 of `@medusajs/medusa` introduced API Routes to replace Express endpoints
## Basic Implementation
```ts title=src/api/store/custom/route.ts
```ts title="src/api/store/custom/route.ts"
import type {
MedusaRequest,
MedusaResponse,
@@ -91,7 +91,7 @@ You can access a path parameter's value in method handlers using the `MedusaRequ
For example:
```ts title=src/api/store/custom/[id]/route.ts
```ts title="src/api/store/custom/[id]/route.ts"
import type {
MedusaRequest,
MedusaResponse,
@@ -119,7 +119,7 @@ The `cors` middleware, which enables Cross-Origin Resource Sharing (CORS), is au
To add CORS configurations to custom API routes under other path prefixes, or override the CORS configurations added by default, define a [middleware](./add-middleware.mdx) on your API routes and pass it the `cors` middleware. For example:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import type {
MiddlewaresConfig,
} from "@medusajs/medusa"
@@ -146,7 +146,7 @@ To disable the `cors` middleware for an API Route, export a `CORS` variable in t
For example:
```ts title=src/api/store/custom/route.ts
```ts title="src/api/store/custom/route.ts"
import type {
MedusaRequest,
MedusaResponse,
@@ -174,7 +174,7 @@ Each of the `body`'s keys are a name of the request body parameters, and its val
For example:
```ts title=src/api/store/custom/route.ts
```ts title="src/api/store/custom/route.ts"
import type {
MedusaRequest,
MedusaResponse,
@@ -194,7 +194,7 @@ If you want to parse other content types, such as `application/x-www-form-urlenc
For example:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import type {
MiddlewaresConfig,
} from "@medusajs/medusa"
@@ -224,7 +224,7 @@ You can opt out of the default body parser by setting the `bodyParser` property
For example:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import { MiddlewaresConfig } from "@medusajs/medusa"
import { raw } from "body-parser"
@@ -243,7 +243,7 @@ You can also disable the default `json` body parser for specific HTTP methods us
For example:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import { MiddlewaresConfig } from "@medusajs/medusa"
import { raw } from "body-parser"
@@ -269,7 +269,7 @@ If you expect the request body of an API Route to be larger than the default, yo
For example:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import { MiddlewaresConfig } from "@medusajs/medusa"
export const config: MiddlewaresConfig = {
@@ -294,7 +294,7 @@ By default, API routes prefixed by `/store` don't require customer authenticatio
For example:
```ts title=src/api/store/custom/route.ts
```ts title="src/api/store/custom/route.ts"
import { CustomerService } from "@medusajs/medusa"
import type {
MedusaRequest,
@@ -327,7 +327,7 @@ API Routes prefixed by `/store/me`, on the other hand, require customer authenti
If you want to disable authentication requirement on your custom API Route prefixed with `/store/me`, export an `AUTHENTICATE` variable in the route file with its value set to `false`. For example:
```ts title=src/api/store/me/custom/route.ts
```ts title="src/api/store/me/custom/route.ts"
import type {
MedusaRequest,
MedusaResponse,
@@ -357,7 +357,7 @@ By default, all API Routes prefixed by `/admin` require admin user authenticatio
For example:
```ts title=src/api/admin/custom/route.ts
```ts title="src/api/admin/custom/route.ts"
import type {
MedusaRequest,
MedusaResponse,
@@ -384,7 +384,7 @@ To disable authentication requirement on an admin API Route, export an `AUTHENTI
For example:
```ts title=src/api/admin/custom/route.ts
```ts title="src/api/admin/custom/route.ts"
import type {
MedusaRequest,
MedusaResponse,
@@ -418,7 +418,7 @@ To protect API routes that aren't prefixed with `/store` or `/admin`, you can us
For example:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import {
authenticate,
requireCustomerAuthentication,
@@ -447,7 +447,7 @@ You can access the configurations exported in `medusa-config.js`, including your
For example:
```ts title=src/api/store/custom/route.ts
```ts title="src/api/store/custom/route.ts"
import type {
MedusaRequest,
MedusaResponse,
@@ -562,7 +562,7 @@ To override the default error handler, pass the `errorHandler` property to the [
For example:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import { MiddlewaresConfig } from "@medusajs/medusa"
export const config: MiddlewaresConfig = {
@@ -578,7 +578,7 @@ To disable the default error handler, set the `errorHandler` property of the [ex
For example:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import { MiddlewaresConfig } from "@medusajs/medusa"
export const config: MiddlewaresConfig = {
@@ -592,7 +592,7 @@ To ensure that errors are still returned in the response when the default error
For example:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import {
MedusaRequest,
MedusaResponse,
@@ -624,7 +624,7 @@ Posts are represented by a custom entity not covered in this guide. You can refe
:::
```ts title=src/api/store/posts/route.ts
```ts title="src/api/store/posts/route.ts"
import type {
MedusaRequest,
MedusaResponse,
@@ -659,7 +659,7 @@ Notice that to retrieve an instance of the repository, you need to retrieve firs
:::
```ts title=src/api/store/posts/route.ts
```ts title="src/api/store/posts/route.ts"
import type {
MedusaRequest,
MedusaResponse,
@@ -22,7 +22,7 @@ Learn more about [middlewares in its guide](./add-middleware.mdx).
Create the file `src/api/middlewares.ts` with the following content:
```ts title=src/api/middlewares.ts
```ts title="src/api/middlewares.ts"
import type {
MiddlewaresConfig,
User,
@@ -40,7 +40,7 @@ In the file you created, which in this case is `src/api/index.ts`, add the follo
<!-- eslint-disable max-len -->
```ts title=src/api/index.ts
```ts title="src/api/index.ts"
import { registerOverriddenValidators } from "@medusajs/medusa"
import {
AdminPostProductsReq as MedusaAdminPostProductsReq,