Role checks in client components: export them from /client, fix the docs

access.md and the 1.16.4 upgrade notes led projects to import isAdmin from
'@intecion/ipal-kit' in a client component; with `next build --webpack` that
fails ('server-only' cannot be imported from a Client Component module).
No existing entry had a client-safe one, so '@intecion/ipal-kit/client' now
exports hasMinimumRole, isAdmin, isEditor and the type Role; the main entry
keeps them for server code.

Docs: access.md (snippet with imports, server vs client, useConfig() carries
admin.user only), AGENT.md, ipal-debug, ipal-reference, ipal-upgrade, the
1.16.4 note, an Unreleased changelog entry. Tests: /client reaches no
server-only module, the predicates' answers with a client config, no
client-component example in the docs imports the main entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-24 22:00:27 +01:00
co-authored by Claude Opus 5.5
parent 1270ebc3d1
commit de414a81a8
13 changed files with 117 additions and 19 deletions
+12
View File
@@ -149,6 +149,18 @@ describe('docs ↔ code', () => {
expect(broken).toEqual([])
})
test('no client-component example imports the main entry', () => {
// '@intecion/ipal-kit' reaches server-only modules: a 'use client' file that imports it does not build with webpack.
const offending: string[] = []
for (const file of docs) {
const blocks = file.endsWith('.md') ? [...read(file).matchAll(/```[a-z]*\n([\s\S]*?)```/g)].map((m) => m[1]) : [read(file)]
for (const block of blocks) {
if (/^['"]use client['"]/m.test(block) && /^import (?!type\b)[^'"]*?from ['"]@intecion\/ipal-kit['"]/m.test(block)) {offending.push(file)}
}
}
expect(offending).toEqual([])
})
test('every module page is listed in docs/README.md', () => {
const index = read('docs/README.md')
const pages = readdirSync(path.join(root, 'docs')).filter((n) => n.endsWith('.md') && !['CHANGELOG.md', 'README.md'].includes(n))
+42
View File
@@ -42,3 +42,45 @@ describe('server entries load no client modules', () => {
expect(clientModulesReachable(entry)).toEqual([])
})
})
/** Modules importing 'server-only' reachable from `entry` — static and dynamic imports (a bundler follows both). */
function serverOnlyReachable(entry: string): string[] {
const seen = new Set<string>()
const hits: string[] = []
const stack = [path.join(root, entry)]
while (stack.length) {
const file = stack.pop()!
if (seen.has(file)) {continue}
seen.add(file)
const src = readFileSync(file, 'utf8')
if (/^import ['"]server-only['"]/m.test(src)) {hits.push(path.relative(root, file))}
for (const m of src.matchAll(/^(?:import|export) (?!type\b)[^'"]*?from ['"]([^'"]+)['"]|\bimport\(\s*['"]([^'"]+)['"]/gm)) {
const next = resolve(file, m[1] ?? m[2])
if (next) {stack.push(next)}
}
}
return hits.sort()
}
describe('client components', () => {
test('/client reaches no server-only module', () => {
expect(serverOnlyReachable('src/exports/client.ts')).toEqual([])
// why a client component must not import the main entry: webpack stops at these
expect(serverOnlyReachable('src/index.ts')).not.toEqual([])
})
test('/client exports the role predicates, with the same fail-closed answers', async () => {
const { hasMinimumRole, isAdmin, isEditor } = await import('../src/modules/access/predicates.js')
const client = readFileSync(path.join(root, 'src/exports/client.ts'), 'utf8')
expect(client).toMatch(/export \{ hasMinimumRole, isAdmin, isEditor \} from '\.\.\/modules\/access\/predicates\.js'/)
const config = { admin: { user: 'users' } } // what useConfig() carries: `custom` is not in the client config
const admin = { collection: 'users', roles: ['admin'] }
const editor = { collection: 'users', roles: ['editor'] }
expect([isAdmin(admin, config), isAdmin(editor, config), isAdmin(null, config)]).toEqual([true, false, false])
expect([isEditor(editor, config), hasMinimumRole(editor, 'user', config)]).toEqual([true, true])
expect(isAdmin({ ...admin, collection: 'customers' }, config)).toBe(false)
// access.staffCollections is not in the client config: name the collections
expect(isAdmin({ ...admin, collection: 'partners' }, config)).toBe(false)
expect(isAdmin({ ...admin, collection: 'partners' }, { staffCollections: ['users', 'partners'] })).toBe(true)
})
})