Skip to content

Commit

Permalink
refactor: move to config providers and remove drivers collection
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 19, 2023
1 parent c9b5a5c commit c1e0bc1
Show file tree
Hide file tree
Showing 23 changed files with 354 additions and 580 deletions.
8 changes: 3 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
* file that was distributed with this source code.
*/

import './src/bindings/types.js'

export { HttpClient as ApiRequest } from '@poppinss/oauth-client'

export * as errors from './src/errors.js'
export { configure } from './configure.js'
export { stubsRoot } from './stubs/main.js'
export { AllyManager } from './src/ally_manager.js'
export { defineConfig } from './src/define_config.js'

export { RedirectRequest } from './src/redirect_request.js'
export { Oauth1Driver } from './src/abstract_drivers/oauth1.js'
export { Oauth2Driver } from './src/abstract_drivers/oauth2.js'
export { default as driversList } from './src/drivers_collection.js'
export { stubsRoot } from './stubs/main.js'
export { configure } from './configure.js'
38 changes: 33 additions & 5 deletions providers/ally_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@
* file that was distributed with this source code.
*/

import { configProvider } from '@adonisjs/core'
import { HttpContext } from '@adonisjs/core/http'
import { RuntimeException } from '@poppinss/utils'
import type { ApplicationService } from '@adonisjs/core/types'

import driversList from '../src/drivers_collection.js'
import { extendHttpContext } from '../src/bindings/http_context.js'
import type { AllyService } from '../src/types.js'
import { AllyManager } from '../src/ally_manager.js'

declare module '@adonisjs/core/http' {
export interface HttpContext {
ally: AllyService
}
}

/**
* AllyProvider extends the HTTP context with the "ally" property
Expand All @@ -19,8 +28,27 @@ export default class AllyProvider {
constructor(protected app: ApplicationService) {}

async boot() {
const config = this.app.config.get<any>('ally')
extendHttpContext(config.services)
await driversList.registerBundledDrivers(config.driversInUse)
const allyConfigProvider = this.app.config.get<any>('ally')

/**
* Resolve config from the provider
*/
const config = await configProvider.resolve<any>(this.app, allyConfigProvider)
if (!config) {
throw new RuntimeException(
'Invalid "config/ally.ts" file. Make sure you are using the "defineConfig" method'
)
}

/**
* Setup HTTPContext getter
*/
HttpContext.getter(
'ally',
function (this: HttpContext) {
return new AllyManager(config, this) as unknown as AllyService
},
true
)
}
}
2 changes: 1 addition & 1 deletion src/abstract_drivers/oauth1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/

import { Exception } from '@poppinss/utils'
import { Oauth1Client } from '@poppinss/oauth-client/oauth1'
import type { HttpContext } from '@adonisjs/core/http'
import { Oauth1Client } from '@poppinss/oauth-client/oauth1'

