diff --git a/src/api-keys/api-keys.spec.ts b/src/api-keys/api-keys.spec.ts index cfef34a8a..f7229a194 100644 --- a/src/api-keys/api-keys.spec.ts +++ b/src/api-keys/api-keys.spec.ts @@ -8,6 +8,7 @@ import { } from '../common/utils/test-utils'; import { WorkOS } from '../workos'; import validateApiKeyFixture from './fixtures/validate-api-key.json'; +import validateApiKeyUserOwnerFixture from './fixtures/validate-api-key-user-owner.json'; import listOrganizationApiKeysFixture from './fixtures/list-organization-api-keys.json'; import createOrganizationApiKeyFixture from './fixtures/create-organization-api-key.json'; @@ -49,6 +50,31 @@ describe('ApiKeys', () => { }); }); + it('deserializes a user owner with organizationId', async () => { + fetchOnce(validateApiKeyUserOwnerFixture); + const response = await workos.apiKeys.createValidation({ + value: 'sk_123', + }); + + expect(response).toEqual({ + apiKey: { + object: 'api_key', + id: 'api_key_01H5JQDV7R7ATEYZDEG0W5PRYS', + owner: { + type: 'user', + id: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS', + organizationId: 'org_01H5JQDV7R7ATEYZDEG0W5PRYS', + }, + name: 'Test User Api Key', + obfuscatedValue: 'sk_…PRYS', + lastUsedAt: null, + permissions: ['read', 'write'], + createdAt: '2023-07-18T02:07:19.911Z', + updatedAt: '2023-07-18T02:07:19.911Z', + }, + }); + }); + it('returns null if key is invalid', async () => { fetchOnce({ api_key: null }); const response = await workos.apiKeys.createValidation({ diff --git a/src/api-keys/fixtures/validate-api-key-user-owner.json b/src/api-keys/fixtures/validate-api-key-user-owner.json new file mode 100644 index 000000000..9b8028e3a --- /dev/null +++ b/src/api-keys/fixtures/validate-api-key-user-owner.json @@ -0,0 +1,17 @@ +{ + "api_key": { + "object": "api_key", + "id": "api_key_01H5JQDV7R7ATEYZDEG0W5PRYS", + "owner": { + "type": "user", + "id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS", + "organization_id": "org_01H5JQDV7R7ATEYZDEG0W5PRYS" + }, + "name": "Test User Api Key", + "obfuscated_value": "sk_…PRYS", + "last_used_at": null, + "permissions": ["read", "write"], + "created_at": "2023-07-18T02:07:19.911Z", + "updated_at": "2023-07-18T02:07:19.911Z" + } +} diff --git a/src/api-keys/interfaces/api-key.interface.ts b/src/api-keys/interfaces/api-key.interface.ts index 2ed911964..15444eb1d 100644 --- a/src/api-keys/interfaces/api-key.interface.ts +++ b/src/api-keys/interfaces/api-key.interface.ts @@ -5,10 +5,16 @@ export interface ApiKey { /** Unique identifier of the API Key. */ id: string; /** The entity that owns the API Key. */ - owner: { - type: 'organization'; - id: string; - }; + owner: + | { + type: 'organization'; + id: string; + } + | { + type: 'user'; + id: string; + organizationId: string; + }; /** A descriptive name for the API Key. */ name: string; /** An obfuscated representation of the API Key value. */ @@ -26,10 +32,16 @@ export interface ApiKey { export interface SerializedApiKey { object: 'api_key'; id: string; - owner: { - type: 'organization'; - id: string; - }; + owner: + | { + type: 'organization'; + id: string; + } + | { + type: 'user'; + id: string; + organization_id: string; + }; name: string; obfuscated_value: string; last_used_at: string | null; diff --git a/src/api-keys/serializers/api-key.serializer.ts b/src/api-keys/serializers/api-key.serializer.ts index 15f39aad1..3b924bbb9 100644 --- a/src/api-keys/serializers/api-key.serializer.ts +++ b/src/api-keys/serializers/api-key.serializer.ts @@ -4,7 +4,14 @@ export function deserializeApiKey(apiKey: SerializedApiKey): ApiKey { return { object: apiKey.object, id: apiKey.id, - owner: apiKey.owner, + owner: + apiKey.owner.type === 'user' + ? { + type: 'user', + id: apiKey.owner.id, + organizationId: apiKey.owner.organization_id, + } + : apiKey.owner, name: apiKey.name, obfuscatedValue: apiKey.obfuscated_value, lastUsedAt: apiKey.last_used_at,