Skip to content
Draft
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
75 changes: 75 additions & 0 deletions src/directory-sync/directory-sync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,81 @@ describe('DirectorySync', () => {
});
});

describe('syncDirectory', () => {
const client = new WorkOS('sk_test', { maxRetries: 0 });

it('returns the queued response from a 202 without sending a request body', async () => {
fetchOnce({ status: 'queued' }, { status: 202 });

const result = await client.directorySync.syncDirectory('directory_123');

expect(result).toEqual({ status: 'queued' });
expect(fetchURL()).toBe(
'https://api.workos.com/directories/directory_123/sync',
);
expect(fetchMethod()).toBe('POST');
expect(fetch.mock.calls[0][1]?.body).toBe('');
});

it('encodes the directory ID as a single path segment', async () => {
fetchOnce({ status: 'queued' }, { status: 202 });

await client.directorySync.syncDirectory(
'directory/with?reserved#characters',
);

expect(fetchURL()).toContain(
'/directories/directory%2Fwith%3Freserved%23characters/sync',
);
});

it('exposes the cooldown through the existing rate-limit exception', async () => {
fetchOnce(
{
code: 'directory_sync_rate_limited',
message: 'Wait before requesting another sync.',
retry_after_seconds: 120,
},
{
status: 429,
headers: { 'Retry-After': '120', 'X-Request-ID': 'req_sync' },
},
);

await expect(
client.directorySync.syncDirectory('directory_123'),
).rejects.toMatchObject({
name: 'RateLimitExceededException',
status: 429,
retryAfter: 120,
requestID: 'req_sync',
});
expect(fetch).toHaveBeenCalledTimes(1);
});

it.each([
[409, 'directory_sync_in_progress'],
[422, 'directory_sync_unsupported'],
[503, 'directory_sync_disabled'],
])(
'propagates a %s response instead of reporting a queued sync',
async (status, code) => {
fetchOnce(
{ code, message: 'The sync was not queued.' },
{ status: Number(status) },
);

await expect(
client.directorySync.syncDirectory('directory_123'),
).rejects.toMatchObject({
status,
code,
});
expect(fetch).toHaveBeenCalledTimes(1);
},
);
});

describe('deleteDirectory', () => {
it('sends a request to delete the directory', async () => {
fetchOnce({}, { status: 202 });
Expand Down
24 changes: 24 additions & 0 deletions src/directory-sync/directory-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DirectoryGroup,
DirectoryGroupResponse,
DirectoryResponse,
DirectorySyncResponse,
DirectoryUserWithGroups,
DirectoryUserWithGroupsResponse,
ListDirectoriesOptions,
Expand Down Expand Up @@ -76,6 +77,29 @@ export class DirectorySync {
return deserializeDirectory(data);
}

/**
* Request an asynchronous sync of a directory.
*
* Currently supports Google Workspace directories in active or validating
* state. Manual requests share a five-minute cooldown across the API,
* Dashboard, Admin Portal, and MCP. A queued response does not indicate
* that the sync has started or completed.
*
* @param id - Unique identifier for the Directory.
* @throws {ConflictException} A sync is already running (409).
* @throws {UnprocessableEntityException} Unsupported provider or state (422).
* @throws {RateLimitExceededException} Cooldown active; inspect retryAfter (429).
* @throws {GenericServerException} Directory syncing is paused (503).
*/
async syncDirectory(id: string): Promise<DirectorySyncResponse> {
const { data } = await this.workos.post<DirectorySyncResponse, undefined>(
`/directories/${encodePathParameter(id)}/sync`,
undefined,
);

return data;
}

/**
* Delete a Directory
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface DirectorySyncResponse {
/** Accepted for asynchronous processing, not confirmation of completion. */
status: 'queued';
}
1 change: 1 addition & 0 deletions src/directory-sync/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './directory.interface';
export * from './directory-group.interface';
export * from './directory-sync-response.interface';
export * from './list-directories-options.interface';
export * from './list-groups-options.interface';
export * from './list-directory-users-options.interface';
Expand Down
Loading