import {
AllyUserContract,
Expand Down
2 changes: 1 addition & 1 deletion src/abstract_drivers/oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/

import { Exception } from '@poppinss/utils'
import { Oauth2Client } from '@poppinss/oauth-client/oauth2'
import type { HttpContext } from '@adonisjs/core/http'
import { Oauth2Client } from '@poppinss/oauth-client/oauth2'

import {
AllyUserContract,
Expand Down
27 changes: 0 additions & 27 deletions src/bindings/http_context.ts

This file was deleted.

25 changes: 0 additions & 25 deletions src/bindings/types.ts

This file was deleted.

69 changes: 0 additions & 69 deletions src/defaults/config.ts

This file was deleted.

145 changes: 103 additions & 42 deletions src/define_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,121 @@
* file that was distributed with this source code.
*/

import { configProvider } from '@adonisjs/core'
import type { HttpContext } from '@adonisjs/core/http'
import type { ConfigProvider } from '@adonisjs/core/types'

import type { AllyDriversList } from './types.js'
import allyDriversCollection from './drivers_collection.js'
import type { GoogleDriver } from './drivers/google.js'
import type { GithubDriver } from './drivers/github.js'
import type { SpotifyDriver } from './drivers/spotify.js'
import type { TwitterDriver } from './drivers/twitter.js'
import type { DiscordDriver } from './drivers/discord.js'
import type { FacebookDriver } from './drivers/facebook.js'
import type { LinkedInDriver } from './drivers/linked_in.js'
import type {
GoogleDriverConfig,
GithubDriverConfig,
SpotifyDriverConfig,
DiscordDriverConfig,
TwitterDriverConfig,
LinkedInDriverConfig,
FacebookDriverConfig,
AllyManagerDriverFactory,
} from '@adonisjs/ally/types'

/**
* Shape of config after it has been resolved from
* the config provider
*/
type ResolvedConfig<
KnownSocialProviders extends Record<
string,
AllyManagerDriverFactory | ConfigProvider<AllyManagerDriverFactory>
>,
> = {
[K in keyof KnownSocialProviders]: KnownSocialProviders[K] extends ConfigProvider<infer A>
? A
: KnownSocialProviders[K]
}

/**
* Define config for the ally
*/
export function defineConfig<
KnownSocialProviders extends Record<
string,
{
[K in keyof AllyDriversList]: { driver: K } & Parameters<AllyDriversList[K]>[0]
}[keyof AllyDriversList]
AllyManagerDriverFactory | ConfigProvider<AllyManagerDriverFactory>
>,
>(
config: KnownSocialProviders
): {
services: {
[K in keyof KnownSocialProviders]: (
ctx: HttpContext
) => ReturnType<AllyDriversList[KnownSocialProviders[K]['driver']]>
}
driversInUse: Set<keyof AllyDriversList>
} {
/**
* Converting user defined config to an object of services
* that can be injected into the AllyManager class
*/
const driversInUse: Set<keyof AllyDriversList> = new Set()

const services = Object.keys(config).reduce(
(result, provider: keyof KnownSocialProviders) => {
const providerConfig = config[provider]
driversInUse.add(providerConfig.driver)
>(config: KnownSocialProviders): ConfigProvider<ResolvedConfig<KnownSocialProviders>> {
return configProvider.create(async (app) => {
const serviceNames = Object.keys(config)
const services = {} as Record<string, AllyManagerDriverFactory>

result[provider] = (ctx: HttpContext) => {
return allyDriversCollection.create<KnownSocialProviders[typeof provider]['driver']>(
providerConfig.driver,
providerConfig,
ctx
)
for (let serviceName of serviceNames) {
const service = config[serviceName]
if (typeof service === 'function') {
services[serviceName] = service
} else {
services[serviceName] = await service.resolver(app)
}
return result
},
{} as {
[K in keyof KnownSocialProviders]: (
ctx: HttpContext
) => ReturnType<AllyDriversList[KnownSocialProviders[K]['driver']]>
}
)

return {
services,
driversInUse,
}
return services as ResolvedConfig<KnownSocialProviders>
})
}

/**
* Helpers to configure social auth services
*/
export const services: {
discord: (config: DiscordDriverConfig) => ConfigProvider<(ctx: HttpContext) => DiscordDriver>
facebook: (config: FacebookDriverConfig) => ConfigProvider<(ctx: HttpContext) => FacebookDriver>
github: (config: GithubDriverConfig) => ConfigProvider<(ctx: HttpContext) => GithubDriver>
google: (config: GoogleDriverConfig) => ConfigProvider<(ctx: HttpContext) => GoogleDriver>
linkedin: (config: LinkedInDriverConfig) => ConfigProvider<(ctx: HttpContext) => LinkedInDriver>
spotify: (config: SpotifyDriverConfig) => ConfigProvider<(ctx: HttpContext) => SpotifyDriver>
twitter: (config: TwitterDriverConfig) => ConfigProvider<(ctx: HttpContext) => TwitterDriver>
} = {
discord(config) {
return configProvider.create(async () => {
const { DiscordDriver } = await import('./drivers/discord.js')
return (ctx) => new DiscordDriver(ctx, config)
})
},
facebook(config) {
return configProvider.create(async () => {
const { FacebookDriver } = await import('./drivers/facebook.js')
return (ctx) => new FacebookDriver(ctx, config)
})
},
github(config) {
return configProvider.create(async () => {
const { GithubDriver } = await import('./drivers/github.js')
return (ctx) => new GithubDriver(ctx, config)
})
},
google(config) {
return configProvider.create(async () => {
const { GoogleDriver } = await import('./drivers/google.js')
return (ctx) => new GoogleDriver(ctx, config)
})
},
linkedin(config) {
return configProvider.create(async () => {
const { LinkedInDriver } = await import('./drivers/linked_in.js')
return (ctx) => new LinkedInDriver(ctx, config)
})
},
spotify(config) {
return configProvider.create(async () => {
const { SpotifyDriver } = await import('./drivers/spotify.js')
return (ctx) => new SpotifyDriver(ctx, config)
})
},
twitter(config) {
return configProvider.create(async () => {
const { TwitterDriver } = await import('./drivers/twitter.js')
return (ctx) => new TwitterDriver(ctx, config)
})
},
}
Loading

0 comments on commit c1e0bc1

Please sign in to comment.