From b1ddb3f1943a08be959e30463e476dde41c34cf7 Mon Sep 17 00:00:00 2001 From: Jonas Hungershausen Date: Wed, 30 Oct 2024 18:08:00 +0100 Subject: [PATCH] fix!: move useSession hook to /client entry point (#255) --- .../api-report/elements-react-client.api.json | 228 +++++++++ .../api-report/elements-react-client.api.md | 22 + .../api-report/elements-react.api.json | 33 -- .../api-report/elements-react.api.md | 8 - .../config/api-extractor-client.json | 463 ++++++++++++++++++ packages/elements-react/package.json | 8 + packages/elements-react/project.json | 9 +- .../src/client/frontendClient.ts | 23 + .../src/{hooks => client}/index.ts | 0 .../src/{hooks => client}/useSession.spec.tsx | 27 +- .../src/{hooks => client}/useSession.ts | 19 +- packages/elements-react/src/index.ts | 1 - .../components/generic/page-header.tsx | 7 +- packages/elements-react/tsup.config.ts | 9 + 14 files changed, 779 insertions(+), 78 deletions(-) create mode 100644 packages/elements-react/api-report/elements-react-client.api.json create mode 100644 packages/elements-react/api-report/elements-react-client.api.md create mode 100644 packages/elements-react/config/api-extractor-client.json create mode 100644 packages/elements-react/src/client/frontendClient.ts rename packages/elements-react/src/{hooks => client}/index.ts (100%) rename packages/elements-react/src/{hooks => client}/useSession.spec.tsx (85%) rename packages/elements-react/src/{hooks => client}/useSession.ts (77%) diff --git a/packages/elements-react/api-report/elements-react-client.api.json b/packages/elements-react/api-report/elements-react-client.api.json new file mode 100644 index 000000000..ca80b6125 --- /dev/null +++ b/packages/elements-react/api-report/elements-react-client.api.json @@ -0,0 +1,228 @@ +{ + "metadata": { + "toolPackage": "@microsoft/api-extractor", + "toolVersion": "7.47.9", + "schemaVersion": 1011, + "oldestForwardsCompatibleVersion": 1001, + "tsdocConfig": { + "$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json", + "noStandardTags": true, + "tagDefinitions": [ + { + "tagName": "@alpha", + "syntaxKind": "modifier" + }, + { + "tagName": "@beta", + "syntaxKind": "modifier" + }, + { + "tagName": "@defaultValue", + "syntaxKind": "block" + }, + { + "tagName": "@decorator", + "syntaxKind": "block", + "allowMultiple": true + }, + { + "tagName": "@deprecated", + "syntaxKind": "block" + }, + { + "tagName": "@eventProperty", + "syntaxKind": "modifier" + }, + { + "tagName": "@example", + "syntaxKind": "block", + "allowMultiple": true + }, + { + "tagName": "@experimental", + "syntaxKind": "modifier" + }, + { + "tagName": "@inheritDoc", + "syntaxKind": "inline" + }, + { + "tagName": "@internal", + "syntaxKind": "modifier" + }, + { + "tagName": "@label", + "syntaxKind": "inline" + }, + { + "tagName": "@link", + "syntaxKind": "inline", + "allowMultiple": true + }, + { + "tagName": "@override", + "syntaxKind": "modifier" + }, + { + "tagName": "@packageDocumentation", + "syntaxKind": "modifier" + }, + { + "tagName": "@param", + "syntaxKind": "block", + "allowMultiple": true + }, + { + "tagName": "@privateRemarks", + "syntaxKind": "block" + }, + { + "tagName": "@public", + "syntaxKind": "modifier" + }, + { + "tagName": "@readonly", + "syntaxKind": "modifier" + }, + { + "tagName": "@remarks", + "syntaxKind": "block" + }, + { + "tagName": "@returns", + "syntaxKind": "block" + }, + { + "tagName": "@sealed", + "syntaxKind": "modifier" + }, + { + "tagName": "@see", + "syntaxKind": "block" + }, + { + "tagName": "@throws", + "syntaxKind": "block", + "allowMultiple": true + }, + { + "tagName": "@typeParam", + "syntaxKind": "block", + "allowMultiple": true + }, + { + "tagName": "@virtual", + "syntaxKind": "modifier" + }, + { + "tagName": "@betaDocumentation", + "syntaxKind": "modifier" + }, + { + "tagName": "@internalRemarks", + "syntaxKind": "block" + }, + { + "tagName": "@preapproved", + "syntaxKind": "modifier" + } + ], + "supportForTags": { + "@alpha": true, + "@beta": true, + "@defaultValue": true, + "@decorator": true, + "@deprecated": true, + "@eventProperty": true, + "@example": true, + "@experimental": true, + "@inheritDoc": true, + "@internal": true, + "@label": true, + "@link": true, + "@override": true, + "@packageDocumentation": true, + "@param": true, + "@privateRemarks": true, + "@public": true, + "@readonly": true, + "@remarks": true, + "@returns": true, + "@sealed": true, + "@see": true, + "@throws": true, + "@typeParam": true, + "@virtual": true, + "@betaDocumentation": true, + "@internalRemarks": true, + "@preapproved": true + }, + "reportUnsupportedHtmlElements": false + } + }, + "kind": "Package", + "canonicalReference": "@ory/elements-react!", + "docComment": "", + "name": "@ory/elements-react", + "preserveMemberOrder": false, + "members": [ + { + "kind": "EntryPoint", + "canonicalReference": "@ory/elements-react!", + "name": "", + "preserveMemberOrder": false, + "members": [ + { + "kind": "Function", + "canonicalReference": "@ory/elements-react!useSession:function(1)", + "docComment": "/**\n * A hook to get the current session from the Ory Network.\n *\n * Usage:\n * ```ts\n * const { session, error, isLoading } = useSession()\n * ```\n *\n * @returns The current session, error and loading state.\n */\n", + "excerptTokens": [ + { + "kind": "Content", + "text": "useSession: (config?: " + }, + { + "kind": "Content", + "text": "{\n sdk: {\n url: string;\n };\n}" + }, + { + "kind": "Content", + "text": ") => " + }, + { + "kind": "Content", + "text": "{\n session: " + }, + { + "kind": "Reference", + "text": "Session", + "canonicalReference": "@ory/client-fetch!Session:interface" + }, + { + "kind": "Content", + "text": " | undefined;\n error: string | undefined;\n isLoading: boolean;\n}" + } + ], + "fileUrlPath": "dist/client/useSession.d.ts", + "returnTypeTokenRange": { + "startIndex": 3, + "endIndex": 6 + }, + "releaseTag": "Public", + "overloadIndex": 1, + "parameters": [ + { + "parameterName": "config", + "parameterTypeTokenRange": { + "startIndex": 1, + "endIndex": 2 + }, + "isOptional": true + } + ], + "name": "useSession" + } + ] + } + ] +} diff --git a/packages/elements-react/api-report/elements-react-client.api.md b/packages/elements-react/api-report/elements-react-client.api.md new file mode 100644 index 000000000..2dd929df2 --- /dev/null +++ b/packages/elements-react/api-report/elements-react-client.api.md @@ -0,0 +1,22 @@ +## API Report File for "@ory/elements-react" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts + +import { Session } from '@ory/client-fetch'; + +// @public +export const useSession: (config?: { + sdk: { + url: string; + }; +}) => { + session: Session | undefined; + error: string | undefined; + isLoading: boolean; +}; + +// (No @packageDocumentation comment for this package) + +``` diff --git a/packages/elements-react/api-report/elements-react.api.json b/packages/elements-react/api-report/elements-react.api.json index 058893fba..16ad13a5f 100644 --- a/packages/elements-react/api-report/elements-react.api.json +++ b/packages/elements-react/api-report/elements-react.api.json @@ -3634,39 +3634,6 @@ "parameters": [], "name": "useOryFlow" }, - { - "kind": "Function", - "canonicalReference": "@ory/elements-react!useSession:function(1)", - "docComment": "/**\n * A hook to get the current session from the Ory Network.\n *\n * Usage:\n * ```ts\n * const { session, error, isLoading } = useSession()\n * ```\n *\n * @returns The current session, error and loading state.\n */\n", - "excerptTokens": [ - { - "kind": "Content", - "text": "useSession: () => " - }, - { - "kind": "Content", - "text": "{\n session: " - }, - { - "kind": "Reference", - "text": "Session", - "canonicalReference": "@ory/client-fetch!Session:interface" - }, - { - "kind": "Content", - "text": " | undefined;\n error: string | undefined;\n isLoading: boolean;\n}" - } - ], - "fileUrlPath": "dist/index.d.ts", - "returnTypeTokenRange": { - "startIndex": 1, - "endIndex": 4 - }, - "releaseTag": "Public", - "overloadIndex": 1, - "parameters": [], - "name": "useSession" - }, { "kind": "TypeAlias", "canonicalReference": "@ory/elements-react!VerificationFlowContainer:type", diff --git a/packages/elements-react/api-report/elements-react.api.md b/packages/elements-react/api-report/elements-react.api.md index e31661abd..44e132079 100644 --- a/packages/elements-react/api-report/elements-react.api.md +++ b/packages/elements-react/api-report/elements-react.api.md @@ -22,7 +22,6 @@ import { PropsWithChildren } from 'react'; import * as react_jsx_runtime from 'react/jsx-runtime'; import { RecoveryFlow } from '@ory/client-fetch'; import { RegistrationFlow } from '@ory/client-fetch'; -import { Session } from '@ory/client-fetch'; import { SettingsFlow } from '@ory/client-fetch'; import { UiNode } from '@ory/client-fetch'; import { UiNodeAnchorAttributes } from '@ory/client-fetch'; @@ -399,13 +398,6 @@ export function useNodeSorter(): (a: UiNode, b: UiNode, ctx: { // @public export function useOryFlow(): FlowContextValue; -// @public -export const useSession: () => { - session: Session | undefined; - error: string | undefined; - isLoading: boolean; -}; - // @public export type VerificationFlowContainer = OryFlow; diff --git a/packages/elements-react/config/api-extractor-client.json b/packages/elements-react/config/api-extractor-client.json new file mode 100644 index 000000000..2c4851282 --- /dev/null +++ b/packages/elements-react/config/api-extractor-client.json @@ -0,0 +1,463 @@ +/** + * Config file for API Extractor. For more info, please visit: https://api-extractor.com + */ +{ + "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json", + + /** + * Optionally specifies another JSON config file that this file extends from. This provides a way for + * standard settings to be shared across multiple projects. + * + * If the path starts with "./" or "../", the path is resolved relative to the folder of the file that contains + * the "extends" field. Otherwise, the first path segment is interpreted as an NPM package name, and will be + * resolved using NodeJS require(). + * + * SUPPORTED TOKENS: none + * DEFAULT VALUE: "" + */ + // "extends": "./shared/api-extractor-base.json" + // "extends": "my-package/include/api-extractor-base.json" + + /** + * Determines the "" token that can be used with other config file settings. The project folder + * typically contains the tsconfig.json and package.json config files, but the path is user-defined. + * + * The path is resolved relative to the folder of the config file that contains the setting. + * + * The default value for "projectFolder" is the token "", which means the folder is determined by traversing + * parent folders, starting from the folder containing api-extractor.json, and stopping at the first folder + * that contains a tsconfig.json file. If a tsconfig.json file cannot be found in this way, then an error + * will be reported. + * + * SUPPORTED TOKENS: + * DEFAULT VALUE: "" + */ + // "projectFolder": "..", + + /** + * (REQUIRED) Specifies the .d.ts file to be used as the starting point for analysis. API Extractor + * analyzes the symbols exported by this module. + * + * The file extension must be ".d.ts" and not ".ts". + * + * The path is resolved relative to the folder of the config file that contains the setting; to change this, + * prepend a folder token such as "". + * + * SUPPORTED TOKENS: , , + */ + "mainEntryPointFilePath": "/dist/client/index.d.ts", + + /** + * A list of NPM package names whose exports should be treated as part of this package. + * + * For example, suppose that Webpack is used to generate a distributed bundle for the project "library1", + * and another NPM package "library2" is embedded in this bundle. Some types from library2 may become part + * of the exported API for library1, but by default API Extractor would generate a .d.ts rollup that explicitly + * imports library2. To avoid this, we might specify: + * + * "bundledPackages": [ "library2" ], + * + * This would direct API Extractor to embed those types directly in the .d.ts rollup, as if they had been + * local files for library1. + * + * The "bundledPackages" elements may specify glob patterns using minimatch syntax. To ensure deterministic + * output, globs are expanded by matching explicitly declared top-level dependencies only. For example, + * the pattern below will NOT match "@my-company/example" unless it appears in a field such as "dependencies" + * or "devDependencies" of the project's package.json file: + * + * "bundledPackages": [ "@my-company/*" ], + */ + "bundledPackages": [], + + /** + * Specifies what type of newlines API Extractor should use when writing output files. By default, the output files + * will be written with Windows-style newlines. To use POSIX-style newlines, specify "lf" instead. + * To use the OS's default newline kind, specify "os". + * + * DEFAULT VALUE: "crlf" + */ + // "newlineKind": "crlf", + + /** + * Specifies how API Extractor sorts members of an enum when generating the .api.json file. By default, the output + * files will be sorted alphabetically, which is "by-name". To keep the ordering in the source code, specify + * "preserve". + * + * DEFAULT VALUE: "by-name" + */ + // "enumMemberOrder": "by-name", + + /** + * Set to true when invoking API Extractor's test harness. When `testMode` is true, the `toolVersion` field in the + * .api.json file is assigned an empty string to prevent spurious diffs in output files tracked for tests. + * + * DEFAULT VALUE: "false" + */ + // "testMode": false, + + /** + * Determines how the TypeScript compiler engine will be invoked by API Extractor. + */ + "compiler": { + /** + * Specifies the path to the tsconfig.json file to be used by API Extractor when analyzing the project. + * + * The path is resolved relative to the folder of the config file that contains the setting; to change this, + * prepend a folder token such as "". + * + * Note: This setting will be ignored if "overrideTsconfig" is used. + * + * SUPPORTED TOKENS: , , + * DEFAULT VALUE: "/tsconfig.json" + */ + // "tsconfigFilePath": "/tsconfig.json", + /** + * Provides a compiler configuration that will be used instead of reading the tsconfig.json file from disk. + * The object must conform to the TypeScript tsconfig schema: + * + * http://json.schemastore.org/tsconfig + * + * If omitted, then the tsconfig.json file will be read from the "projectFolder". + * + * DEFAULT VALUE: no overrideTsconfig section + */ + // "overrideTsconfig": { + // . . . + // } + /** + * This option causes the compiler to be invoked with the --skipLibCheck option. This option is not recommended + * and may cause API Extractor to produce incomplete or incorrect declarations, but it may be required when + * dependencies contain declarations that are incompatible with the TypeScript engine that API Extractor uses + * for its analysis. Where possible, the underlying issue should be fixed rather than relying on skipLibCheck. + * + * DEFAULT VALUE: false + */ + // "skipLibCheck": true, + }, + + /** + * Configures how the API report file (*.api.md) will be generated. + */ + "apiReport": { + /** + * (REQUIRED) Whether to generate an API report. + */ + "enabled": true, + + /** + * The base filename for the API report files, to be combined with "reportFolder" or "reportTempFolder" + * to produce the full file path. The "reportFileName" should not include any path separators such as + * "\" or "/". The "reportFileName" should not include a file extension, since API Extractor will automatically + * append an appropriate file extension such as ".api.md". If the "reportVariants" setting is used, then the + * file extension includes the variant name, for example "my-report.public.api.md" or "my-report.beta.api.md". + * The "complete" variant always uses the simple extension "my-report.api.md". + * + * Previous versions of API Extractor required "reportFileName" to include the ".api.md" extension explicitly; + * for backwards compatibility, that is still accepted but will be discarded before applying the above rules. + * + * SUPPORTED TOKENS: , + * DEFAULT VALUE: "" + */ + "reportFileName": "-client", + + /** + * To support different approval requirements for different API levels, multiple "variants" of the API report can + * be generated. The "reportVariants" setting specifies a list of variants to be generated. If omitted, + * by default only the "complete" variant will be generated, which includes all @internal, @alpha, @beta, + * and @public items. Other possible variants are "alpha" (@alpha + @beta + @public), "beta" (@beta + @public), + * and "public" (@public only). + * + * DEFAULT VALUE: [ "complete" ] + */ + // "reportVariants": ["public", "beta"], + + /** + * Specifies the folder where the API report file is written. The file name portion is determined by + * the "reportFileName" setting. + * + * The API report file is normally tracked by Git. Changes to it can be used to trigger a branch policy, + * e.g. for an API review. + * + * The path is resolved relative to the folder of the config file that contains the setting; to change this, + * prepend a folder token such as "". + * + * SUPPORTED TOKENS: , , + * DEFAULT VALUE: "/etc/" + */ + "reportFolder": "/api-report", + + /** + * Specifies the folder where the temporary report file is written. The file name portion is determined by + * the "reportFileName" setting. + * + * After the temporary file is written to disk, it is compared with the file in the "reportFolder". + * If they are different, a production build will fail. + * + * The path is resolved relative to the folder of the config file that contains the setting; to change this, + * prepend a folder token such as "". + * + * SUPPORTED TOKENS: , , + * DEFAULT VALUE: "/temp/" + */ + "reportTempFolder": "/api-report/temp/" + + /** + * Whether "forgotten exports" should be included in the API report file. Forgotten exports are declarations + * flagged with `ae-forgotten-export` warnings. See https://api-extractor.com/pages/messages/ae-forgotten-export/ to + * learn more. + * + * DEFAULT VALUE: "false" + */ + // "includeForgottenExports": false + }, + + /** + * Configures how the doc model file (*.api.json) will be generated. + */ + "docModel": { + /** + * (REQUIRED) Whether to generate a doc model file. + */ + "enabled": true, + + /** + * The output path for the doc model file. The file extension should be ".api.json". + * + * The path is resolved relative to the folder of the config file that contains the setting; to change this, + * prepend a folder token such as "". + * + * SUPPORTED TOKENS: , , + * DEFAULT VALUE: "/temp/.api.json" + */ + "apiJsonFilePath": "/api-report/-client.api.json" + + /** + * Whether "forgotten exports" should be included in the doc model file. Forgotten exports are declarations + * flagged with `ae-forgotten-export` warnings. See https://api-extractor.com/pages/messages/ae-forgotten-export/ to + * learn more. + * + * DEFAULT VALUE: "false" + */ + // "includeForgottenExports": false, + + /** + * The base URL where the project's source code can be viewed on a website such as GitHub or + * Azure DevOps. This URL path corresponds to the `` path on disk. + * + * This URL is concatenated with the file paths serialized to the doc model to produce URL file paths to individual API items. + * For example, if the `projectFolderUrl` is "https://github.com/microsoft/rushstack/tree/main/apps/api-extractor" and an API + * item's file path is "api/ExtractorConfig.ts", the full URL file path would be + * "https://github.com/microsoft/rushstack/tree/main/apps/api-extractor/api/ExtractorConfig.js". + * + * This setting can be omitted if you don't need source code links in your API documentation reference. + * + * SUPPORTED TOKENS: none + * DEFAULT VALUE: "" + */ + // "projectFolderUrl": "http://github.com/path/to/your/projectFolder" + }, + + /** + * Configures how the .d.ts rollup file will be generated. + */ + "dtsRollup": { + /** + * (REQUIRED) Whether to generate the .d.ts rollup file. + */ + "enabled": false + + /** + * Specifies the output path for a .d.ts rollup file to be generated without any trimming. + * This file will include all declarations that are exported by the main entry point. + * + * If the path is an empty string, then this file will not be written. + * + * The path is resolved relative to the folder of the config file that contains the setting; to change this, + * prepend a folder token such as "". + * + * SUPPORTED TOKENS: , , + * DEFAULT VALUE: "/dist/.d.ts" + */ + // "untrimmedFilePath": "/dist/.d.ts", + + /** + * Specifies the output path for a .d.ts rollup file to be generated with trimming for an "alpha" release. + * This file will include only declarations that are marked as "@public", "@beta", or "@alpha". + * + * If the path is an empty string, then this file will not be written. + * + * The path is resolved relative to the folder of the config file that contains the setting; to change this, + * prepend a folder token such as "". + * + * SUPPORTED TOKENS: , , + * DEFAULT VALUE: "" + */ + // "alphaTrimmedFilePath": "/dist/-alpha.d.ts", + + /** + * Specifies the output path for a .d.ts rollup file to be generated with trimming for a "beta" release. + * This file will include only declarations that are marked as "@public" or "@beta". + * + * If the path is an empty string, then this file will not be written. + * + * The path is resolved relative to the folder of the config file that contains the setting; to change this, + * prepend a folder token such as "". + * + * SUPPORTED TOKENS: , , + * DEFAULT VALUE: "" + */ + // "betaTrimmedFilePath": "/dist/-beta.d.ts", + + /** + * Specifies the output path for a .d.ts rollup file to be generated with trimming for a "public" release. + * This file will include only declarations that are marked as "@public". + * + * If the path is an empty string, then this file will not be written. + * + * The path is resolved relative to the folder of the config file that contains the setting; to change this, + * prepend a folder token such as "". + * + * SUPPORTED TOKENS: , , + * DEFAULT VALUE: "" + */ + // "publicTrimmedFilePath": "/dist/-public.d.ts", + + /** + * When a declaration is trimmed, by default it will be replaced by a code comment such as + * "Excluded from this release type: exampleMember". Set "omitTrimmingComments" to true to remove the + * declaration completely. + * + * DEFAULT VALUE: false + */ + // "omitTrimmingComments": true + }, + + /** + * Configures how the tsdoc-metadata.json file will be generated. + */ + "tsdocMetadata": { + /** + * Whether to generate the tsdoc-metadata.json file. + * + * DEFAULT VALUE: true + */ + // "enabled": true, + /** + * Specifies where the TSDoc metadata file should be written. + * + * The path is resolved relative to the folder of the config file that contains the setting; to change this, + * prepend a folder token such as "". + * + * The default value is "", which causes the path to be automatically inferred from the "tsdocMetadata", + * "typings" or "main" fields of the project's package.json. If none of these fields are set, the lookup + * falls back to "tsdoc-metadata.json" in the package folder. + * + * SUPPORTED TOKENS: , , + * DEFAULT VALUE: "" + */ + // "tsdocMetadataFilePath": "/dist/tsdoc-metadata.json" + }, + + /** + * Configures how API Extractor reports error and warning messages produced during analysis. + * + * There are three sources of messages: compiler messages, API Extractor messages, and TSDoc messages. + */ + "messages": { + /** + * Configures handling of diagnostic messages reported by the TypeScript compiler engine while analyzing + * the input .d.ts files. + * + * TypeScript message identifiers start with "TS" followed by an integer. For example: "TS2551" + * + * DEFAULT VALUE: A single "default" entry with logLevel=warning. + */ + "compilerMessageReporting": { + /** + * Configures the default routing for messages that don't match an explicit rule in this table. + */ + "default": { + /** + * Specifies whether the message should be written to the the tool's output log. Note that + * the "addToApiReportFile" property may supersede this option. + * + * Possible values: "error", "warning", "none" + * + * Errors cause the build to fail and return a nonzero exit code. Warnings cause a production build fail + * and return a nonzero exit code. For a non-production build (e.g. when "api-extractor run" includes + * the "--local" option), the warning is displayed but the build will not fail. + * + * DEFAULT VALUE: "warning" + */ + "logLevel": "warning" + + /** + * When addToApiReportFile is true: If API Extractor is configured to write an API report file (.api.md), + * then the message will be written inside that file; otherwise, the message is instead logged according to + * the "logLevel" option. + * + * DEFAULT VALUE: false + */ + // "addToApiReportFile": false + } + + // "TS2551": { + // "logLevel": "warning", + // "addToApiReportFile": true + // }, + // + // . . . + }, + + /** + * Configures handling of messages reported by API Extractor during its analysis. + * + * API Extractor message identifiers start with "ae-". For example: "ae-extra-release-tag" + * + * DEFAULT VALUE: See api-extractor-defaults.json for the complete table of extractorMessageReporting mappings + */ + "extractorMessageReporting": { + "default": { + "logLevel": "warning" + // "addToApiReportFile": false + }, + "ae-missing-release-tag": { + "logLevel": "none" + }, + // This is okay for the theme, because of the "weird" setup we have, where the theme itself is really a package, + // but we include it as a subfolder with a TS alias. Api Extractor does not know how to deal with that, so we + // have to tell it to ignore it. + "ae-wrong-input-file-type": { + "logLevel": "none" + } + + // "ae-extra-release-tag": { + // "logLevel": "warning", + // "addToApiReportFile": true + // }, + // + // . . . + }, + + /** + * Configures handling of messages reported by the TSDoc parser when analyzing code comments. + * + * TSDoc message identifiers start with "tsdoc-". For example: "tsdoc-link-tag-unescaped-text" + * + * DEFAULT VALUE: A single "default" entry with logLevel=warning. + */ + "tsdocMessageReporting": { + "default": { + "logLevel": "warning" + // "addToApiReportFile": false + } + + // "tsdoc-link-tag-unescaped-text": { + // "logLevel": "warning", + // "addToApiReportFile": true + // }, + // + // . . . + } + } +} diff --git a/packages/elements-react/package.json b/packages/elements-react/package.json index 63c89fdc2..e12dad613 100644 --- a/packages/elements-react/package.json +++ b/packages/elements-react/package.json @@ -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", @@ -19,6 +24,9 @@ "index": [ "./dist/index.d.ts" ], + "client": [ + "./dist/client/index.d.ts" + ], "theme": [ "./dist/theme/default/index.d.ts" ] diff --git a/packages/elements-react/project.json b/packages/elements-react/project.json index 4dff61932..2f33d90e5 100644 --- a/packages/elements-react/project.json +++ b/packages/elements-react/project.json @@ -2,6 +2,7 @@ "targets": { "lint": { "executor": "@nx/eslint:lint", + "dependsOn": ["build"], "options": { "lintFilePatterns": [ "packages/elements-react/src/**/*.ts", @@ -17,13 +18,13 @@ }, "build": { "command": "tsup --clean --dts", - "dependsOn": ["lint", "test"], "options": { "cwd": "packages/elements-react" } }, "test": { "executor": "@nx/jest:jest", + "dependsOn": ["build"], "options": { "jestConfig": "packages/elements-react/jest.config.ts", "coverage": true, @@ -45,7 +46,8 @@ "cwd": "packages/elements-react", "commands": [ "api-extractor run --verbose -c config/api-extractor-core.json", - "api-extractor run --verbose -c config/api-extractor-theme.json" + "api-extractor run --verbose -c config/api-extractor-theme.json", + "api-extractor run --verbose -c config/api-extractor-client.json" ] }, "dependsOn": ["build"] @@ -56,7 +58,8 @@ "cwd": "packages/elements-react", "commands": [ "api-extractor run --local --verbose -c config/api-extractor-core.json", - "api-extractor run --local --verbose -c config/api-extractor-theme.json" + "api-extractor run --local --verbose -c config/api-extractor-theme.json", + "api-extractor run --local --verbose -c config/api-extractor-client.json" ] }, "dependsOn": ["build"] diff --git a/packages/elements-react/src/client/frontendClient.ts b/packages/elements-react/src/client/frontendClient.ts new file mode 100644 index 000000000..66cd0bb2f --- /dev/null +++ b/packages/elements-react/src/client/frontendClient.ts @@ -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 = {}, +) { + const config = new Configuration({ + ...opts, + basePath: sdkUrl, + headers: { + Accept: "application/json", + ...opts.headers, + }, + }) + return new FrontendApi(config) +} diff --git a/packages/elements-react/src/hooks/index.ts b/packages/elements-react/src/client/index.ts similarity index 100% rename from packages/elements-react/src/hooks/index.ts rename to packages/elements-react/src/client/index.ts diff --git a/packages/elements-react/src/hooks/useSession.spec.tsx b/packages/elements-react/src/client/useSession.spec.tsx similarity index 85% rename from packages/elements-react/src/hooks/useSession.spec.tsx rename to packages/elements-react/src/client/useSession.spec.tsx index ce6eff949..9415db38c 100644 --- a/packages/elements-react/src/hooks/useSession.spec.tsx +++ b/packages/elements-react/src/client/useSession.spec.tsx @@ -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(), })), @@ -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, @@ -102,9 +91,7 @@ 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() @@ -112,9 +99,7 @@ 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) }) it("handles errors during session fetching", async () => { @@ -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) }) }) diff --git a/packages/elements-react/src/hooks/useSession.ts b/packages/elements-react/src/client/useSession.ts similarity index 77% rename from packages/elements-react/src/hooks/useSession.ts rename to packages/elements-react/src/client/useSession.ts index 5e58ee7ac..a3a9425c4 100644 --- a/packages/elements-react/src/hooks/useSession.ts +++ b/packages/elements-react/src/client/useSession.ts @@ -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 @@ -38,8 +38,7 @@ export const sessionStore = create()( * * @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 () => { @@ -53,14 +52,22 @@ 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") + if (!config?.sdk.url) { + console.error( + "Could not fetch session. Make sure you have set the SDK URL in the config.", + ) + } } finally { setIsLoading(false) } - }, [config.sdk.url]) + }, [config?.sdk.url]) useEffect(() => { void fetchSession() diff --git a/packages/elements-react/src/index.ts b/packages/elements-react/src/index.ts index 9632dd24c..81c2da38c 100644 --- a/packages/elements-react/src/index.ts +++ b/packages/elements-react/src/index.ts @@ -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" diff --git a/packages/elements-react/src/theme/default/components/generic/page-header.tsx b/packages/elements-react/src/theme/default/components/generic/page-header.tsx index 0b03cbfcb..1533cced1 100644 --- a/packages/elements-react/src/theme/default/components/generic/page-header.tsx +++ b/packages/elements-react/src/theme/default/components/generic/page-header.tsx @@ -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() diff --git a/packages/elements-react/tsup.config.ts b/packages/elements-react/tsup.config.ts index fb2838620..96cb6df3e 100644 --- a/packages/elements-react/tsup.config.ts +++ b/packages/elements-react/tsup.config.ts @@ -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"],