|
| 1 | +# Quarto API and @quarto/types |
| 2 | + |
| 3 | +## Building @quarto/types |
| 4 | + |
| 5 | +To build the @quarto/types package: |
| 6 | + |
| 7 | +``` |
| 8 | +cd packages/quarto-types |
| 9 | +npm run build |
| 10 | +``` |
| 11 | + |
| 12 | +This runs typecheck and then bundles all type definitions into `dist/index.d.ts`. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Updating the Quarto API |
| 17 | + |
| 18 | +The Quarto API is how external execution engines access Quarto's core functionality. The API exists in two places: |
| 19 | + |
| 20 | +1. **Type definitions** in `packages/quarto-types/` - consumed by external engines (TypeScript) |
| 21 | +2. **Implementation** in `src/core/quarto-api.ts` - used within quarto-cli |
| 22 | + |
| 23 | +### Step-by-step: Adding to the Quarto API |
| 24 | + |
| 25 | +Follow these steps in order when adding new functionality to the API: |
| 26 | + |
| 27 | +#### 1. Update quarto-types type definitions |
| 28 | + |
| 29 | +**Add auxiliary types** (if needed): |
| 30 | + |
| 31 | +- Types belong in `packages/quarto-types/src/` |
| 32 | +- Follow the existing file organization: |
| 33 | + - `system.ts` - System/process types (ProcessResult, TempContext, etc.) |
| 34 | + - `console.ts` - Console/UI types (SpinnerOptions, etc.) |
| 35 | + - `jupyter.ts` - Jupyter-specific types |
| 36 | + - `check.ts` - Check command types |
| 37 | + - `execution.ts` - Execution engine types |
| 38 | + - etc. |
| 39 | +- Create new files if needed for logical grouping |
| 40 | + |
| 41 | +**Export types from index.ts:** |
| 42 | + |
| 43 | +```typescript |
| 44 | +// In packages/quarto-types/src/index.ts |
| 45 | +export type * from "./your-new-file.ts"; |
| 46 | +``` |
| 47 | + |
| 48 | +**Add to QuartoAPI interface:** |
| 49 | + |
| 50 | +```typescript |
| 51 | +// In packages/quarto-types/src/quarto-api.ts |
| 52 | + |
| 53 | +// 1. Import any new types at the top |
| 54 | +import type { YourNewType } from "./your-file.ts"; |
| 55 | + |
| 56 | +// 2. Add to the QuartoAPI interface |
| 57 | +export interface QuartoAPI { |
| 58 | + // ... existing namespaces |
| 59 | + |
| 60 | + yourNamespace: { |
| 61 | + yourMethod: (param: YourNewType) => ReturnType; |
| 62 | + }; |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +#### 2. Test the type definitions |
| 67 | + |
| 68 | +```bash |
| 69 | +cd packages/quarto-types |
| 70 | +npm run build |
| 71 | +``` |
| 72 | + |
| 73 | +This will: |
| 74 | + |
| 75 | +- Run `tsc --noEmit` to typecheck |
| 76 | +- Bundle types into `dist/index.d.ts` |
| 77 | +- Show any type errors |
| 78 | + |
| 79 | +Fix any errors before proceeding. |
| 80 | + |
| 81 | +#### 3. Update the internal QuartoAPI interface |
| 82 | + |
| 83 | +The file `src/core/quarto-api.ts` contains a **duplicate** QuartoAPI interface definition used for the internal implementation. Update it to match: |
| 84 | + |
| 85 | +```typescript |
| 86 | +// In src/core/quarto-api.ts (near top of file) |
| 87 | +export interface QuartoAPI { |
| 88 | + // ... existing namespaces |
| 89 | + |
| 90 | + yourNamespace: { |
| 91 | + yourMethod: (param: YourNewType) => ReturnType; |
| 92 | + }; |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +**Note:** This interface must match the one in quarto-types, but uses internal types. |
| 97 | + |
| 98 | +#### 4. Wire up the implementation |
| 99 | + |
| 100 | +Still in `src/core/quarto-api.ts`: |
| 101 | + |
| 102 | +**Add imports** (near top): |
| 103 | + |
| 104 | +```typescript |
| 105 | +import { yourMethod } from "./your-module.ts"; |
| 106 | +``` |
| 107 | + |
| 108 | +**Add to quartoAPI object** (at bottom): |
| 109 | + |
| 110 | +```typescript |
| 111 | +export const quartoAPI: QuartoAPI = { |
| 112 | + // ... existing namespaces |
| 113 | + |
| 114 | + yourNamespace: { |
| 115 | + yourMethod, |
| 116 | + }, |
| 117 | +}; |
| 118 | +``` |
| 119 | + |
| 120 | +#### 5. Verify with typecheck |
| 121 | + |
| 122 | +Run the quarto typecheck: |
| 123 | + |
| 124 | +```bash |
| 125 | +package/dist/bin/quarto |
| 126 | +``` |
| 127 | + |
| 128 | +No output means success! Fix any type errors. |
| 129 | + |
| 130 | +#### 6. Commit with built artifact |
| 131 | + |
| 132 | +**Always commit the built `dist/index.d.ts` file** along with source changes: |
| 133 | + |
| 134 | +```bash |
| 135 | +git add packages/quarto-types/src/your-file.ts \ |
| 136 | + packages/quarto-types/src/index.ts \ |
| 137 | + packages/quarto-types/src/quarto-api.ts \ |
| 138 | + packages/quarto-types/dist/index.d.ts \ |
| 139 | + src/core/quarto-api.ts |
| 140 | + |
| 141 | +git commit -m "Add yourNamespace to Quarto API" |
| 142 | +``` |
| 143 | + |
| 144 | +### Using the Quarto API in source files |
| 145 | + |
| 146 | +#### Inside quarto-cli (internal modules) |
| 147 | + |
| 148 | +```typescript |
| 149 | +// Import the quartoAPI instance |
| 150 | +import { quartoAPI as quarto } from "../../core/quarto-api.ts"; |
| 151 | + |
| 152 | +// Use it |
| 153 | +const caps = await quarto.jupyter.capabilities(); |
| 154 | +await quarto.console.withSpinner({ message: "Working..." }, async () => { |
| 155 | + // do work |
| 156 | +}); |
| 157 | +``` |
| 158 | + |
| 159 | +#### External engines |
| 160 | + |
| 161 | +External engines receive the API via their `init()` method: |
| 162 | + |
| 163 | +```typescript |
| 164 | +let quarto: QuartoAPI; |
| 165 | + |
| 166 | +export const myEngineDiscovery: ExecutionEngineDiscovery = { |
| 167 | + init: (quartoAPI: QuartoAPI) => { |
| 168 | + quarto = quartoAPI; // Store for later use |
| 169 | + }, |
| 170 | + |
| 171 | + // ... other methods can now use quarto |
| 172 | +}; |
| 173 | +``` |
| 174 | + |
| 175 | +#### Removing old imports |
| 176 | + |
| 177 | +When moving functionality to the API, **remove direct imports** from internal modules: |
| 178 | + |
| 179 | +```typescript |
| 180 | +// ❌ OLD - direct import |
| 181 | +import { withSpinner } from "../../core/console.ts"; |
| 182 | + |
| 183 | +// ✅ NEW - use API |
| 184 | +import { quartoAPI as quarto } from "../../core/quarto-api.ts"; |
| 185 | +const result = await quarto.console.withSpinner(...); |
| 186 | +``` |
| 187 | + |
| 188 | +This ensures external engines and internal code use the same interface. |
0 commit comments