-
Notifications
You must be signed in to change notification settings - Fork 17
feat(client.single): support plugin config argument #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 characters
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 characters
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 characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,99 @@ | ||
| /** | ||
| * Mapping of well-known Strapi resource names to their plugin configurations. | ||
| * This enables automatic handling of special Strapi content-types that have | ||
| * different API contracts than regular content-types. | ||
| * Registry of well-known collection-type resources in Strapi. | ||
| * These resources have different API contracts than regular content-types. | ||
| */ | ||
| export const WELL_KNOWN_STRAPI_RESOURCES: Record< | ||
| string, | ||
| { plugin: { name: string; prefix: string } } | ||
| > = { | ||
| // Users from users-permissions plugin don't wrap data and have no route prefix | ||
| const WELL_KNOWN_COLLECTIONS: Record<string, WellKnownResourceConfig> = { | ||
| /** | ||
| * Users from the users-permissions plugin. | ||
| * - Routes: /users (no plugin prefix) | ||
| * - Data wrapping: No (expects raw payloads) | ||
| */ | ||
| users: { | ||
| plugin: { | ||
| name: 'users-permissions', | ||
| prefix: '', | ||
| }, | ||
| wrapsData: false, | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * List of plugin names that do not wrap the inner payload in a "data" attribute. | ||
| * Registry of well-known single-type resources in Strapi. | ||
| * Currently empty as there are no known single-types with special API contracts. | ||
| */ | ||
| export const pluginsThatDoNotWrapDataAttribute = ['users-permissions']; | ||
| const WELL_KNOWN_SINGLES: Record<string, WellKnownResourceConfig> = { | ||
| // Currently no well-known single-types with special API contracts | ||
| // Example structure if needed in the future: | ||
| // '[some-single-type]': { | ||
| // plugin: { name: '[plugin-name]', prefix: '[plugin-prefix]' }, | ||
| // wrapsData: false, | ||
| // }, | ||
| }; | ||
|
|
||
| /** | ||
| * Configuration for well-known Strapi resources that have special API contracts. | ||
| */ | ||
| export interface WellKnownResourceConfig { | ||
| /** | ||
| * Plugin configuration for the resource. | ||
| */ | ||
| plugin: { | ||
| /** | ||
| * Name of the plugin that owns this resource. | ||
| */ | ||
| name: string; | ||
| /** | ||
| * Route prefix for the plugin. | ||
| * Empty string means no prefix is used. | ||
| */ | ||
| prefix: string; | ||
| }; | ||
| /** | ||
| * Whether this resource type wraps request payloads in a "data" object. | ||
| * Regular Strapi content-types wrap data: { data: {...} } | ||
| * Some plugins (like users-permissions) expect unwrapped data: {...} | ||
| */ | ||
| wrapsData: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the configuration for a well-known collection resource, if it exists. | ||
| * | ||
| * @internal | ||
| * @param resource - The collection resource name to look up | ||
| * @returns The resource configuration if found, undefined otherwise | ||
| */ | ||
| export function getWellKnownCollection(resource: string): WellKnownResourceConfig | undefined { | ||
| return WELL_KNOWN_COLLECTIONS[resource]; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the configuration for a well-known single-type resource, if it exists. | ||
| * | ||
| * @internal | ||
| * @param resource - The single-type resource name to look up | ||
| * @returns The resource configuration if found, undefined otherwise | ||
| */ | ||
| export function getWellKnownSingle(resource: string): WellKnownResourceConfig | undefined { | ||
| return WELL_KNOWN_SINGLES[resource]; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a resource should wrap data in a "data" object based on its plugin. | ||
| * | ||
| * @param pluginName - The name of the plugin, if any | ||
| * @returns true if data should be wrapped, false otherwise | ||
| */ | ||
| export function shouldWrapData(pluginName: string | undefined): boolean { | ||
| if (pluginName === undefined) { | ||
| return true; // Regular content-types wrap data | ||
| } | ||
|
|
||
| // Check if this plugin is known to not wrap data | ||
| const knownPlugin = Object.values({ | ||
| ...WELL_KNOWN_COLLECTIONS, | ||
| ...WELL_KNOWN_SINGLES, | ||
| }).find((config) => config.plugin.name === pluginName); | ||
|
|
||
| return knownPlugin?.wrapsData ?? true; | ||
| } | ||
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 characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from './single'; | ||
| export * from './collection'; | ||
| export * from './constants'; |
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 characters
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 characters
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 characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't come up with any use case here. Ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are none that I know of
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me either; pInging @Convly in case he's aware of any :)