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:
co-authored by
Claude Opus 5.5
parent
1270ebc3d1
commit
de414a81a8
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user