import path from 'path' import http from 'http' import cookieSession from 'cookie-session' import connect from 'connect' import cors from 'cors' import { QRequest } from './types/server.d' import connectToDB from './server/modules/db' import { useRouter } from './server/api/modules/useRouter' import createDbFirstData from './server/modules/createDbFirstData' export { Slug } from './server/functions/slug' export * from './server/modules/RequestController' const storeActions = require('./common/store.js') const { default: config } = require(`${process.cwd()}/config.js`) const DB: any = { db: null, } function useDB() { return function ( req: QRequest, __: http.ServerResponse, next: connect.NextFunction ) { req.DB = DB next() } } export default function kolbinsCRM(this: any) { try { this.addServerMiddleware( cookieSession({ name: config.sessionName || 'kS8', keys: ['kolbins-secret'], maxAge: 24 * 60 * 60 * 1000, httpOnly: true, }) ) this.addModule('@nuxtjs/device', true) this.addModule('@nuxtjs/emotion', true) // const corsOptions = { // origin(origin: string, callback: any) { // if (['http://localhost:8080'].includes(origin)) { // callback(null, true) // } else { // callback(new Error('Not allowed by CORS')) // } // }, // } // this.addServerMiddleware(cors(corsOptions as any)) this.addServerMiddleware(useRouter) this.addServerMiddleware(useDB()) this.addServerMiddleware({ path: '/api', handler: path.resolve(__dirname, 'server/app.ts'), }) this.addPlugin({ src: path.resolve(__dirname, 'client/plugins/api.server.ts'), options: { path: __dirname, }, }) this.addPlugin({ src: path.resolve(__dirname, 'client/kolbins.js'), options: { path: __dirname, }, }) this.addPlugin({ src: path.resolve(__dirname, 'client/plugins/api.client.ts'), }) } catch (error) { throw new Error(String(error)) } this.nuxt.hook('listen', async () => { DB.db = await connectToDB() createDbFirstData(DB.db) }) } export const defaultActions = storeActions export const autoComponents = [ '~/components', { path: '~/node_modules/kolbins-crm/client/admin-ui/page-builder/default-sections/', prefix: 'q-default', }, ]