-
Notifications
You must be signed in to change notification settings - Fork 109
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elangobharathi awesome! Welcome to the project! Great to see your contribution!
I had some thoughts here that I'd like to request changes on, but besides that, great work!
src/Get.tsx
Outdated
* If not, it is considered as relative url. | ||
* For example, | ||
* base = "http://example.com/people" and path = "/absolute" | ||
* reolves to "http://example.com/absolute" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reolves
-> resolves
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected. Thank you!
src/Get.tsx
Outdated
* base = "http://example.com/people" and path = "relative" | ||
* reolves to "http://example.com/people/relative" | ||
*/ | ||
const composedUrl = requestPath |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya, I understand. I have now refactored. Thanks!
src/Mutate.tsx
Outdated
@@ -122,8 +122,12 @@ class ContextlessMutate<TData, TError> extends React.Component< | |||
|
|||
const requestPath = | |||
verb === "DELETE" && typeof body === "string" | |||
? url.resolve(base!, url.resolve(path, body)) | |||
: url.resolve(base!, path); | |||
? path.startsWith("/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the other comment about nested ternaries. This also appears to be duplicated logic so maybe put it in a function that is unit tested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a great idea to put that in a function. I have moved it to a util function. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, but we can do better! I like the idea of a util function to composeUrl
, but I think that we can go a bit more deeper to avoid any url.resolve
outside of this util function.
Also don't forgot to add some tests to Mutate
and Poll
, even if it should almost be the same as Get
, we also have some subtilities over there 😉
src/Mutate.tsx
Outdated
verb === "DELETE" && typeof body === "string" | ||
? url.resolve(base!, url.resolve(path, body)) | ||
: url.resolve(base!, path); | ||
let requestPath: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that you can keep the ternary here, for 2 choices is fine 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it back to ternary 😃
src/Mutate.tsx
Outdated
<RestfulReactProvider | ||
{...contextProps} | ||
base={ | ||
props.path.startsWith("/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really check how url.resolve
works, but this should do the stuff for the both cases.
src/util/composeUrl.test.ts
Outdated
import { composeUrl } from "./composeUrl"; | ||
|
||
afterEach(() => { | ||
cleanup(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need to cleanup here, we don't have any react implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got you. Removed it. Thanks.
src/util/composeUrl.test.ts
Outdated
|
||
describe("compose url", () => { | ||
it("should provide absolute url", () => { | ||
const base = "https://my-awesome-api.fake/people"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test with a trailing slash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added now. Thanks.
src/Poll.tsx
Outdated
@@ -209,7 +210,7 @@ class ContextlessPoll<TData, TError> extends React.Component< | |||
const { lastPollIndex } = this.state; | |||
const requestOptions = this.getRequestOptions(); | |||
|
|||
const request = new Request(url.resolve(base!, path), { | |||
const request = new Request(path.startsWith("/") ? url.resolve(base!, path) : `${base!}/${path}`, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be replace by composeUrl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, replaced it now. Thanks
src/Get.tsx
Outdated
<RestfulReactProvider | ||
{...contextProps} | ||
base={ | ||
props.path.startsWith("/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be replace by composeUrl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, replaced it now. Thanks
src/Get.test.tsx
Outdated
</Get> | ||
</RestfulProvider>, | ||
); | ||
await wait(() => expect(children.mock.calls.length).toBe(6)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add some other expects with some data from nock to be sure that we have that we are expected here? I want to know if the .get("/absolute-2/relative-2/relative-3")
is well called
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, added them now. Thank you.
After checking how url.resolve('http://test.com/one/two/three', '/four'); // http://test.com/four
url.resolve('http://test.com/one/two/three', 'four'): // http://test.com/one/two/three/four I was wondering how it was working, and now I finally understand 😄 To provide a real implementation of this feature, you need to add some tests with some render(
<RestfulProvider base="https://my-awesome-api.fake/absolute-1">
<Get path="">
{() => (
<Get path="relative-1">
{() => (
<Get path="/absolute-2">
{() => <Get path="relative-2">{() => <Get path="relative-3">{children}</Get>}</Get>}
</Get>
)}
</Get>
)}
</Get>
</RestfulProvider>
) and also some trailing slashes. I think that |
Great catch, @fabien0102! @elangobharathi, apologies, we may have not been as clear in describing the issue: the idea is that the The current implementation always rolls up to the domain-level instead of a subpath on the |
I found this a while ago - CannerCMS@978a686 These changes were only made in their local branch. |
@TejasQ I am sorry I couldn't fully understand. Could you please explain me this with an example?
|
@fabien0102 Thank you for the comments :) It would be great if you can provide the expected result for your example test case. This would help me understand the expectations a bit more.
|
Absolutely! Here, let's use your PR description with some adjustments in order to make it a little more understandable:
^ this is correct.
^ this is incorrect. Instead,
^ Correct. NoteIn the nested cases, the immediate parent's url becomes base for composition.
emits Does that clear things up? Let me know if I can clarify further. 😄 |
@TejasQ was quicker than me 🥇 @elangobharathi is it clear for you or do you want some test cases? |
@TejasQ That cleared up the base with subpath case @fabien0102 @TejasQ I would definitely need the expected result for some complicated nesting cases. Can you provide the expected result for the following cases?
|
If I don't do any mistake, this should result like this: it("should compose more nested absolute and relative urls", async () => {
nock("https://my-awesome-api.fake")
.get("/absolute-1")
.reply(200);
nock("https://my-awesome-api.fake")
.get("/absolute-1/relative-1")
.reply(200);
nock("https://my-awesome-api.fake")
.get("absolute-1/absolute-2")
.reply(200);
nock("https://my-awesome-api.fake")
.get("absolute-1/absolute-2/relative-2")
.reply(200);
nock("https://my-awesome-api.fake")
.get("absolute-1/absolute-2/relative-2/relative-3")
.reply(200, { path: "absolute-1/absolute-2/relative-2/relative-3" });
const children = jest.fn();
children.mockReturnValue(<div />);
render(
<RestfulProvider base="https://my-awesome-api.fake/absolute-1">
<Get path="">
{() => (
<Get path="relative-1">
{() => (
<Get path="/absolute-2">
{() => <Get path="relative-2">{() => <Get path="relative-3">{children}</Get>}</Get>}
</Get>
)}
</Get>
)}
</Get>
</RestfulProvider>,
);
await wait(() => expect(children.mock.calls.length).toBe(6));
expect(children.mock.calls[5][0]).toEqual({ path: "absolute-1/absolute-2/relative-2/relative-3" });
}); And same for Note: I'm not totally sure abouth the |
@fabien0102 Thank you for the details 😊. @TejasQ I am waiting for your opinion.
|
He asked me! He is 💯 % correct. :) |
@fabien0102 @TejasQ Thank you for your comments. I have addressed them now. Please review and let me know your feedback 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @elangobharathi! I really like the approach. I'm wondering if there's a way we can more cleanly express (in terms of naming), the difference between baseRoot
and base
?
If I was using the library as a newcomer, I could find these two concepts fairly confusing.
Hi @TejasQ Yes, you are right. We should definitely rename them. How about naming them as For example,
While composing
Please let me know if you can think of any better names as well 😄 |
I love that naming! Great idea! |
@TejasQ Thank you. Renaming is done 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Let's see what @fabien0102 has to say about it. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay, I have totally missed this notification 😕
This looks awesome 🎉 Well tested and functionnal, nothing to say, very nice job 💯
@fabien0102 @TejasQ Thank you for the approvals 😊 |
So @elangobharathi if you can rebase it, we're good to go! WOOOO! THANK YOU FOR YOUR CONTRIBUTION! 🚀 |
431abc5
ebfa04a
to
431abc5
Compare
@TejasQ Thank you! I have just rebased it. Please check and merge. 😊 |
Thanks, Elango! It was a true pleasure working with you! |
Thanks @TejasQ ! It was a pleasure working with you too! |
Why
To compose relative urls explicitly using no forward slash in path value. Refer #29 for more background.
Outcome
If the path starts with forward slash, it is considered as absolute url. If not, it is considered as relative url.
For example,
whereas,
Note
In the nested cases, the immediate parent's url becomes base for composition.
For example,
emits
"https://my-awesome-api.fake/MY_SUBROUTE/absolute-1"
"https://my-awesome-api.fake/MY_SUBROUTE/absolute-1/relative-1"
"https://my-awesome-api.fake/MY_SUBROUTE/absolute-2"
"https://my-awesome-api.fake/MY_SUBROUTE/absolute-2/relative-2"
"https://my-awesome-api.fake/MY_SUBROUTE/absolute-2/relative-2/relative-3"