generated from astrohelm/node-workspace
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from astrohelm/dev
Version 0.6.0
- Loading branch information
Showing
16 changed files
with
389 additions
and
63 deletions.
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
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,48 @@ | ||
'use strict'; | ||
|
||
const [test, assert] = [require('node:test'), require('node:assert')]; | ||
const Schema = require('../..'); | ||
|
||
test('[Handyman] Schema with namespace', () => { | ||
const namespace = { User: new Schema('string') }; | ||
const schema = new Schema(['User', 'User'], { namespace }); | ||
const sample = ['Alexander', 'Ivanov']; | ||
|
||
assert.strictEqual(namespace.User.warnings.length + schema.warnings.length, 0); | ||
assert.strictEqual(schema.test(sample).length, 0); | ||
}); | ||
|
||
test('[Handyman] Pull schemas', () => { | ||
const schema = new Schema({ | ||
$id: 'MySchema', | ||
a: 'string', | ||
b: { $id: 'MySubSchema', c: 'number' }, | ||
c: new Schema('?string'), | ||
d: { $type: 'schema', schema: new Schema('number'), $id: 'MySubSchema2' }, | ||
e: { $type: 'schema', schema: new Schema({ $type: 'number', $id: 'MySubSchema3' }) }, | ||
}); | ||
|
||
assert.strictEqual(schema.warnings.length, 0); | ||
assert.strictEqual(!!schema.pull('MySchema'), false); | ||
assert.strictEqual(!!schema.pull('MySubSchema'), true); | ||
assert.strictEqual(!!schema.pull('MySubSchema2'), true); | ||
assert.strictEqual(!!schema.pull('MySubSchema3'), true); | ||
}); | ||
|
||
test('[Handyman] Shorthands', () => { | ||
const schema = new Schema( | ||
{ | ||
a: 'string', //? scalar shorthand | ||
b: '?string', //? optional shorthand | ||
c: ['string', 'string'], //? tuple | ||
d: new Schema('?string'), //? Schema shorthand | ||
e: ['winter', 'spring'], //? Enum shorthand | ||
f: { a: 'number', b: 'string' }, //? Object shorthand | ||
g: { $type: 'array', items: 'string' }, //? Array items shorthand | ||
h: 'MyExternalSchema', | ||
}, | ||
{ namespace: { MyExternalSchema: new Schema('string') } }, | ||
); | ||
|
||
assert.strictEqual(schema.warnings.length, 0); | ||
}); |
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
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 |
---|---|---|
@@ -1,3 +1,94 @@ | ||
# Metatype module / generate typescript:JSDOC from schema | ||
# Metatype module | ||
|
||
> WORK IN PROGRESS | ||
Generate type annotation from schema; | ||
|
||
> Warning: You will receive compressed version; | ||
## Usage | ||
|
||
By default module runs in mjs mode, that means that: | ||
|
||
- It will export all schemas with $id field & root schema | ||
- it will export as default root schema | ||
|
||
> In cjs mode, it will export only root schema | ||
```js | ||
const plan = 'string'; | ||
const schema = new Schema(plan); | ||
schema.dts('Example', { mode: 'mjs' }); | ||
// type Example = string; | ||
// export type = { Example }; | ||
// export default Example; | ||
``` | ||
|
||
## Example | ||
|
||
### Input: | ||
|
||
```js | ||
{ | ||
"firstName": 'string', | ||
"lastName": 'string', | ||
"label": ["member", "guest", "vip"] | ||
"age": '?number', | ||
settings: { alertLevel: 'string', $id: 'Setting' } | ||
} | ||
``` | ||
|
||
### Output (mjs mode): | ||
|
||
```ts | ||
interface Settings { | ||
alertLevel: string; | ||
} | ||
|
||
interface Example { | ||
firstName: string; | ||
lastName: string; | ||
label: 'member' | 'guest' | 'vip'; | ||
age?: number; | ||
settings: Settings; | ||
} | ||
|
||
export type { Example }; | ||
export default Example; | ||
``` | ||
|
||
### Output (cjs mode): | ||
|
||
```ts | ||
interface Settings { | ||
alertLevel: string; | ||
} | ||
|
||
interface Example { | ||
firstName: string; | ||
lastName: string; | ||
label: 'member' | 'guest' | 'vip'; | ||
settings: Settings; | ||
age?: number; | ||
} | ||
|
||
export = Example; | ||
``` | ||
|
||
## Writing custom prototypes with Metatype | ||
|
||
By default all custom types will recieve unknown type; If you want to have custom type, you may | ||
create custom prototype with toTypescript field; | ||
|
||
```js | ||
function Date(plan, tools) { | ||
this.toTypescript = (name, namespace) => { | ||
// If you want to have your type in exports, you can add it name to exports; | ||
const { definitions, exports } = namespace; | ||
// If your type is complex, you can push your builded type / interface to definitions and return it name | ||
const type = `type ${name} = Date`; | ||
definitions.add(type); | ||
//? You can return only name or value that can be assigned to type | ||
return name; // Equal to: | ||
return 'Date'; | ||
}; | ||
} | ||
``` |
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 |
---|---|---|
@@ -1,11 +1,33 @@ | ||
'use strict'; | ||
|
||
const { nameFix } = require('./utils'); | ||
const types = require('./types'); | ||
|
||
module.exports = schema => { | ||
function TypescriptWrapper() { | ||
this.toTypescript = () => 'unknown'; | ||
} | ||
for (const [name, proto] of types.entries()) schema.forge.attach(name, proto); | ||
schema.forge.attach('before', TypescriptWrapper); | ||
schema.forge.attach('before', { toTypescript: () => 'unknown' }); | ||
schema.forge.attach('after', function TypescriptWrapper() { | ||
const compile = this.toTypescript; | ||
this.toTypescript = (name, namespace) => compile(nameFix(name), namespace); | ||
}); | ||
|
||
schema.dts = (name = 'MetaForge', options = {}) => { | ||
const mode = options.mode ?? 'mjs'; | ||
if (name !== nameFix(name)) throw new Error('Invalid name format'); | ||
const namespace = { definitions: new Set(), exports: new Set() }; | ||
const type = schema.toTypescript(name, namespace); | ||
if (type !== name) { | ||
if (namespace.exports.size === 1) { | ||
const definitions = Array.from(namespace.definitions).join(''); | ||
if (mode === 'cjs') return definitions + `export = ${type}`; | ||
return definitions + `export type ${name}=${type};export default ${type};`; | ||
} | ||
namespace.definitions.add(`type ${name}=${type};`); | ||
} | ||
namespace.exports.add(name); | ||
const definitions = Array.from(namespace.definitions).join(''); | ||
if (mode === 'cjs') return definitions + `export = ${name};`; | ||
const exports = `export type{${Array.from(namespace.exports).join(',')}};`; | ||
return definitions + exports + `export default ${name};`; | ||
}; | ||
}; |
Oops, something went wrong.