Skip to content

Commit

Permalink
chore: content-api response format header
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin committed Mar 4, 2024
1 parent 5153a18 commit a6af41a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
7 changes: 6 additions & 1 deletion packages/core/core/src/core-api/controller/index.ts
Expand Up @@ -6,6 +6,7 @@ import type { CoreApi, Schema } from '@strapi/types';
import { transformResponse } from './transform';
import { createSingleTypeController } from './single-type';
import { createCollectionTypeController } from './collection-type';
import requestCtx from '../../services/request-context';

const isSingleType = (contentType: Schema.ContentType): contentType is Schema.SingleType =>
contentTypeUtils.isSingleType(contentType);
Expand All @@ -24,7 +25,11 @@ function createController({

const proto: CoreApi.Controller.Base = {
transformResponse(data, meta) {
return transformResponse(data, meta, { contentType });
const ctx = requestCtx.get();
return transformResponse(data, meta, {
contentType,
useJsonAPIFormat: ctx?.headers?.['strapi-response-format'] === 'v4',
});
},

async sanitizeOutput(data, ctx) {
Expand Down
39 changes: 28 additions & 11 deletions packages/core/core/src/core-api/controller/transform.ts
Expand Up @@ -3,8 +3,9 @@ import type { Common, Schema, UID } from '@strapi/types';

type TransformedEntry = {
id: string;
meta?: Record<string, unknown>;
} & Record<string, unknown>;
documentId?: string | null;
attributes: Record<string, unknown>;
};

type TransformedComponent = {
id: string;
Expand All @@ -13,6 +14,7 @@ type TransformedComponent = {

type Entry = {
id: string;
documentId: string | null;
[key: string]: Entry | Entry[] | string | number | null | boolean | Date;
};

Expand All @@ -27,14 +29,23 @@ function isDZEntries(property: unknown): property is (Entry & { __component: UID
const transformResponse = (
resource: any,
meta: unknown = {},
opts: { contentType?: Schema.ContentType | Schema.Component } = {}
opts: {
contentType?: Schema.ContentType | Schema.Component;
useJsonAPIFormat?: boolean;
} = {
useJsonAPIFormat: false,
}
) => {
if (isNil(resource)) {
return resource;
}

if (!isPlainObject(resource) && !Array.isArray(resource)) {
throw new Error('Entry must be an object or an arrayy of objects');
}

return {
data: transformEntry(resource, opts?.contentType),
data: opts.useJsonAPIFormat ? transformEntry(resource, opts?.contentType) : resource,
meta,
};
};
Expand All @@ -51,7 +62,14 @@ function transformComponent(
return data.map((datum) => transformComponent(datum, component));
}

return transformEntry(data, component);
const res = transformEntry(data, component);

if (isNil(res)) {
return res;
}

const { id, documentId, attributes } = res;
return { id, documentId, ...attributes };
}

function transformEntry<T extends Entry | Entry[] | null>(
Expand All @@ -74,7 +92,7 @@ function transformEntry(
throw new Error('Entry must be an object');
}

const { id, ...properties } = entry;
const { id, documentId, ...properties } = entry;

const attributeValues: Record<string, unknown> = {};

Expand All @@ -88,7 +106,7 @@ function transformEntry(
strapi.contentType(attribute.target as Common.UID.ContentType)
);

attributeValues[key] = data;
attributeValues[key] = { data };
} else if (attribute && attribute.type === 'component' && isEntry(property)) {
attributeValues[key] = transformComponent(property, strapi.components[attribute.component]);
} else if (attribute && attribute.type === 'dynamiczone' && isDZEntries(property)) {
Expand All @@ -102,17 +120,16 @@ function transformEntry(
} else if (attribute && attribute.type === 'media' && isEntry(property)) {
const data = transformEntry(property, strapi.contentType('plugin::upload.file'));

attributeValues[key] = data;
attributeValues[key] = { data };
} else {
attributeValues[key] = property;
}
}

return {
id,
...attributeValues,
// NOTE: not necessary for now
// meta: {},
documentId,
attributes: attributeValues,
};
}

Expand Down

0 comments on commit a6af41a

Please sign in to comment.