-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from deco-cx/feat/shopify-proxy
Feat/shopify proxy
- Loading branch information
Showing
5 changed files
with
145 additions
and
27 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Route } from "../../website/flags/audience.ts"; | ||
import { AppContext } from "../mod.ts"; | ||
|
||
const PATHS_TO_PROXY = [ | ||
"/checkouts/*", | ||
"/cart/*", | ||
"/account", | ||
"/account/*", | ||
"/password", | ||
"/password/*", | ||
"/challenge", | ||
"/challenge/*", | ||
]; | ||
const decoSiteMapUrl = "/sitemap/deco.xml"; | ||
|
||
const buildProxyRoutes = ( | ||
{ storeName, extraPaths, includeSiteMap, generateDecoSiteMap }: { | ||
storeName?: string; | ||
extraPaths: string[]; | ||
includeSiteMap?: string[]; | ||
generateDecoSiteMap?: boolean; | ||
}, | ||
) => { | ||
const publicUrl = new URL(`https://${storeName}.myshopify.com`); | ||
|
||
try { | ||
const hostname = publicUrl.hostname; | ||
|
||
// Rejects TLD mystore.com, keep this if Shopify doesn't support | ||
if (!hostname || hostname.split(".").length <= 2) { | ||
throw new Error(`Invalid hostname from '${publicUrl}'`); | ||
} | ||
|
||
// TODO @lucis: Fix the proxy, MITM | ||
|
||
const urlToProxy = `https://${hostname}`; | ||
const hostToUse = hostname; | ||
|
||
const routeFromPath = (pathTemplate: string): Route => ({ | ||
pathTemplate, | ||
handler: { | ||
value: { | ||
__resolveType: "website/handlers/proxy.ts", | ||
url: urlToProxy, | ||
host: hostToUse, | ||
}, | ||
}, | ||
}); | ||
const routesFromPaths = [...PATHS_TO_PROXY, ...extraPaths].map( | ||
routeFromPath, | ||
); | ||
|
||
const [_include, routes] = generateDecoSiteMap | ||
? [[...(includeSiteMap ?? []), decoSiteMapUrl], [{ | ||
pathTemplate: decoSiteMapUrl, | ||
handler: { | ||
value: { | ||
__resolveType: "website/handlers/sitemap.ts", | ||
}, | ||
}, | ||
}]] | ||
: [includeSiteMap, []]; | ||
|
||
return [ | ||
...routes, | ||
...routesFromPaths, | ||
]; | ||
} catch (e) { | ||
console.log("Error parsing publicUrl from Shopify"); | ||
console.error(e); | ||
return []; | ||
} | ||
}; | ||
|
||
export interface Props { | ||
extraPathsToProxy?: string[]; | ||
/** | ||
* @title Other site maps to include | ||
*/ | ||
includeSiteMap?: string[]; | ||
/** | ||
* @title If deco site map should be exposed at /deco-sitemap.xml | ||
*/ | ||
generateDecoSiteMap?: boolean; | ||
} | ||
|
||
/** | ||
* @title Shopify Proxy Routes | ||
*/ | ||
function loader( | ||
{ extraPathsToProxy = [], includeSiteMap = [], generateDecoSiteMap = true }: | ||
Props, | ||
_req: Request, | ||
ctx: AppContext, | ||
): Route[] { | ||
return buildProxyRoutes({ | ||
generateDecoSiteMap, | ||
includeSiteMap, | ||
storeName: ctx.storeName, | ||
extraPaths: extraPathsToProxy, | ||
}); | ||
} | ||
|
||
export default loader; |
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