Skip to content

Commit

Permalink
Use same props as commerce app (#19)
Browse files Browse the repository at this point in the history
* Depend on commerce app

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Use commerce props

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Use commerce apps

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Share app state with target app

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

---------

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
  • Loading branch information
mcandeia authored Aug 31, 2023
1 parent 878f301 commit 2ac7502
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
5 changes: 1 addition & 4 deletions compat/$live/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { buildSourceMap } from "$live/blocks/utils.tsx";
import type { Manifest as LiveManifest } from "$live/live.gen.ts";

import type { Manifest as WebSiteManifest } from "../../website/manifest.gen.ts";
import webSite from "../../website/mod.ts";
import webSite, { Props } from "../../website/mod.ts";
import type { Manifest as WorkflowsManifest } from "../../workflows/manifest.gen.ts";

import { SourceMap } from "$live/blocks/app.ts";
Expand Down Expand Up @@ -83,9 +83,6 @@ const manifestMappings: ManifestMappings = {
"$live/flags/multivariate.ts": "website/flags/multivariate.ts",
},
};
// deno-lint-ignore no-empty-interface
export interface Props {
}
/**
* @title $live
*/
Expand Down
73 changes: 47 additions & 26 deletions compat/std/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { Manifest as VNDAManifest } from "../../vnda/manifest.gen.ts";
import vnda, { Props as VNDAProps } from "../../vnda/mod.ts";
import type { Manifest as VTEXManifest } from "../../vtex/manifest.gen.ts";
import vtex, { Props as VTEXProps } from "../../vtex/mod.ts";
import commerce, { Props as CommerceProps } from "../../commerce/mod.ts";

import type { Manifest as WebSiteManifest } from "../../website/manifest.gen.ts";
import type {
ShopifyAccount,
Expand Down Expand Up @@ -209,72 +211,88 @@ type Manifest = {
: ManifestWithStdCompat[key];
};

export type Props = VTEXProps | ShopifyProps | VNDAProps;
type AvailableCommerceProps = CommerceProps["commerce"];

const isVTEXProps = (props: Props): props is VTEXProps => {
return (props as VTEXProps).platform === "vtex";
const isVTEXProps = (props: AvailableCommerceProps): props is VTEXProps => {
return (props as VTEXProps)?.platform === "vtex";
};

const isShopifyProps = (props: Props): props is ShopifyProps => {
return (props as ShopifyProps).platform === "shopify";
const isShopifyProps = (
props: AvailableCommerceProps,
): props is ShopifyProps => {
return (props as ShopifyProps)?.platform === "shopify";
};

const isVNDAProps = (props: Props): props is VNDAProps => {
return (props as VNDAProps).platform === "vnda";
const isVNDAProps = (props: AvailableCommerceProps): props is VNDAProps => {
return (props as VNDAProps)?.platform === "vnda";
};
export type State = {
configVTEX?: VTEXAccount;
configShopify?: ShopifyAccount;
configVNDA?: VNDAAccount;
} & Props;
} & AvailableCommerceProps;

export type { CommerceProps as Props };
export default function Std(
props: Props,
): App<Manifest, State, [ReturnType<typeof $live>]> {
props: CommerceProps,
): App<
Manifest,
State,
[ReturnType<typeof $live>, ReturnType<typeof commerce>]
> {
const targetApps: Record<
string,
{ manifest: AppManifest; sourceMap: SourceMap }
> = {};
const state: State = { ...props };
if (isVTEXProps(props)) {

const { dependencies, ...commerceApp } = commerce(
props,
);
let state: State = { ...props.commerce };
if (isVTEXProps(props.commerce)) {
state.configVTEX = {
defaultLocale: "pt-BR",
defaultPriceCurrency: "BRL",
defaultSalesChannel: "1",
...props,
...props.commerce,
};
const { manifest } = vtex(props);
const { manifest, state: appState } = vtex(props.commerce);
state = { ...state, ...appState };
targetApps["vtex"] = {
sourceMap: buildSourceMap(manifest),
manifest,
};
}

if (isShopifyProps(props)) {
state.configShopify = props;
const { manifest } = shopify(props);
if (isShopifyProps(props.commerce)) {
state.configShopify = props.commerce;
const { manifest, state: appState } = shopify(props.commerce);
state = { ...state, ...appState };

targetApps["shopify"] = {
sourceMap: buildSourceMap(manifest),
manifest,
};
}

if (isVNDAProps(props)) {
if (isVNDAProps(props.commerce)) {
state.configVNDA = {
domain: props.publicUrl,
internalDomain: `${props.account}.cdn.vnda.com.br`,
useSandbox: props.sandbox,
authToken: props.authToken,
domain: props.commerce.publicUrl,
internalDomain: `${props.commerce.account}.cdn.vnda.com.br`,
useSandbox: props.commerce.sandbox,
authToken: props.commerce.authToken,
defaultPriceCurrency: "BRL",
};
const { manifest } = vnda(props);
const { manifest, state: appState } = vnda(props.commerce);
state = { ...state, ...appState };

targetApps["vnda"] = {
sourceMap: buildSourceMap(manifest),
manifest,
};
}
const liveApp = $live(props);
const webSiteApp = liveApp.dependencies![0];
const { resolvables: _ignoreResolvables, ...webSiteApp } = dependencies![0];
const sourceMap: SourceMap = {
...buildSourceMap(manifest),
};
Expand Down Expand Up @@ -306,9 +324,12 @@ export default function Std(
}

return {
state: props,
state,
sourceMap,
manifest: _manifest as Manifest,
dependencies: [liveApp],
dependencies: [liveApp, {
...commerceApp,
dependencies: [webSiteApp, dependencies![1]],
}],
};
}
2 changes: 1 addition & 1 deletion compat/std/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forApp } from "$live/clients/withManifest.ts";
import app from "./mod.ts";

export const Runtime = forApp<ReturnType<typeof app>>();
export const Runtime = forApp<ReturnType<typeof app>>();
2 changes: 1 addition & 1 deletion import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.3.0",
"std/": "https://deno.land/std@0.190.0/",
"partytown/": "https://deno.land/x/partytown@0.3.4/",
"deco-sites/std/": "https://denopkg.com/deco-sites/std@1.21.3/",
"deco-sites/std/": "https://denopkg.com/deco-sites/std@1.21.6/",
"deco/": "https://denopkg.com/deco-cx/deco@1.32.1/"
}
}

0 comments on commit 2ac7502

Please sign in to comment.