-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schema): add built-in plugin @cordisjs/schema
- Loading branch information
Showing
5 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @cordisjs/schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.base", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "lib", | ||
}, | ||
"include": [ | ||
"src", | ||
], | ||
} |