This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Correct typo 2. Change utility name "getHostAppUrl" to "getEmbeddedAppUrl", which is much nicer. 3. Add / to the host in our test. This matches the format of the host that Shopify provides 4. Flesh out the docs a little more.
- Loading branch information
1 parent
467c386
commit 05ea6a2
Showing
5 changed files
with
62 additions
and
54 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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
# utils | ||
|
||
## Get the HOST application URL using `getHostAppUrl` | ||
## Get the application URL for embedded apps using `getEmbeddedAppUrl` | ||
|
||
If you need to redirect a request to your app inside the user's admin you can use `getHostAppUrl`. | ||
If you need to redirect a request to your embedded app URL you can use `getEmbeddedAppUrl` | ||
|
||
```ts | ||
const redirectURL = getHostAppUrl(request); | ||
const redirectURL = getEmbeddedAppUrl(request); | ||
res.redirect(redirectURL); | ||
``` | ||
|
||
Using this utility ensures that embedded app URL is properly constructed and brings the merchant to the right place. It is more reliable than using the shop param. | ||
|
||
This utility relies on the host query param being a Base 64 encoded string. All requests from Shopify should include this param in the correct format. |
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,51 @@ | ||
import http from 'http'; | ||
|
||
import getEmbeddedAppUrl from '../get-embedded-app-url'; | ||
import * as ShopifyErrors from '../../error'; | ||
import {Context} from '../../context'; | ||
|
||
describe('getEmbeddedAppUrl', () => { | ||
beforeEach(() => { | ||
Context.API_KEY = 'my-api-key'; | ||
}); | ||
|
||
test('throws an error when no request is passed', () => { | ||
// @ts-expect-error: For JS users test it throws when no request is passed | ||
expect(() => getEmbeddedAppUrl()).toThrow( | ||
ShopifyErrors.MissingRequiredArgument, | ||
); | ||
}); | ||
|
||
test('throws an error when the request has no URL', () => { | ||
const req = { | ||
url: undefined, | ||
} as http.IncomingMessage; | ||
|
||
expect(() => getEmbeddedAppUrl(req)).toThrow( | ||
ShopifyErrors.InvalidRequestError, | ||
); | ||
}); | ||
|
||
test('throws an error when the request has no host query param', () => { | ||
const req = { | ||
url: 'http://www.example.com', | ||
} as http.IncomingMessage; | ||
|
||
expect(() => getEmbeddedAppUrl(req)).toThrow( | ||
ShopifyErrors.InvalidRequestError, | ||
); | ||
}); | ||
|
||
test('returns the host app url', () => { | ||
const host = 'my-store.shopify.com/admin'; | ||
const base64Host = Buffer.from(host, 'utf-8').toString('base64'); | ||
|
||
const req = { | ||
url: `http://www.example.com?host=${base64Host}`, | ||
} as http.IncomingMessage; | ||
|
||
expect(getEmbeddedAppUrl(req)).toBe( | ||
'https://my-store.shopify.com/admin/apps/my-api-key', | ||
); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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
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