Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const defineComponent = async (component, state, context) => {
// TODO BRN: If we ever need to retrigger define (redefine) hydrating state here may be an issue
if (!isComponent(component)) {
throw new TypeError(
`defineComponent expected component parameter to be a component. Instead received ${component}`
)
}
component = hydrateComponent(component, state, context)
if (isFunction(component.define)) {
let children = await or(component.define(context), {})
children = filter(isComponent, map(resolve, children))
if (isObject(children)) {
forEach((child) => {
// TODO BRN: Look for children that already have parents. If this is the case then someone has returned a child from define that was defined by another component (possibly passed along as a variable)
child.parent = component
// child = setKey(appendKey(getKey(component), kdx), child)
}, children)
} else {
throw new Error(
`define() method must return either an object or an array. Instead received ${children} from ${component}.`
)
}
component.children = await map(
async (child, key) => defineComponent(child, get(['children', key], state), context),
children
[normalizedMethod, 'security'],
[{ [securityDefinition.name]: [] }],
updatedMethods
)
securityDefinitions[securityDefinition.name] = securityDefinition.definition
}
}, or(methods, {}))
if (enableCorsOnPath) {
const corsOptionsMethod = getCorsOptionsConfig()
updatedMethods = set('options', corsOptionsMethod, updatedMethods)
}
// set the paths
paths = set([normalizedPath], updatedMethods, paths)
}, or(routes, {}))
const definition = {
swagger: '2.0',
info: {
title: name,
version: new Date().toISOString()
},
schemes: ['https'],
consumes: ['application/json'],
produces: ['application/json'],
securityDefinitions,
paths
}
return definition
}
construct(inputs, context) {
super.construct(inputs, context)
// construct function instances
this.functions = map(
(func, alias) =>
context.construct(Fn, {
...func,
functionName: resolvable(() => or(func.functionName, alias)),
code: resolvable(() => resolveCodePath(resolve(func.code), this.getType().root))
}),
or(this.functions, {})
)
// construct component instances
this.components = map((component, key) => {
if (isComponent(component)) {
return component
} else if (isTypeConstruct(component)) {
// eslint-disable-next-line no-shadow
const { type, inputs } = component
const Type = context.require(type)
component = context.construct(Type, inputs)
if (!isComponent(component)) {
throw new Error(
`The component "${key}" is not of type Component (it's of type ${component.name})`
)
}
return component
async define() {
// TODO BRN: Change this once we support multiple layers here. This could cause collisions between functions and components that are named the same thing.
return {
...or(this.functions, {}),
...or(this.components, {})
}
}
undefined,
normalizedPath,
normalizedMethod
)
const defaultResponses = getDefaultResponses(isCorsEnabled)
updatedMethods = set([normalizedMethod], apiGatewayIntegration, updatedMethods)
updatedMethods = set([normalizedMethod, 'responses'], defaultResponses, updatedMethods)
if (securityDefinition) {
updatedMethods = set(
[normalizedMethod, 'security'],
[{ [securityDefinition.name]: [] }],
updatedMethods
)
securityDefinitions[securityDefinition.name] = securityDefinition.definition
}
}, or(methods, {}))
if (enableCorsOnPath) {
const corsOptionsMethod = getCorsOptionsConfig()
updatedMethods = set('options', corsOptionsMethod, updatedMethods)
}
// set the paths
paths = set([normalizedPath], updatedMethods, paths)
}, or(routes, {}))
async info() {
const services = await reduce(
async (accum, service) => append(await service.info(), accum),
[],
or(this.services, {})
)
const components = await reduce(
async (accum, component) => append(await component.info(), accum),
[],
or(this.components, {})
)
return {
title: this.name,
type: this.name,
data: {},
children: [...components, ...services]
}
}
}
async info() {
const functions = await reduce(
async (accum, func) => append(await func.info(), accum),
[],
or(this.functions, {})
)
const components = await reduce(
async (accum, component) => append(await component.info(), accum),
[],
or(this.components, {})
)
return {
title: this.name,
type: 'Service',
data: {},
children: [...functions, ...components]
}
}
}
async info() {
const services = await reduce(
async (accum, service) => append(await service.info(), accum),
[],
or(this.services, {})
)
const components = await reduce(
async (accum, component) => append(await component.info(), accum),
[],
or(this.components, {})
)
return {
title: this.name,
type: this.name,
data: {},
children: [...components, ...services]
}
}
}
(accum, Type) => union(accum, keys(or(Type.props, {}))),
['inputs'],
this.provider = resolvable(() => or(inputs.provider, context.get('provider')))
}