本文整理匯總了TypeScript中klay-core.defaultModelContext類的典型用法代碼示例。如果您正苦於以下問題:TypeScript defaultModelContext類的具體用法?TypeScript defaultModelContext怎麽用?TypeScript defaultModelContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了defaultModelContext類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: it
it('should reference schema from cache', () => {
const arrayModel = defaultModelContext.array().children(model)
const cache = new Cache()
const arraySchema = components.getSchema(arrayModel, cache, 'Users')
expect(arraySchema).toHaveProperty('$ref', '#/definitions/Users')
const schema = components.getSchema(model, cache)
expect(schema).toHaveProperty('$ref', '#/definitions/UsersItem')
})
示例2: buildArraySchema
function buildArraySchema(
model: IModel,
cache?: ISwaggerSchemaCache,
name?: string,
): swagger.Schema {
const children = model.spec.children as IModel
const childrenForArray = children || defaultModelContext.string()
return {
type: 'array',
items: getSchema(childrenForArray, cache, `${name}Item`),
}
}
示例3:
const filterChildren = filterKeys.map(key => {
let model = valueModel
if (key === '$match') {
model = defaultModelContext.create({type: ModelType.String})
}
if (key === '$in' || key === '$nin') {
model = defaultModelContext
.create({
type: ModelType.Array,
children: valueModel,
swagger: {
inline: true,
alternateQueryModel: defaultModelContext.create({type: ModelType.String}),
},
})
.coerce(vr => (typeof vr.value === 'string' ? vr.setValue(vr.value.split(',')) : vr))
}
return {path: key, model}
})
示例4: it
it('should build a specification', () => {
const routerOpts = {
routes: {
...routerModule.CRUD_ROUTES,
'POST /extras/': {
actionName: 'Build Extras',
bodyModel: defaultModelContext.boolean(),
handler: jest.fn(),
},
},
}
const router = routerModule.createRouter(routerOpts, state.kilnModel, state.executor)
const spec = buildSpecification(kiln, router)
expect(spec).toMatchSnapshot()
})
示例5: buildSpecification
export function buildSpecification(kiln: IKiln, router: IRouter, overrides?: Partial<Spec>): Spec {
const schemaCache = new SwaggerSchemaCache()
for (const kilnModel of kiln.getModels()) {
const arrayModel = defaultModelContext.array().children(kilnModel.model)
getSchema(kilnModel.model, schemaCache, startCase(kilnModel.name))
getSchema(arrayModel, schemaCache, `${startCase(kilnModel.meta.plural)}List`)
}
return {
swagger: '2.0',
basePath: '/',
produces: ['application/json'],
host: 'localhost',
schemes: ['http'],
info: {
title: 'Title',
version: 'Version',
},
paths: buildPaths(router, schemaCache),
definitions: schemaCache.getUniqueSchemas(),
...overrides,
}
}
示例6: createValidationMiddleware
export function createValidationMiddleware(
model: IModel,
pathInReq: ValidateIn = ValidateIn.Body,
options: IValidationMiddlewareOptions = {},
): IAnontatedHandler {
const arrayModel = options.allowedAsList && defaultModelContext.array().children(model)
return function(req: Request, res: Response, next: NextFunction): void {
const sourceData = req[pathInReq]
const validated = req.validated || {}
const result =
arrayModel && Array.isArray(sourceData)
? arrayModel.validate(sourceData)
: model.validate(sourceData)
validated[pathInReq] = result.value
req.validated = validated
if (result.conforms) {
next()
} else {
next(result.toError())
}
}
}
示例7: getAffectedCriteriaValues
byList: false,
idParamName: undefined,
},
getAffectedCriteriaValues(model: IKilnModel, options: IActionOptions): GetCriteriaValues {
return function(req: Request, property: string): AuthCriteriaValue[] {
const incomingItems: any[] = [].concat(req.validated!.body)
const existingItems: any[] = [].concat(req.actionTarget)
return incomingItems.concat(existingItems).map(item => item[property])
}
},
paramsModel(kilnModel: IKilnModel, options: IActionOptions): IModel | undefined {
return options.byId ? paramifyModel(kilnModel.model, options) : undefined
},
bodyModel(kilnModel: IKilnModel, options: IActionOptions): IModel {
const updateModel = updateifyModel(kilnModel.model)
const arrayUpdateModel = defaultModelContext
.array()
.children(updateModel)
.required()
.strict()
return options.byList ? arrayUpdateModel : updateModel
},
handler(
model: IKilnModel,
options: IActionOptions,
executor: IDatabaseExecutor,
): IAnontatedHandler {
if (options.byId && options.byList) {
throw new Error('Cannot update both byId and byList')
}
return function(req: Request, res: Response, next: NextFunction): void {
示例8: queryModel
}
}
return {permission, getAffectedCriteriaValues}
},
queryModel(model: IKilnModel, options: IActionOptions): undefined {
return undefined
},
paramsModel(model: IKilnModel, options: IActionOptions): undefined {
return undefined
},
bodyModel(model: IKilnModel, options: IActionOptions): undefined {
return undefined
},
responseModel(model: IKilnModel, options: IActionOptions): IModel {
const arrayModel = defaultModelContext
.array()
.children(model.model)
.required()
.strict()
return options.byList ? arrayModel : model.model
},
lookupActionTarget(
kilnModel: IKilnModel,
options: IActionOptions,
executor: IDatabaseExecutor,
): IAnontatedHandler | undefined {
const actionType = (this as any).type
if (!actionTypesWithTarget.has(actionType)) return undefined
const pkField = options.idParamName || getPrimaryKeyField(kilnModel.model)
const getPkFromItem = (item: any) => (typeof item === 'object' ? item[pkField] : item)
示例9: getKilnUserAuthMetadata
): (u: string, p: string) => Promise<any> {
const {userExecutor, usernameField, passwordField, passwordOptions} = getKilnUserAuthMetadata(
options,
)
return async (username: string, password: string) => {
const user = (await userExecutor.findOne({where: {[usernameField]: username}})) as any
if (!user) return undefined
const passwordsMatch = await doPasswordsMatch(password, user[passwordField], passwordOptions)
if (!passwordsMatch) return undefined
// TODO: limit returned user fields to just those used by auth grants
return user
}
}
// see https://tools.ietf.org/html/rfc6749#section-4.3.1
export const oauthTokenRequestModel: IModel = defaultModelContext.object().children({
// TODO: handle other grant types
grant_type: defaultModelContext
.string()
.required()
.enum(['password']),
username: defaultModelContext.string().required(),
password: defaultModelContext.string().required(),
})
export const oauthTokenResponseModel: IModel = defaultModelContext.object().children({
access_token: defaultModelContext.string().required(),
token_type: defaultModelContext
.string()
.required()
.enum(['bearer']),
示例10: forEach
forEach(original.spec.children as IModelChild[], child => {
const filterKeys: string[] = []
if (isAllowed(options.allowQueryByEquality, [child.path])) {
filterKeys.push('$eq', '$ne')
if (child.model.spec.type === 'string') {
filterKeys.push('$match')
}
}
if (isAllowed(options.allowQueryByRange, [child.path])) {
filterKeys.push('$lt', '$gt', '$gte', '$lte')
}
if (isAllowed(options.allowQueryByInclusion, [child.path])) {
filterKeys.push('$in', '$nin')
}
// TODO: allow nested querying
if (!filterKeys.length || !includes(ALLOWED_QUERY_TYPES, child.model.spec.type)) return
const modelForSwagger = defaultModelContext.create(pick(child.model.spec, ['type', 'format']))
const valueModel = defaultModelContext.create({
...pick(child.model.spec, ['type', 'format', 'max', 'min', 'enum']),
swagger: {
alternateModel: modelForSwagger,
alternateQueryModel: modelForSwagger,
},
})
const filterChildren = filterKeys.map(key => {
let model = valueModel
if (key === '$match') {
model = defaultModelContext.create({type: ModelType.String})
}
if (key === '$in' || key === '$nin') {
model = defaultModelContext
.create({
type: ModelType.Array,
children: valueModel,
swagger: {
inline: true,
alternateQueryModel: defaultModelContext.create({type: ModelType.String}),
},
})
.coerce(vr => (typeof vr.value === 'string' ? vr.setValue(vr.value.split(',')) : vr))
}
return {path: key, model}
})
const typeName = startCase(child.model.spec.type).replace(/ +/g, '')
// TODO: add format to name if it changes schema
const schemaName = `${typeName}Filters`
children[child.path] = defaultModelContext
.create({
type: ModelType.Object,
strict: true,
children: filterChildren,
swagger: {schemaName},
})
.coerce(parseQueryFilter)
})