Skip to content

Commit

Permalink
chore(astro): Move safe id generation to a shared function (#3698)
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano authored Jul 12, 2024
1 parent 64cc9b5 commit 0937bf6
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-parents-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/astro": patch
---

Add a reusable ID generation function
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ interface Props {
component: 'sign-in' | 'sign-up' | 'organization-list' | 'organization-profile' | 'organization-switcher' | 'user-button' | 'user-profile' | 'google-one-tap'
}
import { customAlphabet, urlAlphabet } from "nanoid";
import { generateSafeId } from '@clerk/astro/internal';
const safeId = customAlphabet(urlAlphabet, 10)();
const safeId = generateSafeId();
const { component, ...props } = Astro.props
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { HTMLTag, Polymorphic } from 'astro/types'
import type { SignInProps } from "@clerk/types";
type Props<Tag extends HTMLTag = HTMLTag> = Polymorphic<SignInProps & { as: Tag; mode?: 'redirect' | 'modal' }>
import { customAlphabet, urlAlphabet } from "nanoid";
import { generateSafeId } from '@clerk/astro/internal';
const safeId = customAlphabet(urlAlphabet, 10)();
const safeId = generateSafeId();
const {
as: Tag = 'button',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { HTMLTag, Polymorphic } from 'astro/types'
import type { SignOutOptions } from '@clerk/types';
type Props<Tag extends HTMLTag = HTMLTag> = Polymorphic<{ as: Tag; } & SignOutOptions>
import { customAlphabet, urlAlphabet } from "nanoid";
import { generateSafeId } from '@clerk/astro/internal';
const safeId = customAlphabet(urlAlphabet, 10)();
const safeId = generateSafeId();
const {
as: Tag = 'button',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { HTMLTag, Polymorphic } from 'astro/types'
import type { SignUpProps } from "@clerk/types";
type Props<Tag extends HTMLTag = HTMLTag> = Polymorphic<SignUpProps & { as: Tag; mode?: 'redirect' | 'modal' }>
import { customAlphabet, urlAlphabet } from "nanoid";
import { generateSafeId } from '@clerk/astro/internal';
const safeId = customAlphabet(urlAlphabet, 10)();
const safeId = generateSafeId();
const {
as: Tag = 'button',
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ import { createInjectionScriptRunner } from './create-injection-script-runner';
const runInjectionScript = createInjectionScriptRunner(createClerkInstance);

export { runInjectionScript };

export { generateSafeId } from './utils/generateSafeId';
10 changes: 10 additions & 0 deletions packages/astro/src/internal/utils/generateSafeId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { customAlphabet, urlAlphabet } from 'nanoid';

/**
* Generates a safe, URL-friendly unique identifier.
*
* @example
* const id = generateSafeId();
* console.log(id); // Outputs something like: "f3x2P9Xn1K"
*/
export const generateSafeId = (defaultSize = 10) => customAlphabet(urlAlphabet, defaultSize)();

0 comments on commit 0937bf6

Please sign in to comment.