diff --git a/index.js b/index.js index 860948bb..0f94a7b2 100644 --- a/index.js +++ b/index.js @@ -888,6 +888,8 @@ function buildValue (location, input) { } module.exports = build +module.exports.default = build +module.exports.build = build module.exports.validLargeArrayMechanisms = validLargeArrayMechanisms diff --git a/package.json b/package.json index 791e3ede..a4d38364 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "5.4.1", "description": "Stringify your JSON at max speed", "main": "index.js", - "types": "index.d.ts", + "types": "types/index.d.ts", "scripts": { "bench": "node ./benchmark/bench.js", "bench:cmp": "node ./benchmark/bench-cmp-branch.js", @@ -11,7 +11,7 @@ "benchmark": "node ./benchmark/bench-cmp-lib.js", "lint": "standard", "lint:fix": "standard --fix", - "test:typescript": "tsc --project ./test/types/tsconfig.json && tsd", + "test:typescript": "tsd", "test:unit": "tap -J test/*.test.js test/**/*.test.js", "test": "npm run test:unit && npm run test:typescript" }, @@ -46,7 +46,6 @@ "standard": "^17.0.0", "tap": "^16.0.1", "tsd": "^0.24.1", - "typescript": "^4.0.2", "webpack": "^5.40.0" }, "dependencies": { @@ -62,8 +61,5 @@ "schema-validator.js" ] }, - "runkitExampleFilename": "./examples/example.js", - "tsd": { - "directory": "test/types" - } + "runkitExampleFilename": "./examples/example.js" } diff --git a/test/types/schema-inference.test-d.ts b/test/types/schema-inference.test-d.ts deleted file mode 100644 index 9815ffb4..00000000 --- a/test/types/schema-inference.test-d.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { expectError } from "tsd"; -import build from "../.."; - -// With inference -interface Schema { - id: string; - a?: number; -} - -const stringify3 = build({ - type: "object", - properties: { a: { type: "string" } }, -}); -stringify3({ id: "123" }); -stringify3({ a: 123, id: "123" }); -expectError(stringify3({ anotherOne: "bar" })); -expectError(stringify3({ a: "bar" })); - -// Without inference -const stringify4 = build({ - type: "object", - properties: { a: { type: "string" } }, -}); -stringify4({ id: "123" }); -stringify4({ a: 123, id: "123" }); -stringify4({ anotherOne: "bar" }); -stringify4({ a: "bar" }); - -// Without inference - string type -const stringify5 = build({ - type: "string", -}); -stringify5("foo"); -expectError(stringify5({ id: "123" })); - -// Without inference - null type -const stringify6 = build({ - type: "null", -}); -stringify6(null); -expectError(stringify6("a string")); - -// Without inference - boolean type -const stringify7 = build({ - type: "boolean", -}); -stringify7(true); -expectError(stringify7("a string")); diff --git a/test/types/tsconfig.json b/test/types/tsconfig.json deleted file mode 100644 index acbda84e..00000000 --- a/test/types/tsconfig.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "compilerOptions": { - "esModuleInterop": true, - "noEmit": true, - "rootDir": "../..", - "strict": true, - "target": "ES2015", - "moduleResolution": "node" - }, - "include": [ - "./test.ts" - ] -} diff --git a/index.d.ts b/types/index.d.ts similarity index 95% rename from index.d.ts rename to types/index.d.ts index e225cea8..b5cfb5d4 100644 --- a/index.d.ts +++ b/types/index.d.ts @@ -1,5 +1,7 @@ import Ajv, { Options as AjvOptions } from "ajv" +type Build = typeof build + declare namespace build { interface BaseSchema { /** @@ -168,6 +170,12 @@ declare namespace build { */ mode?: 'debug' | 'standalone' } + + export const validLargeArrayMechanisms: string[] + export function restore (value: (doc: TDoc) => string): ReturnType + + export const build: Build + export { build as default } } interface DebugOption extends build.Options { diff --git a/test/types/test.ts b/types/index.test-d.ts similarity index 61% rename from test/types/test.ts rename to types/index.test-d.ts index 3e7d2631..e9f078fa 100644 --- a/test/types/test.ts +++ b/types/index.test-d.ts @@ -1,5 +1,6 @@ import Ajv from 'ajv' -import build, { Schema } from '../..' +import build, { restore, Schema, validLargeArrayMechanisms } from '..' +import { expectError, expectType } from 'tsd' // Number schemas const schema1: Schema = { @@ -150,4 +151,69 @@ str = build(schema1, { debugMode: true }).code ajv = build(schema1, { debugMode: true }).ajv str = build(schema1, { mode: 'debug' }).code ajv = build(schema1, { mode: 'debug' }).ajv -str = build(schema1, { mode: 'standalone' }) \ No newline at end of file +str = build(schema1, { mode: 'standalone' }) + +const debugCompiled = build({ + title: 'default string', + type: 'object', + properties: { + firstName: { + type: 'string' + } + } +}, { mode: 'debug' }) +expectType>(build.restore(debugCompiled)) +expectType>(restore(debugCompiled)) + +expectType(build.validLargeArrayMechanisms) +expectType(validLargeArrayMechanisms) + +/** + * Schema inference + */ + +// With inference +interface InferenceSchema { + id: string; + a?: number; +} + +const stringify3 = build({ + type: "object", + properties: { a: { type: "string" } }, +}); +stringify3({ id: "123" }); +stringify3({ a: 123, id: "123" }); +expectError(stringify3({ anotherOne: "bar" })); +expectError(stringify3({ a: "bar" })); + +// Without inference +const stringify4 = build({ + type: "object", + properties: { a: { type: "string" } }, +}); +stringify4({ id: "123" }); +stringify4({ a: 123, id: "123" }); +stringify4({ anotherOne: "bar" }); +stringify4({ a: "bar" }); + +// Without inference - string type +const stringify5 = build({ + type: "string", +}); +stringify5("foo"); +expectError(stringify5({ id: "123" })); + +// Without inference - null type +const stringify6 = build({ + type: "null", +}); +stringify6(null); +expectError(stringify6("a string")); + +// Without inference - boolean type +const stringify7 = build({ + type: "boolean", +}); +stringify7(true); +expectError(stringify7("a string"));