api-ref: custom API reference (#4770)

* initialized next.js project

* finished markdown sections

* added operation schema component

* change page metadata

* eslint fixes

* fixes related to deployment

* added response schema

* resolve max stack issue

* support for different property types

* added support for property types

* added loading for components

* added more loading

* type fixes

* added oneOf type

* removed console

* fix replace with push

* refactored everything

* use static content for description

* fixes and improvements

* added code examples section

* fix path name

* optimizations

* fixed tag navigation

* add support for admin and store references

* general enhancements

* optimizations and fixes

* fixes and enhancements

* added search bar

* loading enhancements

* added loading

* added code blocks

* added margin top

* add empty response text

* fixed oneOf parameters

* added path and query parameters

* general fixes

* added base path env variable

* small fix for arrays

* enhancements

* design enhancements

* general enhancements

* fix isRequired

* added enum values

* enhancements

* general fixes

* general fixes

* changed oas generation script

* additions to the introduction section

* added copy button for code + other enhancements

* fix response code block

* fix metadata

* formatted store introduction

* move sidebar logic to Tags component

* added test env variables

* fix code block bug

* added loading animation

* added expand param + loading

* enhance operation loading

* made responsive + improvements

* added loading provider

* fixed loading

* adjustments for small devices

* added sidebar label for endpoints

* added feedback component

* fixed analytics

* general fixes

* listen to scroll for other headings

* added sample env file

* update api ref files + support new fields

* fix for external docs link

* added new sections

* fix last item in sidebar not showing

* move docs content to www/docs

* change redirect url

* revert change

* resolve build errors

* configure rewrites

* changed to environment variable url

* revert changing environment variable name

* add environment variable for API path

* fix links

* fix tailwind settings

* remove vercel file

* reconfigured api route

* move api page under api

* fix page metadata

* fix external link in navigation bar

* update api spec

* updated api specs

* fixed google lint error

* add max-height on request samples

* add padding before loading

* fix for one of name

* fix undefined types

* general fixes

* remove response schema example

* redesigned navigation bar

* redesigned sidebar

* fixed up paddings

* added feedback component + report issue

* fixed up typography, padding, and general styling

* redesigned code blocks

* optimization

* added error timeout

* fixes

* added indexing with algolia + fixes

* fix errors with algolia script

* redesign operation sections

* fix heading scroll

* design fixes

* fix padding

* fix padding + scroll issues

* fix scroll issues

* improve scroll performance

* fixes for safari

* optimization and fixes

* fixes to docs + details animation

* padding fixes for code block

* added tab animation

* fixed incorrect link

* added selection styling

* fix lint errors

* redesigned details component

* added detailed feedback form

* api reference fixes

* fix tabs

* upgrade + fixes

* updated documentation links

* optimizations to sidebar items

* fix spacing in sidebar item

* optimizations and fixes

* fix endpoint path styling

* remove margin

* final fixes

* change margin on small devices

* generated OAS

* fixes for mobile

* added feedback modal

* optimize dark mode button

* fixed color mode useeffect

* minimize dom size

* use new style system

* radius and spacing design system

* design fixes

* fix eslint errors

* added meta files

* change cron schedule

* fix docusaurus configurations

* added operating system to feedback data

* change content directory name

* fixes to contribution guidelines

* revert renaming content

* added api-reference to documentation workflow

* fixes for search

* added dark mode + fixes

* oas fixes

* handle bugs

* added code examples for clients

* changed tooltip text

* change authentication to card

* change page title based on selected section

* redesigned mobile navbar

* fix icon colors

* fix key colors

* fix medusa-js installation command

* change external regex in algolia

* change changeset

* fix padding on mobile

* fix hydration error

* update depedencies
This commit is contained in:
Shahed Nasser
2023-08-15 18:07:54 +03:00
committed by GitHub
parent 16249ec280
commit 914d773d3a
3270 changed files with 22075 additions and 192064 deletions
@@ -0,0 +1,25 @@
---
title: 'AwilixResolutionError: Could Not Resolve X'
---
import ServiceLifetimeSection from './awilix-resolution-error/_service-lifetime.md'
import CustomRegistrationSection from './awilix-resolution-error/_custom-registration.md'
import FreshInstallationSection from './awilix-resolution-error/_fresh-installation.md'
This troubleshooting guide will help you figure out the different situations that can cause an `AwilixResolutionError`.
## Option 1: Service Lifetime
<ServiceLifetimeSection />
---
## Option 2: Using Try-Catch Block with Custom Registration
<CustomRegistrationSection />
---
## Option 3: Error on A Fresh Installation
<FreshInstallationSection />
@@ -0,0 +1,27 @@
When you register a custom resource using a middleware, make sure that when you use it in a service's constructor you wrap it in a try-catch block. This can cause an error when the Medusa backend first runs, especially if the service is used within a subscriber. Subscribers are built the first time the Medusa backend runs, meaning that their dependencies are registered at that point. Since your custom resource hasn't been registered at this point, it will cause an `AwilixResolutionError` when the backend tries to resolve it.
For that reason, and to avoid other similar situations, make sure to always wrap your custom resources in a try-catch block when you use them inside the constructor of a service. For example:
<!-- eslint-disable prefer-rest-params -->
```ts
import { TransactionBaseService } from "@medusajs/medusa"
class CustomService extends TransactionBaseService {
constructor(container, options) {
super(...arguments)
// use the registered resource.
try {
container.customResource
} catch (e) {
// avoid errors when the backend first loads
}
}
}
export default CustomService
```
You can learn more about this in the [Middlewares documentation](../../development/endpoints/add-middleware.mdx).
@@ -0,0 +1,5 @@
If you get the error on a fresh installation of the Medusa backend, or you haven't made any customizations that would cause this error, try to remove the `node_modules` directory, then run the following command in the root directory of the Medusa backend to re-install the dependencies:
```bash npm2yarn
npm install
```
@@ -0,0 +1,34 @@
If you're registering a custom resource within a middleware, for example a logged-in user, then make sure that all services that are using it have their `LIFE_TIME` static property either set to `Lifetime.SCOPED` or `Lifetime.TRANSIENT`. This mainly applies for services in the core Medusa package, as, by default, their lifetime is `Lifetime.SINGLETON`.
For example:
```ts
import { Lifetime } from "awilix"
import {
ProductService as MedusaProductService,
} from "@medusajs/medusa"
// extending ProductService from the core
class ProductService extends MedusaProductService {
// The default life time for a core service is SINGLETON
static LIFE_TIME = Lifetime.SCOPED
// ...
}
export default ProductService
```
This may require you to extend a service as explained in [this documentation](../../development/services/extend-service.mdx) if necessary.
If you're unsure which service you need to change its `LIFE_TIME` property, it should be mentioned along with the `AwilixResolutionError` message. For example:
```bash noCopy noReport
AwilixResolutionError: Could not resolve 'loggedInUser'.
Resolution path: cartService -> productService -> loggedInUser
```
As shown in the resolution path, you must change the `LIFE_TIME` property of both `cartService` and `productService` to `Lifetime.SCOPED` or `Lifetime.TRANSIENT`.
You can learn about the service lifetime in the [Create a Service documentation](../../development/services/create-service.mdx).
@@ -0,0 +1,33 @@
---
title: 'Resolve Errors Installing Medusa CLI'
---
import PermissionErrorsSection from './cli-installation-errors/_permission-errors.md'
import PowershellErrorSection from './cli-installation-errors/_powershell-error.md'
import YarnError from './cli-installation-errors/_yarn-error.mdx'
In this document, you can find solutions to some common problems that occur when installing Medusas CLI Tool.
## NPM Error: EACCES Permissions Errors
<PermissionErrorsSection />
---
<!-- vale off -->
## Powershell Error: command not found: medusa
<!-- vale on -->
<PowershellErrorSection />
---
<!-- vale off -->
## Yarn Error: command not found: medusa
<!-- vale on -->
<YarnError />
@@ -0,0 +1,3 @@
If you install the Medusa CLI tool with NPM and get a permission error, NPM proposes as a solution either re-installing NPM with a node version manager (nvm), or manually setting npms default directory.
You can check out more information in [NPMs documentation](https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally).
@@ -0,0 +1,7 @@
If you're using Powershell and you installed the CLI tool, but when you try to use it you get the error:
```bash noReport
command not found: medusa
```
Try closing your Powershell window and opening a new one.
@@ -0,0 +1,21 @@
import Troubleshooting from '@site/src/components/Troubleshooting'
import PermissionErrorsSection from './_permission-errors.md'
import PowershellErrorSection from './_powershell-error.md'
import YarnErrorSection from './_yarn-error.mdx'
<Troubleshooting
sections={[
{
title: "NPM Error: EACCES Permissions Errors",
content: <PermissionErrorsSection />
},
{
title: "Powershell Error: command not found: medusa",
content: <PowershellErrorSection />
},
{
title: "Yarn Error: command not found: medusa",
content: <YarnErrorSection />
}
]}
/>
@@ -0,0 +1,31 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
If you install the Medusa CLI tool with Yarn, then try to use the CLI tool but get the error:
```bash noReport
command not found: medusa
```
You have to add Yarns install location to the PATH variable:
<Tabs groupId="operating-systems" isCodeTabs={true}>
<TabItem value="unix" label="MacOS / Linux" default>
```bash
export PATH="$(yarn global bin):$PATH"
```
</TabItem>
<TabItem value="windows" label="Windows">
```bash
# MAKE SURE TO INCLUDE %path%
setx path "%path%;c:\users\YOURUSERNAME\appdata\local\yarn\bin"
# YOURUSERNAME is your account username
```
</TabItem>
</Tabs>
You can learn more in [Yarns documentation](https://classic.yarnpkg.com/en/docs/cli/global#adding-the-install-location-to-your-path).
@@ -0,0 +1,9 @@
---
title: 'General Errors'
---
import ModuleXErrorSection from './common-installation-errors/_module-x-error.mdx'
## Resolve "Cannot find module X" Errors
<ModuleXErrorSection />
@@ -0,0 +1,25 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
This error can occur while installing any of Medusa's projects (for example, Next.js Starter Template). There is no specific cause to this error.
One way to resolve it is by removing the `node_modules` directory in the project and re-installing the dependencies:
<Tabs groupId="operating-systems" isCodeTabs={true}>
<TabItem value="unix" label="MacOS / Linux" default>
```bash
rm -rf node_modules
yarn install
```
</TabItem>
<TabItem value="windows" label="Windows">
```bash
rd /s /q node_modules
yarn install
```
</TabItem>
</Tabs>
@@ -0,0 +1,31 @@
---
title: 'CORS issues'
---
If you are experiencing connection issues when trying to access your Medusa backend from a storefront or the admin dashboard, it is most likely due to Cross-Origin Resource Sharing (CORS) issues.
You might see a log in your browser console, that looks like this:
![CORS error log](https://res.cloudinary.com/dza7lstvk/image/upload/v1668003322/Medusa%20Docs/Other/jnHK115_udgf2n.png)
In your `medusa-config.js` , you should ensure that you've configured your CORS settings correctly. By default, the Medusa starter runs on port `9000`, Medusa Admin runs on port `7000`, and the storefront starters run on port `8000`.
The default configuration uses the following CORS settings:
```js title=medusa-config.js
// CORS when consuming Medusa from admin
const ADMIN_CORS = process.env.ADMIN_CORS ||
"http://localhost:7000,http://localhost:7001"
// CORS to avoid issues when consuming Medusa from a client
const STORE_CORS =
process.env.STORE_CORS || "http://localhost:8000"
```
If you wish to run your storefront or Medusa admin on other ports, you should update the above settings accordingly.
---
## See Also
- [Configure your Medusa backend](../development/backend/configurations.md)
@@ -0,0 +1,16 @@
---
title: 'Common Create-React-App Errors'
---
import TypeError from './create-medusa-app-errors/_typeerror.md'
import OtherErrors from './create-medusa-app-errors/_other-errors.mdx'
## TypeError: cmd is not a function
<TypeError />
---
## Other Errors
<OtherErrors />
@@ -0,0 +1,39 @@
When using the `create-medusa-app` npx command, you might run into an NPM `EAGAIN` error. This error can randomly occur due to conflicting processes.
The easiest solution is to start the command over. Alternatively, if your setup crossed the "create database" point, you can manually perform the following steps in the directory of your created project. You can skip any steps that you're sure have been performed by `create-medusa-app`:
1\. Install dependencies:
```bash npm2yarn
npm install
```
2\. Build project:
```bash npm2yarn
npm run build
```
3\. Run migrations:
```bash
npx medusa migrations run
```
4\. Create an admin user:
```bash
npx medusa user -e user@test.com -p supersecret
```
5\. Optionally seed the database:
```bash
npx medusa seed -f ./data/seed.json
```
6\. Start project:
```bash
npx medusa develop
```
@@ -0,0 +1,31 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
As a last resort to resolve your issue, please try to clear your `npx` cache, as it could hold a cached version of `create-medusa-app` with errors.
<Tabs groupId="operating-systems" queryString="os">
<TabItem value="unix" label="MacOS and Linux" default>
Run the following command:
```bash
rm -rf "$(npm config get cache)/_npx"
```
Try running the `create-medusa-app` command again.
</TabItem>
<TabItem value="windows" label="Windows">
First, find the cache directory with the following command:
```bash
npm config get cache
```
The npx cache should be in the directory `<NPM_CACHE>/_npx`, where `<NPM_CACHE>` is the cache directory returned by the previous command. Delete that directory and try running the `create-medusa-app` command again.
</TabItem>
</Tabs>
If your issue persists, please try to search through [our GitHub issues](https://github.com/medusajs/medusa/issues) to see if there's a solution for your issue. If not, please [create an issue on GitHub](https://github.com/medusajs/medusa/issues/new?assignees=olivermrbl&labels=status:+needs+triaging,+type:+bug&template=bug_report.md&title=) and our team will help you resolve it soon.
@@ -0,0 +1,8 @@
This error typically occurs when you set up a Medusa project with `create-medusa-app` and try to run the Medusa backend.
To resolve this issue, make sure you change into the `backend` directory of the Medusa project you created before trying to start the Medusa backend:
```bash npm2yarn
cd backend
npx medusa develop
```
@@ -0,0 +1,17 @@
---
title: createCustomAdminHooks Error
---
If you've installed Medusa prior to v1.12.3 with the `create-medusa-app` command, then you try to update the `@medusajs/medusa` and `@medusajs/admin` to the latest `beta` versions, you might run into the following error when running your Medusa backend:
```bash
Module '"medusa-react"' has no exported member 'createCustomAdminHooks'.
```
This is because a previous version of `medusa-react` allowed creating custom hooks using `createCustomAdminHooks`. This has now changed to use different utility hooks, which you can learn about [the Medusa React documentation](../medusa-react/overview.mdx#custom-hooks).
To resolve this issue, you have the following options:
1. If you haven't used `createCustomAdminHooks` in your code, then you can delete the content of the `src/admin` directory which holds the widgets that create your onboarding flow, then try running your Medusa backend.
2. If you've used the `createCustomAdminHooks` in your code, refer to the [Medusa React](../medusa-react/overview.mdx#custom-hooks) to learn about the new utility hooks and how you can use them.
@@ -0,0 +1,23 @@
---
title: 'Database Errors'
---
import SaslSection from './database-errors/_sasl.md'
import ConnectionErrorSection from './database-errors/_connection-error.md'
import PrivilegesSection from './database-errors/_privileges.md'
## Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: Client password must be a string
<SaslSection />
---
## Error: connect ECONNREFUSED ::1:5432
<ConnectionErrorSection />
---
## Database User Privileges
<PrivilegesSection />
@@ -0,0 +1,10 @@
When you start your Medusa backend you may run into the following error:
```bash
Error: connect ECONNREFUSED ::1:5432
```
This error occurs because the backend couldn't connect to the PostgreSQL database. The issue could be one of the following:
1. PostgreSQL server isn't running. Make sure it's always running while the Medusa backend is running.
2. The connection URL to your PostgreSQL database is incorrect. This could be because of incorrect credentials, port number, or connection URL format. The format should be `postgres://[user][:password]@[host][:port]/[dbname]`. Make sure that the connection URL format is correct, and the credentials passed in the URL are correct. You can learn more about formatting the connection URL [here](../../development/backend/configurations.md#postgresql-configurations)
@@ -0,0 +1,3 @@
The database user you use in the `database_url` Medusa backend configuration must have create privileges. Otherwise, you'll face problems when running migrations.
If you're using the `postgres` superuser, then it should have these privileges by default. Otherwise, make sure to grant your user create privileges. You can learn how to do that in [PostgreSQL's documentation](https://www.postgresql.org/docs/current/ddl-priv.html).
@@ -0,0 +1,18 @@
You may get the following error while running `medusa new` or while running integration tests during [local development](../../development/fundamentals/local-development.md):
```bash
Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string
```
If the error occurs while running `medusa new` and you've selected to enter your database credentials, either:
1. Make sure your database credentials are correct;
2. Or choose the Skip option to skip entering your database credentials.
If the error occurs while running integration tests, make sure the following variable is set in your system's environment variable:
```bash
DB_HOST=<YOUR_DB_HOST>
DB_USERNAME=<YOUR_DB_USERNAME>
DB_PASSWORD=<YOUR_PASSWORD>
```
@@ -0,0 +1,29 @@
---
title: 'Troubleshooting Documentation Errors'
---
## React Hook Errors
If you have installed the dependencies in the root of the Medusa repository (that is, if you have a `node_modules` directory at the root of the Medusa repository), this will cause an error when running the documentation website.
This is because the content resides in `docs/content`. When that content is being imported from there, a mix up can happen between the dependencies in the root of the Medusa repository and the dependencies in `www/docs` which causes an `invalid hook call` error.
For that reason, when the `start` and `build` scripts in `www/docs` are used, the `clean-node-modules` script is called. This script deleted the `node_modules` directory in the root of the Medusa repository.
---
## Out of Memory Error
If you receive the following error when you run the `build` command in `www/docs`:
```bash noReport
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
```
This is because of an [ongoing issue in Docusaurus that occurs in large documentation websites](https://github.com/facebook/docusaurus/issues/4765). As it is still not resolved, the workaround would be to change the memory limit for Node.js while running the `build` command.
To do that, run the `build` command with the `NODE_OPTIONS` environment variable set:
```bash npm2yarn
NODE_OPTIONS="--max-old-space-size=8192" npm run build
```
@@ -0,0 +1,18 @@
---
title: 'EADDRINUSE Error'
---
When you run your backend you may run to an error similar to the following:
```bash
code: 'EADDRINUSE',
errno: -48,
syscall: 'Listen',
address: '::',
port: 9000
```
This means that there's another process running at port `9000`. You need to either:
- Change the default port used by the Medusa backend. You can do that by setting the `PORT` environment variable to a new port. When you do this, make sure to change the port used in other apps that interact with your Medusa backend, such as in your [admin](../admin/quickstart.mdx#build-command-options) or [storefront](../starters/nextjs-medusa-starter.mdx#changing-medusa-backend-url).
- Terminate other processes running on port `9000`.
@@ -0,0 +1,19 @@
---
title: 'Errors After Update'
---
If you run into errors after updating Medusa and its dependencies, it's highly recommended to check the [Upgrade Guides](../upgrade-guides/index.mdx) if there is a specific guide for your version. These guides include steps required to perform after upgrading Medusa.
If there's no upgrade guide for your version, make sure that you ran the `migrations` command in the root directory of your Medusa backend:
```bash
npx medusa migrations run
```
This ensures your backend has the latest database structure required. Then, try running your Medusa backend again and check whether the same error occurs.
---
## See Also
- [Migrations](../development/entities/migrations/overview.mdx)
@@ -0,0 +1,35 @@
---
title: 'Payment Processor not showing in checkout'
---
You add payment processors to your Medusa instance by adding them as plugins in `medusa-config.js`:
```js title=medusa-config.js
const plugins = [
// ...
{
resolve: `medusa-payment-stripe`,
options: {
api_key: STRIPE_API_KEY,
webhook_secret: STRIPE_WEBHOOK_SECRET,
},
},
]
```
And installing them with your favourite package manager:
```bash npm2yarn
npm install medusa-payment-stripe
```
However, to also show them as part of your checkout flow you need to add them to your regions.
Then, refer to [this user guide](../user-guide/regions/providers.mdx) to learn how to enable the payment processor in a region.
---
## See Also
- [Install Stripe](../plugins/payment/stripe.mdx)
- [Payment Architecture Overview](../modules/carts-and-checkout/payment.md)
@@ -0,0 +1,39 @@
---
title: 'Redis not emitting events'
---
:::note
This troubleshooting guide only applies to Medusa backends using versions before v1.8 of the core Medusa package.
:::
When you create a new Medusa backend, Redis is disabled by default. Instead, a fake Redis backend is used that allows you to start your project but does not actually emit any events.
To enable a real Redis backend, you need to install Redis on your machine and configure it with Medusa.
You can learn how to [install Redis in the Set Up your Development Environment documentation](../development/backend/prepare-environment.mdx#redis).
After installing it, make sure to configure your Medusa backend to use Redis:
```jsx title=medusa-config.js
module.exports = {
projectConfig: {
// ...
redis_url: REDIS_URL,
},
}
```
By default, Medusa connects to Redis over the URL `redis://localhost:6379`. If you need to change that URL, set the following environment variable:
```bash
REDIS_URL=<YOUR_REDIS_URL>
```
---
## See Also
- [Set up your development environment](../development/backend/prepare-environment.mdx)
- [Configure the Medusa backend](../development/backend/configurations.md)
@@ -0,0 +1,18 @@
---
title: 'S3 Plugin ACL Error'
---
If you're using the [S3 Plugin](../plugins/file-service/s3.mdx) and, when you upload an image, you receive the following error on your Medusa backend:
```bash noReport
AccessControlListNotSupported: The bucket does not allow ACLs
```
Try the following:
1. Go to your S3 Bucket, then choose the Permissions tab.
2. Scroll to Object Ownership and click the Edit button.
3. Select ACLs enabled and choose for Object Ownership the radio button Bucket owner preferred.
4. Click the Save Changes.
Try uploading again after making this change. Upload should be successful.
@@ -0,0 +1,22 @@
---
title: 'Signing in to Medusa Admin'
---
If you've created a new Medusa backend and used the `seed` command, the default credentials are:
```bash noReport
email: admin@medusa-test.com
password: supersecret
```
Alternatively, you can create your own users using the Medusa CLI tool:
```bash
npx medusa user -e some@email.com -p somepassword
```
---
## See Also
- [Medusa CLI tool reference](../cli/reference.mdx)
@@ -0,0 +1,11 @@
---
title: 'Upgrading Beta Versions'
---
If you're using `beta` versions of Medusa packages, such as the `@medusajs/medusa` or `@medusajs/admin` packages, simply updating to the latest `beta` version might not work and you'll end up with the same version. This could be due to the version in `yarn.lock` or `package-lock.json` not updating properly.
To resolve this issue, try the following:
- Remove the `yarn.lock` or `package-lock.json` file in your project.
- Remove the `node_modules` directory
- Install again.