Skip to content

Commit 1d6e1f7

Browse files
authored
fix: support function expressions in module config (#537)
1 parent 8708b53 commit 1d6e1f7

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/module.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useLogger, addPlugin, addImports, addTemplate, createResolver, defineNu
66
import GraphQLPlugin from '@rollup/plugin-graphql'
77
import { name, version } from '../package.json'
88
import type { ClientConfig, NuxtApolloConfig, ErrorResponse } from './types'
9+
import { serializeConfig } from './serialize'
910

1011
export type { ClientConfig, ErrorResponse }
1112

@@ -107,8 +108,8 @@ export default defineNuxtModule<NuxtApolloConfig<any>>({
107108
'export default {',
108109
` proxyCookies: ${options.proxyCookies},`,
109110
` clientAwareness: ${options.clientAwareness},`,
110-
` cookieAttributes: ${JSON.stringify(options.cookieAttributes)},`,
111-
` clients: ${JSON.stringify(clients)}`,
111+
` cookieAttributes: ${serializeConfig(options.cookieAttributes)},`,
112+
` clients: ${serializeConfig(clients)}`,
112113
'}'
113114
].join('\n')
114115
})

src/serialize.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Serialize config to be used in templates
3+
* @param obj Config object
4+
* @returns Stringified config with kept function expressions
5+
*/
6+
export const serializeConfig = (obj: any) => {
7+
// Stringify function body
8+
if (typeof obj === 'function') {
9+
return obj.toString()
10+
}
11+
12+
// Run recursively on objects and arrays
13+
if (typeof obj === 'object') {
14+
if (Array.isArray(obj)) {
15+
return `[${obj.map(serializeConfig).join(', ')}]`
16+
} else {
17+
return `{${Object.entries(obj).map(([key, value]) => `${serializeConfig(key)}: ${serializeConfig(value)}`).join(', ')}}`
18+
}
19+
}
20+
21+
return JSON.stringify(obj)
22+
}

0 commit comments

Comments
 (0)