Skip to content

Commit fc9f6fe

Browse files
authored
Add api keys management api methods (#1434)
## Description This PR adds methods for new API key management API endpoints that we recently added. - `organizations.listOrganizationApiKeys` (`GET /organizations/:id/api_keys`) - `organizations.createOrganizationApiKey` (`POST /organizations/:id/api_keys`) - `apiKeys.deleteApiKey` (`DELETE /api_keys/:id`) ## Documentation Does this require changes to the WorkOS Docs? E.g. the [API Reference](https://workos.com/docs/reference) or code snippets need updates. ``` [x] Yes ``` If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.
1 parent 4149430 commit fc9f6fe

14 files changed

+374
-0
lines changed

src/api-keys/api-keys.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,16 @@ describe('ApiKeys', () => {
5151
expect(response).toEqual({ apiKey: null });
5252
});
5353
});
54+
55+
describe('deleteApiKey', () => {
56+
it('sends a delete request', async () => {
57+
fetchOnce({}, { status: 204 });
58+
59+
await workos.apiKeys.deleteApiKey('api_key_01H5JQDV7R7ATEYZDEG0W5PRYS');
60+
61+
expect(fetchURL()).toContain(
62+
'/api_keys/api_key_01H5JQDV7R7ATEYZDEG0W5PRYS',
63+
);
64+
});
65+
});
5466
});

src/api-keys/api-keys.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ export class ApiKeys {
1919

2020
return deserializeValidateApiKeyResponse(data);
2121
}
22+
23+
async deleteApiKey(id: string): Promise<void> {
24+
await this.workos.delete(`/api_keys/${id}`);
25+
}
2226
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PostOptions } from '../../common/interfaces';
2+
3+
export interface CreateOrganizationApiKeyOptions {
4+
organizationId: string;
5+
name: string;
6+
permissions?: string[];
7+
}
8+
9+
export interface SerializedCreateOrganizationApiKeyOptions {
10+
name: string;
11+
permissions?: string[];
12+
}
13+
14+
export interface CreateOrganizationApiKeyRequestOptions
15+
extends Pick<PostOptions, 'idempotencyKey'> {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export interface CreatedApiKey {
2+
object: 'api_key';
3+
id: string;
4+
owner: {
5+
type: 'organization';
6+
id: string;
7+
};
8+
name: string;
9+
obfuscatedValue: string;
10+
value: string;
11+
lastUsedAt: string | null;
12+
permissions: string[];
13+
createdAt: string;
14+
updatedAt: string;
15+
}
16+
17+
export interface SerializedCreatedApiKey {
18+
object: 'api_key';
19+
id: string;
20+
owner: {
21+
type: 'organization';
22+
id: string;
23+
};
24+
name: string;
25+
obfuscated_value: string;
26+
value: string;
27+
last_used_at: string | null;
28+
permissions: string[];
29+
created_at: string;
30+
updated_at: string;
31+
}

src/api-keys/interfaces/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from './api-key.interface';
2+
export * from './create-organization-api-key-options.interface';
3+
export * from './created-api-key.interface';
4+
export * from './list-organization-api-keys-options.interface';
5+
export * from './validate-api-key.interface';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { PaginationOptions } from '../../common/interfaces/pagination-options.interface';
2+
3+
export interface ListOrganizationApiKeysOptions extends PaginationOptions {
4+
organizationId: string;
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {
2+
CreateOrganizationApiKeyOptions,
3+
SerializedCreateOrganizationApiKeyOptions,
4+
} from '../interfaces/create-organization-api-key-options.interface';
5+
6+
export function serializeCreateOrganizationApiKeyOptions(
7+
options: CreateOrganizationApiKeyOptions,
8+
): SerializedCreateOrganizationApiKeyOptions {
9+
return {
10+
name: options.name,
11+
permissions: options.permissions,
12+
};
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {
2+
CreatedApiKey,
3+
SerializedCreatedApiKey,
4+
} from '../interfaces/created-api-key.interface';
5+
6+
export function deserializeCreatedApiKey(
7+
apiKey: SerializedCreatedApiKey,
8+
): CreatedApiKey {
9+
return {
10+
object: apiKey.object,
11+
id: apiKey.id,
12+
owner: apiKey.owner,
13+
name: apiKey.name,
14+
obfuscatedValue: apiKey.obfuscated_value,
15+
value: apiKey.value,
16+
lastUsedAt: apiKey.last_used_at,
17+
permissions: apiKey.permissions,
18+
createdAt: apiKey.created_at,
19+
updatedAt: apiKey.updated_at,
20+
};
21+
}

src/api-keys/serializers/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './api-key.serializer';
2+
export * from './create-organization-api-key-options.serializer';
3+
export * from './created-api-key.serializer';
4+
export * from './validate-api-key.serializer';

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { WebIronSessionProvider } from './common/iron-session/web-iron-session-p
1414
import { IronSessionProvider } from './common/iron-session/iron-session-provider';
1515

1616
export * from './actions/interfaces';
17+
export * from './api-keys/interfaces';
1718
export * from './audit-logs/interfaces';
1819
export * from './common/exceptions';
1920
export * from './common/interfaces';

0 commit comments

Comments
 (0)