Skip to content

Commit

Permalink
feat: add redisConfig method to infer types from config
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 18, 2022
1 parent b29be63 commit abc1ce3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
37 changes: 37 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* @adonisjs/redis
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { RedisConnectionConfig, RedisClusterConfig } from '@ioc:Adonis/Addons/Redis'

/**
* Expected shape of the config accepted by the "redisConfig"
* method
*/
type RedisConfig = {
connections: {
[name: string]: RedisConnectionConfig | RedisClusterConfig
}
}

/**
* Define config for redis
*/
export function redisConfig<T extends RedisConfig & { connection: keyof T['connections'] }>(
config: T
): T {
return config
}

/**
* Pull connections from the config defined inside the "config/redis.ts"
* file
*/
export type InferConnectionsFromConfig<T extends RedisConfig> = {
[K in keyof T['connections']]: T['connections'][K]
}
5 changes: 0 additions & 5 deletions instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,3 @@ REDIS_HOST: Env.schema.string({ format: 'host' }),
REDIS_PORT: Env.schema.number(),
REDIS_PASSWORD: Env.schema.string.optional(),
```

- We expect the `REDIS_CONNECTION` to be one of the whitelisted connections defined inside the `contracts/redis.ts` file.
- The `REDIS_HOST` should always be present and formatted as a valid `host`.
- The `REDIS_PORT` should always be present and a valid number.
- Finally the `REDIS_PASSWORD` is optional but when present should be a valid string.
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
"version": "7.2.0",
"description": "AdonisJS addon for Redis",
"main": "build/providers/RedisProvider.js",
"exports": {
".": {
"types": "./build/adonis-typings/index.d.ts",
"require": "./build/providers/RedisProvider.js"
},
"./config": "./build/config.js",
"./build/config": "./build/config.js",
"./*": "./*"
},
"files": [
"build/adonis-typings",
"build/providers",
"build/src",
"build/config.js",
"build/config.d.js",
"build/templates",
"build/instructions.md"
],
Expand Down
8 changes: 3 additions & 5 deletions templates/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import Env from '@ioc:Adonis/Core/Env'
import { RedisConfig } from '@ioc:Adonis/Addons/Redis'
import { redisConfig } from '@adonisjs/redis/build/config'

/*
|--------------------------------------------------------------------------
Expand All @@ -21,7 +21,7 @@ import { RedisConfig } from '@ioc:Adonis/Addons/Redis'
|
| Make sure to check `contracts/redis.ts` file for defining extra connections
*/
const redisConfig: RedisConfig = {
export default redisConfig({
connection: Env.get('REDIS_CONNECTION'),

connections: {
Expand All @@ -43,6 +43,4 @@ const redisConfig: RedisConfig = {
keyPrefix: '',
},
},
}

export default redisConfig
})
7 changes: 4 additions & 3 deletions templates/contract.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* file.
*/

import { InferConnectionsFromConfig } from '@adonisjs/redis/build/config'
import redisConfig from '../config/redis'

declare module '@ioc:Adonis/Addons/Redis' {
interface RedisConnectionsList {
local: RedisConnectionConfig,
}
interface RedisConnectionsList extends InferConnectionsFromConfig<typeof redisConfig> {}
}

0 comments on commit abc1ce3

Please sign in to comment.