What is @strapi/utils?
@strapi/utils is a utility package for Strapi, a popular open-source headless CMS. This package provides various utility functions that help in developing and managing Strapi applications more efficiently.
What are @strapi/utils's main functionalities?
sanitizeEntity
The `sanitizeEntity` function is used to remove sensitive information from an entity object based on the model's configuration. This is particularly useful for ensuring that sensitive data like passwords are not exposed in API responses.
const { sanitizeEntity } = require('@strapi/utils');
const entity = { id: 1, name: 'John Doe', password: 'secret' };
const sanitizedEntity = sanitizeEntity(entity, { model: strapi.models.user });
console.log(sanitizedEntity);
parseMultipartData
The `parseMultipartData` function is used to parse multipart form data, which is commonly used for file uploads. This function extracts the data and files from the request context.
const { parseMultipartData } = require('@strapi/utils');
const ctx = { request: { body: {}, files: {} } };
const { data, files } = parseMultipartData(ctx);
console.log(data, files);
contentTypes
The `contentTypes` utility provides functions to work with different content types. For example, `isContentType` checks if a given string is a valid content type.
const { contentTypes } = require('@strapi/utils');
const isContentType = contentTypes.isContentType('application/json');
console.log(isContentType);
Other packages similar to @strapi/utils
lodash
Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It provides a wide range of utility functions for common programming tasks, similar to some of the utilities provided by @strapi/utils, but it is not specific to Strapi.
validator
Validator is a library for string validation and sanitization. While @strapi/utils provides some sanitization functions specific to Strapi models, Validator offers a broader range of validation and sanitization functions for general use.
formidable
Formidable is a Node.js module for parsing form data, especially file uploads. It offers similar functionality to the `parseMultipartData` function in @strapi/utils but is more general-purpose and not tied to Strapi.