Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cms/api): add basic API client #238

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open

Conversation

Varun-Sethu
Copy link
Member

PR adds a really small and basic API client for interfacing with the API, the client provides strict type definitions to make refactoring easier if we ever change the API contract between FE and BE (also serves as an implicit source of documentation)

@Varun-Sethu Varun-Sethu force-pushed the varun/APIClient branch 7 times, most recently from ad8b3b8 to 91d59fd Compare September 22, 2022 03:59
@lhjt lhjt changed the title Adding basic API Client feat(backend/api): add basic API Client Sep 23, 2022
@lhjt lhjt changed the title feat(backend/api): add basic API Client feat(cms/api): add basic API Client Sep 23, 2022
@lhjt lhjt changed the title feat(cms/api): add basic API Client feat(cms/api): add basic API client Sep 23, 2022
@lhjt lhjt added enhancement New feature or request cms labels Sep 23, 2022
@lhjt lhjt requested a review from a team as a code owner September 23, 2022 12:43
@lhjt lhjt requested a review from a team as a code owner September 23, 2022 13:51
@lhjt lhjt requested a review from fafnirZ as a code owner September 28, 2022 13:27
Copy link
Contributor

@zax-xyz zax-xyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't lgtm 😎

Comment on lines +74 to +76
return response.ok
? (await response.json()).Response as ResponseType
: (await response.json()) as APIError;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if response.json() fails? gotta double abstract error handling 😳

Comment on lines +1 to +2
import { CreateFilesystemEntryResponse, FilesystemEntry } from "./types/filesystem";
import { APIError, EmptyAPIResponse } from "./types/general";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on using import type since none of these are being used as values?

related: https://typescript-eslint.io/rules/consistent-type-imports/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oooh I didnt even know this was a thing 😎

// Only interface with the BE FS APIs via this class
export class FilesystemAPI {
// GetEntityInfo retrieves all entity information for an FS entity given its ID
public static GetEntityInfo = (EntityID: string): Promise<FilesystemEntry | APIError> =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on abstracting these return types since they're all of the same format (Promise<T | APIError>)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO I feel like that's kinda unnecessary, its a single line type


// SendGetRequest is a small helper functions for sending get request and wrapping the response in an appropriate type
static async SendGetRequest<ResponseType> (url: string): Promise<ResponseType | APIError> {
const response = await fetch(`${API_URL}${url}`);
Copy link
Contributor

@zax-xyz zax-xyz Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on catching errors here as well (e.g., invalid url or dns lookup failures)? (ayo triple abstraction??)

Comment on lines +15 to +16
export const hasFieldOfType = (o: any, fieldName: string, type: string): boolean =>
fieldName in o && (typeof o[fieldName]) === type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if it makes more sense to use fieldType here since you're using fieldName as well - i got really confused here for a minute because i thought type was some sort of built-in, or some typescript magic was going on here 😳

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah thats a good point, shall rename to fieldType

hasFieldOfType(o, "EntityName", "string") &&
hasFieldOfType(o, "IsDocument", "boolean") &&
hasFieldOfType(o, "Parent", "string") &&
hasFieldOfType(o, "Children", typeof []) &&
Copy link
Contributor

@zax-xyz zax-xyz Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't very reliable since typeof [] is just "object" (javascript moment) - you probably want to use Array.isArray here

Copy link
Contributor

@zax-xyz zax-xyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on using stricter linter configurations and/or an autoformatter?

Comment on lines +34 to +43
run: |
GO_MOD=go.mod docker compose --env-file=./config/ci.env.dev up --wait --build backend db staging_db frontend
- name: Backend Logs
run: |
docker logs go_backend
docker logs pg_container
docker ps
docker logs go_backend
# docker exec frontend curl http://backend:8080/api/filesystem/info
# docker exec frontend npm test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh sorry so just to clarify but how is the docker compose build part running any sort of tests? And should should the lines at the bottom be commented out?

cc @sachk

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smh i pointed this out and now jared is STEALING the credit - more ABUSE by csesoc execs :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ketchup-arma3

Copy link
Member Author

@Varun-Sethu Varun-Sethu Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bottom two lines were commented for testing reasons (they should not be commented out), the entire PR is still blocked by the weird networking issue 😢

@Varun-Sethu
Copy link
Member Author

thoughts on using stricter linter configurations and/or an autoformatter?

Hmm seems like its worth doing although u probably want to talk to the FE team abt that


// publish it
const publishResp = await FilesystemAPI.RenameEntity(newEntityId, "docMcStuffins");
expect(IsEmptyApiResponse(publishResp), 'Expected deletion response to be assignable to empty');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tsk tsk 👆👆


// Create a document
const newDocument = await FilesystemAPI.CreateDocument("ebic document of truth", root.EntityID);
expect(IsCreateFilesystemEntryResponse(newDocument), "Expected CreateDocument response to be assignable to CreateFilesystemEntryResponse").toBe(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn’t createDocument take in 3 arguments?

FilesystemAPI.SendGetRequest<FilesystemEntry>(`/api/filesystem/info`);

// CreateEntity constructs an editable un-published filesystem entry
public static CreateDocument = (Name: string, ParentID: string, ownerGroup = 1): Promise<CreateFilesystemEntryResponse | APIError> =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking but it should be CreateDocument not CreateEntity in the comment

Also, some filesystem endpoints are missing, is that intentional 👀

@@ -30,5 +30,7 @@ func main() {
handler := cors.Default().Handler(mux)
handler = c.Handler(handler)

log.Print("CMS Go backend starting on port :8080 :D.")
log.Print("Amongus.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍣

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cms enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants