Skip to content

Commit

Permalink
fix: improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
arashsheyda committed Mar 9, 2024
1 parent c7752ab commit eb99f01
Showing 1 changed file with 167 additions and 29 deletions.
196 changes: 167 additions & 29 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { addServerImportsDir, addServerPlugin, createResolver, defineNuxtModule, logger } from '@nuxt/kit'
import { addCustomTab } from '@nuxt/devtools-kit'
import defu from 'defu'
import type { Config } from 'neo4j-driver'

export interface ModuleOptions {
/**
* The URI of the Neo4j database.
* URI of the Neo4j database.
*
* @default process.env.NEO4J_URI
*
Expand All @@ -16,30 +17,15 @@ export interface ModuleOptions {
* @default process.env.NEO4J_USERNAME
*
*/
auth?: {
/**
* The type of authentication to be used for connecting to the Neo4j database.
*
* @default 'basic'
*
*/
type?: 'basic'
/**
* The USERNAME used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_USERNAME
*
*/
username?: string
/**
* The PASSWORD used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_PASSWORD
*
*/
password?: string
}
/**
auth?: AuthOptions
/**
* Configuration options for the Neo4j driver.
*
* @default {}
*
*/
config?: Config
/**
* Nuxt Devtools support for Neo4j Workspace.
*
* @default true
Expand All @@ -57,9 +43,11 @@ export default defineNuxtModule<ModuleOptions>({
uri: process.env.NEO4J_URI,
auth: {
type: 'basic',
username: process.env.NEO4J_USERNAME,
password: process.env.NEO4J_PASSWORD,
username: process.env.NEO4J_USERNAME as string,
password: process.env.NEO4J_PASSWORD as string,
realm: process.env.NEO4J_REALM as string,
},
config: {},
devtools: true,
},
hooks: {},
Expand All @@ -69,13 +57,43 @@ export default defineNuxtModule<ModuleOptions>({

config.neo4j = defu(config.neo4j || {}, {
...options,
auth: {
...options.auth,

// kerberos
ticket: process.env.NEO4J_TICKET,

// bearer
token: process.env.NEO4J_TOKEN,

// custom
principal: process.env.NEO4J_PRINCIPAL,
credentials: process.env.NEO4J_CREDENTIALS,
realm: process.env.NEO4J_REALM,
scheme: process.env.NEO4J_SCHEME,
parameters: process.env.NEO4J_PARAMETERS,
},
})

if (!config.neo4j.uri)
throw new Error('No URI provided for Neo4j')

if (!config.neo4j.auth.username || !config.neo4j.auth.password)
throw new Error('No credentials provided for Neo4j')
if (config.neo4j.auth.type === 'basic') {
if (!config.neo4j.auth.username || !config.neo4j.auth.password)
throw new Error('No credentials provided for Neo4j')
}
else if (config.neo4j.auth.type === 'kerberos') {
if (!config.neo4j.auth.ticket)
throw new Error('No ticket provided for Neo4j')
}
else if (config.neo4j.auth.type === 'bearer') {
if (!config.neo4j.auth.token)
throw new Error('No token provided for Neo4j')
}
else if (config.neo4j.auth.type === 'custom') {
if (!config.neo4j.auth.principal || !config.neo4j.auth.credentials || !config.neo4j.auth.realm || !config.neo4j.auth.scheme)
throw new Error('No custom credentials provided for Neo4j')
}

nuxt.options.build.transpile = [resolve('./runtime/server')]

Expand All @@ -98,3 +116,123 @@ export default defineNuxtModule<ModuleOptions>({
logger.info('Neo4j module has been initialized!')
},
})

type AuthOptions =
| {
/**
* Type of authentication to be used for connecting to the Neo4j database.
*
* @default 'basic'
*
*/
type: 'basic'
} & BasicAuth
| {
/**
* Type of authentication to be used for connecting to the Neo4j database.
*
* @default 'kerberos'
*
*/
type: 'kerberos'
} & KerberosAuth
| {
/**
* Type of authentication to be used for connecting to the Neo4j database.
*
* @default 'bearer'
*
*/
type: 'bearer'
} & BearerAuth
| {
/**
* Type of authentication to be used for connecting to the Neo4j database.
*
* @default 'custom'
*
*/
type: 'custom'
} & CustomAuth

interface BasicAuth {
/**
* Username is used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_USERNAME
*
*/
username: string
/**
* Password is used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_PASSWORD
*
*/
password: string
/**
* Realm is used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_REALM
*
*/
realm?: string
}

interface KerberosAuth {
/**
* Ticket is used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_TICKET
*
*/
ticket: string
}

interface BearerAuth {
/**
* Token is used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_TOKEN
*
*/
token: string
}

interface CustomAuth {
/**
* Principle is used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_PRINCIPLE
*
*/
principle: string
/**
* Credentials is used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_CREDENTIALS
*
*/
credentials: string
/**
* Realm is used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_REALM
*
*/
realm: string
/**
* Scheme is used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_SCHEME
*
*/
scheme: string
/**
* Parameters is used to authenticate with the Neo4j database.
*
* @default process.env.NEO4J_PARAMETERS
*
*/
parameters?: string
}

0 comments on commit eb99f01

Please sign in to comment.