-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-source-wordpress): adjust how endpoint urls are constructe…
…d to fix fetching for wordpress.com hosted sites and proxied urls (#10624) ## Description As reported in #10427 some WordPress.com sites return a `_links.$.self` with a fullpath `https://public-api.wordpress.com/wp/v2/sites/$site/users/me` and others without `https://public-api.wordpress.com/`. This means it is not a reliable way of removing the `baseUrl` from the `fullUrl`. Instead we pass the route key in as the `fullPath` rather than the `fullUrl`. We then extract the `basePath` from the `baseUrl` before finally removing the `basePath` from the `fullPath`. We also need to extract the raw entity type from the `fullPath` rather than route. We also need to build the `fullUrl` from `baseUrl` and `fullPath` so I introduced the `buildFullUrl` function to do just that. I’ve included tests for both WordPress.com and WordPress.org. ## Related Issues Fixes #10427.
- Loading branch information
1 parent
bc7d472
commit 85b8749
Showing
2 changed files
with
80 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const fetch = require(`../fetch`) | ||
|
||
describe(`Fetching`, () => { | ||
it(`gets raw entity type for wordpress.com`, () => { | ||
const key = `/wp/v2/sites/example.wordpress.com/posts` | ||
const path = fetch.getRawEntityType(key) | ||
expect(path).toEqual(`posts`) | ||
}) | ||
|
||
it(`gets route path for wordpress.com by removing base path from the full path`, () => { | ||
const key = `/wp/v2/sites/example.wordpress.com/posts` | ||
const baseUrl = `https://public-api.wordpress.com/wp/v2/sites/example.wordpress.com` | ||
const path = fetch.getRoutePath(baseUrl, key) | ||
expect(path).toEqual(`/posts`) | ||
}) | ||
|
||
it(`gets route path for wordpress.org by removing base path from the full path`, () => { | ||
const key = `/wp-json/wp/v2/posts` | ||
const baseUrl = `http://dev-gatbsyjswp.pantheonsite.io/wp-json` | ||
const path = fetch.getRoutePath(baseUrl, key) | ||
expect(path).toEqual(`/wp/v2/posts`) | ||
}) | ||
|
||
it(`builds full URL correctly for wordpress.com`, () => { | ||
const key = `/wp/v2/sites/example.wordpress.com/posts` | ||
const baseUrl = `https://public-api.wordpress.com/wp/v2/sites/example.wordpress.com` | ||
const fullUrl = fetch.buildFullUrl(baseUrl, key, true) | ||
expect(fullUrl).toEqual( | ||
`https://public-api.wordpress.com/wp/v2/sites/example.wordpress.com/posts` | ||
) | ||
}) | ||
|
||
it(`builds full URL correctly for wordpress.org`, () => { | ||
const key = `/wp/v2/posts` | ||
const baseUrl = `http://dev-gatbsyjswp.pantheonsite.io/wp-json` | ||
const fullUrl = fetch.buildFullUrl(baseUrl, key, false) | ||
expect(fullUrl).toEqual( | ||
`http://dev-gatbsyjswp.pantheonsite.io/wp-json/wp/v2/posts` | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters