Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@mojule/api-factory
Advanced tools
Caution - work in progress - at this stage this doc is more draft/outline than anything
It's a simple way to build APIs that enables a lot of cool things to be done with little effort, like:
Put a feature list here, including but not limited to:
Code style focus is on paring js down to its most powerful features and keeping
things very simple - eliminate weird this
behaviour, rely on functional
concepts, simple objects (preferably JSON-serializable), object composition
instead of inheritance etc. - but still be able to build complex things out of
simple parts. Class-free OO. Started with Crockford's JS Good Parts but also see
his talks on the better parts, rationale behind ES6 and etc; Crockford, Eric
Elliot, Mattias Petter Johansson et al
this
etc - fragileNever use (unless interop with 3rd party or optimizing hot code):
a == b
const i = 1; if( i ){ /* ... */ }
null
this
Call ApiFactory with your plugins, get back an factory for your specific API type that takes arguments that define the underlying state and get back an API instance
eg:
const ApiFactory = require( '@mojule/api-factory' )
const plugins = require( './path/to/your/plugins' )
// create a Point factory by passing ApiFactory plugins for managing point state
const Point = ApiFactory( plugins )
// create Point API instances
const p1 = Point( 3, 4 )
const p2 = Point( 4, 5 )
// call a plugin
const p3 = p1.add( p2 )
An instance of your API - all your public plugins operating over your state
Plugins can be of four types, static
, core
, api
, private
:
Particularly useful for functions that create new instances, eg a function that takes a serialized JSON version of the state and returns a new api instance
A static plugin closure looks like:
({ statics, Api }) => {
statics.deserialize = jsonObj => { /*...*/ }
}
Things that are core to generating APIs, not specific to your API use case or
the type of state you're working with - if a function would be useful regardless
of the resultant API or underlying state, it belongs in core
Kinda like private statics if you're used to class oriented programming
A plugin closure looks like:
({ core, Api }) => {
// attach your core plugins
core.someCoreFunction = ( ...args ) => { //... }
}
Usually you would override some or all of these for your use case
({ core }) => {
core.createState = ( ...args ) => args[ 0 ]
core.getStateKey = state => state
core.isState = state => true
core.onCreate = api => {}
}
Plugins that can be called internally but aren't exposed to the end consumer of the API
({ privates, state, core, statics, Api }) => {
privates.someFn = ( ...args ) => { /*...*/ }
}
There are no default plugins, as they are dependant on the type of state etc
The plugins that end up being exposed to the end consumer of your API code
({ api, state, core, privates, statics, Api }) => {
api.someFn = ( ...args ) => { /*...*/ }
}
You can use api factory in a redux like style:
const { is } = require( '@mojule/is' )
const isTodo = target =>
is.object( target ) && is.string( target.text ) &&
is.boolean( target.completed )
const isTodoList = target =>
is.array( target ) && target.every( isTodo )
const isTodoState = target =>
is.object( target ) && isTodoList( target.todos ) &&
is.string( target.visibilityFilter )
const visibilityFilter = ( state = 'SHOW_ALL', action = {} ) => {
if( action.type === 'visibility' )
return action.filter
return state
}
const todos = ( state = [], action = {} ) => {
if( action.type === 'add' )
return state.concat([{ text: action.text, completed: false }])
if( action.type === 'toggle' )
return state.map( ( todo, index ) =>
action.index === index ?
{ text: todo.text, completed: !todo.completed } : todo
)
return state
}
const todoApp = ( state = {}, action = {} ) => ({
todos: todos( state.todos, action ),
visibilityFilter: visibilityFilter( state.visibilityFilter, action )
})
const corePlugins =
({ core }) => {
core.createState = ( ...args ) => {
if( isTodoState( args[ 0 ] ) )
return args[ 0 ]
return {
todos: args.map( text => ({
text, completed: false
})),
visibilityFilter: 'SHOW_ALL'
}
}
core.isState = isTodoState
}
const privatePlugins =
({ privates, state, Api }) => {
privates.createAction = ( type, argsMapper ) =>
( ...args ) => Api(
todoApp(
state,
Object.assign(
{ type },
argsMapper( ...args )
)
)
)
}
const publicPlugins =
({ api, state, privates }) => {
const { createAction } = privates
api.add = createAction( 'add', text => ({ text }) )
api.toggle = createAction( 'toggle', index => ({ index }) )
api.visibility = createAction( 'visibility', filter => ({ filter }) )
api.log = () => {
let todos = state.todos
if( state.visibilityFilter === 'SHOW_COMPLETED' ){
console.log( 'Completed tasks' )
todos = todos.filter( t => t.completed )
} else if( state.visibilityFilter === 'SHOW_UNCOMPLETED' ){
console.log( 'Incomplete tasks' )
todos = todos.filter( t => !t.completed )
} else {
console.log( 'All tasks' )
}
console.log( '---' )
todos.forEach( t =>
console.log( t.text, t.completed ? '(completed)' : '(incomplete)' )
)
console.log()
}
}
const Todos = ApiFactory({
core: corePlugins,
privates: privatePlugins,
api: publicPlugins
})
const initial = Todos( 'Eat food', 'Exercise' )
initial.log()
const added = initial.add( 'Foo the bar' )
added.log()
const toggled = added.toggle( 0 )
toggled.log()
const completed = toggled.visibility( 'SHOW_COMPLETED' )
completed.log()
const uncompleted = completed.visibility( 'SHOW_UNCOMPLETED' )
uncompleted.log()
FAQs
Generate APIs over a state
We found that @mojule/api-factory demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.