Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import createDebug from 'debug';

import { AuthManager } from './auth';
import { CollectionTypeManager, SingleTypeManager } from './content-types';
import {
CollectionTypeManager,
SingleTypeManager,
UsersPermissionsUsersManager,
} from './content-types';
import { getWellKnownCollection, getWellKnownSingle } from './content-types/constants';
import { StrapiError, StrapiInitializationError } from './errors';
import { FilesManager } from './files';
import { HttpClient } from './http';
import { AuthInterceptors, HttpInterceptors } from './interceptors';
import { StrapiConfigValidator } from './validators';

import type { UsersPermissionsUsersIdOverloads } from './content-types';
import type { ContentTypeManagerOptions } from './content-types/abstract';
import type { HttpClientConfig } from './http';

Expand Down Expand Up @@ -360,15 +365,32 @@ export class StrapiClient {
* @see CollectionTypeManager
* @see StrapiClient
*/
collection(resource: string, options: ClientCollectionOptions = {}) {
collection<T extends string>(
resource: T,
options: ClientCollectionOptions = {}
): T extends 'users'
? UsersPermissionsUsersManager & UsersPermissionsUsersIdOverloads
: CollectionTypeManager {
const { path, plugin } = options;

// Auto-detect well-known collection resources and apply their plugin configuration
// if no explicit plugin option is provided
const wellKnownConfig = getWellKnownCollection(resource);
const effectivePlugin = plugin ?? wellKnownConfig?.plugin ?? undefined;

return new CollectionTypeManager({ resource, path, plugin: effectivePlugin }, this._httpClient);
if (resource === 'users' && effectivePlugin?.name === 'users-permissions') {
const manager = new UsersPermissionsUsersManager(
{ resource, path, plugin: effectivePlugin },
this._httpClient
);

return manager as any;
}

return new CollectionTypeManager(
{ resource, path, plugin: effectivePlugin },
this._httpClient
) as any;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/content-types/collection/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { CollectionTypeManager } from './manager';
export { UsersPermissionsUsersManager } from './users-permissions-users-manager';
export type { UsersPermissionsUsersIdOverloads } from './users-permissions-users-manager';
22 changes: 22 additions & 0 deletions src/content-types/collection/users-permissions-users-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CollectionTypeManager } from './manager';

import type * as API from '../../types/content-api';

/**
* Specialized manager for users-permissions `users` collection.
* Runtime behavior is inherited from CollectionTypeManager.
*/
export class UsersPermissionsUsersManager extends CollectionTypeManager {}

/**
* Augmented method signatures for users-permissions `users` to accept numeric IDs
*/
export type UsersPermissionsUsersIdOverloads = {
findOne(id: number, queryParams?: API.BaseQueryParams): Promise<API.DocumentResponse>;
update(
id: number,
data: Record<string, unknown>,
queryParams?: API.BaseQueryParams
): Promise<API.DocumentResponse>;
delete(id: number, queryParams?: API.BaseQueryParams): Promise<void>;
};