Skip to content

Commit

Permalink
Merge pull request #6982 from logto-io/yemq-add-core-kit-changeset
Browse files Browse the repository at this point in the history
chore: add core-kit changeset
  • Loading branch information
darcyYe authored Jan 28, 2025
1 parent 8f387cd commit 62eb8ed
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
8 changes: 8 additions & 0 deletions .changeset/loud-trees-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@logto/core-kit": patch
---

refactor user claims type and introduce `userClaimsList`

- Introduce a new exported variable `userClaimsList` containing all possible user claims.
- Utilize `userClaimsList` to derive the `UserClaim` type, ensuring consistency and maintainability.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type UserClaim, completeUserClaims } from '@logto/core-kit';
import { type UserClaim, userClaimsList } from '@logto/core-kit';
import { type SamlApplicationResponse, samlAttributeMappingKeys } from '@logto/schemas';
import { conditionalArray } from '@silverhand/essentials';
import { useForm, Controller } from 'react-hook-form';
Expand Down Expand Up @@ -103,7 +103,7 @@ function AttributeMapping({ data, mutateApplication }: Props) {

// Not using `useMemo` to avoid the reappearance of the available keys when the form values change.
const existingKeys = new Set(formValues.map(([key]) => key).filter(Boolean));
const availableKeys = completeUserClaims.filter((claim) => !existingKeys.has(claim));
const availableKeys = userClaimsList.filter((claim) => !existingKeys.has(claim));

return (
<DetailsForm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { type ToZodObject } from '@logto/connector-kit';
import { completeUserClaims, type UserClaim } from '@logto/core-kit';
import { userClaimsList, type UserClaim } from '@logto/core-kit';
import { z } from 'zod';

export type SamlAttributeMapping = Partial<Record<UserClaim | 'sub', string>>;

export const samlAttributeMappingKeys = Object.freeze([
'sub',
...completeUserClaims,
] satisfies Array<keyof SamlAttributeMapping>);
export const samlAttributeMappingKeys = Object.freeze(['sub', ...userClaimsList] satisfies Array<
keyof SamlAttributeMapping
>);

export const samlAttributeMappingGuard = z
.object(
Expand Down
18 changes: 12 additions & 6 deletions packages/toolkit/core-kit/src/openid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ export enum ReservedResource {

/**
* A comprehensive list of all available user claims that can be used in SAML applications.
* These claims can be mapped to SAML attributes in the application configuration.
* This array serves two purposes:
* 1. Acts as a single source of truth for all possible `UserClaim` values
* 2. Provides a runtime accessible list of all available claims
*
* Note: This array must include ALL possible values from UserClaim type.
* TypeScript will throw a compile-time error if any value is missing.
* Previously, `UserClaim` type was defined directly as a union type. Now, we define this array first
* and derive the `UserClaim` type from it using Zod. This approach maintains type safety while also
* making the complete list of claims available at runtime.
*
* Note: This array must include ALL possible values from `UserClaim` type.
* TypeScript will throw error if any value is missing.
*/
export const completeUserClaims = [
export const userClaimsList = [
// OIDC standard claims
'name',
'given_name',
Expand Down Expand Up @@ -57,9 +63,9 @@ export const completeUserClaims = [
] as const;

/**
* Zod guard for UserClaim type, using completeUserClaims as the single source of truth
* Zod guard for `UserClaim` type, using `userClaimsList` as the single source of truth
*/
export const userClaimGuard = z.enum(completeUserClaims);
export const userClaimGuard = z.enum(userClaimsList);

export type UserClaim = z.infer<typeof userClaimGuard>;

Expand Down

0 comments on commit 62eb8ed

Please sign in to comment.