Skip to content

Commit e9cdd63

Browse files
committed
Merge branch 'main' of github.com:strapi/documentation
2 parents 42b9210 + 437b666 commit e9cdd63

File tree

14 files changed

+1132
-119
lines changed

14 files changed

+1132
-119
lines changed

docusaurus/docs/dev-docs/api/plugins/admin-panel-api.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
---
22
sidebar_label: Admin Panel API
3-
pagination_prev: dev-docs/plugins/developing-plugins
3+
pagination_prev: dev-docs/plugins/development/plugin-structure
4+
toc_max_heading_level: 4
45
---
56

67
# Admin Panel API for plugins
78

8-
A Strapi [plugin](/dev-docs/plugins) can interact with both the [back end](/dev-docs/api/plugins/server-api) or the front end of the Strapi app. The Admin Panel API is about the front end part, i.e. it allows a plugin to customize Strapi's [admin panel](/user-docs/intro).
9+
A Strapi [plugin](/dev-docs/plugins) can interact with both the [back end](/dev-docs/api/plugins/server-api) and the front end of a Strapi application. The Admin Panel API is about the front end part, i.e. it allows a plugin to customize Strapi's [admin panel](/user-docs/intro).
910

1011
The admin panel is a [React](https://reactjs.org/) application that can embed other React applications. These other React applications are the admin parts of each Strapi plugin.
1112

12-
To create a plugin that interacts with the Admin Panel API:
13-
14-
1. Create an [entry file](#entry-file).
15-
2. Within this file, declare and export a plugin interface that uses the [available actions](#available-actions).
16-
3. Require this plugin interface in a `strapi-admin.js` file at the root of the plugin package folder:
13+
:::prerequisites
14+
You have [created a Strapi plugin](/dev-docs/plugins/development/create-a-plugin).
15+
:::
1716

18-
```js title="[plugin-name]/strapi-admin.js"
17+
The Admin Panel API includes:
1918

20-
'use strict';
19+
- an [entry file](#entry-file) which exports the required interface,
20+
- [lifecycle functions](#lifecycle-functions) and the `registerTrad()` [async function](#async-function),
21+
- and several [specific APIs](#available-actions) for your plugin to interact with the admin panel.
2122

22-
module.exports = require('./admin/src').default;
23-
```
23+
:::note
24+
The whole code for the admin panel part of your plugin could live in the `/strapi-admin.js|ts` or `/admin/src/index.js|ts` file. However, it's recommended to split the code into different folders, just like the [structure](/dev-docs/plugins/development/plugin-structure) created by the `strapi generate plugin` CLI generator command.
25+
:::
2426

2527
## Entry file
2628

docusaurus/docs/dev-docs/api/plugins/server-api.md

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@ title: Server API for plugins
33
sidebar_label: Server API
44
displayed_sidebar: devDocsSidebar
55
description: Strapi's Server API for plugins allows a Strapi plugin to customize the back end part (i.e. the server) of your application.
6-
sidebarDepth: 3
7-
86
---
97

108
# Server API for plugins
119

12-
A Strapi [plugin](/dev-docs/plugins) can interact with the backend or the [frontend](/dev-docs/api/plugins/admin-panel-api) of the Strapi application. The Server API is about the backend part.
10+
A Strapi [plugin](/dev-docs/plugins) can interact with both the back end and the [front end](/dev-docs/api/plugins/admin-panel-api) of a Strapi application. The Server API is about the back-end part, i.e. how the plugin interacts with the server part of a Strapi application.
11+
12+
:::prerequisites
13+
You have [created a Strapi plugin](/dev-docs/plugins/development/create-a-plugin).
14+
:::
15+
16+
The Server API includes:
17+
18+
- an [entry file](#entry-file) which export the required interface,
19+
- [lifecycle functions](#lifecycle-functions),
20+
- a [configuration](#configuration) API,
21+
- the ability to add [cron](#cron) jobs,
22+
- and the ability to [customize all elements of the back-end server](#backend-customization).
1323

14-
Creating and using a plugin interacting with the Server API consists of 2 steps:
24+
Once you have declared and exported the plugin interface, you will be able to [use the plugin interface](#usage).
1525

16-
1. Declare and export the plugin interface within the [`strapi-server.js` entry file](#entry-file)
17-
2. [Use the exported interface](#usage)
26+
:::note
27+
The whole code for the server part of your plugin could live in the `/strapi-server.js|ts` or `/server/index.js|ts` file. However, it's recommended to split the code into different folders, just like the [structure](/dev-docs/plugins/development/plugin-structure) created by the `strapi generate plugin` CLI generator command.
28+
:::
1829

1930
## Entry file
2031

@@ -81,7 +92,7 @@ module.exports = () => ({
8192

8293
## Configuration
8394

84-
`config` stores the default plugin configuration.
95+
`config` stores the default plugin configuration. It loads and validates the configuration inputted from the user within the [`./config/plugins.js` configuration file](/dev-docs/configurations/plugins).
8596

8697
**Type**: `Object`
8798

@@ -113,6 +124,10 @@ Once defined, the configuration can be accessed:
113124
- with `strapi.plugin('plugin-name').config('some-key')` for a specific configuration property,
114125
- or with `strapi.config.get('plugin.plugin-name')` for the whole configuration object.
115126

127+
:::tip
128+
Run `yarn strapi console` or `npm run strapi console` to access the strapi object in a live console.
129+
:::
130+
116131
## Cron
117132

118133
The `cron` object allows you to add cron jobs to the Strapi instance.
@@ -155,6 +170,12 @@ strapi.cron.jobs
155170

156171
## Backend customization
157172

173+
All elements of the back-end server of Strapi can be customized through a plugin using the Server API.
174+
175+
:::prerequisites
176+
To better understand this section, ensure you have read through the [back-end customization](/dev-docs/backend-customization) documentation of a Strapi application.
177+
:::
178+
158179
### Content-types
159180

160181
An object with the [content-types](/dev-docs/backend-customization/models) the plugin provides.
@@ -461,50 +482,54 @@ An object with the [middlewares](/dev-docs/configurations/middlewares) the plugi
461482

462483
**Example:**
463484

464-
```js title="./src/plugins/my-plugin/strapi-server.js"
465-
466-
"use strict";
467-
468-
module.exports = require('./server');
469-
```
485+
```js title="./src/plugins/my-plugin/server/middlewares/your-middleware.js"
470486

471-
```js title="./src/plugins/my-plugin/server/index.js"
472-
473-
const middlewares = require('./middlewares');
474-
module.exports = () => ({
475-
middlewares,
476-
});
487+
/**
488+
* The your-middleware.js file
489+
* declares a basic middleware function and exports it.
490+
*/
491+
'use strict';
492+
module.exports = async (ctx, next) => {
493+
console.log("your custom logic")
494+
await next();
495+
}
477496
```
478497

479498
```js title="./src/plugins/my-plugin/server/middlewares/index.js"
480499

481-
const middlewareA = require('./middleware-a');
482-
const middlewareB = require('./middleware-b');
500+
/**
501+
* The middleware function previously created
502+
* is imported from its file and
503+
* exported by the middlewares index.
504+
*/
505+
'use strict';
506+
const yourMiddleware = require('./your-middleware');
483507

484508
module.exports = {
485-
middlewareA,
486-
middlewareB,
509+
yourMiddleware
487510
};
488511
```
489512

490-
```js title="./src/plugins/my-plugin/server/middlewares/middleware-a.js"
513+
```js title="./src/plugins/my-plugin/server/register.js"
491514

492-
module.exports = (options, { strapi }) => {
493-
return async (ctx, next) => {
494-
const start = Date.now();
495-
await next();
496-
const delta = Math.ceil(Date.now() - start);
515+
/**
516+
* The middleware is called from
517+
* the plugin's register lifecycle function.
518+
*/
519+
'use strict';
520+
const middlewares = require('./middlewares');
497521

498-
strapi.log.http(`${ctx.method} ${ctx.url} (${delta} ms) ${ctx.status}`);
499-
};
522+
module.exports = ({ strapi }) => {
523+
strapi.server.use(middlewares.yourMiddleware);
500524
};
501525
```
502526

503527
## Usage
504528

505-
Once a plugin is exported and loaded into Strapi, its features are accessible in the code through getters. The Strapi instance (`strapi`) exposes top-level getters and global getters.
529+
Once a plugin is exported and loaded into Strapi, its features are accessible in the code through getters. The Strapi instance (`strapi`) exposes both top-level getters and global getters:
506530

507-
While top-level getters imply chaining functions, global getters are syntactic sugar that allows direct access using a feature's uid:
531+
- top-level getters imply chaining functions<br/>(e.g., `strapi.plugin('the-plugin-name').controller('the-controller-name'`),
532+
- global getters are syntactic sugar that allows direct access using a feature's uid<br/>(e.g., `strapi.controller('plugin::plugin-name.controller-name')`).
508533

509534
```js
510535
// Access an API or a plugin controller using a top-level getter

docusaurus/docs/dev-docs/configurations/functions.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ The `./src/index.js` file (or `./src/index.ts` file in a [TypeScript-based](/dev
1111

1212
The functions can be synchronous, asynchronous, or return a promise.
1313

14+
``` mermaid
15+
flowchart TB
16+
A([The Strapi application starts.]) --> B{"register()"}
17+
B -- The Strapi application is setup. --> C
18+
C{"bootstrap()"} -- The Strapi back-end server starts. --> D
19+
D(Request)
20+
D
21+
click B "#register"
22+
click C "#bootstrap"
23+
click D "/dev-docs/backend-customization/requests-responses"
24+
```
25+
1426
## Synchronous function
1527

1628
<Tabs groupId="js-ts">
@@ -147,6 +159,8 @@ It can be used to:
147159
- load some [environment variables](/dev-docs/configurations/environment)
148160
- register a [custom field](/dev-docs/custom-fields) that would be used only by the current Strapi application.
149161

162+
`register()` is the very first thing that happens when a Strapi application is starting. This happens _before_ any setup process and you don't have any access to database, routes, policies, or any other backend server elements within the `register()` function.
163+
150164
## Bootstrap
151165

152166
The `bootstrap` lifecycle function, found in `./src/index.js` (or in `./src/index.ts`), is called at every server start.
@@ -157,6 +171,12 @@ It can be used to:
157171
- fill the database with some necessary data
158172
- declare custom conditions for the [Role-Based Access Control (RBAC)](/dev-docs/configurations/rbac) feature
159173

174+
The `bootstrapi()` function is run _before_ the back-end server starts but _after_ the Strapi application has setup, so you have access to anything from the `strapi` object.
175+
176+
:::tip
177+
You can run `yarn strapi console` (or `npm run strapi console`) in the terminal and interact with the `strapi` object.
178+
:::
179+
160180
## Destroy
161181

162182
The `destroy` function, found in `./src/index.js` (or in `./src/index.ts`), is an asynchronous function that runs before the application gets shut down.
Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
title: Developing plugins
3-
# description: todo
3+
description: Generation introduction about Strapi plugins development
44
displayed_sidebar: devDocsSidebar
55
pagination_prev: dev-docs/plugins
6-
pagination_next: dev-docs/api/plugins/admin-panel-api
6+
pagination_next: dev-docs/plugins/development/create-a-plugin
77
---
88

99
# Developing Strapi plugins
@@ -12,76 +12,38 @@ pagination_next: dev-docs/api/plugins/admin-panel-api
1212
This section is about developing Strapi plugins to use them as local plugins or to submit them to the Marketplace. Not what you're looking for? Read the [plugins introduction](/dev-docs/plugins) and find your use case and recommended section to read from there.
1313
:::
1414

15-
Strapi allows the development of plugins that work exactly like the built-in plugins or 3rd-party plugins available from the Marketplace. Once created, your plugin can be:
15+
Strapi allows the development of plugins that work exactly like the built-in plugins or 3rd-party plugins available from the [Marketplace](https://market.strapi.io). Once created, your plugin can be:
1616

1717
- used as a local plugin, working only with a specific Strapi project,
18-
- or submitted to the [Marketplace](https://market.strapi.io) to be shared with the community.
18+
- or [submitted to the Marketplace](https://market.strapi.io/submit-plugin) to be shared with the community.
1919

20-
The first step to developing a Strapi plugin is to create it using the CLI-based generator. Then you'll be able to leverage the [plugin APIs](#plugin-apis) to add features to your plugin.
20+
👉 To start developing a Strapi plugin:
2121

22-
## Plugin creation
23-
24-
Strapi provides a [command line interface (CLI)](/dev-docs/cli) for creating plugins. To create a plugin:
25-
26-
1. Navigate to the root of a Strapi project.
27-
2. Run `yarn strapi generate` or `npm run strapi generate` in a terminal window to start the interactive CLI.
28-
3. Choose "plugin" from the list, press Enter, and give the plugin a name in kebab-case (e.g. `my-plugin`)
29-
4. Choose either `JavaScript` or `TypeScript` for the plugin language.
30-
5. Create a plugins configuration file if one does not already exist: `./config/plugins.js` or `./config/plugins.ts` for TypeScript projects.
31-
6. Enable the plugin by adding it to the [plugins configurations](/dev-docs/configurations/plugins) file:
32-
33-
<Tabs>
34-
<TabItem value="js" label="JavaScript">
35-
36-
```js title="./config/plugins.js"
37-
module.exports = {
38-
// ...
39-
"my-plugin": {
40-
enabled: true,
41-
resolve: "./src/plugins/my-plugin", // path to plugin folder
42-
},
43-
// ...
44-
};
45-
```
46-
47-
</TabItem>
48-
49-
<TabItem value="ts" label="TypeScript">
50-
51-
```js title=./config/plugins.ts
52-
export default {
53-
// ...
54-
"my-plugin": {
55-
enabled: true,
56-
resolve: "./src/plugins/my-plugin", // path to plugin folder
57-
},
58-
// ...
59-
};
60-
```
61-
62-
</TabItem>
63-
</Tabs>
64-
65-
7. Run `npm install` or `yarn` in the newly-created plugin directory.
66-
8. (_TypeScript-specific_) Run `yarn build` or `npm run build` in the plugin directory. This step transpiles the TypeScript files and outputs the JavaScript files to a `dist` directory that is unique to the plugin.
67-
9. Run `yarn build` or `npm run build` at the project root.
68-
10. Run `yarn develop` or `npm run develop` at the project root.
69-
70-
Plugins created using the preceding directions are located in the `plugins` directory of the application (see [project structure](/dev-docs/project-structure)).
71-
72-
:::note
73-
During plugin development it is helpful to use the `--watch-admin` flag to toggle hot reloading of the admin panel. See the [Admin panel customization](/dev-docs/admin-panel-customization) documentation for more details. (TypeScript specific) While developing your plugin, you can run `yarn develop --watch-admin` or `npm run develop -- --watch-admin` in the plugin directory to watch the changes to the TypeScript server files. From 4.15.1 this is no longer required.
74-
:::
22+
1. [Create a plugin](/dev-docs/plugins/development/create-a-plugin) using the CLI-based generator.
23+
2. Learn more about the [structure of a plugin](/dev-docs/plugins/development/plugin-structure).
24+
3. Get an overview of the [plugin APIs](#plugin-apis) to add features to your plugin.
25+
4. Read some [guides](#guides) based on your use case(s).
7526

7627
## Plugin APIs
7728

7829
Strapi provides the following programmatic APIs for plugins to hook into some of Strapi's features:
7930

8031
<CustomDocCardsWrapper>
81-
<CustomDocCard emoji="" title="Server API" description="Use the Server API to have your plugin interact with the backend server of Strapi." link="/dev-docs/api/plugins/server-api" />
8232
<CustomDocCard emoji="" title="Admin Panel API" description="Use the Admin Panel API to have your plugin interact with the admin panel of Strapi." link="/dev-docs/api/plugins/admin-panel-api" />
33+
<CustomDocCard emoji="" title="Server API" description="Use the Server API to have your plugin interact with the backend server of Strapi." link="/dev-docs/api/plugins/server-api" />
8334
</CustomDocCardsWrapper>
8435

8536
:::strapi Custom fields plugins
8637
Plugins can also be used to add [custom fields](/dev-docs/custom-fields) to Strapi.
8738
:::
39+
40+
## Guides
41+
42+
<CustomDocCard small emoji="💁" title="How to store and access data from a Strapi plugin" description="" link="/dev-docs/plugins/guides/store-and-access-data" />
43+
<CustomDocCard small emoji="💁" title="How to pass data from the backend server to the admin panel with a plugin" description="" link="/dev-docs/plugins/guides/pass-data-from-server-to-admin" />
44+
45+
<br />
46+
47+
:::strapi Additional resources
48+
The Strapi blog features a [tutorial series](https://strapi.io/blog/how-to-create-a-strapi-v4-plugin-server-customization-4-6) about creating a Strapi v4 'Todo' plugin. The [contributors documentation](https://contributor.strapi.io/) can also include additional information useful while developing a Strapi plugin.
49+
:::

0 commit comments

Comments
 (0)