Skip to content
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: Custom screens support RELEASE #1012

Merged
merged 25 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"preversion:ci": "sh ./tools/scripts/latestTag.sh",
"version:ci": "pnpm affected:ci --target version --noVerify=true --parallel 1",
"postversion:ci": "pnpm run build:ci",
"print-affected:ci": "nx print-affected --base=$(sh ./tools/scripts/latestTag.sh) --select=projects",
"print-affected:ci": "nx show projects --affected --base=$(sh ./tools/scripts/latestTag.sh) --select=projects",
"format": "nx format:write",
"format:ci": "nx format:check",
"postinstall": "pnpm update @descope/web-components-ui"
Expand Down
8 changes: 4 additions & 4 deletions packages/libs/sdk-helpers/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export class State<T extends StateObject> {

#token = 0;

#updateOnlyOnChange = false;
#forceUpdate = true;

constructor(init: T = {} as T, { updateOnlyOnChange = true } = {}) {
constructor(init: T = {} as T, { forceUpdate = false } = {}) {
this.#state = init;
this.#updateOnlyOnChange = updateOnlyOnChange;
this.#forceUpdate = forceUpdate;
}

get current() {
Expand All @@ -73,7 +73,7 @@ export class State<T extends StateObject> {
typeof newState === 'function' ? newState(this.#state) : newState;

const nextState = { ...this.#state, ...internalNewState };
if (!this.#updateOnlyOnChange || !compareObjects(this.#state, nextState)) {
if (this.#forceUpdate || !compareObjects(this.#state, nextState)) {
const prevState = this.#state;
this.#state = nextState;
Object.freeze(this.#state);
Expand Down
2 changes: 2 additions & 0 deletions packages/libs/sdk-mixins/src/mixins/configMixin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ export type ClientCondition = {

export type ClientConditionResult = {
screenId: string;
screenName: string;
interactionId: string;
};

export type FlowConfig = {
startScreenId?: string;
startScreenName?: string;
version: number;
targetLocales?: string[];
conditions?: ClientCondition[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('AppComponent', () => {
providers: [
DescopeAuthConfig,
{ provide: DescopeAuthConfig, useValue: { projectId: 'test' } }
],
]
}).compileComponents();
});

Expand Down
76 changes: 38 additions & 38 deletions packages/sdks/core-js-sdk/src/createSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,48 @@ const withSdkConfigValidations = withValidations([
/** Add the ability to pass multiple hooks instead of one when creating an SDK instance */
const withMultipleHooks =
<T extends object>(createSdk: (config: SdkConfig) => T) =>
(
config: Omit<SdkConfig, 'hooks'> & {
hooks?: {
beforeRequest?: BeforeRequest | BeforeRequest[];
afterRequest?: AfterRequest | AfterRequest[];
transformResponse?: Hooks['transformResponse'];
};
},
) => {
const beforeRequest: BeforeRequest = (conf) => {
// get the before hooks from the config while function is running
// because the hooks might change after sdk creation
const beforeRequestHooks = [].concat(config.hooks?.beforeRequest || []);
return beforeRequestHooks?.reduce((acc, fn) => fn(acc), conf);
};

const afterRequest: AfterRequest = async (req, res) => {
// get the after hooks from the config while function is running
// because the hooks might change after sdk creation
const afterRequestHooks = [].concat(config.hooks?.afterRequest || []);
// do not remove this check - on old versions of react-native it is required
if (afterRequestHooks.length == 0) return;
const results = await Promise.allSettled(
afterRequestHooks?.map((fn) => fn(req, res?.clone())),
);
// eslint-disable-next-line no-console
results.forEach(
(result) =>
result.status === 'rejected' && config.logger?.error(result.reason),
);
(
config: Omit<SdkConfig, 'hooks'> & {
hooks?: {
beforeRequest?: BeforeRequest | BeforeRequest[];
afterRequest?: AfterRequest | AfterRequest[];
transformResponse?: Hooks['transformResponse'];
};
},
) => {
const beforeRequest: BeforeRequest = (conf) => {
// get the before hooks from the config while function is running
// because the hooks might change after sdk creation
const beforeRequestHooks = [].concat(config.hooks?.beforeRequest || []);
return beforeRequestHooks?.reduce((acc, fn) => fn(acc), conf);
};

return createSdk({
...config,
hooks: {
beforeRequest,
afterRequest,
transformResponse: config.hooks?.transformResponse,
},
});
const afterRequest: AfterRequest = async (req, res) => {
// get the after hooks from the config while function is running
// because the hooks might change after sdk creation
const afterRequestHooks = [].concat(config.hooks?.afterRequest || []);
// do not remove this check - on old versions of react-native it is required
if (afterRequestHooks.length == 0) return;
const results = await Promise.allSettled(
afterRequestHooks?.map((fn) => fn(req, res?.clone())),
);
// eslint-disable-next-line no-console
results.forEach(
(result) =>
result.status === 'rejected' && config.logger?.error(result.reason),
);
};

return createSdk({
...config,
hooks: {
beforeRequest,
afterRequest,
transformResponse: config.hooks?.transformResponse,
},
});
};

/** Descope SDK client */
export default withSdkConfigValidations(
withMultipleHooks(
Expand Down
6 changes: 3 additions & 3 deletions packages/sdks/nextjs-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
},
"./client": {
"import": {
"types": "./dist/types/client/index.d.ts",
"types": "./dist/types/client/index.d.ts",
"default": "./dist/esm/client/index.js"
},
"require": "./dist/cjs/client/index.js"
},
"./server": {
"import": {
"types": "./dist/types/server/index.d.ts",
"types": "./dist/types/server/index.d.ts",
"default": "./dist/esm/server/index.js"
},
"require": "./dist/cjs/server/index.js"
Expand Down Expand Up @@ -129,7 +129,7 @@
"rollup-plugin-dotenv": "^0.5.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-livereload": "^2.0.5",
"rollup-plugin-no-emit": "1.2.1",
"rollup-plugin-no-emit": "1.2.1",
"rollup-plugin-preserve-directives": "^0.4.0",
"rollup-plugin-serve": "^2.0.3",
"rollup-plugin-swc3": "^0.12.0",
Expand Down
34 changes: 17 additions & 17 deletions packages/sdks/nextjs-sdk/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const commonPlugins = (outputDir) => [
}),
typescript({
tsconfig: './tsconfig.json',
declaration: false,
declaration: false
}),
// swcPreserveDirectives(),
preserveDirectives({ supressPreserveModulesWarning: true }),
Expand Down Expand Up @@ -89,20 +89,20 @@ const configurations = ['server', 'client', ''].flatMap((entry) => {
});

export default [
...configurations,
{
input: 'src/index.ts',
output: [{ dir: './dist', format: 'esm' }],
plugins: [
typescript({
tsconfig: './tsconfig.json',
compilerOptions: {
rootDir: './src',
declaration: true,
declarationDir: './dist/types',
},
}),
noEmit({ match: (file) => file.endsWith('.js') }),
],
},
...configurations,
{
input: 'src/index.ts',
output: [{ dir: './dist', format: 'esm' }],
plugins: [
typescript({
tsconfig: './tsconfig.json',
compilerOptions: {
rootDir: './src',
declaration: true,
declarationDir: './dist/types'
}
}),
noEmit({ match: (file) => file.endsWith('.js') })
]
}
];
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const AccessKeyManagement = React.forwardRef<

useImperativeHandle(ref, () => innerRef);

const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } = React.useContext(Context);
const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } =
React.useContext(Context);

return (
<Suspense fallback={null}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const ApplicationsPortal = React.forwardRef<

useImperativeHandle(ref, () => innerRef);

const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } = React.useContext(Context);
const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } =
React.useContext(Context);

return (
<Suspense fallback={null}>
Expand Down
3 changes: 2 additions & 1 deletion packages/sdks/react-sdk/src/components/AuditManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const AuditManagement = React.forwardRef<HTMLElement, AuditManagementProps>(

useImperativeHandle(ref, () => innerRef);

const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } = React.useContext(Context);
const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } =
React.useContext(Context);

return (
<Suspense fallback={null}>
Expand Down
19 changes: 12 additions & 7 deletions packages/sdks/react-sdk/src/components/Descope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const DescopeWC = lazy(async () => {
return {
default: withPropsMapping(
React.forwardRef<HTMLElement>((props, ref) => (
<descope-wc ref={ref} {...props} />
<descope-wc ref={ref} {...props} />
)),
),
};
Expand All @@ -69,6 +69,8 @@ const Descope = React.forwardRef<HTMLElement, DescopeProps>(
restartOnError,
errorTransformer,
styleId,
onScreenUpdate,
children,
},
ref,
) => {
Expand Down Expand Up @@ -151,9 +153,9 @@ const Descope = React.forwardRef<HTMLElement, DescopeProps>(
* it can be removed once this issue will be solved
* https://bugs.chromium.org/p/chromium/issues/detail?id=1404106#c2
*/
<form>
<Suspense fallback={null}>
<DescopeWC
<form>
<Suspense fallback={null}>
<DescopeWC
projectId={projectId}
flowId={flowId}
baseUrl={baseUrl}
Expand Down Expand Up @@ -181,10 +183,13 @@ const Descope = React.forwardRef<HTMLElement, DescopeProps>(
// props
'errorTransformer.prop': errorTransformer,
'logger.prop': logger,
'onScreenUpdate.prop': onScreenUpdate,
}}
/>
</Suspense>
</form>
>
{children}
</DescopeWC>
</Suspense>
</form>
);
},
);
Expand Down
3 changes: 2 additions & 1 deletion packages/sdks/react-sdk/src/components/RoleManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const RoleManagement = React.forwardRef<HTMLElement, RoleManagementProps>(

useImperativeHandle(ref, () => innerRef);

const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } = React.useContext(Context);
const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } =
React.useContext(Context);

return (
<Suspense fallback={null}>
Expand Down
3 changes: 2 additions & 1 deletion packages/sdks/react-sdk/src/components/UserManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const UserManagement = React.forwardRef<HTMLElement, UserManagementProps>(

useImperativeHandle(ref, () => innerRef);

const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } = React.useContext(Context);
const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } =
React.useContext(Context);

return (
<Suspense fallback={null}>
Expand Down
10 changes: 10 additions & 0 deletions packages/sdks/react-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ export type DescopeProps = {
// use to override client context in flow execution
client?: Record<string, any>;
styleId?: string;
onScreenUpdate?: (
screenName: string,
state: Record<string, any>,
next: (
interactionId: string,
form: Record<string, any>,
) => Promise<unknown>,
ref: HTMLElement,
) => boolean;
children?: React.ReactNode;
};

export type UserManagementProps = WidgetProps;
Expand Down
9 changes: 7 additions & 2 deletions packages/sdks/vue-sdk/src/Descope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ const emit = defineEmits<{
(e: 'error', payload: CustomEvent<ErrorResponse>): void;
(e: 'ready', payload: CustomEvent<Record<string, never>>): void;
}>();
const { projectId, baseUrl, baseStaticUrl, storeLastAuthenticatedUser, baseCdnUrl } =
useOptions();
const {
projectId,
baseUrl,
baseStaticUrl,
storeLastAuthenticatedUser,
baseCdnUrl,
} = useOptions();
const sdk = useDescope();

const formStr = computed(() => (props.form ? JSON.stringify(props.form) : ''));
Expand Down
2 changes: 1 addition & 1 deletion packages/sdks/vue-sdk/src/UserProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ defineProps({
},
});

const { projectId, baseUrl, baseStaticUrl, baseCdnUrl} = useOptions();
const { projectId, baseUrl, baseStaticUrl, baseCdnUrl } = useOptions();
</script>
Loading