Skip to content

Commit

Permalink
Add explicit return types (#224)
Browse files Browse the repository at this point in the history
Co-authored-by: juliusmarminge <julius0216@outlook.com>
  • Loading branch information
colinhacks and juliusmarminge authored Apr 23, 2024
1 parent 083f77e commit bed246c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
7 changes: 7 additions & 0 deletions .changeset/polite-fireants-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@t3-oss/env-nextjs": patch
"@t3-oss/env-core": patch
"@t3-oss/env-nuxt": patch
---

refactor: add explicit return type to make lib more portable
48 changes: 31 additions & 17 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type ZodError, type ZodObject, type ZodType, z } from "zod";
import type { TypeOf, ZodError, ZodObject, ZodType } from "zod";
import { object } from "zod";

export type ErrorMessage<T extends string> = T;
export type Simplify<T> = {
Expand Down Expand Up @@ -191,22 +192,35 @@ export type EnvOptions<
| (StrictOptions<TPrefix, TServer, TClient, TShared, TExtends> &
ServerClientOptions<TPrefix, TServer, TClient>);

export function createEnv<
TPrefix extends string | undefined,
TServer extends Record<string, ZodType> = NonNullable<unknown>,
TClient extends Record<string, ZodType> = NonNullable<unknown>,
TShared extends Record<string, ZodType> = NonNullable<unknown>,
const TExtends extends Array<Record<string, unknown>> = [],
>(
opts: EnvOptions<TPrefix, TServer, TClient, TShared, TExtends>,
): Readonly<
type TPrefixFormat = string | undefined;
type TServerFormat = Record<string, ZodType>;
type TClientFormat = Record<string, ZodType>;
type TSharedFormat = Record<string, ZodType>;
type TExtendsFormat = Array<Record<string, unknown>>;

export type CreateEnv<
TServer extends TServerFormat,
TClient extends TClientFormat,
TShared extends TSharedFormat,
TExtends extends TExtendsFormat,
> = Readonly<
Simplify<
z.infer<ZodObject<TServer>> &
z.infer<ZodObject<TClient>> &
z.infer<ZodObject<TShared>> &
TypeOf<ZodObject<TServer>> &
TypeOf<ZodObject<TClient>> &
TypeOf<ZodObject<TShared>> &
UnReadonlyObject<Reduce<TExtends>>
>
> {
>;

export function createEnv<
TPrefix extends TPrefixFormat,
TServer extends TServerFormat = NonNullable<unknown>,
TClient extends TClientFormat = NonNullable<unknown>,
TShared extends TSharedFormat = NonNullable<unknown>,
const TExtends extends TExtendsFormat = [],
>(
opts: EnvOptions<TPrefix, TServer, TClient, TShared, TExtends>,
): CreateEnv<TServer, TClient, TShared, TExtends> {
const runtimeEnv = opts.runtimeEnvStrict ?? opts.runtimeEnv ?? process.env;

const emptyStringAsUndefined = opts.emptyStringAsUndefined ?? false;
Expand All @@ -225,9 +239,9 @@ export function createEnv<
const _client = typeof opts.client === "object" ? opts.client : {};
const _server = typeof opts.server === "object" ? opts.server : {};
const _shared = typeof opts.shared === "object" ? opts.shared : {};
const client = z.object(_client);
const server = z.object(_server);
const shared = z.object(_shared);
const client = object(_client);
const server = object(_server);
const shared = object(_shared);
const isServer =
opts.isServer ?? (typeof window === "undefined" || "Deno" in window);

Expand Down
6 changes: 4 additions & 2 deletions packages/nextjs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ServerClientOptions, StrictOptions } from "@t3-oss/env-core";
import { createEnv as createEnvCore } from "@t3-oss/env-core";
import { type CreateEnv, createEnv as createEnvCore } from "@t3-oss/env-core";
import type { ZodType } from "zod";

const CLIENT_PREFIX = "NEXT_PUBLIC_" as const;
Expand Down Expand Up @@ -57,7 +57,9 @@ export function createEnv<
> = NonNullable<unknown>,
TShared extends Record<string, ZodType> = NonNullable<unknown>,
const TExtends extends Array<Record<string, unknown>> = [],
>(opts: Options<TServer, TClient, TShared, TExtends>) {
>(
opts: Options<TServer, TClient, TShared, TExtends>,
): CreateEnv<TServer, TClient, TShared, TExtends> {
const client = typeof opts.client === "object" ? opts.client : {};
const server = typeof opts.server === "object" ? opts.server : {};
const shared = opts.shared;
Expand Down
11 changes: 8 additions & 3 deletions packages/nuxt/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createEnv as createEnvCore } from "@t3-oss/env-core";
import { type CreateEnv, createEnv as createEnvCore } from "@t3-oss/env-core";
import type { ServerClientOptions, StrictOptions } from "@t3-oss/env-core";
import type { ZodType } from "zod";

Expand All @@ -18,10 +18,15 @@ type Options<

export function createEnv<
TServer extends Record<string, ZodType> = NonNullable<unknown>,
TClient extends Record<string, ZodType> = NonNullable<unknown>,
TClient extends Record<
`${ClientPrefix}${string}`,
ZodType
> = NonNullable<unknown>,
TShared extends Record<string, ZodType> = NonNullable<unknown>,
const TExtends extends Array<Record<string, unknown>> = [],
>(opts: Options<TServer, TClient, TShared, TExtends>) {
>(
opts: Options<TServer, TClient, TShared, TExtends>,
): CreateEnv<TServer, TClient, TShared, TExtends> {
const client = typeof opts.client === "object" ? opts.client : {};
const server = typeof opts.server === "object" ? opts.server : {};
const shared = opts.shared;
Expand Down

0 comments on commit bed246c

Please sign in to comment.