Skip to content

Commit

Permalink
feat: implemented frontend api client
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldziher committed Jan 15, 2024
1 parent b9e0761 commit 8239c12
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 55 deletions.
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@testing-library/dom": "^9.3.4",
"@testing-library/jest-dom": "^6.2.0",
"@testing-library/react": "^14.1.2",
"@types/react": "18.2.47",
"@types/react": "18.2.48",
"@types/react-dom": "18.2.18",
"@types/react-syntax-highlighter": "^15.5.11",
"@types/validator": "^13.11.8",
Expand Down
74 changes: 74 additions & 0 deletions frontend/src/api/project-invitation-api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { ProjectFactory, ProjectInvitationFactory } from 'tests/factories';
import { mockFetch } from 'tests/mocks';

import {
handleDeleteProjectInvitation,
handleRetrieveProjectInvitations,
} from '@/api/project-invitation-api';
import { HttpMethod } from '@/constants';

describe('project invitations API tests', () => {
const bearerToken = 'Bearer test_token';

describe('handleRetrieveProjectInvitations', () => {
it('returns a list of project users', async () => {
const project = await ProjectFactory.build();
const projectInvitations = await ProjectInvitationFactory.batch(2);

mockFetch.mockResolvedValueOnce({
json: () => Promise.resolve(projectInvitations),
ok: true,
});

const data = await handleRetrieveProjectInvitations({
projectId: project.id,
});

expect(data).toEqual(projectInvitations);
expect(mockFetch).toHaveBeenCalledWith(
new URL(
`http://www.example.com/v1/projects/${project.id}/invitation/`,
),
{
headers: {
'Authorization': bearerToken,
'Content-Type': 'application/json',
'X-Request-Id': expect.any(String),
},
method: HttpMethod.Get,
},
);
});
});

describe('handleDeleteProjectInvitation', () => {
it('returns a list of project users', async () => {
const project = await ProjectFactory.build();
const projectInvitation = await ProjectInvitationFactory.build();

mockFetch.mockResolvedValueOnce({
json: () => Promise.resolve(),
ok: true,
});

await handleDeleteProjectInvitation({
invitationId: projectInvitation.id,
projectId: project.id,
});

expect(mockFetch).toHaveBeenCalledWith(
new URL(
`http://www.example.com/v1/projects/${project.id}/invitation/${projectInvitation.id}/`,
),
{
headers: {
'Authorization': bearerToken,
'Content-Type': 'application/json',
'X-Request-Id': expect.any(String),
},
method: HttpMethod.Delete,
},
);
});
});
});
27 changes: 27 additions & 0 deletions frontend/src/api/project-invitation-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { fetcher } from '@/api/fetcher';
import { HttpMethod } from '@/constants';
import { ProjectInvitation } from '@/types';

export async function handleRetrieveProjectInvitations({
projectId,
}: {
projectId: string;
}): Promise<ProjectInvitation[]> {
return await fetcher<ProjectInvitation[]>({
method: HttpMethod.Get,
url: `projects/${projectId}/invitation/`,
});
}

export async function handleDeleteProjectInvitation({
projectId,
invitationId,
}: {
invitationId: string;
projectId: string;
}): Promise<void> {
await fetcher<undefined>({
method: HttpMethod.Delete,
url: `projects/${projectId}/invitation/${invitationId}/`,
});
}
10 changes: 10 additions & 0 deletions frontend/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,13 @@ export interface PromptTestRecord<T extends ModelVendor> {
totalTokensCost: string;
userInput: Record<string, string>;
}

// Project Invitation

export interface ProjectInvitation {
createdAt: string;
email: string;
id: string;
permission: AccessPermission;
updatedAt: string;
}
11 changes: 11 additions & 0 deletions frontend/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
OpenAIModelType,
OpenAIPromptMessage,
Project,
ProjectInvitation,
ProjectUserAccount,
PromptConfig,
PromptTestRecord,
Expand Down Expand Up @@ -146,3 +147,13 @@ export const PromptTestRecordFactory = new TypeFactory<PromptTestRecord<any>>(
userInput: { userInput: 'what am I?' },
}),
);

export const ProjectInvitationFactory = new TypeFactory<ProjectInvitation>(
() => ({
createdAt: faker.date.past().toISOString(),
email: faker.internet.email(),
id: faker.string.uuid(),
permission: AccessPermission.ADMIN,
updatedAt: faker.date.past().toISOString(),
}),
);
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,6 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74=
github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-redis/cache/v9 v9.0.0 h1:0thdtFo0xJi0/WXbRVu8B066z8OvVymXTJGaXrVWnN0=
Expand Down Expand Up @@ -939,8 +937,6 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.1 h1:5I9etrGkLrN+2XPCsi6XLlV5DITbSL/xBZdmAxFcXPI=
github.com/jackc/pgx/v5 v5.5.1/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/pgx/v5 v5.5.2 h1:iLlpgp4Cp/gC9Xuscl7lFL1PhhW+ZLtXZcrfCt4C3tA=
github.com/jackc/pgx/v5 v5.5.2/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@protobuf-ts/runtime": "^2.9.3",
"@protobuf-ts/runtime-rpc": "^2.9.3",
"@tool-belt/eslint-config": "^5.0.5",
"@types/node": "20.11.0",
"@types/node": "20.11.1",
"@types/webpack": "^5.28.5",
"@types/webpack-node-externals": "^3.0.4",
"@typescript-eslint/eslint-plugin": "^6.18.1",
Expand Down
Loading

0 comments on commit 8239c12

Please sign in to comment.