Merge pull request #9720 from strapi/i18n/handle-locale-query-param

Handle more usecase for the _locale query param
This commit is contained in:
Alexandre BODIN 2021-03-15 15:53:48 +01:00 committed by GitHub
commit 4e5daff7f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 22 deletions

View File

@ -7,17 +7,24 @@ const { isLocalized } = require('./content-types');
const { syncLocalizations, updateNonLocalizedFields } = require('./localizations'); const { syncLocalizations, updateNonLocalizedFields } = require('./localizations');
const LOCALE_QUERY_FILTER = '_locale'; const LOCALE_QUERY_FILTER = '_locale';
const SINGLE_ENTRY_ACTIONS = ['findOne', 'update', 'delete'];
/** /**
* Adds default locale or replaces _locale by locale in query params * Adds default locale or replaces _locale by locale in query params
* @param {object} params - query params * @param {object} params - query params
*/ */
const wrapParams = async (params = {}) => { const wrapParams = async (params = {}, ctx = {}) => {
if (params.id) { const { action } = ctx;
if (has('id', params) && SINGLE_ENTRY_ACTIONS.includes(action)) {
return params; return params;
} }
if (has(LOCALE_QUERY_FILTER, params)) { if (has(LOCALE_QUERY_FILTER, params)) {
if (params[LOCALE_QUERY_FILTER] === 'all') {
return omit(LOCALE_QUERY_FILTER, params);
}
return { return {
...omit(LOCALE_QUERY_FILTER, params), ...omit(LOCALE_QUERY_FILTER, params),
locale: params[LOCALE_QUERY_FILTER], locale: params[LOCALE_QUERY_FILTER],
@ -41,8 +48,10 @@ const decorator = service => ({
* @param {object} ctx - Query context * @param {object} ctx - Query context
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async wrapOptions(opts = {}, ctx) {
const wrappedOptions = await service.wrapOptions.apply(this, [opts, ctx]); async wrapOptions(opts = {}, ctx = {}) {
const wrappedOptions = await service.wrapOptions.call(this, opts, ctx);
const model = strapi.db.getModel(ctx.model); const model = strapi.db.getModel(ctx.model);
if (!isLocalized(model)) { if (!isLocalized(model)) {
@ -51,7 +60,7 @@ const decorator = service => ({
return { return {
...wrappedOptions, ...wrappedOptions,
params: await wrapParams(wrappedOptions.params), params: await wrapParams(wrappedOptions.params, ctx),
}; };
}, },
@ -63,7 +72,7 @@ const decorator = service => ({
*/ */
async create(opts, ctx) { async create(opts, ctx) {
const model = strapi.db.getModel(ctx.model); const model = strapi.db.getModel(ctx.model);
const entry = await service.create.apply(this, [opts, ctx]); const entry = await service.create.call(this, opts, ctx);
if (isLocalized(model)) { if (isLocalized(model)) {
await syncLocalizations(entry, { model }); await syncLocalizations(entry, { model });
@ -83,13 +92,14 @@ const decorator = service => ({
const { data, ...restOptions } = opts; const { data, ...restOptions } = opts;
const entry = await service.update.apply(this, [ const entry = await service.update.call(
this,
{ {
data: omit('locale', data), data: omit(['locale', 'localizations'], data),
...restOptions, ...restOptions,
}, },
ctx, ctx
]); );
if (isLocalized(model)) { if (isLocalized(model)) {
await updateNonLocalizedFields(entry, { model }); await updateNonLocalizedFields(entry, { model });

View File

@ -60,7 +60,7 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async find(opts, { model }) { async find(opts, { model }) {
const { params, populate } = await this.wrapOptions(opts, { model }); const { params, populate } = await this.wrapOptions(opts, { model, action: 'find' });
const { kind } = db.getModel(model); const { kind } = db.getModel(model);
@ -80,7 +80,7 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async findPage(opts, { model }) { async findPage(opts, { model }) {
const { params, populate } = await this.wrapOptions(opts, { model }); const { params, populate } = await this.wrapOptions(opts, { model, action: 'findPage' });
return db.query(model).findPage(params, populate); return db.query(model).findPage(params, populate);
}, },
@ -92,7 +92,10 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async findWithRelationCounts(opts, { model }) { async findWithRelationCounts(opts, { model }) {
const { params, populate } = await this.wrapOptions(opts, { model }); const { params, populate } = await this.wrapOptions(opts, {
model,
action: 'findWithRelationCounts',
});
return db.query(model).findWithRelationCounts(params, populate); return db.query(model).findWithRelationCounts(params, populate);
}, },
@ -104,7 +107,7 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async findOne(opts, { model }) { async findOne(opts, { model }) {
const { params, populate } = await this.wrapOptions(opts, { model }); const { params, populate } = await this.wrapOptions(opts, { model, action: 'findOne' });
return db.query(model).findOne(params, populate); return db.query(model).findOne(params, populate);
}, },
@ -116,7 +119,7 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async count(opts, { model }) { async count(opts, { model }) {
const { params } = await this.wrapOptions(opts, { model }); const { params } = await this.wrapOptions(opts, { model, action: 'count' });
return db.query(model).count(params); return db.query(model).count(params);
}, },
@ -128,7 +131,7 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async create(opts, { model }) { async create(opts, { model }) {
const { data, files } = await this.wrapOptions(opts, { model }); const { data, files } = await this.wrapOptions(opts, { model, action: 'create' });
const modelDef = db.getModel(model); const modelDef = db.getModel(model);
@ -158,7 +161,7 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async update(opts, { model }) { async update(opts, { model }) {
const { params, data, files } = await this.wrapOptions(opts, { model }); const { params, data, files } = await this.wrapOptions(opts, { model, action: 'update' });
const modelDef = db.getModel(model); const modelDef = db.getModel(model);
const existingEntry = await db.query(model).findOne(params); const existingEntry = await db.query(model).findOne(params);
@ -191,7 +194,7 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async delete(opts, { model }) { async delete(opts, { model }) {
const { params } = await this.wrapOptions(opts, { model }); const { params } = await this.wrapOptions(opts, { model, action: 'delete' });
const entry = await db.query(model).delete(params); const entry = await db.query(model).delete(params);
@ -211,7 +214,7 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async search(opts, { model }) { async search(opts, { model }) {
const { params, populate } = await this.wrapOptions(opts, { model }); const { params, populate } = await this.wrapOptions(opts, { model, action: 'search' });
return db.query(model).search(params, populate); return db.query(model).search(params, populate);
}, },
@ -223,7 +226,10 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async searchWithRelationCounts(opts, { model }) { async searchWithRelationCounts(opts, { model }) {
const { params, populate } = await this.wrapOptions(opts, { model }); const { params, populate } = await this.wrapOptions(opts, {
model,
action: 'searchWithRelationCounts',
});
return db.query(model).searchWithRelationCounts(params, populate); return db.query(model).searchWithRelationCounts(params, populate);
}, },
@ -235,7 +241,7 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async searchPage(opts, { model }) { async searchPage(opts, { model }) {
const { params, populate } = await this.wrapOptions(opts, { model }); const { params, populate } = await this.wrapOptions(opts, { model, action: 'searchPage' });
return db.query(model).searchPage(params, populate); return db.query(model).searchPage(params, populate);
}, },
@ -247,7 +253,7 @@ const createDefaultImplementation = ({ db, eventHub, entityValidator }) => ({
* @param {object} ctx.model - Model that is being used * @param {object} ctx.model - Model that is being used
*/ */
async countSearch(opts, { model }) { async countSearch(opts, { model }) {
const { params } = await this.wrapOptions(opts, { model }); const { params } = await this.wrapOptions(opts, { model, action: 'countSearch' });
return db.query(model).countSearch(params); return db.query(model).countSearch(params);
}, },