Skip to content

Commit

Permalink
feat: implement session apis
Browse files Browse the repository at this point in the history
  • Loading branch information
vitoUwu committed Feb 21, 2025
1 parent fdc26a5 commit 2423a42
Show file tree
Hide file tree
Showing 9 changed files with 915 additions and 14 deletions.
38 changes: 38 additions & 0 deletions vtex/actions/session/createSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { AppContext } from "../../mod.ts";
import { proxySetCookie } from "../../utils/cookies.ts";
import type { CreateEditSessionResponse } from "../../utils/openapi/vcs.openapi.gen.ts";

interface Props {
publicProperties: Record<string, unknown>;
}

async function action(
props: Props,
req: Request,
ctx: AppContext,
): Promise<CreateEditSessionResponse | null> {
const { vcs } = ctx;

try {
const response = await vcs["POST /api/sessions"]({}, {
body: {
public: {
...props.publicProperties,
},
},
});

if (!response.ok) {
throw new Error(`Failed to create session: ${response.status}`);
}

proxySetCookie(response.headers, ctx.response.headers, req.url);

return await response.json();
} catch (error) {
console.error("Error creating VTEX session:", error);
return null;
}
}

export default action;
39 changes: 39 additions & 0 deletions vtex/actions/session/deleteSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { AppContext } from "../../mod.ts";
import { parseCookie } from "../../utils/vtexId.ts";

export interface DeleteSession {
logOutFromSession: string;
}

const mutation = `mutation LogOutFromSession($sessionId: ID) {
logOutFromSession(sessionId: $sessionId) @context(provider: "vtex.store-graphql@2.x")
}`;

interface Props {
sessionId: string;
}

async function action(
{ sessionId }: Props,
req: Request,
ctx: AppContext,
): Promise<DeleteSession | null> {
const { io } = ctx;
const { cookie, payload } = parseCookie(req.headers, ctx.account);

if (!payload?.sub || !payload?.userId) {
return null;
}

try {
return await io.query<DeleteSession, { sessionId: string }>({
query: mutation,
variables: { sessionId },
}, { headers: { cookie } });
} catch (error) {
console.error(error);
return null;
}
}

export default action;
41 changes: 41 additions & 0 deletions vtex/actions/session/editSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { AppContext } from "../../mod.ts";
import { proxySetCookie } from "../../utils/cookies.ts";
import type { CreateEditSessionResponse } from "../../utils/openapi/vcs.openapi.gen.ts";
import { parseCookie } from "../../utils/vtexId.ts";

interface Props {
publicProperties: Record<string, unknown>;
}

async function action(
props: Props,
req: Request,
ctx: AppContext,
): Promise<CreateEditSessionResponse | null> {
const { vcs } = ctx;
const { cookie } = parseCookie(req.headers, ctx.account);

try {
const response = await vcs["PATCH /api/sessions"]({}, {
body: {
public: {
...props.publicProperties,
},
},
headers: { cookie },
});

if (!response.ok) {
throw new Error(`Failed to edit session: ${response.status}`);
}

proxySetCookie(response.headers, ctx.response.headers, req.url);

return await response.json();
} catch (error) {
console.error("Error editing VTEX session:", error);
return null;
}
}

export default action;
42 changes: 42 additions & 0 deletions vtex/loaders/session/getSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { AppContext } from "../../mod.ts";
import type { GetSessionResponse } from "../../utils/openapi/vcs.openapi.gen.ts";
import { parseCookie } from "../../utils/vtexId.ts";

interface Props {
/**
* Items are the keys of the values you wish to get. They follow the format namespace1.key1,namespace2.key2.
*
* If you wish to recover the data sent on Create new session, it should be public.{key}, replacing {key} with the name of the custom property you created. Following the example request presented in Create new session, it would be public.variable1,public.variable2.
*
* If you want to retrieve all keys from Session Manager, you can use the wildcard operator (*) as a value for this query parameter.
*/
items: string[];
}

async function loader(
props: Props,
req: Request,
ctx: AppContext,
): Promise<GetSessionResponse | null> {
const { vcs } = ctx;
const { cookie } = parseCookie(req.headers, ctx.account);

try {
const response = await vcs["GET /api/sessions"]({
items: props.items.join(","),
}, {
headers: { cookie },
});

if (!response.ok) {
throw new Error(`Failed to get session: ${response.status}`);
}

return await response.json();
} catch (error) {
console.error("Error getting VTEX session:", error);
return null;
}
}

export default loader;
51 changes: 51 additions & 0 deletions vtex/loaders/session/getUserSessions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { AppContext } from "../../mod.ts";
import type { LoginSessionsInfo } from "../../utils/types.ts";
import { parseCookie } from "../../utils/vtexId.ts";

const query = `query getUserSessions {
loginSessionsInfo {
currentLoginSessionId
loginSessions {
id
cacheId
deviceType
city
lastAccess
browser
os
ip
fullAddress
firstAccess
}
}
}`;

