Skip to content

Instantly share code, notes, and snippets.

@olbboy
Forked from hucancode/README.md
Created September 21, 2022 04:36
Show Gist options
  • Save olbboy/08c3937c5bbb3e2091e0a461d061acb0 to your computer and use it in GitHub Desktop.
Save olbboy/08c3937c5bbb3e2091e0a461d061acb0 to your computer and use it in GitHub Desktop.

Revisions

  1. @hucancode hucancode revised this gist Feb 7, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions flatten-response.js
    Original 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.data) {
    if(obj && obj.data) {
    return flattenData(obj);
    }
    if(obj.attributes) {
    if(obj && obj.attributes) {
    return flattenAttrs(obj);
    }
    return obj;
  2. @hucancode hucancode revised this gist Feb 4, 2022. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions flatten-response.js
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,7 @@ function flattenAttrs(obj) {
    }

    function flatten(obj) {
    if(Array.isArray(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')) {
    console.log(`API request (${ctx.url}) detected, transforming response json...`);
    ctx.response.body = {
    data: flatten(ctx.response.body.data),
    meta: ctx.response.body.meta
    };
    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;
  3. @hucancode hucancode revised this gist Jan 21, 2022. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions flatten-response.js
    Original file line number Diff line number Diff line change
    @@ -32,15 +32,15 @@ function flatten(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
    };
    }
    };
    };
    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;
  4. @hucancode hucancode revised this gist Jan 21, 2022. No changes.
  5. @hucancode hucancode revised this gist Jan 21, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion flatten-response.js
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ function flattenData(obj) {
    }

    function flattenAttrs(obj) {
    let attrs = {}
    let attrs = {};
    for (var key in obj.attributes) {
    attrs[key] = flatten(obj.attributes[key]);
    }
  6. @hucancode hucancode created this gist Jan 21, 2022.
    46 changes: 46 additions & 0 deletions flatten-response.js
    Original 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
    };
    }
    };
    };
    13 changes: 13 additions & 0 deletions middlewares.js
    Original 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',
    ];