1 | import createLoaders from './loaders'
|
2 | import { loadExtension } from './extensions'
|
3 |
|
4 | const debug = require('debug')('graphbrainz:context')
|
5 |
|
6 | export 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 |
|
25 | export 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 | }
|