Skip to content

API: Guantr Constructor

The Guantr class can be instantiated directly with new as an alternative to createGuantr(). The constructor accepts options (storage, context, circuit breaker) but does not accept initial rules — you must call setRules() separately after construction.

Importing

ts
import { Guantr } from 'guantr';
import type { GuantrMeta, GuantrOptions } from 'guantr';

Constructor Signature

ts
class Guantr<Meta> {
  constructor(options?: GuantrOptions<GuantrContextFromMeta<Meta>>);
}

Generics

  • Meta: (Optional) The same GuantrMeta<ResourceMap, Context> type passed to all Guantr APIs. When omitted, untyped mode is used.

Parameters

  • options: (Optional) A GuantrOptions object.
PropertyTypeDefaultDescription
contextContext | (() => Context | PromiseLike<Context>){}Evaluation context (static object) or a function that resolves it on each check.
storageStorageInMemoryStorageCustom storage adapter.
maxRuleIterationsnumber1000Maximum rule evaluations per check. Must be a positive integer; a TypeError is thrown at construction time if invalid.

Returns

  • Guantr<Meta> — A new instance with the configured storage, context provider, and circuit breaker limit. The instance can and cannot callables are immediately usable (though no rules are set yet).

Comparison with createGuantr

Featurenew Guantr(options)createGuantr(options)
Initial rulesMust call setRules() separatelyCan pass rules as first argument
Rule callback syntaxNot directly — use setRules(callback)Supported as first argument
Rule array syntaxNot directly — use setRules(array)Supported as first argument
Storage & contextVia optionsVia options
Type inferenceFull generic supportFull generic support
ReturnSynchronousPromise<Guantr<Meta>>

The createGuantr factory is equivalent to new Guantr(options) followed by setRules(rules) when rules are provided. Use new Guantr() when you always intend to call setRules() later and don't need the convenience of inline rule definition.

Examples

Basic instantiation

ts
import { Guantr } from 'guantr';

const guantr = new Guantr();

await guantr.setRules((allow, deny) => {
  allow('read', 'article');
});

With custom storage and context

ts
const guantr = new Guantr<MyMeta>({
  storage: new MyCustomStorage(),
  context: async () => {
    const user = await getCurrentUser();
    return { userId: user?.id ?? null };
  },
});

await guantr.setRules([{ effect: 'allow', action: 'read', resource: 'post' }]);

With custom circuit breaker limit

ts
const guantr = new Guantr({
  maxRuleIterations: 500,
});

Invalid maxRuleIterations

ts
// Throws TypeError: "maxRuleIterations must be a positive integer"
new Guantr({ maxRuleIterations: 0 });
new Guantr({ maxRuleIterations: -1 });
new Guantr({ maxRuleIterations: 1.5 });

See also

  • createGuantr() — Recommended factory function with rule initialization support.
  • setRules() — Set rules after construction.
  • can() — Permission checking.