Skip to content

Commit

Permalink
Merge pull request #468 from bandada-infra/ref/api-sdk
Browse files Browse the repository at this point in the history
Refactor API SDK
  • Loading branch information
vplasencia authored Apr 1, 2024
2 parents 0c99739 + f55437e commit e5c65d6
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 103 deletions.
2 changes: 2 additions & 0 deletions apps/api/src/app/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ export class GroupsService {
.toString()
.slice(0, 32)

if (credentials === undefined) credentials = null

const group = this.groupRepository.create({
id: _groupId,
name,
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/pages/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default function HomePage(): JSX.Element {

const group = await getGroup(groupId)

if (group === null) {
if (group === null || group.credentials === null) {
setLoading(false)
return
}
Expand Down
8 changes: 3 additions & 5 deletions apps/client/src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ApiSdk, GroupResponse, InviteResponse } from "@bandada/api-sdk"
import { ApiSdk, Group, Invite } from "@bandada/api-sdk"

const api = new ApiSdk(import.meta.env.VITE_API_URL)

export async function getInvite(
inviteCode: string
): Promise<InviteResponse | null> {
export async function getInvite(inviteCode: string): Promise<Invite | null> {
try {
return await api.getInvite(inviteCode)
} catch (error: any) {
Expand All @@ -20,7 +18,7 @@ export async function getInvite(
}
}

export async function getGroup(groupId: string): Promise<GroupResponse | null> {
export async function getGroup(groupId: string): Promise<Group | null> {
try {
return await api.getGroup(groupId)
} catch (error: any) {
Expand Down
146 changes: 141 additions & 5 deletions libs/api-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,39 @@ yarn add @bandada/api-sdk

Creates a new instance of ApiSdk using the API URL and the [config](https://axios-http.com/docs/req_config).

- Creates a new instance using the Bandada API URL and the default config.
- Create a new instance using the Bandada API URL and the default config.

This is what you need if you are using the production Bandada API.

```ts
import { ApiSdk } from "@bandada/api-sdk"

const apiSdk = new ApiSdk()
```

- Creates a new instance using a custom API URL.
- Create a new instance using a [Supported URL](https://github.com/bandada-infra/bandada/blob/main/libs/api-sdk/src/types/index.ts#L43).

This is useful when working with the development environment.

```ts
import { ApiSdk, SupportedUrl } from "@bandada/api-sdk"

const apiSdk = new ApiSdk(SupportedUrl.DEV)
```

- Create a new instance using a custom API URL.

```ts
import { ApiSdk } from "@bandada/api-sdk"

const apiSdk = new ApiSdk("https://example.com/api")
```

- Creates a new instance using a custom API URL and config.
- Create a new instance using a custom API URL and config.

```ts
import { ApiSdk } from "@bandada/api-sdk"

const url = "https://example.com/api"
const config = {
headers: {
Expand All @@ -95,15 +113,122 @@ const config = {
const apiSdk = new ApiSdk(url, config)
```

\# **getGroups**(): _Promise\<GroupResponse[]>_
\# **createGroup**(): _Promise\<Group>_

Creates a Bandada group.

```ts
const groupCreateDetails = {
name: "Group 1",
description: "This is Group 1.",
treeDepth: 16,
fingerprintDuration: 3600
}
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

const group = await apiSdk.createGroup(groupCreateDetails, apiKey)
```

\# **createGroups**(): _Promise\<Group[]>_

Creates one or many Bandada groups.

```ts
const groupsCreateDetails = [
{
name: "Group 1",
description: "This is Group 1.",
treeDepth: 16,
fingerprintDuration: 3600
},
{
name: "Group 2",
description: "This is Group 2.",
treeDepth: 16,
fingerprintDuration: 3600
}
]
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

const groups = await apiSdk.createGroups(groupsCreateDetails, apiKey)
```

\# **removeGroup**(): _Promise\<void>_

Removes a specific Bandada group.

```ts
const groupId = "10402173435763029700781503965100"
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

await apiSdk.removeGroup(groupId, apiKey)
```

\# **removeGroups**(): _Promise\<void>_

Removes one or many Bandada groups.

```ts
const groupIds = [
"10402173435763029700781503965100",
"20402173435763029700781503965200"
]
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

await apiSdk.removeGroups(groupIds, apiKey)
```

\# **updateGroup**(): _Promise\<Group>_

Updates a specific Bandada group.

```ts
const groupId = "10402173435763029700781503965100"
const groupUpdateDetails = {
description: "This is a new group.",
treeDepth: 20,
fingerprintDuration: 4000
}
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

await apiSdk.updateGroup(groupId, groupUpdateDetails, apiKey)
```

\# **updateGroups**(): _Promise\<Group[]>_

Updates one or many Bandada groups.

```ts
const groupIds = [
"10402173435763029700781503965100",
"20402173435763029700781503965200"
]
const updatedGroups: Array<GroupUpdateDetails> = [
{
description: "This is a new group1.",
treeDepth: 32,
fingerprintDuration: 7200
},
{
description: "This is a new group2.",
treeDepth: 32,
fingerprintDuration: 7200
}
]
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

await apiSdk.updateGroups(groupId, groupUpdateDetails, apiKey)
```

\# **getGroups**(): _Promise\<Group[]>_

Returns the list of groups.

```ts
const groups = await apiSdk.getGroups()
```

\# **getGroup**(): _Promise\<GroupResponse>_
\# **getGroup**(): _Promise\<Group>_

Returns a specific group.

Expand Down Expand Up @@ -194,3 +319,14 @@ const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

await apiSdk.removeMembersByApiKey(groupId, memberIds, apiKey)
```

\# **getInvite**(): _Promise\<Invite>_

Returns a specific invite.

```ts
const inviteCode = "C5VAG4HD"
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

const invite = await apiSdk.getInvite(inviteCode)
```
54 changes: 31 additions & 23 deletions libs/api-sdk/src/apiSdk.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
SupportedUrl,
GroupResponse,
InviteResponse,
GroupRequest,
GroupUpdateRequest
Group,
Invite,
GroupCreationDetails,
GroupUpdateDetails
} from "./types"
import checkParameter from "./checkParameter"
import {
Expand Down Expand Up @@ -79,38 +79,46 @@ export default class ApiSdk {
* Returns the list of groups.
* @returns List of groups.
*/
async getGroups(): Promise<GroupResponse[]> {
async getGroups(): Promise<Group[]> {
const groups = await getGroups(this._config)

return groups
}

/**
* Creates a group using the API key.
* @param groupData Data to create the group.
* @param groupCreationDetails Data to create the group.
* @param apiKey The API key of the admin of the group.
* @returns The created group.
*/
async createGroup(
groupData: GroupRequest,
groupCreationDetails: GroupCreationDetails,
apiKey: string
): Promise<GroupResponse> {
const groups = await createGroups(this._config, [groupData], apiKey)
): Promise<Group> {
const groups = await createGroups(
this._config,
[groupCreationDetails],
apiKey
)

return groups[0]
}

/**
* Creates one or more groups using the API key.
* @param groupsData Data to create the groups.
* @param groupsCreationDetails Data to create the groups.
* @param apiKey The API key of the admin of the groups.
* @returns The created groups.
*/
async createGroups(
groupsData: Array<GroupRequest>,
groupsCreationDetails: Array<GroupCreationDetails>,
apiKey: string
): Promise<Array<GroupResponse>> {
const groups = await createGroups(this._config, groupsData, apiKey)
): Promise<Array<Group>> {
const groups = await createGroups(
this._config,
groupsCreationDetails,
apiKey
)

return groups
}
Expand Down Expand Up @@ -138,19 +146,19 @@ export default class ApiSdk {
/**
* Update a specific group using the API key.
* @param groupId The group id.
* @param groupData Data to update the group.
* @param groupUpdateDetails Data to update the group.
* @param apiKey The API key of the admin of the group.
* @returns The updated group.
*/
async updateGroup(
groupId: string,
groupData: GroupUpdateRequest,
groupUpdateDetails: GroupUpdateDetails,
apiKey: string
): Promise<GroupResponse> {
): Promise<Group> {
const group = await updateGroup(
this._config,
groupId,
groupData,
groupUpdateDetails,
apiKey
)

Expand All @@ -160,19 +168,19 @@ export default class ApiSdk {
/**
* Updats one or more groups using the API key.
* @param groupIds The group ids.
* @param groupsData Data to update the groups.
* @param groupsUpdateDetails Data to update the groups.
* @param apiKey The API key of the admin of the groups.
* @returns The updated groups.
*/
async updateGroups(
groupIds: Array<string>,
groupsData: Array<GroupUpdateRequest>,
groupsUpdateDetails: Array<GroupUpdateDetails>,
apiKey: string
): Promise<Array<GroupResponse>> {
): Promise<Array<Group>> {
const groups = await updateGroups(
this._config,
groupIds,
groupsData,
groupsUpdateDetails,
apiKey
)

Expand All @@ -184,7 +192,7 @@ export default class ApiSdk {
* @param groupId Group id.
* @returns Specific group.
*/
async getGroup(groupId: string): Promise<GroupResponse> {
async getGroup(groupId: string): Promise<Group> {
const group = await getGroup(this._config, groupId)

return group
Expand Down Expand Up @@ -301,7 +309,7 @@ export default class ApiSdk {
* @param inviteCode Invite code.
* @returns Specific invite.
*/
async getInvite(inviteCode: string): Promise<InviteResponse> {
async getInvite(inviteCode: string): Promise<Invite> {
const invite = getInvite(this._config, inviteCode)

return invite
Expand Down
Loading

0 comments on commit e5c65d6

Please sign in to comment.