Skip to content
5 changes: 5 additions & 0 deletions .changeset/backend-cimd-oauth-applications.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': minor
---

Add Client ID Metadata Document (CIMD) client support to `clerkClient.oauthApplications`. Pass `clientIdMetadataDocumentUrl` to `create()` to pre-register a client from its metadata document URL; `name` is optional in that case, and only `name` and `scopes` can be set. Pass `clientIdMetadataDocument: true` to `list()` to list only CIMD clients. `OAuthApplication` now exposes `clientIdMetadataDocument` and `clientIdMetadataDocumentDetails`, which holds the client's admission status and the state of its last metadata fetch.
27 changes: 27 additions & 0 deletions .typedoc/__tests__/extract-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ describe('extract-methods snapshots', () => {
expect(content).not.toContain('revoke-session-params.mdx');
});

it('marks union-merged params optional unless every arm requires them', async () => {
const createOAuthApplication = await readGenerated('backend/o-auth-applications-api/methods/create.mdx');
expect(createOAuthApplication).toContain('| `clientIdMetadataDocumentUrl?` |');
expect(createOAuthApplication).toContain('| `name?` |');
expect(createOAuthApplication).not.toContain('`never`');

const getOrganization = await readGenerated('backend/organization-api/methods/get-organization.mdx');
expect(getOrganization).toContain('| `organizationId?` |');
expect(getOrganization).toContain('| `slug?` |');

const createUser = await readGenerated('backend/user-api/methods/create-user.mdx');
expect(createUser).toContain('| `passwordDigest?` |');

const updateOAuthApplication = await readGenerated('backend/o-auth-applications-api/methods/update.mdx');
expect(updateOAuthApplication).toContain('| `name` |');
});

it('marks union-merged properties optional on standalone type pages', async () => {
const createOAuthApplicationParams = await readGenerated('backend/create-o-auth-application-params.mdx');
expect(createOAuthApplicationParams).toMatch(/\| `clientIdMetadataDocumentUrl\?` +\| `string`/);
expect(createOAuthApplicationParams).toMatch(/\| `redirectUris\?` +\| <code>string\[\] \\\| null<\/code>/);
expect(createOAuthApplicationParams).not.toContain('`never`');

const getOrganizationParams = await readGenerated('backend/get-organization-params.mdx');
expect(getOrganizationParams).toMatch(/\| `organizationId\?` +\|/);
});

it('simple callable: clerk.signOut()', async () => {
const content = await readGenerated('shared/clerk/methods/sign-out.mdx');
await expect(content).toMatchFileSnapshot('./__snapshots__/clerk-methods-sign-out.mdx');
Expand Down
144 changes: 144 additions & 0 deletions .typedoc/__tests__/type-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import {
DeclarationReflection,
FileRegistry,
ProjectReflection,
ReferenceType,
ReflectionKind,
ReflectionType,
} from 'typedoc';
import { describe, expect, it, vi } from 'vitest';

import { collectUnionArms, markOptionalUnlessRequiredInEveryArm } from '../type-utils.mjs';

const project = new ProjectReflection('project', new FileRegistry());
const parent = new DeclarationReflection('Params', ReflectionKind.TypeAlias, project);
const findNothing = () => undefined;
const property = (name: string) => new DeclarationReflection(name, ReflectionKind.Property, parent);

describe('union property merging', () => {
it('ignores an arm whose properties could not be collected', () => {
const strategy = property('strategy');
const collected = { type: 'reference', name: 'EmailCodeConfig' };
const unresolved = { type: 'reference', name: 'Omit' };
const union = { type: 'union', types: [collected, unresolved] } as any;

const { arms, requirednessArms } = collectUnionArms(
union,
t => (t === collected ? [strategy] : undefined),
findNothing,
);

expect(arms).toHaveLength(2);
expect(markOptionalUnlessRequiredInEveryArm([strategy], requirednessArms)[0].flags.isOptional).toBe(false);
});

it('ignores an intersection arm with an uncollected member', () => {
const strategy = property('strategy');
const collected = { type: 'reference', name: 'EmailCodeConfig' };
const unresolved = { type: 'reference', name: 'Omit' };
const partial = { type: 'intersection', types: [{ type: 'reference', name: 'Other' }, unresolved] };
const union = { type: 'union', types: [collected, partial] } as any;

const { requirednessArms } = collectUnionArms(
union,
t => {
if (t === collected) {
return [strategy];
}
return t === unresolved ? undefined : [property('other')];
},
findNothing,
);

expect(markOptionalUnlessRequiredInEveryArm([strategy], requirednessArms)[0].flags.isOptional).toBe(false);
});

it('counts an arm that resolved to no properties', () => {
const x = property('x');
const base = { type: 'reference', name: 'X' };
const omitted = { type: 'reference', name: 'Omit' };
const union = { type: 'union', types: [base, omitted] } as any;

const { requirednessArms } = collectUnionArms(union, t => (t === base ? [x] : []), findNothing);

expect(markOptionalUnlessRequiredInEveryArm([x], requirednessArms)[0].flags.isOptional).toBe(true);
});

it.each([
['an empty interface', ReflectionKind.Interface],
['an alias of an empty object type', ReflectionKind.TypeAlias],
])('counts %s that the collector reports as undefined', (_, kind) => {
const a = property('a');
const empty = new DeclarationReflection('Empty', kind, project);
project.registerReflection(empty, undefined, undefined);
if (kind === ReflectionKind.TypeAlias) {
empty.type = new ReflectionType(new DeclarationReflection('__type', ReflectionKind.TypeLiteral, empty));
}
const base = { type: 'reference', name: 'A' };
const union = {
type: 'union',
types: [base, ReferenceType.createResolvedReference('Empty', empty, project)],
} as any;

const { requirednessArms } = collectUnionArms(union, t => (t === base ? [a] : undefined), findNothing);

expect(markOptionalUnlessRequiredInEveryArm([a], requirednessArms)[0].flags.isOptional).toBe(true);
});

it.each([
['counts an empty declaration found by name', true],
['ignores a reference not found by name', false],
])('%s', (_, isFound) => {
const a = property('a');
const empty = new DeclarationReflection('EmptyByName', ReflectionKind.Interface, project);
const base = { type: 'reference', name: 'A' };
const union = { type: 'union', types: [base, { type: 'reference', name: 'EmptyByName' }] } as any;

const { requirednessArms } = collectUnionArms(
union,
t => (t === base ? [a] : undefined),
name => (isFound && name === 'EmptyByName' ? empty : undefined),
);

expect(markOptionalUnlessRequiredInEveryArm([a], requirednessArms)[0].flags.isOptional).toBe(isFound);
});

it('collects each arm and intersection member once', () => {
const a = { type: 'reference', name: 'A' };
const b = { type: 'reference', name: 'B' };
const c = { type: 'reference', name: 'C' };
const union = { type: 'union', types: [a, { type: 'intersection', types: [b, c] }] } as any;
const collect = vi.fn(() => [property('x')]);

collectUnionArms(union, collect, findNothing);

expect(collect.mock.calls).toEqual([[a], [b], [c]]);
});

it('treats `object` as an arm that requires nothing, and ignores null', () => {
const digest = property('passwordDigest');
const hashing = { type: 'reference', name: 'UserPasswordHashingParams' };
const collect = (t: unknown) => (t === hashing ? [digest] : undefined);
const withObject = { type: 'union', types: [hashing, { type: 'intrinsic', name: 'object' }] } as any;
const withNull = { type: 'union', types: [hashing, { type: 'literal', value: null }] } as any;
const isOptional = (union: any) =>
markOptionalUnlessRequiredInEveryArm([digest], collectUnionArms(union, collect, findNothing).requirednessArms)[0]
.flags.isOptional;

expect(isOptional(withObject)).toBe(true);
expect(isOptional(withNull)).toBe(false);
});

it('changes only the optional flag on the copy and leaves the original alone', () => {
const original = property('organizationId');

const [copy] = markOptionalUnlessRequiredInEveryArm([original], [[original], []]);

expect(copy).not.toBe(original);
expect(copy.flags.isOptional).toBe(true);
expect(original.flags.isOptional).toBe(false);
expect(copy.parent).toBe(parent);
expect(copy.project).toBe(project);
expect(copy.getFullName()).toBe(original.getFullName());
});
});
26 changes: 21 additions & 5 deletions .typedoc/custom-theme.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { applyTodoStrippingToComment } from './comment-utils.mjs';
import { backTicks, heading, htmlTable, removeLineBreaks, table } from './markdown-helpers.mjs';
import { REFERENCE_OBJECTS_LIST } from './reference-objects.mjs';
import { isInlineModifierWithoutStandalonePage } from './standalone-page-tag.mjs';
import { unwrapOptional } from './type-utils.mjs';
import {
collectUnionArms,
markOptionalUnlessRequiredInEveryArm,
propertyReflectionTypeIsNever,
unwrapOptional,
} from './type-utils.mjs';

export { REFERENCE_OBJECTS_LIST };

Expand Down Expand Up @@ -391,14 +396,25 @@ function collectPropertyReflectionsFromUnionObjectArms(t, visitedReflectionIds,
return [];
}
const union = /** @type {import('typedoc').UnionType} */ (unwrapped);
const { arms, requirednessArms } = collectUnionArms(
union,
arm => {
const properties = collectPropertyReflectionsFromIntersectionArm(arm, visitedReflectionIds, project);
return properties.length ? properties : undefined;
},
name => (project ? findNamedTypeDeclaration(project, name) : undefined),
);
/** @type {Map<string, import('typedoc').DeclarationReflection>} */
const byName = new Map();
for (const arm of union.types) {
for (const p of collectPropertyReflectionsFromIntersectionArm(arm, visitedReflectionIds, project)) {
byName.set(p.name, p);
for (const arm of arms) {
for (const p of arm) {
if (!propertyReflectionTypeIsNever(p)) {
byName.set(p.name, p);
}
}
}
return [...byName.values()].sort((a, b) => a.name.localeCompare(b.name));
const merged = [...byName.values()].sort((a, b) => a.name.localeCompare(b.name));
return markOptionalUnlessRequiredInEveryArm(merged, requirednessArms);
}

/**
Expand Down
24 changes: 12 additions & 12 deletions .typedoc/extract-methods.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ function methodFormatForPageUrl(pageUrl) {
}
import { toFileSlug } from './slug.mjs';
import { isInlineModifierWithoutStandalonePage } from './standalone-page-tag.mjs';
import { unwrapOptional } from './type-utils.mjs';
import {
collectUnionArms,
markOptionalUnlessRequiredInEveryArm,
propertyReflectionTypeIsNever,
unwrapOptional,
} from './type-utils.mjs';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -769,14 +774,6 @@ function appendSignatureOnlyReturns(declComment, sigComment) {
return formatReturnsLineFromTag(tag);
}

/**
* @param {import('typedoc').DeclarationReflection} prop
*/
function propertyReflectionTypeIsNever(prop) {
const ty = unwrapOptional(prop.type, { deep: true });
return ty?.type === 'intrinsic' && ty.name === 'never';
}

/**
* Union discriminators often use `otherProp?: never`. Prefer the branch with a documentable type.
*
Expand Down Expand Up @@ -955,10 +952,13 @@ function resolveDeclarationWithObjectMembers(t, project) {
}
if (t.type === 'union') {
const u = /** @type {import('typedoc').UnionType} */ (t);
return mergePropertyArms(
u.types.map(inner => resolveDeclarationWithObjectMembers(inner, project)),
{ skipNever: true, pickBetter: true },
const { arms, requirednessArms } = collectUnionArms(
u,
inner => resolveDeclarationWithObjectMembers(inner, project),
name => (project ? lookupInterfaceOrTypeAliasByName(project, name) : undefined),
);
const merged = mergePropertyArms(arms, { skipNever: true, pickBetter: true });
return merged && markOptionalUnlessRequiredInEveryArm(merged, requirednessArms);
}
return undefined;
}
Expand Down
Loading
Loading