Skip to content

Commit

Permalink
feat(schema): add built-in plugin @cordisjs/schema
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 3, 2024
1 parent d377564 commit ab63f5f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/cordis/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as core from '@cordisjs/core'
import { Logger, LoggerService } from '@cordisjs/logger'
import { SchemaService } from '@cordisjs/schema'
import { TimerService } from '@cordisjs/timer'

export * from '@cordisjs/core'
export { default as Schema, default as z } from 'schemastery'
export { Schema, z } from '@cordisjs/schema'
export { Logger } from '@cordisjs/logger'
export { TimerService } from '@cordisjs/timer'

Expand All @@ -21,9 +22,11 @@ export class Context extends core.Context {
this.baseDir = globalThis.process?.cwd() || ''

this.provide('logger', undefined, true)
this.provide('schema', undefined, true)
this.provide('timer', undefined, true)

this.plugin(LoggerService)
this.plugin(SchemaService)

Check failure on line 29 in packages/cordis/src/index.ts

View workflow job for this annotation

GitHub Actions / build

No overload matches this call.
this.plugin(TimerService)
}
}
Expand Down
49 changes: 49 additions & 0 deletions packages/schema/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@cordisjs/schema",
"description": "Schema service for cordis",
"version": "0.1.0",
"type": "module",
"main": "lib/index.cjs",
"module": "lib/index.mjs",
"types": "lib/index.d.ts",
"exports": {
".": {
"require": "./lib/index.cjs",
"import": "./lib/index.mjs",
"types": "./lib/index.d.ts"
},
"./package.json": "./package.json"
},
"files": [
"lib",
"src"
],
"author": "Shigma <shigma10826@gmail.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/cordiverse/cordis.git",
"directory": "packages/schema"
},
"bugs": {
"url": "https://github.com/cordiverse/cordis/issues"
},
"homepage": "https://github.com/cordiverse/cordis",
"keywords": [
"cordis",
"schema",
"service",
"plugin",
"config"
],
"devDependencies": {
"@cordisjs/core": "^3.16.1"
},
"peerDependencies": {
"@cordisjs/core": "^3.16.1"
},
"dependencies": {
"cosmokit": "^1.6.2",
"schemastery": "^3.14.6"
}
}
1 change: 1 addition & 0 deletions packages/schema/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @cordisjs/schema
58 changes: 58 additions & 0 deletions packages/schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Dict, remove } from 'cosmokit'
import { Context, Service } from '@cordisjs/core'
import Schema from 'schemastery'

export { default as Schema, default as z } from 'schemastery'

const kSchemaOrder = Symbol('cordis.schema.order')

declare module '@cordisjs/core' {
interface Context {
schema: SchemaService
}

interface Events {
'internal/schema'(name: string): void
}
}

export class SchemaService extends Service {
_data: Dict<Schema> = Object.create(null)

constructor(public ctx: Context) {
super(ctx, 'schema', true)
}

extend(name: string, schema: Schema, order = 0) {
const caller = this[Context.current]
const target = this.get(name)
const index = target.list.findIndex(a => a[kSchemaOrder] < order)
schema[kSchemaOrder] = order
if (index >= 0) {
target.list.splice(index, 0, schema)
} else {
target.list.push(schema)
}
this.ctx.emit('internal/schema', name)
caller.on('dispose', () => {
remove(target.list, schema)
this.ctx.emit('internal/schema', name)
})
}

get(name: string) {
return (this._data[name] ||= Schema.intersect([])) as Schema & { list: Schema[] }
}

set(name: string, schema: Schema) {
const caller = this[Context.current]
this._data[name] = schema
this.ctx.emit('internal/schema', name)
caller?.on('dispose', () => {
delete this._data[name]
this.ctx.emit('internal/schema', name)
})
}
}

export default SchemaService
10 changes: 10 additions & 0 deletions packages/schema/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
},
"include": [
"src",
],
}

0 comments on commit ab63f5f

Please sign in to comment.