ploy types
Generate TypeScript type definitions from your ploy.yaml bindings.
ploy types
Generate TypeScript type definitions from your ploy.yaml bindings. Creates an env.d.ts file and updates tsconfig.json so that your editor and compiler know about all the runtime bindings available to your worker or Next.js app.
ploy types [options]Options
| Flag | Description | Default |
|---|---|---|
-o, --output <path> | Output file path | env.d.ts |
Usage
Run ploy types at the root of your project after any change to ploy.yaml:
ploy types
# Generated env.d.tsThe generated file is automatically referenced in tsconfig.json. Commit it to your repository.
Custom Output Path
ploy types -o src/env.d.tsGenerated Output
Given a ploy.yaml like:
kind: worker
env:
API_URL: https://api.example.com
db:
DB: default
queue:
TASKS: tasks
workflow:
FLOW: my_flow
cache:
CACHE: default
state:
STATE: default
auth:
binding: PLOY_AUTH
ai: trueploy types generates:
declare module "@meetploy/nextjs" {
interface PloyEnv {
vars: {
API_URL: string;
};
DB: D1Database;
TASKS: QueueBinding;
FLOW: WorkflowBinding;
CACHE: CacheBinding;
STATE: StateBinding;
PLOY_AUTH: PloyAuth;
AI_URL: string;
AI_TOKEN: string;
}
}
declare global {
interface PloyEnv {
vars: {
API_URL: string;
};
DB: D1Database;
TASKS: QueueBinding;
FLOW: WorkflowBinding;
CACHE: CacheBinding;
STATE: StateBinding;
PLOY_AUTH: PloyAuth;
AI_URL: string;
AI_TOKEN: string;
}
}
export {};Supported Binding Types
| ploy.yaml key | Type in PloyEnv |
|---|---|
env | vars: { KEY: string } per variable |
db | D1Database per binding name |
queue | QueueBinding per binding name |
workflow | WorkflowBinding per binding name |
cache | CacheBinding per binding name |
state | StateBinding per binding name |
fs | FileStorageBinding per binding name |
timer | TimerBinding per binding name |
auth | PloyAuth as PLOY_AUTH |
ai: true | AI_URL: string and AI_TOKEN: string properties |
Using the Types
After running ploy types, the PloyEnv global interface is available everywhere in your project. With @meetploy/start:
import { ploy } from "@meetploy/start";
const worker = ploy<PloyEnv>()
.get("/", {}, async (ctx) => {
// ctx.env is fully typed
const users = await ctx.env.DB.prepare("SELECT * FROM users").all();
return { users: users.results };
})
.build();
export default worker;With a plain worker:
export default {
async fetch(request: Request, env: PloyEnv): Promise<Response> {
const result = await env.DB.prepare("SELECT * FROM users").all();
return Response.json(result.results);
},
};With Next.js via @meetploy/nextjs:
import { getPloyEnv } from "@meetploy/nextjs";
export async function GET() {
const env = getPloyEnv<PloyEnv>();
const result = await env.DB.prepare("SELECT * FROM users").all();
return Response.json(result.results);
}Run ploy types any time you add or remove bindings in ploy.yaml. Add it to
your postinstall or prepare script to keep types in sync automatically.
package.json Integration
{
"scripts": {
"types": "ploy types",
"dev": "ploy dev",
"build": "ploy build",
"postinstall": "ploy types"
}
}How is this guide?
Last updated on