Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: jsr publish #227

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-books-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@t3-oss/env-core": patch
---

add `VERCEL_PROJECT_PRODUCTION_URL` to vercel preset
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/src/app/docs/core/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ npm install @t3-oss/env-core zod

<Callout>

`@t3-oss/env-core` requires a minimum of `typescript@4.7.2`.
`@t3-oss/env-core` requires a minimum of `typescript@5.0.0`.

</Callout>

Expand Down
2 changes: 1 addition & 1 deletion docs/src/app/docs/nextjs/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pnpm add @t3-oss/env-nextjs zod

<Callout>

`@t3-oss/env-nextjs` requires a minimum of `typescript@4.7.2`.
`@t3-oss/env-nextjs` requires a minimum of `typescript@5.0.0`.

</Callout>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"workspaces": ["docs", "examples/*", "packages/*"],
"scripts": {
"build": "turbo build --filter @t3-oss/env*",
"dev": "turbo watch build --filter @t3-oss/env*",
"dev": "turbo watch dev --filter @t3-oss/env*",
"lint": "biome check .",
"lint:fix": "biome check . --apply",
"test": "turbo test",
Expand Down
41 changes: 41 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Typesafe Envs made Simple

This is the framework agnostic core package of t3-env.

## Installation

```bash
npm i @t3-oss/env-core zod
```

## Usage

```ts
// src/env.ts
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";

export const env = createEnv({
/*
* Serverside Environment variables, not available on the client.
* Will throw if you access these variables on the client.
*/
server: {
DATABASE_URL: z.string().url(),
OPEN_AI_API_KEY: z.string().min(1),
},
/*
* Environment variables available on the client (and server).
*
* 💡 You'll get type errors if these are not prefixed with PUBLIC_.
*/
clientPrefix: 'PUBLIC_',
client: {
PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().min(1),
},
/*
* Specify what values should be validated by your schemas above.
*/
runtimeEnv: process.env,
});
```
10 changes: 10 additions & 0 deletions packages/core/jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@t3-oss/env-core",
"version": "0.10.3",
"exports": {
".": "./src/index.ts",
"./presets": "./src/presets.ts"
},
"include": ["src/**/*.ts", "README.md", "LICENSE", "package.json"],
"exclude": ["node_modules", "dist", "test"]
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"files": ["dist", "package.json", "LICENSE", "README.md"],
"scripts": {
"build": "bunchee",
"dev": "bunchee -w",
"dev": "bunchee -w --no-clean",
"typecheck": "tsc --noEmit"
},
"peerDependencies": {
Expand Down
39 changes: 38 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/**
* This is the core package of t3-env.
* It contains the `createEnv` function that you can use to create your schema.
* @module
*/
import type { TypeOf, ZodError, ZodObject, ZodType } from "zod";
import { object } from "zod";

/** @internal */
export type ErrorMessage<T extends string> = T;
/** @internal */
export type Simplify<T> = {
[P in keyof T]: T[P];
} & {};

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
// biome-ignore lint/suspicious/noExplicitAny: accept any object
type Impossible<T extends Record<string, any>> = Partial<
Record<keyof T, never>
>;
Expand All @@ -24,6 +31,9 @@ type Reduce<
: never
: never;

/**
* The options that can be passed to the `createEnv` function.
*/
export interface BaseOptions<
TShared extends Record<string, ZodType>,
TExtends extends Array<Record<string, unknown>>,
Expand Down Expand Up @@ -79,6 +89,11 @@ export interface BaseOptions<
emptyStringAsUndefined?: boolean;
}

/**
* Using this interface doesn't validate all environment variables are specified
* in the `runtimeEnv` object. You may want to use `StrictOptions` instead if
* your framework performs static analysis and tree-shakes unused variables.
*/
export interface LooseOptions<
TShared extends Record<string, ZodType>,
TExtends extends Array<Record<string, unknown>>,
Expand All @@ -93,6 +108,12 @@ export interface LooseOptions<
runtimeEnv: Record<string, string | boolean | number | undefined>;
}

