Skip to content

Commit

Permalink
refactor: add package and rewrite slow typings
Browse files Browse the repository at this point in the history
- `json-schema-to-ts` seem to be slower at inferring the type, because it
fully expands the type (while
`@profusion/json-schema-to-typescript-definitions` will not). For this
reason, we should keep `FromSchema<>` usage to a minimum
- Add `json-schema-to-ts` package to package.json

Part of #2
  • Loading branch information
lcsmuller committed May 18, 2023
1 parent e25930f commit 12316f3
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 18 deletions.
28 changes: 28 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"eslint.enable": true,
"eslint.validate": [
"javascript",
"typescript"
],
"editor.tabSize": 2,
"editor.rulers": [80],
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
},
"files.insertFinalNewline": true,
"prettier.printWidth": 80,
"prettier.tabWidth": 2,
"prettier.useTabs": false,
"prettier.singleQuote": true,
"prettier.trailingComma": "all",
"prettier.semi": true,
"prettier.arrowParens": "avoid"
}
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const validateEnvSchema = <
>(
schema: S,
container: Record<string, string | undefined> = process.env,
customize?: Customizations,
customize: Customizations | undefined = undefined,
): EnvSchemaConvertedValues<S, Customizations> =>
createValidateEnvSchema(schema, customize)(container);

Expand Down
22 changes: 13 additions & 9 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export type EnvSchemaPartialValues<S extends BaseEnvSchema> = Partial<
export type EnvSchemaPropertyValue<
S extends BaseEnvSchema,
K extends keyof S['properties'],
> = K extends keyof TypeFromJSONSchema<S> ? TypeFromJSONSchema<S>[K] : never;
V = TypeFromJSONSchema<S>,
> = K extends keyof V ? V[K] : never;

