-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat(VTEX): Implement Session related APIs #1020
Open
vitoUwu
wants to merge
3
commits into
main
Choose a base branch
from
feat/vtex-session
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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; |
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,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; |
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,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>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change type to |
||
} | ||
|
||
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; |
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,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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test if I can get sessions from other users |
||
} | ||
|
||
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
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; |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change type to
Record<string, { value: string }>