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

Define model and refactor ApiClient #59

Merged
merged 24 commits into from
Sep 26, 2024
Merged

Define model and refactor ApiClient #59

merged 24 commits into from
Sep 26, 2024

Conversation

psrpinto
Copy link
Member

@psrpinto psrpinto commented Sep 25, 2024

It's probably best to review the final result as some iteration was involved

Prior to this PR, I think there were some issues with the architecture:

  1. The data model of our app wasn't explicitly defined. For this reason, the Post model was split between the api/ and the parser/, for example.
  2. Because of the lack of a defined model, the format of the API was being "leaked" into other parts of the app (e.g. UI screens depended on types defined under api/. This would tie the app to the format of the API. (There is some discussion on that here).
  3. ApiClient contained all the methods of all data types (e.g. post, settings, users, etc) so it wouldn't scale (it would become a huge file that handles all types of data).

This PR fixes all of the above issues, by:

  1. Specifying the model of our app under model/, which contains (for now) Post, SiteSettings and User. These are the structures that hold the data our app needs, in a format that is adequate for our use cases.
  2. Splitting ApiClient into multiple modules, so that you can call apiClient.posts.create() instead of apiClient.createPost(). Each module gets its own class, and can implement whatever methods are required for the given API.
  3. Making it so that ApiClient is an "adapter" that always returns instances of our model types, instead of API-specific types. In short, types defined under api/ should never be used outside of api/. The app should only used the types defined under model/.

I'm doing this now because I was experiencing issues due to the lack of a model when implementing the feature that retrieves an existing post and displays it on the app (title, title parsed, date, date parsed, etc), and also because I think it's better to address these issues early so that we can build on top of a strong foundation.

Some code examples:

const client = new ApiClient( ... );

const post = await client.posts.create( { guid: 'foo' });
// post is an instance of model/Post

const updatedPost = await client.posts.update( post.id, {
	title: new PostTitle( 'Foo original', 'Foo parsed')
} );
// updatedPost is an instance of model/Post

const gotPost = await client.posts.getByGuid( 'foo' );
// gotPost is an instance of model/Post

@psrpinto psrpinto marked this pull request as ready for review September 25, 2024 15:32
Copy link
Member

@ashfame ashfame left a comment

Choose a reason for hiding this comment

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

Great work! Structure of meta fields on post object in REST API response is what I chose. Let me know if you think changing any of that would simplify anything here as well.

@psrpinto psrpinto merged commit cfb1636 into trunk Sep 26, 2024
3 checks passed
@psrpinto psrpinto deleted the model branch October 7, 2024 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants