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. Use new URL, rather than url.parse, which is to be deprecated.
  • Loading branch information
byrichardpowell committed Jul 12, 2022
1 parent 467c386 commit 02bd65c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
6 changes: 3 additions & 3 deletions docs/usage/utils.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# 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);
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
import http from 'http';

import getHostAppUrl from '../get-host-app-url';
import getEmbeddedAppUrl from '../get-embedded-app-url';
import * as ShopifyErrors from '../../error';
import {Context} from '../../context';

describe('getHostAppUrl', () => {
describe('getEmbeddedAppUrl', () => {
beforeEach(() => {
Context.API_KEY = 'my-api-key';
});

test('thows an error when no request is passed', () => {
test('throws an error when no request is passed', () => {
// @ts-expect-error: For JS users test it throws when no request is passed
expect(() => getHostAppUrl()).toThrow(
expect(() => getEmbeddedAppUrl()).toThrow(
ShopifyErrors.MissingRequiredArgument,
);
});

test('thows an error when the request has no URL', () => {
test('throws an error when the request has no URL', () => {
const req = {
url: undefined,
} as http.IncomingMessage;

expect(() => getHostAppUrl(req)).toThrow(ShopifyErrors.InvalidRequestError);
expect(() => getEmbeddedAppUrl(req)).toThrow(
ShopifyErrors.InvalidRequestError,
);
});

test('thows an error when the request has no host query param', () => {
test('throws an error when the request has no host query param', () => {
const req = {
url: 'www.example.com',
url: 'http://www.example.com',
} as http.IncomingMessage;

expect(() => getHostAppUrl(req)).toThrow(ShopifyErrors.InvalidRequestError);
expect(() => getEmbeddedAppUrl(req)).toThrow(
ShopifyErrors.InvalidRequestError,
);
});

test('returns the host app url', () => {
const host = 'my-store.shopify.com';
const base64Host = Buffer.from(host, 'utf-8').toString('base64');

const req = {
url: `www.example.com?host=${base64Host}`,
url: `http://www.example.com?host=${base64Host}`,
} as http.IncomingMessage;

expect(getHostAppUrl(req)).toBe(
expect(getEmbeddedAppUrl(req)).toBe(
'https://my-store.shopify.com/apps/my-api-key',
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import http from 'http';
import url from 'url';

import * as ShopifyErrors from '../error';
import {Context} from '../context';
Expand All @@ -9,10 +8,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 All @@ -22,15 +23,15 @@ export default function getHostAppUrl(request: http.IncomingMessage): string {
);
}

const query = url.parse(request.url, true).query;
const host = new URL(request.url).searchParams.get('host');

if (typeof query.host !== 'string') {
if (!host) {
throw new ShopifyErrors.InvalidRequestError(
'Request does not contain a host query parameter',
);
}

const host = Buffer.from(query.host, 'base64').toString();
const decodedHost = Buffer.from(host, 'base64').toString();

return `https://${host}/apps/${Context.API_KEY}`;
return `https://${decodedHost}/apps/${Context.API_KEY}`;
}
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 02bd65c

Please # to comment.