authorized-rules
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -17,3 +17,3 @@ export declare type JwtToken = { | ||
export declare const authorize: <T1 = void, T2 = void, T3 = void, T4 = void>(jwtToken: string, executionRule: Rule<T1 | T2 | T3 | T4>) => Promise<(T1 | T2 | T3 | T4)[]>; | ||
export declare const and: <T extends void | {}>(rules: Rule<T>[]) => Rule<T>; | ||
export declare const or: <T extends void | {}>(rules: Rule<T>[]) => Rule<T>; | ||
export declare const and: <T extends {}>(rules: Rule<T>[]) => Rule<T>; | ||
export declare const or: <T extends {}>(rules: Rule<T>[]) => Rule<T>; |
{ | ||
"name": "authorized-rules", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -41,3 +41,3 @@ import jwtDecoder from 'jwt-decode'; | ||
export const and = <T extends {} | void>(rules: Rule<T>[]): Rule<T> => async (token: JwtToken) => { | ||
export const and = <T extends {}>(rules: Rule<T>[]): Rule<T> => async (token: JwtToken) => { | ||
let data: T[] = []; | ||
@@ -54,3 +54,3 @@ for (const rule of rules) { | ||
export const or = <T extends {} | void>(rules: Rule<T>[]): Rule<T> => async (token: JwtToken) => { | ||
export const or = <T extends {}>(rules: Rule<T>[]): Rule<T> => async (token: JwtToken) => { | ||
let data: T[] = []; | ||
@@ -57,0 +57,0 @@ for (const rule of rules) { |
import * as jwt from 'jsonwebtoken'; | ||
import {and, authorize, or, Rule, RuleResult} from '../../src/index'; | ||
const expertKey = '67c776d3-06a7-40fe-bbc2-4df45781e861'; | ||
type Value = { | ||
@@ -10,7 +8,7 @@ value: string; | ||
const getAccessToken = ({sub, clientId}: {sub: string; clientId: string}) => | ||
const jwtToken = | ||
jwt.sign( | ||
{ | ||
sub, | ||
client_id: clientId, | ||
sub: "222222", | ||
client_id: "'some-client-id'", | ||
grant_type: 'client_credentials', | ||
@@ -22,9 +20,2 @@ iss: 'some-issuer', | ||
const request = { | ||
headers: { | ||
'x-api-key': expertKey, | ||
authorization: `Bearer ${getAccessToken({sub: '222222', clientId: 'some-client-id'})}`, | ||
}, | ||
}; | ||
const createRule = (ruleName: string, passed: boolean, data: Value[]) => (): Rule<Value> => async (): Promise< | ||
@@ -44,3 +35,3 @@ RuleResult<Value> | ||
await authorize(request as any, and([rule1(), rule2()])); | ||
await authorize(jwtToken, and([rule1(), rule2()])); | ||
}); | ||
@@ -52,3 +43,3 @@ | ||
const [result1, result2] = await authorize<Value, Value>(request as any, and([rule1(), rule2()])); | ||
const [result1, result2] = await authorize<Value, Value>(jwtToken, and([rule1(), rule2()])); | ||
expect(result1 && result1.value).toBe('value1'); | ||
@@ -64,4 +55,4 @@ expect(result2 && result2.value).toBe('value2'); | ||
await authorize(request as any, or([rule1(), rule2()])); | ||
await authorize(request as any, or([rule2(), rule1()])); | ||
await authorize(jwtToken, or([rule1(), rule2()])); | ||
await authorize(jwtToken, or([rule2(), rule1()])); | ||
}); | ||
@@ -72,7 +63,7 @@ it('gathers all results until the rule that passed', async () => { | ||
let [result1, result2] = await authorize(request as any, or([rule1(), rule2()])); | ||
let [result1, result2] = await authorize(jwtToken, or([rule1(), rule2()])); | ||
expect(result1.value).toBe('value1'); | ||
expect(result2).toBeUndefined(); | ||
[result2, result1] = await authorize(request as any, or([rule2(), rule1()])); | ||
[result2, result1] = await authorize(jwtToken, or([rule2(), rule1()])); | ||
expect(result2.value).toBe('value2'); | ||
@@ -79,0 +70,0 @@ expect(result1.value).toBe('value1'); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16646
304