|
1 | 1 | /** |
2 | | - * Mapping of well-known Strapi resource names to their plugin configurations. |
3 | | - * This enables automatic handling of special Strapi content-types that have |
4 | | - * different API contracts than regular content-types. |
| 2 | + * Registry of well-known collection-type resources in Strapi. |
| 3 | + * These resources have different API contracts than regular content-types. |
5 | 4 | */ |
6 | | -export const WELL_KNOWN_STRAPI_RESOURCES: Record< |
7 | | - string, |
8 | | - { plugin: { name: string; prefix: string } } |
9 | | -> = { |
10 | | - // Users from users-permissions plugin don't wrap data and have no route prefix |
| 5 | +const WELL_KNOWN_COLLECTIONS: Record<string, WellKnownResourceConfig> = { |
| 6 | + /** |
| 7 | + * Users from the users-permissions plugin. |
| 8 | + * - Routes: /users (no plugin prefix) |
| 9 | + * - Data wrapping: No (expects raw payloads) |
| 10 | + */ |
11 | 11 | users: { |
12 | 12 | plugin: { |
13 | 13 | name: 'users-permissions', |
14 | 14 | prefix: '', |
15 | 15 | }, |
| 16 | + wrapsData: false, |
16 | 17 | }, |
17 | 18 | }; |
18 | 19 |
|
19 | 20 | /** |
20 | | - * List of plugin names that do not wrap the inner payload in a "data" attribute. |
| 21 | + * Registry of well-known single-type resources in Strapi. |
| 22 | + * Currently empty as there are no known single-types with special API contracts. |
21 | 23 | */ |
22 | | -export const pluginsThatDoNotWrapDataAttribute = ['users-permissions']; |
| 24 | +const WELL_KNOWN_SINGLES: Record<string, WellKnownResourceConfig> = { |
| 25 | + // Currently no well-known single-types with special API contracts |
| 26 | + // Example structure if needed in the future: |
| 27 | + // '[some-single-type]': { |
| 28 | + // plugin: { name: '[plugin-name]', prefix: '[plugin-prefix]' }, |
| 29 | + // wrapsData: false, |
| 30 | + // }, |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Configuration for well-known Strapi resources that have special API contracts. |
| 35 | + */ |
| 36 | +export interface WellKnownResourceConfig { |
| 37 | + /** |
| 38 | + * Plugin configuration for the resource. |
| 39 | + */ |
| 40 | + plugin: { |
| 41 | + /** |
| 42 | + * Name of the plugin that owns this resource. |
| 43 | + */ |
| 44 | + name: string; |
| 45 | + /** |
| 46 | + * Route prefix for the plugin. |
| 47 | + * Empty string means no prefix is used. |
| 48 | + */ |
| 49 | + prefix: string; |
| 50 | + }; |
| 51 | + /** |
| 52 | + * Whether this resource type wraps request payloads in a "data" object. |
| 53 | + * Regular Strapi content-types wrap data: { data: {...} } |
| 54 | + * Some plugins (like users-permissions) expect unwrapped data: {...} |
| 55 | + */ |
| 56 | + wrapsData: boolean; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Gets the configuration for a well-known collection resource, if it exists. |
| 61 | + * |
| 62 | + * @internal |
| 63 | + * @param resource - The collection resource name to look up |
| 64 | + * @returns The resource configuration if found, undefined otherwise |
| 65 | + */ |
| 66 | +export function getWellKnownCollection(resource: string): WellKnownResourceConfig | undefined { |
| 67 | + return WELL_KNOWN_COLLECTIONS[resource]; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Gets the configuration for a well-known single-type resource, if it exists. |
| 72 | + * |
| 73 | + * @internal |
| 74 | + * @param resource - The single-type resource name to look up |
| 75 | + * @returns The resource configuration if found, undefined otherwise |
| 76 | + */ |
| 77 | +export function getWellKnownSingle(resource: string): WellKnownResourceConfig | undefined { |
| 78 | + return WELL_KNOWN_SINGLES[resource]; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Checks if a resource should wrap data in a "data" object based on its plugin. |
| 83 | + * |
| 84 | + * @param pluginName - The name of the plugin, if any |
| 85 | + * @returns true if data should be wrapped, false otherwise |
| 86 | + */ |
| 87 | +export function shouldWrapData(pluginName: string | undefined): boolean { |
| 88 | + if (pluginName === undefined) { |
| 89 | + return true; // Regular content-types wrap data |
| 90 | + } |
| 91 | + |
| 92 | + // Check if this plugin is known to not wrap data |
| 93 | + const knownPlugin = Object.values({ |
| 94 | + ...WELL_KNOWN_COLLECTIONS, |
| 95 | + ...WELL_KNOWN_SINGLES, |
| 96 | + }).find((config) => config.plugin.name === pluginName); |
| 97 | + |
| 98 | + return knownPlugin?.wrapsData ?? true; |
| 99 | +} |
0 commit comments