@orion-js/resolvers
Advanced tools
Comparing version 3.0.0-alpha.20 to 3.0.0-alpha.23
@@ -1,3 +0,7 @@ | ||
import resolver from './resolver'; | ||
import { resolver, modelResolver } from './resolver'; | ||
export * from './resolver/types'; | ||
export { resolver }; | ||
import checkPermissions from './resolver/getExecute/checkPermissions'; | ||
import { addPermissionChecker } from './resolver/permisionsCheckers'; | ||
import cleanParams from './resolver/cleanParams'; | ||
import cleanReturns from './resolver/cleanReturns'; | ||
export { resolver, modelResolver, checkPermissions, addPermissionChecker, cleanParams, cleanReturns }; |
@@ -16,5 +16,14 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.resolver = void 0; | ||
const resolver_1 = __importDefault(require("./resolver")); | ||
exports.resolver = resolver_1.default; | ||
exports.cleanReturns = exports.cleanParams = exports.addPermissionChecker = exports.checkPermissions = exports.modelResolver = exports.resolver = void 0; | ||
const resolver_1 = require("./resolver"); | ||
Object.defineProperty(exports, "resolver", { enumerable: true, get: function () { return resolver_1.resolver; } }); | ||
Object.defineProperty(exports, "modelResolver", { enumerable: true, get: function () { return resolver_1.modelResolver; } }); | ||
__exportStar(require("./resolver/types"), exports); | ||
const checkPermissions_1 = __importDefault(require("./resolver/getExecute/checkPermissions")); | ||
exports.checkPermissions = checkPermissions_1.default; | ||
const permisionsCheckers_1 = require("./resolver/permisionsCheckers"); | ||
Object.defineProperty(exports, "addPermissionChecker", { enumerable: true, get: function () { return permisionsCheckers_1.addPermissionChecker; } }); | ||
const cleanParams_1 = __importDefault(require("./resolver/cleanParams")); | ||
exports.cleanParams = cleanParams_1.default; | ||
const cleanReturns_1 = __importDefault(require("./resolver/cleanReturns")); | ||
exports.cleanReturns = cleanReturns_1.default; |
@@ -10,2 +10,6 @@ "use strict"; | ||
return; | ||
// for typed model | ||
if (rawParams.getModel) { | ||
rawParams = rawParams.getModel(); | ||
} | ||
if (rawParams.__isModel) { | ||
@@ -12,0 +16,0 @@ rawParams = rawParams.getSchema(); |
@@ -9,5 +9,7 @@ "use strict"; | ||
return; | ||
await (0, permisionsCheckers_1.checkPermissions)(options.permissionsOptions, viewer, { | ||
await (0, permisionsCheckers_1.checkPermissions)({ | ||
resolver: options, | ||
parent, | ||
params | ||
params, | ||
viewer | ||
}); | ||
@@ -14,0 +16,0 @@ if (options.checkPermission) { |
import { ResolverOptions, Execute } from '../types'; | ||
export default function (options: ResolverOptions): Execute<any>; | ||
export default function (options: ResolverOptions): Execute<import("../types").Params, any, undefined>; |
@@ -9,3 +9,3 @@ "use strict"; | ||
function default_1(options, result) { | ||
const { returns } = options; | ||
let { returns } = options; | ||
if (returns) { | ||
@@ -12,0 +12,0 @@ if ((0, isArray_1.default)(returns) && returns[0].__isModel) { |
import { CreateResolver } from './types'; | ||
import { CreateModelResolver } from '..'; | ||
declare const resolver: CreateResolver; | ||
export default resolver; | ||
declare const modelResolver: CreateModelResolver; | ||
export { resolver, modelResolver }; |
@@ -6,2 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.modelResolver = exports.resolver = void 0; | ||
const getExecute_1 = __importDefault(require("./getExecute")); | ||
@@ -11,4 +12,6 @@ const cleanParams_1 = __importDefault(require("./cleanParams")); | ||
const cache_1 = require("@orion-js/cache"); | ||
const resolver = function (options) { | ||
const cleanReturns_1 = __importDefault(require("./cleanReturns")); | ||
const createResolver = function (options) { | ||
options.params = (0, cleanParams_1.default)(options.params); | ||
options.returns = (0, cleanReturns_1.default)(options.returns); | ||
if (!options.cacheProvider) { | ||
@@ -28,2 +31,5 @@ options.cacheProvider = cache_1.defaultCache; | ||
}; | ||
exports.default = resolver; | ||
const resolver = createResolver; | ||
exports.resolver = resolver; | ||
const modelResolver = createResolver; | ||
exports.modelResolver = modelResolver; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const index_1 = __importDefault(require("./index")); | ||
const index_1 = require("./index"); | ||
it('should return a function with a resolver id', () => { | ||
const resolver = (0, index_1.default)({ | ||
const resolver = (0, index_1.resolver)({ | ||
params: {}, | ||
@@ -18,3 +15,3 @@ returns: String, | ||
it('should execute the function', async () => { | ||
const resolver = (0, index_1.default)({ | ||
const resolver = (0, index_1.resolver)({ | ||
params: { | ||
@@ -35,3 +32,3 @@ value: { | ||
let index = 1; | ||
const resolver = (0, index_1.default)({ | ||
const resolver = (0, index_1.resolver)({ | ||
params: { | ||
@@ -60,6 +57,3 @@ value: { | ||
it('should create typed resolvers', async () => { | ||
const resolve = async (params) => { | ||
return params.value * 2; | ||
}; | ||
const resolver = (0, index_1.default)({ | ||
const resolver = (0, index_1.resolver)({ | ||
params: { | ||
@@ -71,25 +65,24 @@ value: { | ||
returns: Number, | ||
resolve | ||
resolve: async (params, viewer) => { | ||
return params.value * 2; | ||
} | ||
}); | ||
const result = await resolver.execute({ params: { value: 2 } }); | ||
expect(result).toBe(4); | ||
const result1 = await resolver.resolve({ | ||
value: 2 | ||
}); | ||
const result2 = await resolver.execute({ | ||
params: { value: 2 } | ||
}); | ||
expect(result1).toBe(4); | ||
expect(result2).toBe(4); | ||
}); | ||
it('should create typed model resolvers', async () => { | ||
const resolver = (0, index_1.default)({ | ||
params: { | ||
value: { | ||
type: Number | ||
} | ||
}, | ||
const resolver = (0, index_1.modelResolver)({ | ||
returns: Number, | ||
resolve: async function (model, params) { | ||
resolve: async function (model, params, viewer) { | ||
return model.value * 2; | ||
} | ||
}); | ||
const inModel = resolver.resolve; | ||
const inModelResult = await inModel({ value: 2 }); | ||
/** | ||
* We are testing the typescript removes one argument on the resolve function. | ||
*/ | ||
expect(inModelResult).toBe(4); | ||
resolver.resolve({}, { value: '' }); | ||
const inModel = resolver.modelResolve; | ||
}); | ||
@@ -108,18 +101,44 @@ it('should accept a model as params', async () => { | ||
}; | ||
const resolver = (0, index_1.default)({ | ||
params: aModel, | ||
class TypedParams { | ||
static getModel() { | ||
return aModel; | ||
} | ||
} | ||
const resolver = (0, index_1.modelResolver)({ | ||
params: TypedParams, | ||
returns: Number, | ||
resolve: async function (params) { | ||
resolve: async function (item, params, viewer) { | ||
return params.value * 2; | ||
} | ||
}); | ||
const inModel = resolver.resolve; | ||
const inModelResult = await inModel({ value: 2 }); | ||
/** | ||
* We are testing the typescript removes one argument on the resolve function. | ||
*/ | ||
expect(inModelResult).toBe(4); | ||
const inModel = resolver.modelResolve; | ||
}); | ||
it('should accept a model as returns', async () => { | ||
const aModel = { | ||
__isModel: true, | ||
name: 'Returns', | ||
getSchema() { | ||
return { | ||
value: { | ||
type: 'string' | ||
} | ||
}; | ||
} | ||
}; | ||
class Returns { | ||
static getModel() { | ||
return aModel; | ||
} | ||
} | ||
const resolver = (0, index_1.resolver)({ | ||
returns: Returns, | ||
resolve: async (params, viewer) => { | ||
return { value: 2 }; | ||
} | ||
}); | ||
const result = await resolver.resolve({}); | ||
expect(result.value).toBe(2); | ||
}); | ||
it('should correctly clean params when no params are passed', async () => { | ||
const resolver = (0, index_1.default)({ | ||
const resolver = (0, index_1.resolver)({ | ||
resolve: async ({ title }) => { | ||
@@ -126,0 +145,0 @@ return `${title}`; |
@@ -1,2 +0,3 @@ | ||
export declare const addPermissionChecker: (func: any) => void; | ||
export declare const checkPermissions: (...args: any[]) => Promise<void>; | ||
import { PermissionChecker, PermissionCheckerOptions } from './types'; | ||
export declare const addPermissionChecker: (func: PermissionChecker) => void; | ||
export declare const checkPermissions: (options: PermissionCheckerOptions) => Promise<void>; |
@@ -5,10 +5,10 @@ "use strict"; | ||
const helpers_1 = require("@orion-js/helpers"); | ||
const checkers = []; | ||
global.checkers = []; | ||
const addPermissionChecker = function (func) { | ||
checkers.push(func); | ||
global.checkers.push(func); | ||
}; | ||
exports.addPermissionChecker = addPermissionChecker; | ||
const checkPermissions = async function (...args) { | ||
for (const checker of checkers) { | ||
const errorMessage = await checker(...args); | ||
const checkPermissions = async function (options) { | ||
for (const checker of global.checkers) { | ||
const errorMessage = await checker(options); | ||
if (errorMessage) { | ||
@@ -15,0 +15,0 @@ throw new helpers_1.PermissionsError(errorMessage); |
import { OrionCache } from '@orion-js/cache'; | ||
export declare type GlobalResolve<T = Promise<any>> = (params: any, viewer: any) => T; | ||
export declare type ModelResolve<T = Promise<any>> = (parent: any, params: any, viewer?: any) => T; | ||
export declare type GlobalResolverResolve<ParamsType = any, ReturnType = any> = (params?: ParamsType, viewer?: any) => Promise<ReturnType>; | ||
export declare type ModelResolverResolve<ModelType = any, ParamsType = any, ReturnType = any> = (item: ModelType, params?: ParamsType, viewer?: any) => Promise<ReturnType>; | ||
export declare type GlobalCheckPermissions = (params: any, viewer: any) => Promise<string>; | ||
@@ -16,11 +16,11 @@ export declare type ModelCheckPermissions = (parent: any, params: any, viewer: any) => Promise<string>; | ||
} | ||
export interface ExecuteParams { | ||
params?: Params; | ||
export interface ExecuteParams<ParamsType extends Params = Params, ModelType = undefined> { | ||
params?: ParamsType; | ||
viewer?: any; | ||
parent?: any; | ||
parent?: ModelType; | ||
} | ||
export declare type Execute<ReturnType = any> = (executeOptions: ExecuteParams) => Promise<ReturnType>; | ||
export declare type Execute<ParamsType = Params, ReturnType = any, ModelType = undefined> = (executeOptions: ExecuteParams<ParamsType, ModelType>) => Promise<ReturnType>; | ||
export interface SharedResolverOptions { | ||
resolverId?: string; | ||
params?: Params; | ||
params?: any; | ||
returns?: any; | ||
@@ -35,13 +35,22 @@ mutation?: boolean; | ||
} | ||
export interface ResolverOptions extends SharedResolverOptions { | ||
resolve: GlobalResolve | ModelResolve; | ||
export interface ResolverOptions<ParamsType extends Params = Params, ReturnType = any, ModelType = undefined> extends SharedResolverOptions { | ||
resolve: ModelType extends undefined ? GlobalResolverResolve<ParamsType, ReturnType> : ModelResolverResolve<ModelType, Params, ReturnType>; | ||
} | ||
declare type AnyFunction = (...args: any) => any; | ||
export interface Resolver<ResolveFunction extends AnyFunction = AnyFunction> extends SharedResolverOptions { | ||
execute: Execute<ReturnType<ResolveFunction>>; | ||
resolve: ResolveFunction; | ||
export interface Resolver<ParamsType = any, ReturnType = any, ModelType = undefined> extends SharedResolverOptions { | ||
execute: Execute<ParamsType, ReturnType, ModelType>; | ||
resolve: ModelType extends undefined ? GlobalResolverResolve<ParamsType, ReturnType> : ModelResolverResolve<ModelType, Params, ReturnType>; | ||
modelResolve?: ModelType extends undefined ? undefined : GlobalResolverResolve<ParamsType, ReturnType>; | ||
} | ||
export declare type CreateResolver = <ResolveFunction extends AnyFunction>(options: ResolverOptions) => Resolver<ResolveFunction>; | ||
declare type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R ? (...args: P) => R : never; | ||
export declare type ModelResolverFunction<F extends ModelResolve> = OmitFirstArg<F>; | ||
export {}; | ||
export declare type CreateResolver = <ParamsType, ReturnType>(options: { | ||
resolve: (params?: ParamsType, viewer?: any) => Promise<ReturnType>; | ||
} & ResolverOptions<ParamsType, ReturnType>) => Resolver<ParamsType, ReturnType, undefined>; | ||
export declare type CreateModelResolver = <ModelType, ParamsType, ReturnType>(options: { | ||
resolve: (item: ModelType, params?: ParamsType, viewer?: any) => Promise<ReturnType>; | ||
} & ResolverOptions<ParamsType, ReturnType, ModelType>) => Resolver<ParamsType, ReturnType, ModelType>; | ||
export interface PermissionCheckerOptions { | ||
resolver: ResolverOptions; | ||
parent: any; | ||
params: any; | ||
viewer: any; | ||
} | ||
export declare type PermissionChecker = (options: PermissionCheckerOptions) => Promise<string | void>; |
{ | ||
"name": "@orion-js/resolvers", | ||
"version": "3.0.0-alpha.20", | ||
"version": "3.0.0-alpha.23", | ||
"main": "lib/index.js", | ||
@@ -19,5 +19,5 @@ "types": "lib/index.d.ts", | ||
"dependencies": { | ||
"@orion-js/cache": "^3.0.0-alpha.10", | ||
"@orion-js/helpers": "^3.0.0-alpha.11", | ||
"@orion-js/schema": "^3.0.0-alpha.13" | ||
"@orion-js/cache": "^3.0.0-alpha.23", | ||
"@orion-js/helpers": "^3.0.0-alpha.23", | ||
"@orion-js/schema": "^3.0.0-alpha.23" | ||
}, | ||
@@ -35,3 +35,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "145e17f5a9d6bbf02f166c5faa416be60752a42f" | ||
"gitHead": "3a2082794e3b1a8748a5aace8d68e5c18a92b707" | ||
} |
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
24746
32
603