async function loader(
_props: unknown,
req: Request,
ctx: AppContext,
): Promise<LoginSessionsInfo | null> {
const { io } = ctx;
const { cookie, payload } = parseCookie(req.headers, ctx.account);

if (!payload?.sub || !payload?.userId) {
return null;
}

try {
const data = await io.query<
{ loginSessionsInfo: LoginSessionsInfo },
null
>(
{ query },
{ headers: { cookie } },
);

return data.loginSessionsInfo;
} catch (e) {
console.error(e);
return null;
}
}

export default loader;
38 changes: 24 additions & 14 deletions vtex/manifest.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import * as $$$$$$$$$18 from "./actions/masterdata/updateDocument.ts";
import * as $$$$$$$$$19 from "./actions/newsletter/subscribe.ts";
import * as $$$$$$$$$20 from "./actions/notifyme.ts";
import * as $$$$$$$$$21 from "./actions/review/submit.ts";
import * as $$$$$$$$$22 from "./actions/trigger.ts";
import * as $$$$$$$$$23 from "./actions/wishlist/addItem.ts";
import * as $$$$$$$$$24 from "./actions/wishlist/removeItem.ts";
import * as $$$$$$$$$22 from "./actions/session/createSession.ts";
import * as $$$$$$$$$23 from "./actions/session/deleteSession.ts";
import * as $$$$$$$$$24 from "./actions/session/editSession.ts";
import * as $$$$$$$$$25 from "./actions/trigger.ts";
import * as $$$$$$$$$26 from "./actions/wishlist/addItem.ts";
import * as $$$$$$$$$27 from "./actions/wishlist/removeItem.ts";
import * as $$$$0 from "./handlers/sitemap.ts";
import * as $$$0 from "./loaders/cart.ts";
import * as $$$1 from "./loaders/categories/tree.ts";
Expand Down Expand Up @@ -64,10 +67,12 @@ import * as $$$32 from "./loaders/product/extensions/suggestions.ts";
import * as $$$33 from "./loaders/product/wishlist.ts";
import * as $$$34 from "./loaders/promotion/getPromotionById.ts";
import * as $$$35 from "./loaders/proxy.ts";
import * as $$$36 from "./loaders/user.ts";
import * as $$$37 from "./loaders/wishlist.ts";
import * as $$$38 from "./loaders/workflow/product.ts";
import * as $$$39 from "./loaders/workflow/products.ts";
import * as $$$36 from "./loaders/session/getSession.ts";
import * as $$$37 from "./loaders/session/getUserSessions.ts";
import * as $$$38 from "./loaders/user.ts";
import * as $$$39 from "./loaders/wishlist.ts";
import * as $$$40 from "./loaders/workflow/product.ts";
import * as $$$41 from "./loaders/workflow/products.ts";
import * as $$$$$$0 from "./sections/Analytics/Vtex.tsx";
import * as $$$$$$$$$$0 from "./workflows/events.ts";
import * as $$$$$$$$$$1 from "./workflows/product/index.ts";
Expand Down Expand Up @@ -110,10 +115,12 @@ const manifest = {
"vtex/loaders/product/wishlist.ts": $$$33,
"vtex/loaders/promotion/getPromotionById.ts": $$$34,
"vtex/loaders/proxy.ts": $$$35,
"vtex/loaders/user.ts": $$$36,
"vtex/loaders/wishlist.ts": $$$37,
"vtex/loaders/workflow/product.ts": $$$38,
"vtex/loaders/workflow/products.ts": $$$39,
"vtex/loaders/session/getSession.ts": $$$36,
"vtex/loaders/session/getUserSessions.ts": $$$37,
"vtex/loaders/user.ts": $$$38,
"vtex/loaders/wishlist.ts": $$$39,
"vtex/loaders/workflow/product.ts": $$$40,
"vtex/loaders/workflow/products.ts": $$$41,
},
"handlers": {
"vtex/handlers/sitemap.ts": $$$$0,
Expand Down Expand Up @@ -144,9 +151,12 @@ const manifest = {
"vtex/actions/newsletter/subscribe.ts": $$$$$$$$$19,
"vtex/actions/notifyme.ts": $$$$$$$$$20,
"vtex/actions/review/submit.ts": $$$$$$$$$21,
"vtex/actions/trigger.ts": $$$$$$$$$22,
"vtex/actions/wishlist/addItem.ts": $$$$$$$$$23,
"vtex/actions/wishlist/removeItem.ts": $$$$$$$$$24,
"vtex/actions/session/createSession.ts": $$$$$$$$$22,
"vtex/actions/session/deleteSession.ts": $$$$$$$$$23,
"vtex/actions/session/editSession.ts": $$$$$$$$$24,
"vtex/actions/trigger.ts": $$$$$$$$$25,
"vtex/actions/wishlist/addItem.ts": $$$$$$$$$26,
"vtex/actions/wishlist/removeItem.ts": $$$$$$$$$27,
},
"workflows": {
"vtex/workflows/events.ts": $$$$$$$$$$0,
Expand Down
Loading

0 comments on commit 2423a42

Please sign in to comment.