From ef6dc102ba66e3b84f005141c59268620dd86791 Mon Sep 17 00:00:00 2001 From: Adam Jenkins Date: Thu, 12 Nov 2026 11:59:21 -0400 Subject: [PATCH] feat(directory-sync): Add directory-scoped user and group reads Add listDirectoryUsers, getDirectoryUser, listDirectoryGroups and getDirectoryGroup for the GET /directories/{directoryId}/users and /groups routes, and mark the flat listUsers, getUser, listGroups and getGroup deprecated in favor of them. The scoped user reads return the slim DirectoryUser without the groups array. Co-Authored-By: Claude Fable 5.1 --- src/directory-sync/directory-sync.spec.ts | 165 +++++++++++++++++ src/directory-sync/directory-sync.ts | 175 +++++++++++++++++- .../fixtures/directory-user.json | 38 ++++ .../fixtures/list-directory-user.json | 46 +++++ .../get-directory-group-options.interface.ts | 6 + .../get-directory-user-options.interface.ts | 6 + src/directory-sync/interfaces/index.ts | 6 +- ...t-groups-in-directory-options.interface.ts | 8 + ...st-users-in-directory-options.interface.ts | 12 ++ 9 files changed, 460 insertions(+), 2 deletions(-) create mode 100644 src/directory-sync/fixtures/directory-user.json create mode 100644 src/directory-sync/fixtures/list-directory-user.json create mode 100644 src/directory-sync/interfaces/get-directory-group-options.interface.ts create mode 100644 src/directory-sync/interfaces/get-directory-user-options.interface.ts create mode 100644 src/directory-sync/interfaces/list-groups-in-directory-options.interface.ts create mode 100644 src/directory-sync/interfaces/list-users-in-directory-options.interface.ts diff --git a/src/directory-sync/directory-sync.spec.ts b/src/directory-sync/directory-sync.spec.ts index 463cf27d5..75cc74156 100644 --- a/src/directory-sync/directory-sync.spec.ts +++ b/src/directory-sync/directory-sync.spec.ts @@ -13,6 +13,10 @@ import listDirectoryFixture from './fixtures/list-directory.json'; import directoryFixture from './fixtures/directory.json'; import listDirectoryGroupFixture from './fixtures/list-directory-group.json'; import directoryGroupFixture from './fixtures/directory-group.json'; +// @oagen-ignore-start +import listDirectoryUserFixture from './fixtures/list-directory-user.json'; +import directoryUserFixture from './fixtures/directory-user.json'; +// @oagen-ignore-end const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU'); @@ -170,6 +174,167 @@ describe('DirectorySync', () => { }); // @oagen-ignore-start + describe('listDirectoryUsers', () => { + it('lists users scoped to the directory with filters on the wire', async () => { + fetchOnce(listDirectoryUserFixture); + + const { data, listMetadata } = + await workos.directorySync.listDirectoryUsers({ + directoryId: 'directory_01ECAZ4NV9QMV47GW873HDCX74', + group: 'directory_group_01E1JJS84MFPPQ3G655FHTKX6Z', + idpId: '2836', + email: 'marcelina.davis@example.com', + limit: 10, + }); + + expect(fetchMethod()).toBe('GET'); + expect(new URL(String(fetchURL())).pathname).toBe( + '/directories/directory_01ECAZ4NV9QMV47GW873HDCX74/users', + ); + expect(fetchSearchParams()).toEqual({ + group: 'directory_group_01E1JJS84MFPPQ3G655FHTKX6Z', + idp_id: '2836', + email: 'marcelina.davis@example.com', + limit: '10', + order: 'desc', + }); + expect(listMetadata).toEqual({ before: null, after: null }); + expect(data).toHaveLength(1); + expect(data[0].id).toBe('directory_user_01E1JG7J09H96KYP8HM9B0G5SJ'); + expect(data[0].directoryId).toBe('directory_01ECAZ4NV9QMV47GW873HDCX74'); + expect(data[0].name).toBe('Marcelina Davis'); + expect(data[0]).not.toHaveProperty('groups'); + }); + + it('sends only pagination when no filters are given', async () => { + fetchOnce(listDirectoryUserFixture); + + await workos.directorySync.listDirectoryUsers({ + directoryId: 'directory_01ECAZ4NV9QMV47GW873HDCX74', + }); + + expect(fetchSearchParams()).toEqual({ order: 'desc' }); + }); + + it('encodes the directory id as a single path segment', async () => { + fetchOnce(listDirectoryUserFixture); + + await workos.directorySync.listDirectoryUsers({ + directoryId: '../directory_groups', + }); + + expect(new URL(String(fetchURL())).pathname).toBe( + '/directories/..%2Fdirectory_groups/users', + ); + }); + + it('throws when the API responds with an error', async () => { + fetchOnce({ message: 'Not Found' }, { status: 404 }); + + await expect( + workos.directorySync.listDirectoryUsers({ + directoryId: 'directory_01ECAZ4NV9QMV47GW873HDCX74', + }), + ).rejects.toThrow(); + }); + }); + + describe('getDirectoryUser', () => { + it('returns the user without groups', async () => { + fetchOnce(directoryUserFixture); + + const result = await workos.directorySync.getDirectoryUser({ + directoryId: 'directory_01ECAZ4NV9QMV47GW873HDCX74', + userId: 'directory_user_01E1JG7J09H96KYP8HM9B0G5SJ', + }); + + expect(fetchMethod()).toBe('GET'); + expect(new URL(String(fetchURL())).pathname).toBe( + '/directories/directory_01ECAZ4NV9QMV47GW873HDCX74/users/directory_user_01E1JG7J09H96KYP8HM9B0G5SJ', + ); + expect(result.id).toBe('directory_user_01E1JG7J09H96KYP8HM9B0G5SJ'); + expect(result.customAttributes).toEqual({ + department: 'Engineering', + job_title: 'Software Engineer', + }); + expect(result.createdAt.toISOString()).toBe('2026-01-15T12:00:00.000Z'); + expect(result).not.toHaveProperty('groups'); + }); + + it('throws when the API responds with an error', async () => { + fetchOnce({ message: 'Not Found' }, { status: 404 }); + + await expect( + workos.directorySync.getDirectoryUser({ + directoryId: 'directory_01ECAZ4NV9QMV47GW873HDCX74', + userId: 'directory_user_01E1JG7J09H96KYP8HM9B0G5SJ', + }), + ).rejects.toThrow(); + }); + }); + + describe('listDirectoryGroups', () => { + it('lists groups scoped to the directory with the user filter on the wire', async () => { + fetchOnce(listDirectoryGroupFixture); + + const { data, listMetadata } = + await workos.directorySync.listDirectoryGroups({ + directoryId: 'directory_01ECAZ4NV9QMV47GW873HDCX74', + user: 'directory_user_01E1JG7J09H96KYP8HM9B0G5SJ', + }); + + expect(fetchMethod()).toBe('GET'); + expect(new URL(String(fetchURL())).pathname).toBe( + '/directories/directory_01ECAZ4NV9QMV47GW873HDCX74/groups', + ); + expect(fetchSearchParams()).toEqual({ + user: 'directory_user_01E1JG7J09H96KYP8HM9B0G5SJ', + order: 'desc', + }); + expect(listMetadata).toEqual({ before: null, after: null }); + expect(data).toHaveLength(1); + expectDirectoryGroup(data[0]); + }); + + it('throws when the API responds with an error', async () => { + fetchOnce({ message: 'Not Found' }, { status: 404 }); + + await expect( + workos.directorySync.listDirectoryGroups({ + directoryId: 'directory_01ECAZ4NV9QMV47GW873HDCX74', + }), + ).rejects.toThrow(); + }); + }); + + describe('getDirectoryGroup', () => { + it('returns the group', async () => { + fetchOnce(directoryGroupFixture); + + const result = await workos.directorySync.getDirectoryGroup({ + directoryId: 'directory_01ECAZ4NV9QMV47GW873HDCX74', + groupId: 'directory_group_01E1JJS84MFPPQ3G655FHTKX6Z', + }); + + expect(fetchMethod()).toBe('GET'); + expect(new URL(String(fetchURL())).pathname).toBe( + '/directories/directory_01ECAZ4NV9QMV47GW873HDCX74/groups/directory_group_01E1JJS84MFPPQ3G655FHTKX6Z', + ); + expectDirectoryGroup(result); + }); + + it('throws when the API responds with an error', async () => { + fetchOnce({ message: 'Not Found' }, { status: 404 }); + + await expect( + workos.directorySync.getDirectoryGroup({ + directoryId: 'directory_01ECAZ4NV9QMV47GW873HDCX74', + groupId: 'directory_group_01E1JJS84MFPPQ3G655FHTKX6Z', + }), + ).rejects.toThrow(); + }); + }); + describe('path parameter encoding', () => { it('keeps a traversal payload inside a single path segment', async () => { fetchOnce(); diff --git a/src/directory-sync/directory-sync.ts b/src/directory-sync/directory-sync.ts index 7b16570ab..f13f5c526 100644 --- a/src/directory-sync/directory-sync.ts +++ b/src/directory-sync/directory-sync.ts @@ -24,10 +24,19 @@ import type { ListDirectoryUsersOptions } from './interfaces/list-directory-user import type { GetUserOptions } from './interfaces/get-user-options.interface'; import type { DefaultCustomAttributes, + DirectoryUser, + DirectoryUserResponse, DirectoryUserWithGroups, DirectoryUserWithGroupsResponse, } from './interfaces/directory-user.interface'; -import { deserializeDirectoryUserWithGroups } from './serializers/directory-user.serializer'; +import { + deserializeDirectoryUser, + deserializeDirectoryUserWithGroups, +} from './serializers/directory-user.serializer'; +import type { ListUsersInDirectoryOptions } from './interfaces/list-users-in-directory-options.interface'; +import type { GetDirectoryUserOptions } from './interfaces/get-directory-user-options.interface'; +import type { ListGroupsInDirectoryOptions } from './interfaces/list-groups-in-directory-options.interface'; +import type { GetDirectoryGroupOptions } from './interfaces/get-directory-group-options.interface'; // @oagen-ignore-end const serializeListDirectoriesOptions = ( @@ -45,6 +54,23 @@ const serializeListDirectoriesOptions = ( return wire as PaginationOptions; }; +// @oagen-ignore-start +const serializeListUsersInDirectoryOptions = ( + options: Omit, +): PaginationOptions => { + const wire: Record = { + limit: options.limit, + before: options.before, + after: options.after, + order: options.order, + group: options.group, + email: options.email, + }; + if (options.idpId !== undefined) wire.idp_id = options.idpId; + return wire as PaginationOptions; +}; +// @oagen-ignore-end + export class DirectorySync { constructor(private readonly workos: WorkOS) {} @@ -121,6 +147,7 @@ export class DirectorySync { * @throws {AuthorizationException} 403 * @throws {NotFoundException} 404 * @throws {UnprocessableEntityException} 422 + * @deprecated Use {@link DirectorySync.listDirectoryGroups} instead. */ async listGroups( options: ListDirectoryGroupsOptions, @@ -154,6 +181,7 @@ export class DirectorySync { * @returns {Promise} * @throws {AuthorizationException} 403 * @throws {NotFoundException} 404 + * @deprecated Use {@link DirectorySync.getDirectoryGroup} instead. */ async getGroup( options: DirectorySyncGetGroupOptions, @@ -179,6 +207,7 @@ export class DirectorySync { * @throws {NotFoundException} 404 * @throws {UnprocessableEntityException} 422 * @throws {RateLimitExceededException} 429 + * @deprecated Use {@link DirectorySync.listDirectoryUsers} instead. */ async listUsers( options: ListDirectoryUsersOptions, @@ -222,6 +251,7 @@ export class DirectorySync { * @returns {Promise>} * @throws {AuthorizationException} 403 * @throws {NotFoundException} 404 + * @deprecated Use {@link DirectorySync.getDirectoryUser} instead. */ async getUser( options: GetUserOptions, @@ -234,4 +264,147 @@ export class DirectorySync { return deserializeDirectoryUserWithGroups(data); } // @oagen-ignore-end + + // @oagen-ignore-start + // Directory-scoped reads. These succeed the flat /directory_users and + // /directory_groups routes: users come back without the unbounded `groups` + // array, so the responses stay small and the routes are not rate limited + // the way the flat ones are. Hand-owned until oagen emits them from the spec. + /** + * List Users for a Directory + * + * Get a list of the Directory Users in a Directory, optionally filtered to the members of a Directory Group. + * @param options - Pagination and filter options. + * @param options.directoryId - Unique identifier of the Directory. + * @example "directory_01ECAZ4NV9QMV47GW873HDCX74" + * @param options.group - Filter Directory Users to the members of this Directory Group. + * @example "directory_group_01E64QTDNS0EGJ0FMCVY9BWGZT" + * @param options.idpId - Filter Directory Users by the identity provider's unique identifier. + * @example "2836" + * @param options.email - Filter Directory Users by their primary email address. + * @example "jane.doe@example.com" + * @returns {Promise, PaginationOptions>>} + * @throws {AuthorizationException} 403 + * @throws {NotFoundException} 404 + * @throws {UnprocessableEntityException} 422 + */ + async listDirectoryUsers< + TCustomAttributes extends object = DefaultCustomAttributes, + >( + options: ListUsersInDirectoryOptions, + ): Promise< + AutoPaginatable, PaginationOptions> + > { + const { directoryId, ...rest } = options; + const endpoint = `/directories/${encodeURIComponent(directoryId)}/users`; + const wire = serializeListUsersInDirectoryOptions(rest); + + return new AutoPaginatable( + await fetchAndDeserialize< + DirectoryUserResponse, + DirectoryUser + >(this.workos, endpoint, deserializeDirectoryUser, wire), + (params) => + fetchAndDeserialize< + DirectoryUserResponse, + DirectoryUser + >(this.workos, endpoint, deserializeDirectoryUser, params), + wire, + ); + } + + /** + * Get a User for a Directory + * + * Get the details of a Directory User in a Directory. + * @param options - The request options. + * @param options.directoryId - Unique identifier of the Directory. + * @example "directory_01ECAZ4NV9QMV47GW873HDCX74" + * @param options.userId - Unique identifier for the Directory User. + * @example "directory_user_01E1JG7J09H96KYP8HM9B0G5SJ" + * @returns {Promise>} + * @throws {AuthorizationException} 403 + * @throws {NotFoundException} 404 + */ + async getDirectoryUser< + TCustomAttributes extends object = DefaultCustomAttributes, + >( + options: GetDirectoryUserOptions, + ): Promise> { + const { directoryId, userId } = options; + const { data } = await this.workos.get< + DirectoryUserResponse + >( + `/directories/${encodeURIComponent( + directoryId, + )}/users/${encodeURIComponent(userId)}`, + ); + + return deserializeDirectoryUser(data); + } + + /** + * List Groups for a Directory + * + * Get a list of the Directory Groups in a Directory, optionally filtered to the groups a Directory User belongs to. + * @param options - Pagination and filter options. + * @param options.directoryId - Unique identifier of the Directory. + * @example "directory_01ECAZ4NV9QMV47GW873HDCX74" + * @param options.user - Filter Directory Groups to those this Directory User belongs to. + * @example "directory_user_01E1JG7J09H96KYP8HM9B0G5SJ" + * @returns {Promise>} + * @throws {AuthorizationException} 403 + * @throws {NotFoundException} 404 + * @throws {UnprocessableEntityException} 422 + */ + async listDirectoryGroups( + options: ListGroupsInDirectoryOptions, + ): Promise> { + const { directoryId, ...wire } = options; + const endpoint = `/directories/${encodeURIComponent(directoryId)}/groups`; + + return new AutoPaginatable( + await fetchAndDeserialize( + this.workos, + endpoint, + deserializeDirectoryGroup, + wire, + ), + (params) => + fetchAndDeserialize( + this.workos, + endpoint, + deserializeDirectoryGroup, + params, + ), + wire, + ); + } + + /** + * Get a Group for a Directory + * + * Get the details of a Directory Group in a Directory. + * @param options - The request options. + * @param options.directoryId - Unique identifier of the Directory. + * @example "directory_01ECAZ4NV9QMV47GW873HDCX74" + * @param options.groupId - Unique identifier for the Directory Group. + * @example "directory_group_01E1JJS84MFPPQ3G655FHTKX6Z" + * @returns {Promise} + * @throws {AuthorizationException} 403 + * @throws {NotFoundException} 404 + */ + async getDirectoryGroup( + options: GetDirectoryGroupOptions, + ): Promise { + const { directoryId, groupId } = options; + const { data } = await this.workos.get( + `/directories/${encodeURIComponent( + directoryId, + )}/groups/${encodeURIComponent(groupId)}`, + ); + + return deserializeDirectoryGroup(data); + } + // @oagen-ignore-end } diff --git a/src/directory-sync/fixtures/directory-user.json b/src/directory-sync/fixtures/directory-user.json new file mode 100644 index 000000000..67263ead5 --- /dev/null +++ b/src/directory-sync/fixtures/directory-user.json @@ -0,0 +1,38 @@ +{ + "object": "directory_user", + "id": "directory_user_01E1JG7J09H96KYP8HM9B0G5SJ", + "directory_id": "directory_01ECAZ4NV9QMV47GW873HDCX74", + "organization_id": "org_01EZTR6WYX1A0DSE2CYMGXQ24Y", + "idp_id": "2836", + "email": "marcelina.davis@example.com", + "first_name": "Marcelina", + "last_name": "Davis", + "name": "Marcelina Davis", + "emails": [ + { + "primary": true, + "type": "work", + "value": "marcelina.davis@example.com" + } + ], + "job_title": "Software Engineer", + "username": "mdavis", + "state": "active", + "raw_attributes": { + "key": {} + }, + "custom_attributes": { + "department": "Engineering", + "job_title": "Software Engineer" + }, + "role": { + "slug": "admin" + }, + "roles": [ + { + "slug": "admin" + } + ], + "created_at": "2026-01-15T12:00:00.000Z", + "updated_at": "2026-01-15T12:00:00.000Z" +} diff --git a/src/directory-sync/fixtures/list-directory-user.json b/src/directory-sync/fixtures/list-directory-user.json new file mode 100644 index 000000000..81b85607d --- /dev/null +++ b/src/directory-sync/fixtures/list-directory-user.json @@ -0,0 +1,46 @@ +{ + "data": [ + { + "object": "directory_user", + "id": "directory_user_01E1JG7J09H96KYP8HM9B0G5SJ", + "directory_id": "directory_01ECAZ4NV9QMV47GW873HDCX74", + "organization_id": "org_01EZTR6WYX1A0DSE2CYMGXQ24Y", + "idp_id": "2836", + "email": "marcelina.davis@example.com", + "first_name": "Marcelina", + "last_name": "Davis", + "name": "Marcelina Davis", + "emails": [ + { + "primary": true, + "type": "work", + "value": "marcelina.davis@example.com" + } + ], + "job_title": "Software Engineer", + "username": "mdavis", + "state": "active", + "raw_attributes": { + "key": {} + }, + "custom_attributes": { + "department": "Engineering", + "job_title": "Software Engineer" + }, + "role": { + "slug": "admin" + }, + "roles": [ + { + "slug": "admin" + } + ], + "created_at": "2026-01-15T12:00:00.000Z", + "updated_at": "2026-01-15T12:00:00.000Z" + } + ], + "list_metadata": { + "before": null, + "after": null + } +} diff --git a/src/directory-sync/interfaces/get-directory-group-options.interface.ts b/src/directory-sync/interfaces/get-directory-group-options.interface.ts new file mode 100644 index 000000000..eacebfbf2 --- /dev/null +++ b/src/directory-sync/interfaces/get-directory-group-options.interface.ts @@ -0,0 +1,6 @@ +export interface GetDirectoryGroupOptions { + /** Unique identifier of the Directory. */ + directoryId: string; + /** Unique identifier for the Directory Group. */ + groupId: string; +} diff --git a/src/directory-sync/interfaces/get-directory-user-options.interface.ts b/src/directory-sync/interfaces/get-directory-user-options.interface.ts new file mode 100644 index 000000000..d12232e95 --- /dev/null +++ b/src/directory-sync/interfaces/get-directory-user-options.interface.ts @@ -0,0 +1,6 @@ +export interface GetDirectoryUserOptions { + /** Unique identifier of the Directory. */ + directoryId: string; + /** Unique identifier for the Directory User. */ + userId: string; +} diff --git a/src/directory-sync/interfaces/index.ts b/src/directory-sync/interfaces/index.ts index 9fb7de62b..5b20b90fd 100644 --- a/src/directory-sync/interfaces/index.ts +++ b/src/directory-sync/interfaces/index.ts @@ -6,18 +6,22 @@ export * from './directory-metadata-user.interface'; export * from './directory-metadata.interface'; export * from './directory-state.interface'; export * from './directory-sync-get-group-options.interface'; +export * from './directory-token.interface'; export * from './directory-type.interface'; export * from './directory-user-with-groups-email.interface'; export * from './directory-user-with-groups-state.interface'; export * from './directory-user.interface'; export * from './directory.interface'; export * from './event-directory.interface'; -export * from './directory-token.interface'; +export * from './get-directory-group-options.interface'; export * from './get-directory-options.interface'; +export * from './get-directory-user-options.interface'; export * from './get-user-options.interface'; export * from './list-directories-options.interface'; export * from './list-directory-users-options.interface'; +export * from './list-groups-in-directory-options.interface'; export * from './list-groups-options.interface'; +export * from './list-users-in-directory-options.interface'; export * from './slim-role.interface'; export type { DefaultCustomAttributes, diff --git a/src/directory-sync/interfaces/list-groups-in-directory-options.interface.ts b/src/directory-sync/interfaces/list-groups-in-directory-options.interface.ts new file mode 100644 index 000000000..3b2fe8967 --- /dev/null +++ b/src/directory-sync/interfaces/list-groups-in-directory-options.interface.ts @@ -0,0 +1,8 @@ +import { PaginationOptions } from '../../common/interfaces/pagination-options.interface'; + +export interface ListGroupsInDirectoryOptions extends PaginationOptions { + /** Unique identifier of the Directory. */ + directoryId: string; + /** Filter Directory Groups to those this Directory User belongs to. */ + user?: string; +} diff --git a/src/directory-sync/interfaces/list-users-in-directory-options.interface.ts b/src/directory-sync/interfaces/list-users-in-directory-options.interface.ts new file mode 100644 index 000000000..eb3899832 --- /dev/null +++ b/src/directory-sync/interfaces/list-users-in-directory-options.interface.ts @@ -0,0 +1,12 @@ +import { PaginationOptions } from '../../common/interfaces/pagination-options.interface'; + +export interface ListUsersInDirectoryOptions extends PaginationOptions { + /** Unique identifier of the Directory. */ + directoryId: string; + /** Filter Directory Users to the members of this Directory Group. */ + group?: string; + /** Filter Directory Users by the identity provider's unique identifier (`idp_id`). */ + idpId?: string; + /** Filter Directory Users by their primary email address. */ + email?: string; +}