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

Introduce public fetch method for reading complete responses; introduce new head() method #110

Merged
merged 17 commits into from
Dec 12, 2022

Conversation

trevor-scheer
Copy link
Member

Introduce a public fetch method which returns a complete response object and the parsed body rather than just the parsed body. This is useful when additional information about the response is needed, such as accessing response headers.

With the introduction of the fetch method, providing a new head() method is now trivial. Previously, a head() method was useless without access to the response headers (and access to the body is semantically meaningless for a HEAD request. Unlike the other fetch method methods, the head() method returns the response object rather than a body.

Fixes #27, Fixes #49

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 5, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@trevor-scheer trevor-scheer changed the title initial commit Introduce public fetch method for reading complete responses; introduce new head() method Dec 5, 2022
@glasser
Copy link
Member

glasser commented Dec 9, 2022

After this is merged, we can add something to the larger fetch return value that includes cache info (#41)

src/HTTPCache.ts Outdated Show resolved Hide resolved
@@ -489,4 +489,23 @@ describe('HTTPCache', () => {

expect(await response.json()).toEqual({ name: 'Ada Lovelace' });
});

describe('HEAD requests', () => {
it('returns a cached GET response when the method is HEAD', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

is this a good thing? seems strange to me to get something with a body from a HEAD.

Copy link
Member Author

Choose a reason for hiding this comment

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

Squashed this behavior via 4cf502e

Copy link
Member

Choose a reason for hiding this comment

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

why isn't the test failing then?

@trevor-scheer trevor-scheer force-pushed the trevor/complete-response-fetch-head-method branch from d82e06c to 7d6cb82 Compare December 9, 2022 18:23
@trevor-scheer trevor-scheer marked this pull request as ready for review December 9, 2022 18:27
Update default `cacheKeyFor` to include method

In its previous form, `cacheKeyFor` only used the URL to calculate the cache
key. As a result, when `cacheOptions.ttl` was specified, the method was ignored.
Copy link
Member

Choose a reason for hiding this comment

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

Changesets in this PR are inconsistent as to whether lines are hard-wrapped or not. Not sure what we usually do or if it matters (I think we mostly don't hard-wrap lines in MD files? definitely not in docs?) but I definitely noticed on GH that it's not consistent across the 3 here.

src/HTTPCache.ts Outdated Show resolved Hide resolved
src/__tests__/RESTDataSource.test.ts Show resolved Hide resolved
@@ -489,4 +489,23 @@ describe('HTTPCache', () => {

expect(await response.json()).toEqual({ name: 'Ada Lovelace' });
});

describe('HEAD requests', () => {
it('returns a cached GET response when the method is HEAD', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

why isn't the test failing then?

@trevor-scheer trevor-scheer force-pushed the trevor/complete-response-fetch-head-method branch from 9d55193 to 6817d22 Compare December 9, 2022 21:33
@trevor-scheer trevor-scheer force-pushed the trevor/complete-response-fetch-head-method branch from 998145e to 3a81baf Compare December 9, 2022 21:41
README.md Outdated
const cacheKey = this.cacheKeyFor(url, request);
if (request.method === 'GET') {
if (['GET', 'HEAD'].includes(request.method)) {
Copy link
Member

Choose a reason for hiding this comment

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

I think you need the const method = here too

@trevor-scheer trevor-scheer merged commit ea43a27 into main Dec 12, 2022
@trevor-scheer trevor-scheer deleted the trevor/complete-response-fetch-head-method branch December 12, 2022 17:07
@github-actions github-actions bot mentioned this pull request Dec 12, 2022
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.

header in datasource-rest add HEAD requests in RESTDataSource Class
2 participants