Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs review #6848

Merged
merged 8 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ module.exports = {
['/v3.x/getting-started/contributing', 'Contributing'],
['/v3.x/getting-started/troubleshooting', 'Troubleshooting'],
'/v3.x/getting-started/quick-start',
'/v3.x/getting-started/quick-start-tutorial',
],
},
{
Expand Down
4 changes: 3 additions & 1 deletion docs/v3.x/concepts/configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Instead of writing those credentials into your configuration files, you can defi

**Example**

**Path —** `.env`

```
DATABASE_PASSWORD=acme
```
Expand All @@ -70,7 +72,7 @@ Now you can access those variables in your configuration files and application.

In your configuration files you will have access to a `env` utility that allows defining defaults and casting values.

`config/database.js`
**Path —** `./config/database.js`

```js
module.exports = ({ env }) => ({
Expand Down
88 changes: 87 additions & 1 deletion docs/v3.x/concepts/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
- `parseMultipartData`: This function parses Strapi's formData format.
- `sanitizeEntity`: This function removes all private fields from the model and its relations.

#### Collection Type

:::: tabs

::: tab find
Expand Down Expand Up @@ -194,7 +196,7 @@ const { sanitizeEntity } = require('strapi-utils');

module.exports = {
/**
* delete a record.
* Delete a record.
*
* @return {Object}
*/
Expand All @@ -212,6 +214,90 @@ module.exports = {

::::

#### Single Type

:::: tabs

::: tab find

#### `find`

```js
const { sanitizeEntity } = require('strapi-utils');

module.exports = {
/**
* Retrieve the record.
*
* @return {Array}
*/

async find(ctx) {
const entity = await strapi.services.restaurant.findOne();
return sanitizeEntity(entity, { model: strapi.models.restaurant });
},
};
```

:::

::: tab update

#### `update`

```js
const { parseMultipartData, sanitizeEntity } = require('strapi-utils');

module.exports = {
/**
* Update the record.
*
* @return {Object}
*/

async update(ctx) {
let entity;
if (ctx.is('multipart')) {
const { data, files } = parseMultipartData(ctx);
entity = await strapi.services.restaurant.createOrUpdate(data, {
files,
});
} else {
entity = await strapi.services.restaurant.createOrUpdate(ctx.request.body);
}

return sanitizeEntity(entity, { model: strapi.models.restaurant });
},
};
```

:::

::: tab delete

#### `delete`

```js
const { sanitizeEntity } = require('strapi-utils');

module.exports = {
/**
* Delete the record.
*
* @return {Object}
*/

async delete(ctx) {
const entity = await strapi.services.restaurant.delete();
return sanitizeEntity(entity, { model: strapi.models.restaurant });
},
};
```

:::

::::

## Custom controllers

You can also create custom controllers to build your own business logic and API endpoints.
Expand Down
82 changes: 42 additions & 40 deletions docs/v3.x/concepts/middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,46 @@ By default this file doesn't exist, you will have to create it.
- `{middlewareName}` (Object): Configuration of one middleware
- `enabled` (boolean): Tells Strapi to run the middleware or not

### Settings

**Example**:

**Path —** `./config/middleware.js`.

```js
module.exports = {
//...
settings: {
cors: {
origin: 'http://localhost',
},
},
};
```

### Load order

The middlewares are injected into the Koa stack asynchronously. Sometimes it happens that some of these middlewares need to be loaded in a specific order. To define a load order, create or edit the file `./config/middleware.js`.

**Path —** `./config/middleware.js`.

```js
module.exports = {
load: {
before: ['responseTime', 'logger', 'cors', 'responses'],
order: [
"Define the middlewares' load order by putting their name in this array in the right order",
],
after: ['parser', 'router'],
},
};
```

- `load`:
- `before`: Array of middlewares that need to be loaded in the first place. The order of this array matters.
- `order`: Array of middlewares that need to be loaded in a specific order.
- `after`: Array of middlewares that need to be loaded at the end of the stack. The order of this array matters.

## Core middleware configurations

The core of Strapi embraces a small list of middlewares for performances, security and great error handling.
Expand Down Expand Up @@ -139,7 +179,7 @@ The following middlewares cannot be disabled: responses, router, logger and boom
The session doesn't work with `mongo` as a client. The package that we should use is broken for now.
:::

## Response middlewares
### Response middlewares

- [`gzip`](https://en.wikipedia.org/wiki/Gzip)
- `enabled` (boolean): Enable or not GZIP response compression.
Expand Down Expand Up @@ -178,45 +218,7 @@ The session doesn't work with `mongo` as a client. The package that we should us
- `whiteList` (array): Whitelisted IPs. Default value: `[]`.
- `blackList` (array): Blacklisted IPs. Default value: `[]`.

**Example**:

**Path —** `./config/middleware.js`.

```js
module.exports = {
//...
settings: {
cors: {
origin: 'http://localhost',
},
},
};
```

### Load order

The middlewares are injected into the Koa stack asynchronously. Sometimes it happens that some of these middlewares need to be loaded in a specific order. To define a load order, create or edit the file `./config/middleware.js`.

**Path —** `./config/middleware.js`.

```js
module.exports = {
load: {
before: ['responseTime', 'logger', 'cors', 'responses'],
order: [
"Define the middlewares' load order by putting their name in this array in the right order",
],
after: ['parser', 'router'],
},
};
```

- `load`:
- `before`: Array of middlewares that need to be loaded in the first place. The order of this array matters.
- `order`: Array of middlewares that need to be loaded in a specific order.
- `after`: Array of middlewares that need to be loaded at the end of the stack. The order of this array matters.

### Examples
## Example

Create your custom middleware.

Expand Down
98 changes: 98 additions & 0 deletions docs/v3.x/concepts/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ You can read about `strapi.query` calls [here](./queries.md).
In the following example your controller, service and model are named `restaurant`.
:::

#### Collection Type

:::: tabs

::: tab find
Expand Down Expand Up @@ -277,6 +279,102 @@ module.exports = {

::::

#### Single Type

:::: tabs

::: tab find

#### `find`

```js
const _ = require('lodash');

module.exports = {
/**
* Promise to fetch the record
*
* @return {Promise}
*/
async find(populate) {
const results = await strapi.query('restaurant').find({ _limit: 1 }, populate);
return _.first(results) || null;
},
};
```

- `populate` (array): you have to mention data you want populate `["author", "author.name", "comment", "comment.content"]`

:::

::: tab createOrUpdate

#### `createOrUpdate`

```js
const _ = require('lodash');

module.exports = {
/**
* Promise to add/update the record
*
* @return {Promise}
*/

async createOrUpdate(data, { files } = {}) {
const results = await strapi.query('restaurant').find({ _limit: 1 });
const entity = _.first(results) || null;

let entry;
if (!entity) {
entry = await strapi.query('restaurant').create(data);
} else {
entry = await strapi.query('restaurant').update({ id: entity.id }, data);
}

if (files) {
// automatically uploads the files based on the entry and the model
await strapi.entityService.uploadFiles(entry, files, {
model: 'restaurant',
// if you are using a plugin's model you will have to add the `plugin` key (plugin: 'users-permissions')
});
return this.findOne({ id: entry.id });
}

return entry;
},
};
```

:::

::: tab delete

#### `delete`

```js
module.exports = {
/**
* Promise to delete a record
*
* @return {Promise}
*/

delete() {
const results = await strapi.query('restaurant').find({ _limit: 1 });
const entity = _.first(results) || null;

if (!entity) return;

return strapi.query('restaurant').delete({id: entity.id});
},
};
```

:::

::::

## Custom services

You can also create custom services to build your own business logic.
Expand Down