Create scope schema and test it.
npm install @shortlnk/scope
const Scope = require('@shortlnk/scope');
const scopeSchema = 'service:users:*';
const scope = new Scope(scopeSchema);
scope.scheme; // ['service', 'users', '*']
scope.test('service:users:read'); // true
scope.test('service:users:write'); // true
scope.test('service:bots:read'); // false
scope..exportLiterals(); // ['service:users:*']
const Scope = require('@shortlnk/scope');
const scopeSchema = 'service:users:{read,write}';
const scope = new Scope(scopeSchema);
scope.scheme; // ['service', 'users', ['read', 'write']]
scope.test('service:users:read'); // true
scope.test('service:users:write'); // true
scope.test('service:users:delete'); // false
scope.exportLiterals(); // ['service:users:read', 'service:users:write']
const Scope = require('@shortlnk/scope');
const scopeSchema = 'service.host:users:{read,write}:*';
const scope = new Scope(scopeSchema);
scope.scheme; // [['service' ['host']], 'users', ['read', 'write'], '*']
scope.test('service.host:users:read:1'); // true
scope.test('service.host:users:write:2'); // true
scope.test('service.host:users:delete:3'); // false
scope.exportLiterals(); // ['service.host:users:read:*', 'service.host:users:write:*']
const Scope = require('@shortlnk/scope');
const scopeSchema = 'service.host:{profile,member}.*:*.{read,write}:*:{exchange,transfer}.{read,write}';
const scope = new Scope(scopeSchema);
scope.scheme; // [['service', ['host']], [['profile', 'member'], ['*']], [['*'], ['read', 'write']], '*', [['exchange', 'transfer'], ['read', 'write']]]
scope.test('service.host:profile.1:dm.read:friend:exchange.read'); // true
scope.test('service.host:member.all:dm.write:friend:transfer.write'); // true
scope.test('service.host:member.3:write:exchange.read'); // false
scope.exportLiterals();
/**
* [
* 'service.host:profile.*:*.read:*:exchange.read',
* 'service.host:profile.*:*.read:*:exchange.write',
* 'service.host:profile.*:*.read:*:transfer.read',
* 'service.host:profile.*:*.read:*:transfer.write',
*
* 'service.host:profile.*:*.write:*:exchange.read',
* 'service.host:profile.*:*.write:*:exchange.write',
* 'service.host:profile.*:*.write:*:transfer.read',
* 'service.host:profile.*:*.write:*:transfer.write',
*
* 'service.host:member.*:*.read:*:exchange.read',
* 'service.host:member.*:*.read:*:exchange.write',
* 'service.host:member.*:*.read:*:transfer.read',
* 'service.host:member.*:*.read:*:transfer.write',
* 'service.host:member.*:*.write:*:exchange.read',
* 'service.host:member.*:*.write:*:exchange.write',
* 'service.host:member.*:*.write:*:transfer.read',
* 'service.host:member.*:*.write:*:transfer.write',
* ]
*/
export type Item = string | '*' | Array<string | '*'>;
export type Section = Item | [Item, Item]
export type Scopewildcard = Section[];
export type MultiPoolVerbal = `{${string | '*' | ','}}`;
export type ScopeVerbal = `${string | '*' | ':' | MultiPoolVerbal}` | `${string | '*' | MultiPoolVerbal}`;
export type Scheme = Scopewildcard | ScopeVerbal;
export type ScopeLiteral = `${string | ':' | ScopeVerbal}`;
Create a new scope instance.
schema
- The scope schema.
The scheme of the scope.
Test if the scope is valid.
scope
- The scope to test.
boolean
-true
if the scope is valid,false
otherwise.
Export all possible literals of the scope.
ScopeLiteral[]
- All possible literals of the scope.
MIT License
Copyright (c) 2024 shortlnk
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.