ReferenceAccess.ts overview
Read access to reference material: unowned corpus rows on OwnedAccess’s visibility axis, with no draft or moderation axis. Compile a view without a visibility column with context: { unscoped: true }; otherwise a NULL reads as private. ReferenceAccess.readScope has the owned shape, so one renderer filters both.
Exports Grouped by Category
constructors
make
Build the reference-material read access.
Example
import assert from "node:assert/strict"
import { Context, Effect, Layer } from "effect"
import * as Option from "effect/Option"
import * as Auth from "@thomasfosterau/effect-auth"
interface AppUser {
readonly id: string
readonly role: string
}
class CurrentViewer extends Context.Service<CurrentViewer, Option.Option<AppUser>>()("@app/CurrentViewer") {}
const Roles = Auth.Ladder.make(["user", "contributor", "moderator"])
const Axes = Roles.axes({ viewPrivate: "moderator" })
const guards = Auth.Guard.make({ ladder: Roles, viewer: CurrentViewer })
const reference = Auth.ReferenceAccess.make({ guards, viewPrivate: Axes.viewPrivate })
// bea follows the herbarium account.
const Follows = Layer.succeed(Auth.OwnedAccess.IsFollower, {
isFollower: (followerId, ownerId) => Effect.succeed(followerId === "bea" && ownerId === "herbarium"),
following: () => Auth.OwnedAccess.nobody
})
const as = (viewer: AppUser | null, row: Auth.ReferenceAccess.ReferenceRow) =>
Effect.runSync(
reference
.canView(row)
.pipe(Effect.provide(Layer.merge(Layer.succeed(CurrentViewer, Option.fromNullishOr(viewer)), Follows)))
)
const herbarium = { id: "herbarium", role: "user" }
const bea = { id: "bea", role: "user" }
assert.equal(as(null, {}), true) // no visibility axis on the view
assert.equal(as(bea, { visibility: "followers", followersOf: "herbarium" }), true)
assert.equal(as(herbarium, { visibility: "followers", followersOf: "herbarium" }), true)
// The followers-of account is a reader, not an owner.
assert.equal(as(herbarium, { visibility: "private", followersOf: "herbarium" }), false)
assert.equal(as({ id: "mod", role: "moderator" }, { visibility: "private" }), true)
Signature
declare const make: <Id, User extends Guard.Viewer, Rung extends string>(
options: ReferenceAccessOptions<Id, User, Rung>
) => ReferenceAccess<Id>
Since v0.3.0
models
ReferenceAccess (interface)
The reference-material read access for one guard kit and one viewPrivate axis. Build it with make.
Signature
export interface ReferenceAccess<Id> {
/** The per-row predicate. Never fails; `false` is a 404 to the caller. */
readonly canView: (row: ReferenceRow) => Effect.Effect<boolean, never, Id>
/** The per-request read scope a list renders as its `WHERE`; the owned shape. */
readonly readScope: Effect.Effect<OwnedAccess.OwnedReadScope, never, Id>
/**
* The declarative read policies {@link ReferenceAccess.canView} decides,
* each scoped to `Action::"read"`. The row is the resource (`visibility`,
* `followersOf` and `group` attributes, each absent when the row has none);
* `context.unscoped` says the view has no visibility axis, and
* `context.follows` / `context.member` are the seams' answers. Their
* principal scopes are unscoped (`any`), which never reaches a system
* principal.
*/
readonly policies: ReadonlyArray<Policy.DeclarativePolicy>
/**
* The `ReadScope.Binding` {@link ReferenceAccess.policies} compile against:
* `context.follows` is the set `following` over `followersOf`, and
* `context.member` the set `groups` over `group`. Supply `unscoped` in the
* compile request's context.
*/
readonly binding: ReadScope.Binding
}
Since v0.3.0
ReferenceAccessOptions (interface)
Options for make.
Signature
export interface ReferenceAccessOptions<Id, User extends Guard.Viewer, Rung extends string> {
readonly guards: Guard.Guards<Id, User, Rung>
/** The axis that sees every reference row. */
readonly viewPrivate: NoInfer<Rung> | Ladder.Axis<NoInfer<Rung>>
/** The resource entity type rows are decided as. Defaults to `"Reference"`. */
readonly referenceType?: string
}
Since v0.3.0
ReferenceRow (interface)
The facts a read of one reference row is decided from.
Signature
export interface ReferenceRow {
/** Absent: the view has no visibility axis, so the row is visible. */
readonly visibility?: OwnedAccess.Visibility
/** The account whose followers (and itself) may read a `followers` row; absent or undefined: nobody. */
readonly followersOf?: string
/** The group whose members may read a `group` row; absent or undefined: none. */
readonly groupId?: string
}
Since v0.3.0