Skip to content

Commit

Permalink
docs(changeset): feat: add global intelliSense helper interface, upda…
Browse files Browse the repository at this point in the history
…te all packages and add cache module
  • Loading branch information
nailiable committed Nov 6, 2024
1 parent 60ea5b8 commit 6da9241
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 5 deletions.
9 changes: 9 additions & 0 deletions .changeset/dry-cooks-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@nailyjs/typeorm": patch
"@nailyjs/config": patch
"@nailyjs/eslint": patch
"@nailyjs/cache": patch
"@nailyjs/ioc": patch
---

feat: add global intelliSense helper interface, update all packages and add cache module
4 changes: 2 additions & 2 deletions naily.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { defineConfig } from './packages/config/src/helper'

export default defineConfig({
naily: {
cli: {},

eslint: {
type: 'lib',
rules: {
Expand All @@ -18,6 +16,8 @@ export default defineConfig({
},
},

cacheManager: {},

typeorm: {
type: 'sqlite',
database: path.join(cwd(), './node_modules/.cache/naily/typeorm.db'),
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"build:rpc": "pnpm -F @nailyjs/rpc build",
"build:eslint": "pnpm -F @nailyjs/eslint build",
"build:unplugin-rpc": "pnpm -F unplugin-rpc build",
"build:cache": "pnpm -F @nailyjs/cache build",
"play:rpc": "pnpm -F rpc dev",

"lint": "eslint .",
Expand Down
33 changes: 33 additions & 0 deletions packages/cache/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@nailyjs/cache",
"type": "module",
"version": "2.0.1",
"description": "Cache manager package for naily.js",
"author": "Naily Zero <[email protected]> (https://naily.cc)",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsup",
"watch": "tsup -w",
"prepublishOnly": "tsup"
},
"dependencies": {
"@nailyjs/config": "workspace:*",
"@nailyjs/ioc": "workspace:*",
"cache-manager": "^6.1.2"
},
"devDependencies": {
"tsup": "^8.3.0"
}
}
40 changes: 40 additions & 0 deletions packages/cache/src/cache.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Value } from '@nailyjs/config'
import { Autowired, ClassWrapper, Container, Optional, Service } from '@nailyjs/ioc'
import { createCache } from 'cache-manager'
import { CustomCacheManager } from './configure-cache-protocol'

export const CACHE_MANAGER = '__naily_cache_manager__'

@Service()
export class CacheFactoryService {
constructor(
@Value('naily.cacheManager')
private cacheOptions: Naily.Configuration.NailyUserConfig['cacheManager'],
@Optional()
@Autowired(CustomCacheManager)
private readonly configureService: CustomCacheManager,
) {}

async setup(container: Container): Promise<void> {
let cacheOptions = this.cacheOptions
if (this.configureService && typeof this.configureService.configure === 'function')
cacheOptions = await this.configureService.configure(cacheOptions)

if (Array.isArray(cacheOptions)) {
for (const cacheOption of cacheOptions) {
const cacheInstance = createCache(cacheOption)
container.createConstantWrapper(cacheOption.injectionToken, cacheInstance).save()
}
}
else {
const cacheInstance = createCache()
container.createConstantWrapper(CACHE_MANAGER, cacheInstance).save()
}
}

static getInstance(container: Container): CacheFactoryService {
const wrapper = container.getContainer().get(CacheFactoryService) as ClassWrapper<CacheFactoryService>
if (wrapper && wrapper.wrapperType === 'class') return wrapper.getClassFactory().getOrCreateInstance()
return container.createClassWrapper(CacheFactoryService).getClassFactory().getOrCreateInstance()
}
}
4 changes: 4 additions & 0 deletions packages/cache/src/configure-cache-protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const CustomCacheManager = '__naily_custom_cache_manager__'
export interface CustomCacheManager {
configure(cacheManagerOptions: Naily.Configuration.NailyUserConfig['cache']): Naily.Configuration.NailyUserConfig['cache'] | Promise<Naily.Configuration.NailyUserConfig['cache']>
}
4 changes: 4 additions & 0 deletions packages/cache/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './cache.service'
export * from './configure-cache-protocol'
export * from './plugin'
export * from './types'
14 changes: 14 additions & 0 deletions packages/cache/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Container, IocPlugin } from '@nailyjs/ioc'
import { CacheFactoryService } from './cache.service'

class CachePluginImpl implements IocPlugin {
name: string = 'naily:cache-manager-plugin'

beforeRun(container: Container): void {
CacheFactoryService.getInstance(container).setup(container)
}
}

export function CachePlugin(): IocPlugin {
return new CachePluginImpl()
}
24 changes: 24 additions & 0 deletions packages/cache/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { InjectionTokenable } from '@nailyjs/ioc'
import { CreateCacheOptions } from 'cache-manager'

declare global {
namespace Naily {
namespace Configuration {
interface NailyUserConfig {
/**
* Cache manager options.
*
* @see https://github.com/jaredwray/cacheable
* @see https://www.npmjs.com/package/cache-manager
*/
cacheManager?: CacheManagerOptions | InjectionTokenable<CacheManagerOptions>[]
}
interface NailyUserIntelliSense {
cacheManager?: CacheManagerOptions
}
}
}
}

export interface CacheManagerOptions extends CreateCacheOptions {
}
10 changes: 10 additions & 0 deletions packages/cache/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "ES2022",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "ES2022",
"moduleResolution": "Bundler"
},
"include": ["src"]
}
11 changes: 11 additions & 0 deletions packages/cache/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entry: {
index: './src/index.ts',
},
dts: true,
sourcemap: true,
clean: true,
format: ['cjs', 'esm'],
})
2 changes: 1 addition & 1 deletion packages/config/src/decorators/value.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ValueMetadata {
parameterIndex?: number
}

export function Value<ConfigObject extends Record<string, any> = Naily.Configuration.UserConfig>(jexl: Path<FullObject<ConfigObject>> | (string & {})): PropertyDecorator & ParameterDecorator
export function Value<ConfigObject extends Record<string, any> = Naily.Configuration.UserIntelliSense>(jexl: Path<FullObject<ConfigObject>> | (string & {})): PropertyDecorator & ParameterDecorator
export function Value<Key extends string>(jexl: Key): PropertyDecorator & ParameterDecorator
export function Value(jexl: string = ''): PropertyDecorator & ParameterDecorator {
return ((target: Object, propertyKey: string | symbol, parameterIndex?: number) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/config/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ declare global {
naily?: NailyUserConfig
[key: string]: any
}

export interface NailyUserIntelliSense {
[key: string]: any
}
export interface UserIntelliSense {
naily?: NailyUserIntelliSense
[key: string]: any
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/eslint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ export async function nailyProxy(): Promise<ReturnType<typeof antfu>> {
}

export default naily

export * from './eslint-bootstrap'
export * from './types'
6 changes: 6 additions & 0 deletions packages/eslint/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ declare global {
* ```
* It will proxy the config from `naily.config.ts` to this `naily.eslint` object.
*/
eslint?: Parameters<typeof antfu>[0] & {
extraOptions?: Array<Parameters<typeof antfu>[1]>
}
}

interface NailyUserIntelliSense {
eslint?: Omit<Parameters<typeof antfu>[0], 'overrides'> & {
extraOptions?: Array<Parameters<typeof antfu>[1]>
}
Expand Down
3 changes: 3 additions & 0 deletions packages/ioc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ export interface IPostConstructCatchContext extends ErrorHandlerContext {
export interface PostConstructErrorHandler extends ErrorHandler {
catch(error: any, context: IPostConstructCatchContext): any
}
export type InjectionTokenable<T extends Record<string | symbol | number, any>> = T & {
injectionToken: string
}
7 changes: 5 additions & 2 deletions packages/typeorm/src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { DataSource, type DataSourceOptions } from 'typeorm'

declare global {
namespace Naily {
export namespace Configuration {
export interface NailyUserConfig {
namespace Configuration {
interface NailyUserConfig {
/** TypeORM configuration */
typeorm?: DataSourceOptions
}
interface NailyUserIntelliSense {
typeorm?: DataSourceOptions
}
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6da9241

Please sign in to comment.