/**
* Errors are stored per-property/variable, if something
Expand Down Expand Up @@ -119,14 +120,15 @@ export type EnvSchemaCustomSerializers<S extends BaseEnvSchema> = Readonly<
export type EnvSchemaPostValidateFn<
S extends BaseEnvSchema,
K extends keyof S['properties'],
V = EnvSchemaPropertyValue<S, K>,
> = (
value: EnvSchemaPropertyValue<S, K> | undefined,
value: V | undefined,
propertySchema: S['properties'][K],
key: K,
schema: Readonly<S>,
allValues: EnvSchemaPartialValues<S>,
errors: Readonly<EnvSchemaMaybeErrors<S>>,
) => EnvSchemaPropertyValue<S, K> | undefined;
) => V | undefined;

/**
* Customize the validator to be executed for each property,
Expand All @@ -149,8 +151,9 @@ export type EnvSchemaConvertFn<
S extends BaseEnvSchema,
K extends keyof S['properties'],
R,
V = EnvSchemaPropertyValue<S, K>,
> = (
value: EnvSchemaPropertyValue<S, K> | undefined,
value: V | undefined,
propertySchema: S['properties'][K],
key: K,
schema: Readonly<S>,
Expand Down Expand Up @@ -193,10 +196,11 @@ type EnvSchemaConvertedValue<
S extends BaseEnvSchema,
K extends keyof S['properties'],
Convert,
V = TypeFromJSONSchema<S>,
> = Convert extends EnvSchemaConvertFn<S, K, infer N>
? N
: K extends keyof TypeFromJSONSchema<S>
? TypeFromJSONSchema<S>[K]
: K extends keyof V
? V[K]
: never;

type EnvSchemaConvertedValuesWithConvertInternal<
Expand All @@ -219,7 +223,7 @@ export type EnvSchemaConverters<

export type EnvSchemaConvertedPartialValuesWithConvert<
S extends BaseEnvSchema,
Converters extends EnvSchemaCustomConverters<S> | undefined,
Converters,
> = Converters extends EnvSchemaCustomConverters<S>
? Partial<EnvSchemaConvertedValuesWithConvertInternal<S, Converters>>
: EnvSchemaPartialValues<S>;
Expand All @@ -236,9 +240,9 @@ export type EnvSchemaConvertedPartialValues<
EnvSchemaConverters<S, Customizations>
>;

export type EnvSchemaConvertedValuesWithConvert<
type EnvSchemaConvertedValuesWithConvert<
S extends BaseEnvSchema,
Converters extends EnvSchemaCustomConverters<S> | undefined,
Converters,
> = Converters extends EnvSchemaCustomConverters<S>
? EnvSchemaConvertedValuesWithConvertInternal<S, Converters>
: TypeFromJSONSchema<S>;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"husky": "^8.0.3",
"install-peers-cli": "^2.2.0",
"jest": "^29.5.0",
"json-schema-to-ts": "^2.8.0",
"lint-staged": "^13.2.2",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"target": "ES2015",
"moduleResolution": "node",
Expand Down
51 changes: 43 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"@babel/traverse" "^7.21.5"
"@babel/types" "^7.21.5"

"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.20.2", "@babel/helper-plugin-utils@^7.8.0":
"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.20.2", "@babel/helper-plugin-utils@^7.8.0":
version "7.21.5"
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.21.5.tgz#345f2377d05a720a4e5ecfa39cbf4474a4daed56"
integrity sha512-0WDaIlXKOX/3KfBK/dwP1oQGiPh6rjMkT7HIRv7i5RR2VUMwrx5ZL0dwBkKx7+SW1zwNdgjHd34IMk5ZjTeHVg==
Expand Down Expand Up @@ -157,7 +157,7 @@
chalk "^2.0.0"
js-tokens "^4.0.0"

"@babel/parser@^7.1.0", "@babel/parser@^7.14.7", "@babel/parser@^7.20.7", "@babel/parser@^7.21.5", "@babel/parser@^7.21.8":
"@babel/parser@^7.1.0", "@babel/parser@^7.12.13", "@babel/parser@^7.14.7", "@babel/parser@^7.20.7", "@babel/parser@^7.21.5", "@babel/parser@^7.21.8":
version "7.21.8"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.21.8.tgz#642af7d0333eab9c0ad70b14ac5e76dbde7bfdf8"
integrity sha512-6zavDGdzG3gUqAdWvlLFfk+36RilI+Pwyuuh7HItyeScCWP3k6i8vKclAQ0bM/0y/Kz/xiwvxhMv9MgTJP5gmA==
Expand Down Expand Up @@ -247,11 +247,11 @@
"@babel/helper-plugin-utils" "^7.8.0"

"@babel/plugin-syntax-top-level-await@^7.8.3":
version "7.14.5"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-top-level-await/-/plugin-syntax-top-level-await-7.14.5.tgz#c1cfdadc35a646240001f06138247b741c34d94c"
integrity sha512-hx++upLv5U1rgYfwe1xBQUhRmU41NEvpUvrp8jkrSCdvGSnM5/qdRMtylJ6PG5OFkBaHkbTAKTnd3/YyESRHFw==
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-top-level-await/-/plugin-syntax-top-level-await-7.12.13.tgz#c5f0fa6e249f5b739727f923540cf7a806130178"
integrity sha512-A81F9pDwyS7yM//KwbCSDqy3Uj4NMIurtplxphWxoYtNPov7cJsDkAFNNyVlIZ3jwGycVsurZ+LtOA8gZ376iQ==
dependencies:
"@babel/helper-plugin-utils" "^7.14.5"
"@babel/helper-plugin-utils" "^7.12.13"

"@babel/plugin-syntax-typescript@^7.7.2":
version "7.21.4"
Expand All @@ -260,7 +260,14 @@
dependencies:
"@babel/helper-plugin-utils" "^7.20.2"

"@babel/template@^7.20.7", "@babel/template@^7.3.3":
"@babel/runtime@^7.18.3":
version "7.21.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.5.tgz#8492dddda9644ae3bda3b45eabe87382caee7200"
integrity sha512-8jI69toZqqcsnqGGqwGS4Qb1VwLOEp4hz+CXPywcvjs60u3B4Pom/U/7rm4W8tMOYEB+E9wgD0mW1l3r8qlI9Q==
dependencies:
regenerator-runtime "^0.13.11"

"@babel/template@^7.20.7":
version "7.20.7"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.20.7.tgz#a15090c2839a83b02aa996c0b4994005841fd5a8"
integrity sha512-8SegXApWe6VoNw0r9JHpSteLKTpTiLZ4rMlGIm9JQ18KiCtyQiAMEazujAHrUS5flrcqYZa75ukev3P6QmUwUw==
Expand All @@ -269,6 +276,15 @@
"@babel/parser" "^7.20.7"
"@babel/types" "^7.20.7"

"@babel/template@^7.3.3":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.12.13.tgz#530265be8a2589dbb37523844c5bcb55947fb327"
integrity sha512-/7xxiGA57xMo/P2GVvdEumr8ONhFOhfgq2ihK3h1e6THqzTAkHbkXgB0xI9yeTfIUoH3+oAeHhqm/I43OTbbjA==
dependencies:
"@babel/code-frame" "^7.12.13"
"@babel/parser" "^7.12.13"
"@babel/types" "^7.12.13"

"@babel/traverse@^7.21.5", "@babel/traverse@^7.7.2":
version "7.21.5"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.21.5.tgz#ad22361d352a5154b498299d523cf72998a4b133"
Expand All @@ -285,7 +301,7 @@
debug "^4.1.0"
globals "^11.1.0"

"@babel/types@^7.0.0", "@babel/types@^7.18.6", "@babel/types@^7.20.7", "@babel/types@^7.21.0", "@babel/types@^7.21.4", "@babel/types@^7.21.5", "@babel/types@^7.3.0", "@babel/types@^7.3.3":
"@babel/types@^7.0.0", "@babel/types@^7.12.13", "@babel/types@^7.18.6", "@babel/types@^7.20.7", "@babel/types@^7.21.0", "@babel/types@^7.21.4", "@babel/types@^7.21.5", "@babel/types@^7.3.0", "@babel/types@^7.3.3":
version "7.21.5"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.21.5.tgz#18dfbd47c39d3904d5db3d3dc2cc80bedb60e5b6"
integrity sha512-m4AfNvVF2mVC/F7fDEdH2El3HzUg9It/XsCxZiOTTA3m3qYfcSVSbTfM6Q9xG+hYDniZssYhlXKKUMD5m8tF4Q==
Expand Down Expand Up @@ -3183,6 +3199,15 @@ json-parse-even-better-errors@^2.3.0:
resolved "https://registry.yarnpkg.com/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz#7c47805a94319928e05777405dc12e1f7a4ee02d"
integrity sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==

json-schema-to-ts@^2.8.0:
version "2.8.0"
resolved "https://registry.yarnpkg.com/json-schema-to-ts/-/json-schema-to-ts-2.8.0.tgz#4e31a6ac98fd7ceae2a7ad065fa73977f0588ddb"
integrity sha512-fNAnQ6N0a/0rJlqkzKBdb+5EnMgcKTlrAwMLg3adlKRTT1uNNT4ziqwBzMhj4vp768pO9PoQzZcUyv3QWmD0pQ==
dependencies:
"@babel/runtime" "^7.18.3"
"@types/json-schema" "^7.0.9"
ts-algebra "^1.2.0"

json-schema-traverse@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660"
Expand Down Expand Up @@ -3933,6 +3958,11 @@ redent@^3.0.0:
indent-string "^4.0.0"
strip-indent "^3.0.0"

regenerator-runtime@^0.13.11:
version "0.13.11"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==

regexp.prototype.flags@^1.4.3:
version "1.5.0"
resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.5.0.tgz#fe7ce25e7e4cca8db37b6634c8a2c7009199b9cb"
Expand Down Expand Up @@ -4426,6 +4456,11 @@ trim-newlines@^3.0.0:
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-3.0.1.tgz#260a5d962d8b752425b32f3a7db0dcacd176c144"
integrity sha512-c1PTsA3tYrIsLGkJkzHF+w9F2EyxfXGo4UyJc4pFL++FMjnq0HJS69T3M7d//gKrFKwy429bouPescbjecU+Zw==

ts-algebra@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/ts-algebra/-/ts-algebra-1.2.0.tgz#f91c481207a770f0d14d055c376cbee040afdfc9"
integrity sha512-kMuJJd8B2N/swCvIvn1hIFcIOrLGbWl9m/J6O3kHx9VRaevh00nvgjPiEGaRee7DRaAczMYR2uwWvXU22VFltw==

ts-jest@^29.1.0:
version "29.1.0"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-29.1.0.tgz#4a9db4104a49b76d2b368ea775b6c9535c603891"
Expand Down

0 comments on commit 12316f3

Please sign in to comment.