diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..9217ef6 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,16 @@ +export type EnvSchemaData = { + [key: string]: unknown; +}; + +export type EnvSchemaOpt = { + schema?: object; + data?: [EnvSchemaData, ...EnvSchemaData[]] | EnvSchemaData; + env?: boolean; + dotenv?: boolean | object; +}; + +declare function loadAndValidateEnvironment( + _opts?: EnvSchemaOpt +): EnvSchemaData; + +export default loadAndValidateEnvironment; diff --git a/package.json b/package.json index 2f0711b..c24ac49 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Validate your env variable using Ajv and dotenv", "main": "index.js", "scripts": { - "test": "standard | snazzy && tap test/*.test.js" + "test": "standard | snazzy && tap test/*.test.js && npm run typescript", + "typescript": "tsd" }, "repository": { "type": "git", @@ -30,7 +31,8 @@ "pre-commit": "^1.2.2", "snazzy": "^8.0.0", "standard": "^14.0.2", - "tap": "^12.7.0" + "tap": "^12.7.0", + "tsd": "^0.13.1" }, "dependencies": { "ajv": "^6.10.2", @@ -41,5 +43,8 @@ "dotenv", "tap" ] + }, + "tsd": { + "directory": "test/types" } } diff --git a/test/types/types.test.ts b/test/types/types.test.ts new file mode 100644 index 0000000..9176aca --- /dev/null +++ b/test/types/types.test.ts @@ -0,0 +1,55 @@ +import { expectError, expectType } from "tsd"; +import envSchema, { EnvSchemaData, EnvSchemaOpt } from "../.."; + +const schema = { + type: "object", + required: ["PORT"], + properties: { + PORT: { + type: "string", + default: 3000, + }, + }, +}; +const data = { + foo: "bar", +}; + +expectType(envSchema()); + +const emptyOpt: EnvSchemaOpt = {}; +expectType(emptyOpt); + +const optWithSchema: EnvSchemaOpt = { + schema, +}; +expectType(optWithSchema); + +const optWithData: EnvSchemaOpt = { + data, +}; +expectType(optWithData); + +expectError({ + data: [], // min 1 item +}); + +const optWithArrayData: EnvSchemaOpt = { + data: [{}], +}; +expectType(optWithArrayData); + +const optWithMultipleItemArrayData: EnvSchemaOpt = { + data: [{}, {}], +}; +expectType(optWithMultipleItemArrayData); + +const optWithDotEnvBoolean: EnvSchemaOpt = { + dotenv: true, +}; +expectType(optWithDotEnvBoolean); + +const optWithDotEnvOpt: EnvSchemaOpt = { + dotenv: true, +}; +expectType(optWithDotEnvOpt);