-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathworkos-client.ts
More file actions
183 lines (170 loc) · 5.17 KB
/
workos-client.ts
File metadata and controls
183 lines (170 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Unified WorkOS client for CLI commands.
*
* Wraps @workos-inc/node SDK for documented endpoints and extends with
* raw-fetch methods for undocumented/write-only endpoints (webhooks, redirect URIs, etc.).
* Commands import one client; they don't care whether a method is SDK-backed or raw fetch.
*/
import { WorkOS } from '@workos-inc/node';
import { workosRequest, type WorkOSListResponse } from './workos-api.js';
import { resolveApiKey, resolveApiBaseUrl } from './api-key.js';
export interface WebhookEndpoint {
id: string;
endpoint_url: string;
events: string[];
secret?: string;
created_at: string;
updated_at: string;
}
export interface AuditLogAction {
action: string;
}
export interface AuditLogRetention {
retention_period_in_days: number;
}
export interface WorkOSCLIClient {
sdk: WorkOS;
webhooks: {
list(): Promise<WorkOSListResponse<WebhookEndpoint>>;
create(endpointUrl: string, events: string[]): Promise<WebhookEndpoint>;
delete(id: string): Promise<void>;
};
redirectUris: {
add(uri: string): Promise<{ success: boolean; alreadyExists: boolean }>;
};
corsOrigins: {
add(origin: string): Promise<{ success: boolean; alreadyExists: boolean }>;
};
homepageUrl: {
set(url: string): Promise<void>;
};
auditLogs: {
listActions(): Promise<WorkOSListResponse<AuditLogAction>>;
getSchema(action: string): Promise<unknown>;
getRetention(orgId: string): Promise<AuditLogRetention>;
};
}
/**
* Create a unified WorkOS client.
*
* @param apiKey - Explicit API key; falls back to resolveApiKey()
* @param baseUrl - Explicit base URL; falls back to resolveApiBaseUrl()
*/
export function createWorkOSClient(apiKey?: string, baseUrl?: string): WorkOSCLIClient {
const key = apiKey ?? resolveApiKey();
const base = baseUrl ?? resolveApiBaseUrl();
// Parse hostname from base URL for SDK init
const hostname = new URL(base).hostname;
const sdk = new WorkOS(key, { apiHostname: hostname });
return {
sdk,
webhooks: {
async list() {
return workosRequest<WorkOSListResponse<WebhookEndpoint>>({
method: 'GET',
path: '/webhook_endpoints',
apiKey: key,
baseUrl: base,
});
},
async create(endpointUrl: string, events: string[]) {
return workosRequest<WebhookEndpoint>({
method: 'POST',
path: '/webhook_endpoints',
apiKey: key,
baseUrl: base,
body: { endpoint_url: endpointUrl, events },
});
},
async delete(id: string) {
await workosRequest<null>({
method: 'DELETE',
path: `/webhook_endpoints/${id}`,
apiKey: key,
baseUrl: base,
});
},
},
redirectUris: {
async add(uri: string) {
try {
await workosRequest({
method: 'POST',
path: '/user_management/redirect_uris',
apiKey: key,
baseUrl: base,
body: { uri },
});
return { success: true, alreadyExists: false };
} catch (error: unknown) {
const { WorkOSApiError } = await import('./workos-api.js');
if (error instanceof WorkOSApiError) {
if (error.statusCode === 409 || (error.statusCode === 422 && error.message.includes('already exists'))) {
return { success: true, alreadyExists: true };
}
}
throw error;
}
},
},
corsOrigins: {
async add(origin: string) {
try {
await workosRequest({
method: 'POST',
path: '/user_management/cors_origins',
apiKey: key,
baseUrl: base,
body: { origin },
});
return { success: true, alreadyExists: false };
} catch (error: unknown) {
const { WorkOSApiError } = await import('./workos-api.js');
if (error instanceof WorkOSApiError) {
if (error.statusCode === 409 || (error.statusCode === 422 && error.message.includes('already exists'))) {
return { success: true, alreadyExists: true };
}
}
throw error;
}
},
},
homepageUrl: {
async set(url: string) {
await workosRequest({
method: 'PUT',
path: '/user_management/app_homepage_url',
apiKey: key,
baseUrl: base,
body: { url },
});
},
},
auditLogs: {
async listActions() {
return workosRequest<WorkOSListResponse<AuditLogAction>>({
method: 'GET',
path: '/audit_logs/actions',
apiKey: key,
baseUrl: base,
});
},
async getSchema(action: string) {
return workosRequest<unknown>({
method: 'GET',
path: `/audit_logs/actions/${encodeURIComponent(action)}/schemas`,
apiKey: key,
baseUrl: base,
});
},
async getRetention(orgId: string) {
return workosRequest<AuditLogRetention>({
method: 'GET',
path: `/organizations/${encodeURIComponent(orgId)}/audit_logs_retention`,
apiKey: key,
baseUrl: base,
});
},
},
};
}