Skip to content

Commit ecddda7

Browse files
committed
remove class wrapper for public interface
1 parent 81c2d89 commit ecddda7

File tree

2 files changed

+67
-199
lines changed

2 files changed

+67
-199
lines changed

src/index.public.spec.ts

Lines changed: 51 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,83 @@
1-
import { WorkOS } from './index.public';
1+
import { userManagement, sso, webhooks, actions } from './index.public';
22

3-
describe('WorkOS Public API', () => {
4-
let workosPublic: WorkOS;
5-
6-
beforeEach(() => {
7-
workosPublic = new WorkOS({
8-
clientId: 'client_123',
9-
apiHostname: 'api.workos.dev',
10-
});
11-
});
12-
13-
describe('instantiation', () => {
14-
it('should instantiate without requiring an API key', () => {
15-
expect(() => new WorkOS()).not.toThrow();
16-
});
17-
18-
it('should accept public configuration options', () => {
19-
const client = new WorkOS({
20-
clientId: 'test_client',
21-
apiHostname: 'test.api.com',
22-
https: false,
23-
port: 3000,
24-
});
25-
26-
expect(client).toBeDefined();
27-
expect(client.version).toBeDefined();
28-
});
29-
});
30-
31-
describe('exposed services', () => {
32-
it('should expose webhooks service', () => {
33-
expect(workosPublic.webhooks).toBeDefined();
34-
expect(workosPublic.webhooks.verifyHeader).toBeDefined();
35-
expect(workosPublic.webhooks.computeSignature).toBeDefined();
36-
expect(workosPublic.webhooks.constructEvent).toBeDefined();
37-
});
38-
39-
it('should expose actions service', () => {
40-
expect(workosPublic.actions).toBeDefined();
41-
expect(workosPublic.actions.verifyHeader).toBeDefined();
42-
expect(workosPublic.actions.signResponse).toBeDefined();
43-
expect(workosPublic.actions.constructAction).toBeDefined();
3+
describe('Public Exports', () => {
4+
describe('userManagement exports', () => {
5+
it('should expose getAuthorizationUrl', () => {
6+
expect(userManagement.getAuthorizationUrl).toBeDefined();
7+
expect(typeof userManagement.getAuthorizationUrl).toBe('function');
448
});
459

46-
it('should expose client-safe user management methods', () => {
47-
expect(workosPublic.userManagement).toBeDefined();
48-
expect(
49-
workosPublic.userManagement.authenticateWithCodeAndVerifier,
50-
).toBeDefined();
51-
expect(workosPublic.userManagement.getAuthorizationUrl).toBeDefined();
52-
expect(workosPublic.userManagement.getLogoutUrl).toBeDefined();
53-
expect(workosPublic.userManagement.getJwksUrl).toBeDefined();
10+
it('should expose getLogoutUrl', () => {
11+
expect(userManagement.getLogoutUrl).toBeDefined();
12+
expect(typeof userManagement.getLogoutUrl).toBe('function');
5413
});
5514

56-
it('should expose client-safe SSO methods', () => {
57-
expect(workosPublic.sso).toBeDefined();
58-
expect(workosPublic.sso.getAuthorizationUrl).toBeDefined();
15+
it('should expose getJwksUrl', () => {
16+
expect(userManagement.getJwksUrl).toBeDefined();
17+
expect(typeof userManagement.getJwksUrl).toBe('function');
5918
});
6019

61-
it('should expose version', () => {
62-
expect(workosPublic.version).toBeDefined();
63-
expect(typeof workosPublic.version).toBe('string');
64-
});
65-
});
66-
67-
describe('method behavior', () => {
68-
it('should be able to call URL generation methods', () => {
69-
const authUrl = workosPublic.userManagement.getAuthorizationUrl({
20+
it('should generate authorization URLs correctly', () => {
21+
const url = userManagement.getAuthorizationUrl({
7022
clientId: 'client_123',
7123
redirectUri: 'https://example.com/callback',
72-
provider: 'GoogleOAuth',
24+
provider: 'authkit',
7325
});
7426

75-
expect(authUrl).toContain('api.workos.dev');
76-
expect(authUrl).toContain('client_id=client_123');
77-
expect(authUrl).toContain('provider=GoogleOAuth');
78-
});
79-
80-
it('should be able to call JWKS URL generation', () => {
81-
const jwksUrl = workosPublic.userManagement.getJwksUrl('client_123');
82-
expect(jwksUrl).toBe('https://api.workos.dev/sso/jwks/client_123');
27+
expect(url).toContain('client_id=client_123');
28+
expect(url).toContain('redirect_uri=https%3A%2F%2Fexample.com%2Fcallback');
29+
expect(url).toContain('provider=authkit');
8330
});
8431

85-
it('should be able to call logout URL generation', () => {
86-
const logoutUrl = workosPublic.userManagement.getLogoutUrl({
32+
it('should generate logout URLs correctly', () => {
33+
const url = userManagement.getLogoutUrl({
8734
sessionId: 'session_123',
8835
returnTo: 'https://example.com',
8936
});
9037

91-
expect(logoutUrl).toContain('api.workos.dev');
92-
expect(logoutUrl).toContain('session_id=session_123');
38+
expect(url).toContain('session_id=session_123');
39+
expect(url).toContain('return_to=https%3A%2F%2Fexample.com');
9340
});
9441

95-
it('should be able to call SSO authorization URL generation', () => {
96-
const authUrl = workosPublic.sso.getAuthorizationUrl({
97-
clientId: 'client_123',
98-
redirectUri: 'https://example.com/callback',
99-
provider: 'GoogleOAuth',
100-
});
101-
102-
expect(authUrl).toContain('api.workos.dev');
103-
expect(authUrl).toContain('client_id=client_123');
42+
it('should generate JWKS URLs correctly', () => {
43+
const url = userManagement.getJwksUrl('client_123');
44+
expect(url).toBe('https://api.workos.com/sso/jwks/client_123');
10445
});
10546
});
10647

107-
describe('server-only methods should not be exposed', () => {
108-
it('should not expose getUser on userManagement', () => {
109-
expect((workosPublic.userManagement as any).getUser).toBeUndefined();
110-
});
111-
112-
it('should not expose createUser on userManagement', () => {
113-
expect((workosPublic.userManagement as any).createUser).toBeUndefined();
48+
describe('sso exports', () => {
49+
it('should expose getAuthorizationUrl', () => {
50+
expect(sso.getAuthorizationUrl).toBeDefined();
51+
expect(typeof sso.getAuthorizationUrl).toBe('function');
11452
});
11553

116-
it('should not expose listUsers on userManagement', () => {
117-
expect((workosPublic.userManagement as any).listUsers).toBeUndefined();
118-
});
54+
it('should generate SSO authorization URLs correctly', () => {
55+
const url = sso.getAuthorizationUrl({
56+
clientId: 'client_123',
57+
redirectUri: 'https://example.com/callback',
58+
provider: 'GoogleOAuth',
59+
});
11960

120-
it('should not expose updateUser on userManagement', () => {
121-
expect((workosPublic.userManagement as any).updateUser).toBeUndefined();
61+
expect(url).toContain('client_id=client_123');
62+
expect(url).toContain('provider=GoogleOAuth');
12263
});
64+
});
12365

124-
it('should not expose server-only authentication methods', () => {
125-
expect(
126-
(workosPublic.userManagement as any).authenticateWithPassword,
127-
).toBeUndefined();
128-
expect(
129-
(workosPublic.userManagement as any).authenticateWithMagicAuth,
130-
).toBeUndefined();
131-
expect(
132-
(workosPublic.userManagement as any).authenticateWithRefreshToken,
133-
).toBeUndefined();
66+
describe('webhooks exports', () => {
67+
it('should expose webhooks service', () => {
68+
expect(webhooks).toBeDefined();
69+
expect(webhooks.verifyHeader).toBeDefined();
70+
expect(webhooks.computeSignature).toBeDefined();
71+
expect(webhooks.constructEvent).toBeDefined();
13472
});
13573
});
13674

137-
describe('type safety', () => {
138-
it('should provide proper TypeScript types for exposed methods', () => {
139-
// These should compile without errors
140-
const authUrl: string = workosPublic.userManagement.getAuthorizationUrl({
141-
clientId: 'test',
142-
redirectUri: 'https://example.com',
143-
provider: 'GoogleOAuth',
144-
});
145-
146-
const jwksUrl: string =
147-
workosPublic.userManagement.getJwksUrl('client_id');
148-
149-
expect(typeof authUrl).toBe('string');
150-
expect(typeof jwksUrl).toBe('string');
75+
describe('actions exports', () => {
76+
it('should expose actions service', () => {
77+
expect(actions).toBeDefined();
78+
expect(actions.verifyHeader).toBeDefined();
79+
expect(actions.signResponse).toBeDefined();
80+
expect(actions.constructAction).toBeDefined();
15181
});
15282
});
153-
});
83+
});

src/index.public.ts

Lines changed: 16 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
import { WorkOSBase } from './workos';
2-
import { WorkOSOptions } from './common/interfaces';
1+
/**
2+
* Public methods that can be safely used without an API key.
3+
* These are primarily URL builders and cryptographic utilities.
4+
*/
35

4-
// Export public methods directly (new approach)
6+
// Export public methods directly - this is the recommended approach
57
export * as userManagement from './public/user-management';
68
export * as sso from './public/sso';
79

10+
// Export webhooks and actions (these don't need API keys)
11+
import { Webhooks } from './webhooks/webhooks';
12+
import { Actions } from './actions/actions';
13+
import { SubtleCryptoProvider } from './common/crypto/subtle-crypto-provider';
14+
15+
const cryptoProvider = new SubtleCryptoProvider();
16+
export const webhooks = new Webhooks(cryptoProvider);
17+
export const actions = new Actions(cryptoProvider);
18+
819
// Re-export types for convenience
920
export type {
1021
AuthorizationURLOptions as UserManagementAuthorizationURLOptions,
@@ -13,78 +24,5 @@ export type {
1324

1425
export type { SSOAuthorizationURLOptions } from './public/sso';
1526

16-
interface WorkOSPublicOptions {
17-
clientId?: string;
18-
apiHostname?: string;
19-
https?: boolean;
20-
port?: number;
21-
}
22-
23-
/**
24-
* WorkOS public-safe SDK for browser environments.
25-
* Only exposes methods that are safe to use without API keys.
26-
* @deprecated Use the direct method imports instead:
27-
* import { userManagement, sso } from '@workos-inc/node/public';
28-
*/
29-
export class WorkOS {
30-
private _base: WorkOSBase;
31-
32-
// Client-safe services
33-
readonly userManagement: {
34-
authenticateWithCodeAndVerifier: typeof WorkOSBase.prototype.userManagement.authenticateWithCodeAndVerifier;
35-
getAuthorizationUrl: typeof WorkOSBase.prototype.userManagement.getAuthorizationUrl;
36-
getLogoutUrl: typeof WorkOSBase.prototype.userManagement.getLogoutUrl;
37-
getJwksUrl: typeof WorkOSBase.prototype.userManagement.getJwksUrl;
38-
};
39-
40-
readonly sso: {
41-
getAuthorizationUrl: typeof WorkOSBase.prototype.sso.getAuthorizationUrl;
42-
};
43-
44-
readonly webhooks: typeof WorkOSBase.prototype.webhooks;
45-
readonly actions: typeof WorkOSBase.prototype.actions;
46-
47-
constructor(options: WorkOSPublicOptions = {}) {
48-
// Convert public options to base WorkOS options format
49-
const baseOptions: WorkOSOptions = {
50-
clientId: options.clientId,
51-
apiHostname: options.apiHostname,
52-
https: options.https,
53-
port: options.port,
54-
};
55-
56-
// Create base instance without API key
57-
this._base = new WorkOSBase(undefined, baseOptions);
58-
59-
// Initialize public-safe service methods
60-
this.userManagement = {
61-
authenticateWithCodeAndVerifier:
62-
this._base.userManagement.authenticateWithCodeAndVerifier.bind(
63-
this._base.userManagement,
64-
),
65-
getAuthorizationUrl: this._base.userManagement.getAuthorizationUrl.bind(
66-
this._base.userManagement,
67-
),
68-
getLogoutUrl: this._base.userManagement.getLogoutUrl.bind(
69-
this._base.userManagement,
70-
),
71-
getJwksUrl: this._base.userManagement.getJwksUrl.bind(
72-
this._base.userManagement,
73-
),
74-
};
75-
76-
this.sso = {
77-
getAuthorizationUrl: this._base.sso.getAuthorizationUrl.bind(
78-
this._base.sso,
79-
),
80-
};
81-
82-
// These services are fully public-safe - expose entirely
83-
this.webhooks = this._base.webhooks;
84-
this.actions = this._base.actions;
85-
}
86-
87-
get version() {
88-
return this._base.version;
89-
}
90-
}
27+
// Note: If you need authenticateWithCodeAndVerifier, use the full WorkOS SDK
28+
// as it requires server-side API key authentication

0 commit comments

Comments
 (0)