Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
PR Feedback
Browse files Browse the repository at this point in the history
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
byrichardpowell committed Jul 13, 2022
1 parent 467c386 commit 05ea6a2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 54 deletions.
8 changes: 5 additions & 3 deletions docs/usage/utils.md
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.
51 changes: 51 additions & 0 deletions src/utils/__tests__/get-embedded-app-url.test.ts
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',
);
});
});
47 changes: 0 additions & 47 deletions src/utils/__tests__/get-host-app-url.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import {Context} from '../context';
*
* @param request Current HTTP request
*/
export default function getHostAppUrl(request: http.IncomingMessage): string {
export default function getEmbeddedAppUrl(
request: http.IncomingMessage,
): string {
if (!request) {
throw new ShopifyErrors.MissingRequiredArgument(
'getHostAppUrl requires a request object argument',
'getEmbeddedAppUrl requires a request object argument',
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import validateHmac from './hmac-validator';
import validateShop from './shop-validator';
import versionCompatible from './version-compatible';
import withSession from './with-session';
import getHostAppUrl from './get-host-app-url';
import getEmbeddedAppUrl from './get-embedded-app-url';

const ShopifyUtils = {
decodeSessionToken,
Expand All @@ -27,7 +27,7 @@ const ShopifyUtils = {
validateShop,
versionCompatible,
withSession,
getHostAppUrl,
getEmbeddedAppUrl,
};

export default ShopifyUtils;

0 comments on commit 05ea6a2

Please sign in to comment.