/**
* Using this interface validates all environment variables are specified
* in the `runtimeEnv` object. If you miss one, you'll get a type error. Useful
* if you want to make sure all environment variables are set for frameworks that
* perform static analysis and tree-shakes unused variables.
*/
export interface StrictOptions<
TPrefix extends string | undefined,
TServer extends Record<string, ZodType>,
Expand Down Expand Up @@ -127,6 +148,12 @@ export interface StrictOptions<
runtimeEnv?: never;
}

/**
* This interface is used to define the client-side environment variables.
* It's used in conjunction with the `clientPrefix` option to ensure
* that all client-side variables are prefixed with the same string.
* Common examples of prefixes are `NEXT_PUBLIC_`, `NUXT_PUBLIC` or `PUBLIC_`.
*/
export interface ClientOptions<
TPrefix extends string | undefined,
TClient extends Record<string, ZodType>,
Expand All @@ -150,6 +177,10 @@ export interface ClientOptions<
}>;
}

/**
* This interface is used to define the schema for your
* server-side environment variables.
*/
export interface ServerOptions<
TPrefix extends string | undefined,
TServer extends Record<string, ZodType>,
Expand Down Expand Up @@ -198,6 +229,9 @@ type TClientFormat = Record<string, ZodType>;
type TSharedFormat = Record<string, ZodType>;
type TExtendsFormat = Array<Record<string, unknown>>;

/**
* Creates a new environment variable schema.
*/
export type CreateEnv<
TServer extends TServerFormat,
TClient extends TClientFormat,
Expand All @@ -212,6 +246,9 @@ export type CreateEnv<
>
>;

/**
* Create a new environment variable schema.
*/
export function createEnv<
TPrefix extends TPrefixFormat,
TServer extends TServerFormat = NonNullable<unknown>,
Expand Down
40 changes: 35 additions & 5 deletions packages/core/src/presets.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
/**
* This contains presets for common environment variables used
* in 3rd party services so you don't have to write them yourself.
* Include them in your `createEnv.extends` option array.
* @module
*/
import { z } from "zod";
import { createEnv } from ".";
import { createEnv } from "./index";

