Skip to content

Commit

Permalink
feat(backend-core): Retrieve instance organizations
Browse files Browse the repository at this point in the history
Added a method in the organizations API to retrieve a list of
organizations for the instance specified by the API key.
The results can be paginated and are sorted by descending creation date.
Most recent organizations are returned first.
  • Loading branch information
gkats committed May 16, 2022
1 parent b07167f commit a24c4d3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/backend-core/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Reference of the methods supported in the Clerk Backend API wrapper. [API refere
- [createInvitation(params)](#createinvitationparams)
- [revokeInvitation(invitationId)](#revokeinvitationinvitationid)
- [Organization operations](#organization-operations)
- [getOrganizationList()](#getorganizationlist)
- [createOrganization(params)](#createorganizationparams)
- [updateOrganization(organizationId, params)](#updateorganizationorganizationid-params)
- [updateOrganizationMetadata(organizationId, params)](#updateorganizationmetadataorganizationid-params)
Expand Down Expand Up @@ -159,6 +160,20 @@ const invitation = await clerkAPI.invitations.revokeInvitation('inv_some-id');

Organization operations are exposed by the `organizations` sub-api (`clerkAPI.organizations`).

#### getOrganizationList()

Retrieves a list of organizations for an instance.

The instance is determined by the API key you've provided when configuring the API. You can either set the `CLERK_API_KEY` environment variable, or provide the `apiKey` property explicitly when configuring the API client.

Results can be paginated by providing an optional `limit` and `offset` pair of parameters.

Results will be ordered by descending creation date. Most recent organizations will be first in the list.

```ts
const organizations = await clerkAPI.organizations.getOrganizationList();
```

#### createOrganization(params)

Creates a new organization with the given name and optional slug.
Expand Down
23 changes: 23 additions & 0 deletions packages/backend-core/src/__tests__/apis/OrganizationApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ import {
import { OrganizationInvitationStatus, OrganizationMembershipRole } from '../../api/resources/Enums';
import { TestBackendAPIClient } from '../TestBackendAPI';

test('getOrganizationList() retrieves a list of organizations', async () => {
const resJSON = {
data: [
{
object: 'organization',
id: 'org_randomid',
name: 'Acme Inc',
slug: 'acme-inc',
public_metadata: {},
private_metadata: {},
created_at: 1611948436,
updated_at: 1611948436,
},
],
};
nock('https://api.clerk.dev').get('/v1/organizations?').reply(200, resJSON);

const organizationList = await TestBackendAPIClient.organizations.getOrganizationList();
expect(organizationList).toBeInstanceOf(Array);
expect(organizationList.length).toEqual(1);
expect(organizationList[0]).toBeInstanceOf(Organization);
});

test('createOrganization() creates an organization', async () => {
const name = 'Acme Inc';
const slug = 'acme-inc';
Expand Down
13 changes: 13 additions & 0 deletions packages/backend-core/src/api/collection/OrganizationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ type OrganizationMetadataParams = {
privateMetadata?: Record<string, unknown>;
};

type GetOrganizationListParams = {
limit?: number;
offset?: number;
};

type CreateParams = {
name: string;
slug?: string;
Expand Down Expand Up @@ -66,6 +71,14 @@ type RevokeOrganizationInvitationParams = {
};

export class OrganizationApi extends AbstractApi {
public async getOrganizationList(params?: GetOrganizationListParams) {
return this._restClient.makeRequest<Organization[]>({
method: 'GET',
path: basePath,
queryParams: params,
});
}

public async createOrganization(params: CreateParams) {
const { publicMetadata, privateMetadata } = params;
return this._restClient.makeRequest<Organization>({
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

## Overview

[Clerk](https://clerk.dev?utm_source=github&utm_medium=clerk_sdk_node) is the easiest way to add authentication and user management to your Node application. To gain a better understanding API and the SDK, refer to
[Clerk](https://clerk.dev?utm_source=github&utm_medium=clerk_sdk_node) is the easiest way to add authentication and user management to your Node application. To gain a better understanding API and the SDK, refer to
the <a href="https://reference.clerk.dev/reference/backend-api-reference" target="_blank">official Backend API documentation</a>.

## Table of contents
Expand Down

0 comments on commit a24c4d3

Please sign in to comment.