Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
chore: clean up types
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef committed Jun 2, 2024
1 parent 1663804 commit c215526
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 44 deletions.
2 changes: 1 addition & 1 deletion packages/astro-clerk-auth/src/client/hotload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Clerk } from '@clerk/clerk-js';
import { AstroClerkIntegrationParams, AstroClerkUpdateOptions } from '../types';
import type { AstroClerkIntegrationParams, AstroClerkUpdateOptions } from '../types';
import { $clerk, $csrState } from '../stores/internal';
import { waitForClerkScript } from '../internal/utils/loadClerkJSScript';
import { runOnce } from './run-once';
Expand Down
6 changes: 3 additions & 3 deletions packages/astro-clerk-auth/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Clerk } from '@clerk/clerk-js';
import type { AstroClerkIntegrationParams, AstroClerkUpdateOptions } from '../types';
import type { AstroClerkCreateInstanceParams, AstroClerkUpdateOptions } from '../types';
import { $clerk, $csrState } from '../stores/internal';
import type { CreateClerkInstanceInternalFn } from './types';
import { runOnce } from './run-once';
import { mountAllClerkAstroJSComponents } from './mount-clerk-astro-js-components';

let initOptions: AstroClerkIntegrationParams | undefined;
let initOptions: AstroClerkCreateInstanceParams | undefined;

/**
* Prevents firing clerk.load multiple times
*/
export const createClerkInstance: CreateClerkInstanceInternalFn = runOnce(createClerkInstanceInternal);

