UNPKG

1.01 kBJavaScriptView Raw
1import createLoaders from './loaders'
2import { loadExtension } from './extensions'
3
4const debug = require('debug')('graphbrainz:context')
5
6export function extendContext(extension, context, options) {
7 if (extension.extendContext) {
8 if (typeof extension.extendContext === 'function') {
9 debug(
10 `Extending context via a function from the “${
11 extension.name
12 }” extension.`
13 )
14 context = extension.extendContext(context, options)
15 } else {
16 throw new Error(
17 `Extension “${extension.name}” contains an invalid \`extendContext\` ` +
18 `value: ${extension.extendContext}`
19 )
20 }
21 }
22 return context
23}
24
25export function createContext(options = {}) {
26 const { client } = options
27 const loaders = createLoaders(client)
28 const context = { client, loaders }
29 const { extensions = [] } = options
30 return extensions.reduce((context, extension) => {
31 return extendContext(loadExtension(extension), context, options)
32 }, context)
33}