Add filtering logic on collection level to hide endpoints for example as below.
I think this pattern of writing the configuration in the custom section is quite powerful and would allow for more complex manipulation in the future. Such as adding headers, tags, or other customizations.
const userOpenApiConfig: CustomCollectionOpenApiConfiguration = {
openapi: {
hidden: false,
paths: {
"/api/{slug}": {
post: {
hidden: true,
},
get: {
hidden: true,
},
},
"/api/{slug}/{id}": {
patch: {
hidden: true,
},
},
},
},
};
export const Users: CollectionConfig = {
slug: "user",
custom: {
...userOpenApiConfig,
},
}
Example filter implementation on collection
const getCollectionOpenApiConfig = (collection: Collection) => {
const collectionOpenApiConfig = collection.config
.custom as CustomCollectionOpenApiConfiguration;
if (
collectionOpenApiConfig &&
openApiCollectionConfigKeyName in collectionOpenApiConfig
) {
const updatedPaths = Object.fromEntries(
Object.entries(collectionOpenApiConfig.openapi.paths).map(
([path, methods]) => [
path.replace("{slug}", collection.config.slug),
methods,
]
)
);
return {
...collectionOpenApiConfig.openapi,
paths: updatedPaths,
};
}
return undefined;
};
export const filterHiddenEndpoints = (
apiSchema: Record<
string,
OpenAPIV3.PathItemObject & OpenAPIV3_1.PathItemObject
>,
collection: Collection
) => {
const openApiConfig = getCollectionOpenApiConfig(collection);
if (!openApiConfig) {
return apiSchema;
}
for (const [path, methods] of Object.entries(apiSchema)) {
for (const [method, operation] of Object.entries(methods)) {
const pathConfig = openApiConfig.paths[path];
if (!!pathConfig) {
const methodConfig = (pathConfig as any)[
method
] as OpenApiMethodOptions;
if (!!methodConfig && !!methodConfig.hidden) {
console.log(`${path}.${method} - hidden`);
delete (methods as any)[method];
}
}
}
if (Object.keys(methods).length === 0) {
console.log(`${path} - all methods hidden, removing path`);
delete apiSchema[path];
}
}
return apiSchema;
};
It would also be nice to be able to filter away collections in the PluginConfig, for example if we want to hide all "payload-" collections.
const excludeCollections: (string | RegExp)[] = [
"sessions",
new RegExp("^payload-.*"),
];
const filterCollections = (
req: PayloadRequest,
excludeCollections: (string | RegExp)[]
) => {
const collections = Object.values(req.payload.collections).filter((x) => {
const slug = x.config.slug;
return !excludeCollections.some((pattern) => {
if (typeof pattern === "string") {
return pattern === slug;
} else {
return pattern.test(slug);
}
});
});
return collections;
};
(Bug) Hidden fields are shown
Hidden fields are used in the models. Perhaps for fields one could check field.hidden property when generating the response objects. Haven’t had time to test this aspect of it yet though.
Add filtering logic on collection level to hide endpoints for example as below.
I think this pattern of writing the configuration in the custom section is quite powerful and would allow for more complex manipulation in the future. Such as adding headers, tags, or other customizations.
Example filter implementation on collection
It would also be nice to be able to filter away collections in the PluginConfig, for example if we want to hide all "payload-" collections.
(Bug) Hidden fields are shown
Hidden fields are used in the models. Perhaps for fields one could check field.hidden property when generating the response objects. Haven’t had time to test this aspect of it yet though.