export function createClerkInstanceInternal(options?: AstroClerkIntegrationParams) {
export function createClerkInstanceInternal(options?: AstroClerkCreateInstanceParams) {
let clerkJSInstance = window.Clerk as Clerk;
if (!clerkJSInstance) {
clerkJSInstance = new Clerk(options?.publishableKey!);
Expand Down
4 changes: 2 additions & 2 deletions packages/astro-clerk-auth/src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AstroClerkIntegrationParams } from '../types';
import type { AstroClerkCreateInstanceParams } from '../types';

type CreateClerkInstanceInternalFn = (options?: AstroClerkIntegrationParams) => Promise<unknown>;
type CreateClerkInstanceInternalFn = (options?: AstroClerkCreateInstanceParams) => Promise<unknown>;

export type { CreateClerkInstanceInternalFn };
10 changes: 7 additions & 3 deletions packages/astro-clerk-auth/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="astro/client" />

interface ImportMetaEnv {
interface InternalEnv {
readonly PUBLIC_ASTRO_APP_CLERK_FRONTEND_API?: string;
readonly PUBLIC_ASTRO_APP_CLERK_PUBLISHABLE_KEY?: string;
readonly PUBLIC_ASTRO_APP_CLERK_JS_URL?: string;
Expand All @@ -11,14 +11,18 @@ interface ImportMetaEnv {
readonly CLERK_API_VERSION?: string;
readonly CLERK_JWT_KEY?: string;
readonly CLERK_SECRET_KEY?: string;
readonly PUBLIC_ASTRO_APP_CLERK_DOMAIN?: string;
readonly PUBLIC_ASTRO_APP_CLERK_IS_SATELLITE?: string;
readonly PUBLIC_ASTRO_APP_CLERK_PROXY_URL?: string;
readonly PUBLIC_ASTRO_APP_CLERK_SIGN_IN_URL?: string;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
readonly env: InternalEnv;
}

declare namespace App {
interface Locals {
runtime: { env: ImportMetaEnv };
runtime: { env: InternalEnv };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function createInjectionScriptRunner(creator: CreateClerkInstanceInternalFn) {
clientSafeVars = JSON.parse(clientSafeVarsContainer.textContent || '{}');
}

// @ts-ignore
await creator(mergeEnvVarsWithParams({ ...astroClerkOptions, ...clientSafeVars }));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AstroClerkIntegrationParams } from '../types';

const mergeEnvVarsWithParams = (params?: AstroClerkIntegrationParams) => {
const mergeEnvVarsWithParams = (params?: AstroClerkIntegrationParams & { publishableKey?: string }) => {
const {
signInUrl: paramSignIn,
signUpUrl: paramSignUp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function buildClerkHotloadScript(locals: APIContext['locals']) {
const domain = getSafeEnv(locals).domain!;
const scriptSrc = clerkJsScriptUrl({
clerkJSUrl: getSafeEnv(locals).clerkJsUrl,
// @ts-ignore
clerkJSVariant: getSafeEnv(locals).clerkJsVariant,
clerkJSVersion: getSafeEnv(locals).clerkJsVersion,
domain,
Expand Down
30 changes: 6 additions & 24 deletions packages/astro-clerk-auth/src/server/get-safe-env.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
import type { APIContext } from 'astro';

interface ImportMetaEnv {
readonly PUBLIC_ASTRO_APP_CLERK_FRONTEND_API?: string;
readonly PUBLIC_ASTRO_APP_CLERK_PUBLISHABLE_KEY?: string;
readonly PUBLIC_ASTRO_APP_CLERK_JS_URL?: string;
readonly PUBLIC_ASTRO_APP_CLERK_JS_VARIANT?: 'headless' | '';
readonly PUBLIC_ASTRO_APP_CLERK_JS_VERSION?: string;
readonly CLERK_API_KEY?: string;
readonly CLERK_API_URL?: string;
readonly CLERK_API_VERSION?: string;
readonly CLERK_JWT_KEY?: string;
readonly CLERK_SECRET_KEY?: string;
readonly PUBLIC_ASTRO_APP_CLERK_DOMAIN?: string;
readonly PUBLIC_ASTRO_APP_CLERK_IS_SATELLITE?: string;
readonly PUBLIC_ASTRO_APP_CLERK_PROXY_URL?: string;
readonly PUBLIC_ASTRO_APP_CLERK_SIGN_IN_URL?: string;
}

type ContextOrLocals = APIContext | APIContext['locals'];

export function getContextEnvVar(
envVarName: keyof ImportMetaEnv,
contextOrLocals: ContextOrLocals,
): string | undefined {
function getContextEnvVar(envVarName: keyof InternalEnv, contextOrLocals: ContextOrLocals): string | undefined {
const locals = 'locals' in contextOrLocals ? contextOrLocals.locals : contextOrLocals;

if (locals?.runtime?.env) {
Expand All @@ -32,7 +12,7 @@ export function getContextEnvVar(
return import.meta.env[envVarName];
}

export function getSafeEnv(context: ContextOrLocals) {
function getSafeEnv(context: ContextOrLocals) {
const envVars = {
domain: getContextEnvVar('PUBLIC_ASTRO_APP_CLERK_DOMAIN', context),
isSatellite: getContextEnvVar('PUBLIC_ASTRO_APP_CLERK_IS_SATELLITE', context) === 'true',
Expand All @@ -41,15 +21,15 @@ export function getSafeEnv(context: ContextOrLocals) {
sk: getContextEnvVar('CLERK_SECRET_KEY', context),
signInUrl: getContextEnvVar('PUBLIC_ASTRO_APP_CLERK_SIGN_IN_URL', context),
clerkJsUrl: getContextEnvVar('PUBLIC_ASTRO_APP_CLERK_JS_URL', context),
clerkJsVariant: getContextEnvVar('PUBLIC_ASTRO_APP_CLERK_JS_VARIANT', context),
clerkJsVariant: getContextEnvVar('PUBLIC_ASTRO_APP_CLERK_JS_VARIANT', context) as 'headless' | '' | undefined,
clerkJsVersion: getContextEnvVar('PUBLIC_ASTRO_APP_CLERK_JS_VERSION', context),
apiVersion: getContextEnvVar('CLERK_API_VERSION', context),
apiUrl: getContextEnvVar('CLERK_API_URL', context),
};
return envVars;
}

export function getClientSafeEnv(context: ContextOrLocals) {
function getClientSafeEnv(context: ContextOrLocals) {
const envVars = {
domain: getContextEnvVar('PUBLIC_ASTRO_APP_CLERK_DOMAIN', context),
isSatellite: getContextEnvVar('PUBLIC_ASTRO_APP_CLERK_IS_SATELLITE', context) === 'true',
Expand All @@ -59,3 +39,5 @@ export function getClientSafeEnv(context: ContextOrLocals) {
};
return envVars;
}

export { getSafeEnv, getClientSafeEnv };
20 changes: 12 additions & 8 deletions packages/astro-clerk-auth/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ import type {
Without,
} from '@clerk/types';

export type AstroClerkUpdateOptions = Pick<ClerkOptions, 'appearance' | 'localization'>;
type AstroClerkUpdateOptions = Pick<ClerkOptions, 'appearance' | 'localization'>;

export type AstroClerkIntegrationParams = Without<
type AstroClerkIntegrationParams = Without<
ClerkOptions,
'isSatellite' | 'sdkMetadata' | 'telemetry' | 'standardBrowser' | 'selectInitialSession'
> &
MultiDomainAndOrProxyPrimitives & {
/** Clerk Publishable Key string. */
publishableKey: string;
};
MultiDomainAndOrProxyPrimitives;

type AstroClerkCreateInstanceParams = AstroClerkIntegrationParams & { publishableKey: string };

declare global {
interface Window {
__astro_clerk_component_props: Map<string, Map<string, Record<string, unknown>>>;
}
}

export type ProtectComponentDefaultProps =
type ProtectComponentDefaultProps =
| {
condition?: never;
role: OrganizationCustomRoleKey;
Expand All @@ -46,4 +45,9 @@ export type ProtectComponentDefaultProps =
permission?: never;
};

export {};
export type {
AstroClerkUpdateOptions,
AstroClerkIntegrationParams,
AstroClerkCreateInstanceParams,
ProtectComponentDefaultProps,
};

0 comments on commit c215526

Please sign in to comment.