- 
      
- 
        Save olbboy/08c3937c5bbb3e2091e0a461d061acb0 to your computer and use it in GitHub Desktop. 
Revisions
- 
        hucancode revised this gist Feb 7, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,10 +22,10 @@ function flatten(obj) { if(Array.isArray(obj)) { return flattenArray(obj); } if(obj && obj.data) { return flattenData(obj); } if(obj && obj.attributes) { return flattenAttrs(obj); } return obj; 
- 
        hucancode revised this gist Feb 4, 2022 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,8 +19,7 @@ function flattenAttrs(obj) { } function flatten(obj) { if(Array.isArray(obj)) { return flattenArray(obj); } if(obj.data) { @@ -34,13 +33,14 @@ function flatten(obj) { async function respond(ctx, next) { await next(); if (!ctx.url.startsWith('/api')) { return; } console.log(`API request (${ctx.url}) detected, transforming response json...`); ctx.response.body = { data: flatten(ctx.response.body.data), meta: ctx.response.body.meta }; } module.exports = () => respond; 
- 
        hucancode revised this gist Jan 21, 2022 . 1 changed file with 12 additions and 12 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,15 +32,15 @@ function flatten(obj) { return obj; } async function respond(ctx, next) { await next(); if (ctx.url.startsWith('/api')) { console.log(`API request (${ctx.url}) detected, transforming response json...`); ctx.response.body = { data: flatten(ctx.response.body.data), meta: ctx.response.body.meta }; } } module.exports = () => respond; 
- 
        hucancode revised this gist Jan 21, 2022 . No changes.There are no files selected for viewing
- 
        hucancode revised this gist Jan 21, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ function flattenData(obj) { } function flattenAttrs(obj) { let attrs = {}; for (var key in obj.attributes) { attrs[key] = flatten(obj.attributes[key]); } 
- 
        hucancode created this gist Jan 21, 2022 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ // src/middlewares/flatten-response.js function flattenArray(obj) { return obj.map(e => flatten(e)); } function flattenData(obj) { return flatten(obj.data); } function flattenAttrs(obj) { let attrs = {} for (var key in obj.attributes) { attrs[key] = flatten(obj.attributes[key]); } return { id: obj.id, ...attrs }; } function flatten(obj) { if(Array.isArray(obj)) { return flattenArray(obj); } if(obj.data) { return flattenData(obj); } if(obj.attributes) { return flattenAttrs(obj); } return obj; } module.exports = () => { return async (ctx, next) => { await next(); if (ctx.url.startsWith('/api')) { console.log(`API request (${ctx.url}) detected, transforming response json...`); ctx.response.body = { data: flatten(ctx.response.body.data), meta: ctx.response.body.meta }; } }; }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ // config/middlewares.js module.exports = [ 'strapi::errors', 'strapi::security', 'strapi::cors', 'strapi::poweredBy', 'strapi::logger', 'strapi::query', 'strapi::body', 'global::flatten-response', 'strapi::favicon', 'strapi::public', ];