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

swap functions for classes #1

Merged
merged 4 commits into from
Jan 8, 2019
Merged

Conversation

jcreamer898
Copy link

Just some ideas around using classes to reduce some duplication.

Copy link
Owner

@kwelch kwelch left a comment

Choose a reason for hiding this comment

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

I have a few comments, but in general this is much cleaner. I just need your thoughts on those few places I can make the actual changes.

@@ -103,7 +103,7 @@ describe('request', () => {

expect(users).toBeDefined();
Object.keys(users).forEach((key) => {
const value = users[key];
const value = (users as any)[key];
Copy link
Owner

Choose a reason for hiding this comment

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

why as any? it should be an object. There wasn't any type issues here before

Copy link
Author

Choose a reason for hiding this comment

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

It was still blowing up, but I'll double check.

Copy link
Author

Choose a reason for hiding this comment

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

It does this without that...

Copy link
Owner

Choose a reason for hiding this comment

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

That was the issue I was running into. I fixed it by adding

[key:string]: () => Promise<User>

Which led to the original question.


describe('users.me()', () => {
let users: UserApi;

beforeEach(() => {
Copy link
Owner

Choose a reason for hiding this comment

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

do we prefer multiple beforeEach or single instance for the tests

Copy link
Author

Choose a reason for hiding this comment

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

Probably multiple in case there needs to be a different setup in one of the tests.

export abstract class BaseApi<T> {
request: JSONRequest<T>;

constructor(req: JSONRequest<T>) {
Copy link
Owner

Choose a reason for hiding this comment

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

Can we just make this JSONRequest<{}>? That way we don't have to double instance before and then the makeJsonRequest can take in a JSONRequest<{}> and then return a <T>. I really like the ideas of generics, but I feel like we may be over using it here a few times.

Copy link
Author

Choose a reason for hiding this comment

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

You can't do that because it needs to know which type you setup in the inherited class.

};
};
}: SdkConfig = {}): Sdk => ({
request: makeRequest(baseUrl, token),
Copy link
Owner

Choose a reason for hiding this comment

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

See notes about making this using the same function.

Copy link
Author

Choose a reason for hiding this comment

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

Not sure I see another note?


export interface SdkConfig {
token?: string;
baseUrl?: string;
}

export type JSONRequest = (
export type JSONRequest<T = {}> = (
Copy link
Owner

Choose a reason for hiding this comment

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

Actually it looks like sine this has a default, we could probably leave it off and it should work in most of the cases.

Copy link
Author

Choose a reason for hiding this comment

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

This is what happens when there's no default...

Copy link
Owner

Choose a reason for hiding this comment

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

Right, I want us to keep the default. I was talking about the other places.

/**
* API for working with Users
*/
export class UserApi extends BaseApi<User> {
Copy link
Owner

Choose a reason for hiding this comment

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

This looks so much cleaner. 👍

Copy link
Author

Choose a reason for hiding this comment

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

Thanks!

@kwelch
Copy link
Owner

kwelch commented Jan 8, 2019

Going to merge. We can transfer some of this conversation to the main PR.

@kwelch kwelch merged commit 392ac6d into kwelch:add-users-api Jan 8, 2019
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