/**
* Vercel System Environment Variables
* @see https://vercel.com/docs/projects/environment-variables/system-environment-variables#system-environment-variables
*/
export const vercel = () =>
createEnv({
export const vercel = (): Readonly<{
VERCEL?: string | undefined;
VERCEL_ENV?: "development" | "preview" | "production" | undefined;
VERCEL_URL?: string | undefined;
VERCEL_BRANCH_URL?: string | undefined;
VERCEL_REGION?: string | undefined;
VERCEL_AUTOMATION_BYPASS_SECRET?: string | undefined;
VERCEL_GIT_PROVIDER?: string | undefined;
VERCEL_GIT_REPO_SLUG?: string | undefined;
VERCEL_GIT_REPO_OWNER?: string | undefined;
VERCEL_GIT_REPO_ID?: string | undefined;
VERCEL_GIT_COMMIT_REF?: string | undefined;
VERCEL_GIT_COMMIT_SHA?: string | undefined;
VERCEL_GIT_COMMIT_MESSAGE?: string | undefined;
VERCEL_GIT_COMMIT_AUTHOR_LOGIN?: string | undefined;
VERCEL_GIT_COMMIT_AUTHOR_NAME?: string | undefined;
VERCEL_GIT_PREVIOUS_SHA?: string | undefined;
VERCEL_GIT_PULL_REQUEST_ID?: string | undefined;
}> => {
return createEnv({
server: {
VERCEL: z.string().optional(),
VERCEL_ENV: z.enum(["development", "preview", "production"]).optional(),
VERCEL_URL: z.string().optional(),
VERCEL_PROJECT_PRODUCTION_URL: z.string().optional(),
VERCEL_BRANCH_URL: z.string().optional(),
VERCEL_REGION: z.string().optional(),
VERCEL_AUTOMATION_BYPASS_SECRET: z.string().optional(),
Expand All @@ -28,15 +53,20 @@ export const vercel = () =>
},
runtimeEnv: process.env,
});
};

/**
* @see https://docs.uploadthing.com/getting-started/appdir#add-env-variables
*/
export const uploadthing = () =>
createEnv({
export const uploadthing = (): Readonly<{
UPLOADTHING_SECRET: string;
UPLOADTHING_APP_ID?: string | undefined;
}> => {
return createEnv({
server: {
UPLOADTHING_SECRET: z.string(),
UPLOADTHING_APP_ID: z.string().optional(),
},
runtimeEnv: process.env,
});
};
51 changes: 51 additions & 0 deletions packages/nextjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Typesafe Envs made Simple

The Next.js package comes preconfigured for Next.js and also enforces some extra rules by default to make sure you have out-of-the-box compatibility in all different Next.js runtimes.

## Installation

```bash
npm i @t3-oss/env-nextjs zod
```

## Usage

```ts
// src/env.ts
import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";

export const env = createEnv({
/*
* Serverside Environment variables, not available on the client.
* Will throw if you access these variables on the client.
*/
server: {
DATABASE_URL: z.string().url(),
OPEN_AI_API_KEY: z.string().min(1),
},
/*
* Environment variables available on the client (and server).
*
* 💡 You'll get type errors if these are not prefixed with NEXT_PUBLIC_.
*/
client: {
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().min(1),
},
/*
* Specify what values should be validated by your schemas above.
*
* If you're using Next.js < 13.4.4, you'll need to specify the runtimeEnv manually
* For Next.js >= 13.4.4, you can use the experimental__runtimeEnv option and
* only specify client-side variables.
*/
runtimeEnv: {
DATABASE_URL: process.env.DATABASE_URL,
OPEN_AI_API_KEY: process.env.OPEN_AI_API_KEY,
NEXT_PUBLIC_PUBLISHABLE_KEY: process.env.NEXT_PUBLIC_PUBLISHABLE_KEY,
},
// experimental__runtimeEnv: {
// NEXT_PUBLIC_PUBLISHABLE_KEY: process.env.NEXT_PUBLIC_PUBLISHABLE_KEY,
// }
});
```
10 changes: 10 additions & 0 deletions packages/nextjs/jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@t3-oss/env-nextjs",
"version": "0.10.1",
"exports": {
".": "./src/index.ts",
"./presets": "./src/presets.ts"
},
"include": ["src/**/*.ts", "README.md", "LICENSE", "package.json"],
"exclude": ["node_modules", "dist", "test"]
}
2 changes: 1 addition & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"files": ["dist", "package.json", "LICENSE", "README.md"],
"scripts": {
"build": "bunchee",
"dev": "bunchee -w",
"dev": "bunchee -w --no-clean",
"typecheck": "tsc --noEmit",
"prepack": "bun ../../replace-workspace-protocol.ts"
},
Expand Down
8 changes: 8 additions & 0 deletions packages/nextjs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* This is the nextjs package of t3-env.
* It contains the `createEnv` function that you can use to create your schema.
* @module
*/
import type {
CreateEnv,
ServerClientOptions,
Expand Down Expand Up @@ -53,6 +58,9 @@ type Options<
}
);

/**
* Create a new environment variable schema.
*/
export function createEnv<
TServer extends Record<string, ZodType> = NonNullable<unknown>,
TClient extends Record<
Expand Down
6 changes: 6 additions & 0 deletions packages/nextjs/src/presets.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
/**
* This contains presets for common environment variables used
* in 3rd party services so you don't have to write them yourself.
* Include them in your `createEnv.extends` option array.
* @module
*/
export * from "@t3-oss/env-core/presets";
Loading