Strapi
Current state: Stable
The Strapi class is the main object used in Strapi projects.
An instance of Strapi class is available as a global in any Strapi project: global.strapi
.
Class: Strapi
new Strapi(opts)
opts
: <Object> Options that can be used on Strapi startupautoReload
: <Boolean> Default: true- If false, deactivate auto reload
- If you modify any file in your Strapi project, it reloads your nodejs app
- If any content-type is changed, it will reload the nodejs app
serveAdminPanel
: <Boolean> Default: true- Should the admin panel be loaded and serve as a web client
- The admin panel build will not be delivered if false
appDir
: <String> Default:process.cwd()
- The directory relative or absolute path where Strapi will write every file (schemas, generated APIs, controllers or services)
distDir
: <String> Default: appDir value- The directory relative or absolute path where Strapi will read configurations, schemas and any compiled code
Instances of the Strapi class can be created using the new keyword.
const strapiInstance = new Strapi();
strapi.container
The container provides a simple and efficient way to register and manage resources, making it easy to access and use them throughout the application. By registering a registry with the container, it can be easily retrieved by other parts of the application, making it a powerful tool for organizing and reusing code across the entire codebase.
See Container.
strapi.dirs
- <Object>
Stored paths of file system.
dirs.dist
: <StrapiPathObject>- Build folder
dirs.app
: <StrapiPathObject>- Sources folder
dirs.static
: <Object> Define path to directories involving web client displaypublic
: <String> Path to the folder to serve publicly (like files, images, etc..)
StrapiPathObject
- <Object>
A set of paths to specific Strapi project parts.
root
: <String> Root pathsrc
: <String> Sources route path to project filesapi
: <String> Path to the folder containing project developers' API files (content-types, controllers, services, routes, etc..)components
: <String> Path to the folder containing project developers' componentspolicies
: <String> Path to the folder where the Strapi project developers' policies are stored- A set of functions that check the state of the data and prevent the access to the API accordingly
middlewares
: <String> Path to the folder where the Strapi project developers' middlewares are stored- A set of function that wrap around routes and requests
config
: <String> Path to the folder containing project developers' config files
strapi.isLoaded
- <Boolean>
true
: Everything (allregister
andbootstrap
functions available in your strapi project) has been loadedfalse
: There is something loading
Note: register
functions are called before the bootstrap
functions.
strapi.reload()
Reload the app.
This function defines itself at the construction of the Strapi class.
strapi.server
Strapi server object.
strapi.fs
Wrapper around FS NodeJS module.
strapi.eventHub
The strapi.eventHub
object is used to manipulate events within a Strapi project. It is an instance of the built-in EventEmitter class from Node.js, which provides a simple way to emit and listen for events.
The strapi.eventHub
object is created using the createEventHub()
function in the EventHub module of the Strapi core. This function returns a new instance of the EventHub class, which extends the EventEmitter class and adds some additional functionality specific to Strapi.
Examples:
// Listen for a 'user.updated' event and log the data
strapi.eventHub.on('user.updated', (data) => {
console.log(`User ${data.id} has been updated`);
});
// Emit a 'user.created' event with some data
strapi.eventHub.emit('user.created', { username: 'johndoe', email: 'johndoe@example.com' });
In this example, we are emitting a user.created
event with some data attached to it, and then listening for a user.updated event and logging the data. These events can be used to trigger actions within the Strapi application or to communicate with external systems.
For more information on how to use the EventEmitter class and its methods, see the Node.js documentation.
strapi.startupLogger
Object containing predefined logger functions. Used for Strapi startup. (do not use as a logger elsewhere)
strapi.log
A logger provided by Strapi that uses the Winston logging library. It is the result of calling the winston.createLogger()
function with the configuration defined by the user of the Strapi application.
The logger provides various methods for logging messages at different levels of severity, including error, warn, info, verbose, debug, and silly. The logging level can be set via the configuration to control which messages are logged.
Examples
// Log an error message
strapi.log.error('Failed to start server', { error: err });
// Log a warning message
strapi.log.warn('Server is running in development mode');
// Log an informational message
strapi.log.info(`Server started on port ${PORT}`);
// Log a verbose message
strapi.log.verbose('Application state', { user: currentUser });
// Log a debug message
strapi.log.debug('API request received', { method: req.method, path: req.path });
// Log a silly message
strapi.log.silly('Entered loop', { count: i });
In these examples, we are logging messages at different levels of severity, including error, warn, info, verbose, debug, and silly. We are also passing in metadata as an object in the second parameter of each logging method.
The messages logged by strapi.log will be output according to the logging configuration set by the user of the Strapi application. This configuration determines which messages are logged and where they are logged (e.g. console, file, etc.).
strapi.cron
Module to schedule cron jobs for Strapi project. It is an instance of a custom Cron object.
strapi.telemetry
The strapi.telemetry
property provides access to the telemetry service instance. This service collects anonymous usage data about your Strapi application to help the Strapi team improve the product.
By default, the telemetry service is enabled, but you can disable it by setting the telemetryDisabled property to true in your application's package.json file, or by setting the STRAPI_TELEMETRY_DISABLED
environment variable to true. You can also disable telemetry programmatically by setting the isDisabled property of the strapi.telemetry
instance to true.
strapi.requestContext
- <Object> Context Storage
run(store, cb)
: <Function>store
: <Any> Value that should be retrievedcb
: <Function> Callback
get()
<Function>
The request context stores the ctx object from KoaJS on each request. This allows users to have access to the context from anywhere through the Strapi instance.
strapi.customFields
- <Object>
register(customField)
: <Function> Register a new custom field
This property is a shortcut to strapi.container.get('custom-fields').add(customField)
.
Examples
strapi.customFields.register({
name: 'color',
plugin: 'color-picker',
type: 'string',
});
strapi.config
- <Object>
Shortcut to strapi.container.get('config')
.
See the config container.
strapi.services
- <Object[]>
Shortcut to strapi.container.get('services').getAll()
.
See the services' container.
strapi.service(uid)
uid
: <String>
Shortcut to strapi.container.get('services').get(uid)
.
See the services' container.
strapi.controllers
- <Object[]>
Shortcut to strapi.container.get('controllers').getAll()
.
See the controllers' container.
strapi.controller(uid)
uid
: <String>
Shortcut to strapi.container.get('controllers').get(uid)
.
See the controllers' container.
strapi.contentTypes
- <Object[]>
Shortcut to strapi.container.get('content-types').getAll()
.
See the content-types' container.
strapi.contentType(name)
name
: <String>
Shortcut to strapi.container.get('content-types').get(name)
.
See the content-types' container.
strapi.policies
- <Object[]>
Shortcut to strapi.container.get('policies').getAll()
.
See the policies' container.
strapi.policy(name)
name
: <String>
Shortcut to strapi.container.get('policies').get(name)
.
See the policies' container.
strapi.middlewares
- <Object[]>
Shortcut to strapi.container.get('middlewares').getAll()
.
See the middlewares container.
strapi.middleware(name)
name
: <String>
Shortcut to strapi.container.get('middlewares').get(name)
.
See the middlewares container.
strapi.plugins
- <Object[]>
Shortcut to strapi.container.get('plugins').getAll()
.
See the plugins' container.
strapi.plugin(name)
name
: <String>
Shortcut to strapi.container.get('plugins').get(name)
.
See the plugins' container.
strapi.hooks
- <Object[]>
Shortcut to strapi.container.get('hooks').getAll()
.
See the hooks' container.
strapi.hook(name)
name
: <String>
Shortcut to strapi.container.get('hooks').get(name)
.
See the hooks' container.
strapi.api
- <Object[]>
Shortcut to strapi.container.get('apis').getAll()
.
See the apis container.
strapi.auth
- <Object>
Shortcut to strapi.container.get('auth')
.
See the auth' container.
strapi.contentAPI
- <Object>
Shortcut to strapi.container.get('content-api')
.
See the content-api container.
strapi.sanitizers
- <Object>
Shortcut to strapi.container.get('sanitizers')
.
See the sanitizers' container.
strapi.validators
- <Object>
Shortcut to strapi.container.get('validators')
.
See the validators' container.
strapi.start()
- Returns: Promise
TODO
strapi.destroy()
- Returns: Promise
TODO
strapi.sendStartupTelemetry()
TODO
strapi.openAdmin({ isInitialized })
- Returns: Promise
TODO
strapi.postListen()
- Returns: Promise
TODO
strapi.listen()
- Returns: Promise
TODO
strapi.stopWithError()
TODO
strapi.stop(exitCode)
TODO
strapi.loadAdmin()
- Returns: Promise
TODO
strapi.loadPlugins()
- Returns: Promise
TODO
strapi.loadPolicies()
- Returns: Promise
TODO
strapi.loadAPIs()
- Returns: Promise
TODO
strapi.loadComponents()
- Returns: Promise
TODO
strapi.loadMiddlewares()
- Returns: Promise
TODO
strapi.loadApp()
- Returns: Promise
TODO
strapi.loadSanitizers()
- Returns: Promise
TODO
strapi.registerInternalHooks()
TODO
strapi.register()
- Returns: Promise
TODO
strapi.bootstrap()
- Returns: Promise
TODO
strapi.load()
- Returns: Promise
TODO
strapi.startWebhooks()
- Returns: Promise
TODO
strapi.reload()
TODO
strapi.runLifecyclesFunctions()
- Returns: Promise
TODO
strapi.getModel(uid)
uid
: <String>
TODO
strapi.query(uid)
uid
: <String>
TODO
Strapi containers
The strapi containers are accessible via strapi.container.get('name-of-the-container')
.
config
- <Object>
get(path, defaultValue)
: <Function>path
: <String>defaultValue
: <Any>- Returns: <Any> - The value located at
path
or, if undefined,defaultValue
.
set(path, value)
: <Function>path
: <String> - Where the value should be storedvalue
: <Any>
has(path)
: <Function>path
: <String>- Returns: <Boolean> - Does the
path
match a value stored in the config container.
launchedAt
: <Number> Default:Date.now()
Date in milliseconds when the server has startedserveAdminPanel
: <Boolean> Default:true
See Strapi constructor optionsautoReload
: <Boolean> Default:false
See Strapi constructor optionsenvironment
: <String> - process.env.NODE_ENVuuid
: <String> - string extracted frompackage.json
located instrapi.uuid
packageJsonStrapi
: <Object> - object extracted frompackage.json
located instrapi
(except uuid)info
: <Object>- everything stored in the
package.json
strapi
: <String> - Current version of Strapi
- everything stored in the
Every file stored under the config
folder will be injected in this config container object.
services
TODO
controllers
TODO
content-types
TODO
policies
TODO
plugins
TODO
hooks
TODO
apis
TODO
auth
TODO
content-api
TODO
sanitizers
TODO
validators
TODO