Skip to content

Commit

Permalink
fix!: move useSession hook to its own entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-jonas committed Oct 30, 2024
1 parent d95a4ed commit 85f6445
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 34 deletions.
8 changes: 8 additions & 0 deletions packages/elements-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./client": {
"types": "./dist/client/index.d.ts",
"import": "./dist/client/index.mjs",
"require": "./dist/client/index.js"
},
"./theme": {
"types": "./dist/theme/default/index.d.ts",
"import": "./dist/theme/default/index.mjs",
Expand All @@ -19,6 +24,9 @@
"index": [
"./dist/index.d.ts"
],
"client": [
"./dist/client/index.d.ts"
],
"theme": [
"./dist/theme/default/index.d.ts"
]
Expand Down
23 changes: 23 additions & 0 deletions packages/elements-react/src/client/frontendClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

import {
Configuration,
ConfigurationParameters,
FrontendApi,
} from "@ory/client-fetch"

export function frontendClient(
sdkUrl: string,
opts: Partial<ConfigurationParameters> = {},
) {
const config = new Configuration({
...opts,
basePath: sdkUrl,
headers: {
Accept: "application/json",
...opts.headers,
},
})
return new FrontendApi(config)
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@ import { Session } from "@ory/client-fetch"
import "@testing-library/jest-dom"
import "@testing-library/jest-dom/jest-globals"
import { act, render, screen, waitFor } from "@testing-library/react"
import { useOryFlow } from "../context/flow-context"
import { frontendClient } from "../util/client"
import { sessionStore, useSession } from "./useSession"
import { frontendClient } from "./frontendClient"

// Mock the necessary imports
jest.mock("../context/flow-context", () => ({
useOryFlow: jest.fn(),
}))

jest.mock("../util/client", () => ({
jest.mock("./frontendClient", () => ({
frontendClient: jest.fn(() => ({
toSession: jest.fn(),
})),
Expand Down Expand Up @@ -44,14 +38,9 @@ describe("useSession", () => {
},
expires_at: new Date(),
}
const mockConfig = {
sdk: { url: "https://mock-sdk-url" },
}

beforeEach(() => {
jest.clearAllMocks()
// Mock the flow context
;(useOryFlow as jest.Mock).mockReturnValue({ config: mockConfig })
sessionStore.setState({
isLoading: false,
session: undefined,
Expand Down Expand Up @@ -102,19 +91,15 @@ describe("useSession", () => {

// this is fine, because jest is not calling the function
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(frontendClient(mockConfig.sdk.url).toSession).toHaveBeenCalledTimes(
1,
)
expect(frontendClient("").toSession).toHaveBeenCalledTimes(1)

act(() => {
render(<TestComponent />)
})

// this is fine, because jest is not calling the function
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(frontendClient(mockConfig.sdk.url).toSession).toHaveBeenCalledTimes(
1,
)
expect(frontendClient("").toSession).toHaveBeenCalledTimes(1)
})

it("handles errors during session fetching", async () => {
Expand Down Expand Up @@ -155,8 +140,6 @@ describe("useSession", () => {

// this is fine, because jest is not calling the function
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(frontendClient(mockConfig.sdk.url).toSession).toHaveBeenCalledTimes(
1,
)
expect(frontendClient("").toSession).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
"use client"

import { Session } from "@ory/client-fetch"
import { useCallback, useEffect } from "react"
import { create, useStore } from "zustand"
import { subscribeWithSelector } from "zustand/middleware"
import { useOryFlow } from "../context/flow-context"
import { frontendClient } from "../util/client"
import { frontendClient } from "./frontendClient"

type SessionStore = {
setIsLoading: (loading: boolean) => void
Expand Down Expand Up @@ -38,8 +38,7 @@ export const sessionStore = create<SessionStore>()(
*
* @returns The current session, error and loading state.
*/
export const useSession = () => {
const { config } = useOryFlow()
export const useSession = (config?: { sdk: { url: string } }) => {
const store = useStore(sessionStore)

const fetchSession = useCallback(async () => {
Expand All @@ -53,14 +52,17 @@ export const useSession = () => {
setIsLoading(true)

try {
const sessionData = await frontendClient(config.sdk.url).toSession()
const sessionData = await frontendClient(
config?.sdk.url ??
window.location.protocol + "//" + window.location.host,
).toSession()
setSession(sessionData)
} catch (e) {
setError(e instanceof Error ? e.message : "Unknown error occurred")
} finally {
setIsLoading(false)
}
}, [config.sdk.url])
}, [config?.sdk.url])

useEffect(() => {
void fetchSession()
Expand Down
1 change: 0 additions & 1 deletion packages/elements-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ export type * from "./types"
export * from "./components"
export * from "./context"
export * from "./util"
export * from "./hooks"
export { locales as OryLocales } from "./locales"
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

import {
OryPageHeaderProps,
useComponents,
useSession,
} from "@ory/elements-react"
import { OryPageHeaderProps, useComponents } from "@ory/elements-react"
import { UserMenu } from "../ui/user-menu"
import { useSession } from "@ory/elements-react/client"

export const DefaultPageHeader = (_props: OryPageHeaderProps) => {
const { Card } = useComponents()
Expand Down
9 changes: 9 additions & 0 deletions packages/elements-react/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export default defineConfig([
"react-intl",
],
},
{
dts: true,
minify: false,
sourcemap: true,
bundle: false,
format: ["cjs", "esm"],
entry: ["src/client/**/*.ts"],
outDir: "dist/client",
},
{
...baseConfig,
entry: ["src/theme/default/index.ts"],
Expand Down

0 comments on commit 85f6445

Please sign in to comment.