From 49e9c03fe3726b5203444675538802046436709a Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Thu, 22 Oct 2026 11:04:07 -0700 Subject: [PATCH 1/7] docs(cli): drop the TypeScript guide from the cg init reference checklist The TypeScript guide starts from the Hono template repository and no longer runs cg init. --- lib/cli/DEVELOPMENT.md | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/cli/DEVELOPMENT.md b/lib/cli/DEVELOPMENT.md index b9f756c54..6955ef419 100644 --- a/lib/cli/DEVELOPMENT.md +++ b/lib/cli/DEVELOPMENT.md @@ -76,6 +76,5 @@ Steps to follow when preparing a new release of the CommonGrants CLI. Feature wo - [ ] [Express.js template README](../../templates/express-js/README.md) - [ ] [Quickstart guide](../../website/src/content/docs/getting-started.mdx) - [ ] [Using Python](../../website/src/content/docs/guides/using-python.mdx) - - [ ] [Using TypeScript](../../website/src/content/docs/guides/using-typescript.mdx) - [ ] If the `check spec` command was updated, references to that command in the following locations have been updated: - [ ] [FastAPI template Makefile](../../templates/fast-api/Makefile) From 68bdb18e75d0a9fb82a8170a0be408fddfe44a22 Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:23:42 -0700 Subject: [PATCH 2/7] docs(website): start the TypeScript guide from the Hono template repository Replace the cg init and Express setup steps and project tree with the Hono template's Use this template flow, Node.js 24 prerequisite, and structure. --- .../content/docs/guides/using-typescript.mdx | 86 +++++-------------- 1 file changed, 22 insertions(+), 64 deletions(-) diff --git a/website/src/content/docs/guides/using-typescript.mdx b/website/src/content/docs/guides/using-typescript.mdx index 72cca1894..8f1c51ec9 100644 --- a/website/src/content/docs/guides/using-typescript.mdx +++ b/website/src/content/docs/guides/using-typescript.mdx @@ -7,15 +7,15 @@ sidebar: import { FileTree, Steps } from "@astrojs/starlight/components"; -Learn how to implement a CommonGrants API in TypeScript using the [Express.js](https://expressjs.com/) web framework and the [CommonGrants TypeScript SDK](https://github.com/HHS/simpler-grants-protocol/tree/main/lib/ts-sdk#readme). +Learn how to implement a CommonGrants API in TypeScript using the [Hono API template](https://github.com/common-grants/cg-api-template-hono) and the [CommonGrants TypeScript SDK](https://github.com/HHS/simpler-grants-protocol/tree/main/lib/ts-sdk#readme). ## Quickstart -This guide walks you through setting up a new project from the CommonGrants Express.js template, then adding the SDK to validate your data and define custom fields. +This guide walks you through creating a new project from the CommonGrants Hono template, then using the SDK to validate your data and define custom fields. :::note[Prerequisites] -- [Node.js](https://nodejs.org/) v22.12 or later (check with `node --version`). The template compiles to CommonJS, and loading the ESM-only SDK from CommonJS needs Node.js 22.12+. +- [Node.js](https://nodejs.org/) v24 (check with `node --version`). The template supports Node.js 24 only and includes an `.nvmrc` file. - [pnpm](https://pnpm.io/installation) v11 or later (check with `pnpm --version`). The template's scripts use pnpm. - Port `3000` free for the development server. @@ -23,64 +23,31 @@ This guide walks you through setting up a new project from the CommonGrants Expr ### First steps -Get an Express.js project up and running with the following steps: +Get the Hono template up and running with the following steps: -1. Create a new directory for your project: +1. Open the [template repository](https://github.com/common-grants/cg-api-template-hono) and click **Use this template** to create your own repository from it. - ```bash - mkdir common-grants-api - cd common-grants-api - ``` - -2. Set up your project using the CommonGrants CLI: +2. Clone your new repository: ```bash - npx @common-grants/cli@0.4.1 init --template express-js - ``` - - `npx` ships with Node.js. Bun users can run `bunx` instead. `pnpm dlx` can't run `cg init`. - -3. Create `pnpm-workspace.yaml`, which `cg init` doesn't create: - - ```yaml title=pnpm-workspace.yaml - allowBuilds: - "@scarf/scarf": false - esbuild: false - overrides: - "ajv@>=7.0.0-alpha.0 <8.18.0": ">=8.18.0" - "brace-expansion@>=1 <1.1.21": "1.1.21" - "brace-expansion@>=3 <5.0.12": "5.0.12" - "fast-uri@>=3 <3.1.8": "3.1.8" - "flatted@<3.4.0": "3.4.2" - "minimatch@>=3 <3.1.5": "3.1.5" - "path-to-regexp@>=0.1 <0.1.13": "0.1.13" - "picomatch@>=2 <2.3.2": "2.3.2" - "picomatch@>=4 <4.0.4": "4.0.4" - "proxy-addr@>=1.1.0 <2.0.8": "2.0.8" - "qs@<6.16.0": "6.16.0" - "rollup@>=4 <4.59.0": "4.60.1" - "source-map-js@>=1 <1.2.2": "1.2.2" - "tar@<7.5.21": "7.5.21" - "vite@>=7.0.0 <=7.3.1": ">=7.3.2" - "yaml@>=2 <2.8.3": "2.8.3" + git clone https://github.com//.git + cd ``` - `allowBuilds` skips optional install scripts. Without it, pnpm stops every `pnpm run` command with an "Ignored build scripts" error. `overrides` sets security floors for transitive dependencies and matches the scaffolded `pnpm-lock.yaml`. - -4. Install the dependencies: +3. Install the dependencies: ```bash pnpm install ``` -5. Run the project: +4. Run the project: ```bash pnpm run dev ``` -6. Open the API docs at `http://localhost:3000/docs`. Press `Ctrl+C` in the terminal to stop the server when you're done. +5. Open the API docs at `http://localhost:3000/docs`. Press `Ctrl+C` in the terminal to stop the server when you're done. @@ -90,27 +57,18 @@ The template includes the following files and directories: -- package.json # Node.js project configuration and dependencies -- pnpm-lock.yaml # Locked versions of dependencies -- pnpm-workspace.yaml # pnpm settings (created in step 3) -- tsconfig.json # TypeScript configuration -- tspconfig.yaml # TypeSpec emitter configuration -- eslint.config.js # ESLint configuration -- .prettierrc # Prettier configuration -- .prettierignore # Files Prettier skips -- .gitignore # Files Git ignores -- README.md # Project documentation -- main.tsp # Empty file left by `cg init`, safe to delete - src/ - - api/ - - index.ts # Express application setup and config - - controllers/ # Request handlers and routers - - errors/ # API error types - - middleware/ # Error handling and validation middleware - - schemas/ # Zod schemas for (de)serialization - - services/ # Business logic and data operations - - typespec/ - - main.tsp # The TypeSpec specification for your API + - index.ts Node entrypoint — the only file that knows about Node + - app.ts `createApp({ repository })` builds the app + - http.ts Shared error shapes + - routes/ + - opportunities.ts Route definitions, handlers, request/response validation + - data/ + - schema.ts The one opportunity schema — extend custom fields here + - repository.ts The interface your data source implements: list / get / search + - fixtures.ts The sample-data implementation of that interface + - opportunities.json The bundled sample data +- test/ Vitest suites: fixtures, app, routes, OpenAPI From e17031af88af2ddc81e413df45da8146f2a1231e Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:23:42 -0700 Subject: [PATCH 3/7] docs(website): drop the spec check and TypeSpec steps from the TypeScript guide The Hono template has no TypeSpec spec and currently fails cg check spec. Restoring the check is tracked in #1239. --- .../content/docs/guides/using-typescript.mdx | 50 ------------------- 1 file changed, 50 deletions(-) diff --git a/website/src/content/docs/guides/using-typescript.mdx b/website/src/content/docs/guides/using-typescript.mdx index 8f1c51ec9..dfa6954a1 100644 --- a/website/src/content/docs/guides/using-typescript.mdx +++ b/website/src/content/docs/guides/using-typescript.mdx @@ -102,56 +102,6 @@ The SDK exposes its modules through `package.json` `exports`, which the template "moduleResolution": "NodeNext", ``` -### Update the spec to v0.5 - -Point the template's TypeSpec spec at protocol v0.5 so the API docs at `/docs` match the SDK's schemas. Replace `src/typespec/main.tsp` with: - -```tsp title=src/typespec/main.tsp -import "@typespec/http"; -import "@typespec/versioning"; -import "@common-grants/core"; - -using TypeSpec.Http; - -@service(#{ title: "CommonGrants API" }) -@Versioning.useDependency(CommonGrants.Versions.v0_5) -namespace Custom; - -@tag("Opportunities") -@route("/common-grants/opportunities") -namespace Routes { - alias OpportunitiesRouter = CommonGrants.Routes.Opportunities; - - op list is OpportunitiesRouter.list; - op read is OpportunitiesRouter.read; - op search is OpportunitiesRouter.search; -} -``` - -Besides the version, this moves `@Versioning.useDependency` onto the service namespace and nests the routes under it. The template's original layout applies the version incorrectly, so the read route comes out as `/common-grants/opportunities/{id}/{oppId}`. - -### Validate your spec - -Compile the spec and check it against the CommonGrants v0.5 base spec: - -```bash -pnpm run typespec -pnpm exec cg check spec tsp-output/@typespec/openapi3/openapi.Custom.yaml --protocol-version 0.5.0 -``` - -The check should report `Spec is compliant with base spec, with 3 warnings`. The warnings list the award routes, which are optional in v0.5 and not implemented by the template. Errors, such as a missing or extra route, fail the command with exit code 1. - -`cg check spec` validates the API specification, not the running server. To check the code, confirm it type-checks and runs: - -```bash -pnpm exec tsc --noEmit -pnpm run dev -``` - -Press `Ctrl+C` to stop the server. - -The docs now describe v0.5 models, but the API's responses still follow the template's own schemas until you swap in the SDK's, as described next. - ### Validate data with the SDK The template ships its own Zod schemas in `src/api/schemas/`. The SDK provides maintained equivalents for the opportunity and award models, filters, and responses, so you can replace the template's schemas as you build: From 9252bf472321f143782c87fb7d8f08fd1323d8ce Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:23:43 -0700 Subject: [PATCH 4/7] docs(website): correct the TypeScript guide's paths and services for the Hono template Point the schema, services, and plugin examples at the Hono template's files: the repository interface, src/data/schema.ts, and ESM import paths. --- .../content/docs/guides/using-typescript.mdx | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/website/src/content/docs/guides/using-typescript.mdx b/website/src/content/docs/guides/using-typescript.mdx index dfa6954a1..17b9d93a9 100644 --- a/website/src/content/docs/guides/using-typescript.mdx +++ b/website/src/content/docs/guides/using-typescript.mdx @@ -104,7 +104,9 @@ The SDK exposes its modules through `package.json` `exports`, which the template ### Validate data with the SDK -The template ships its own Zod schemas in `src/api/schemas/`. The SDK provides maintained equivalents for the opportunity and award models, filters, and responses, so you can replace the template's schemas as you build: +The template and the SDK split the work. The template owns HTTP: routing, request and response validation, error bodies, the OpenAPI document, and the API docs at `/docs`. The SDK owns the CommonGrants schemas: the template builds its opportunity model, filters, and response envelopes from them, so when you update the SDK, your API picks up that release's schema changes. + +Use the same schemas to validate records from your data source: ```typescript import { OpportunityBaseSchema } from "@common-grants/sdk/schemas"; @@ -115,19 +117,20 @@ import type { OpportunityBase } from "@common-grants/sdk/types"; const opportunity: OpportunityBase = OpportunityBaseSchema.parse(rawData); ``` -### Implementing services +### Connect your own data -The services layer in `src/api/services/` holds the business logic and data operations for the API. Focus on `opportunity.service.ts`, which implements the `OpportunitiesService` class behind these routes: +The template reads the bundled sample data through the `OpportunityRepository` interface in `src/data/repository.ts`. The interface has three methods, `list`, `get`, and `search`, behind these routes: - `GET /common-grants/opportunities` - `GET /common-grants/opportunities/{oppId}` - `POST /common-grants/opportunities/search` -Specific changes to make in this file: +To serve your own data: + +1. Implement `OpportunityRepository` against your database or upstream API, in place of the sample-data implementation, `fixtureRepository` in `src/data/fixtures.ts`. Parse each record with an SDK schema, or map it with a plugin's `toCommon`. +2. Pass your implementation to `createApp` in `src/index.ts`. -- Replace the mock data with a real data source, e.g. a database query or a remote API call, and parse each record with an SDK schema. -- Look opportunities up by `id` instead of title in `OpportunitiesService.getOpportunityByTitle`. -- Add the sorting and filtering logic to the `OpportunitiesService.searchOpportunities` method. +The routes, validation, and OpenAPI document keep working without changes. The template's [porting guide](https://github.com/common-grants/cg-api-template-hono/blob/main/PORTING.md) walks through these steps. ### Adding custom fields @@ -139,7 +142,7 @@ For example, let's say you need to add a `legacyId` field to map opportunities t Create a plugin that declares the custom field. The plugin compiles an `Opportunity` schema with a typed `customFields` property: -```typescript title=src/api/schemas/plugin.ts +```typescript title=src/data/plugin.ts import { z } from "zod"; import { definePlugin } from "@common-grants/sdk/extensions"; @@ -171,10 +174,10 @@ The `as const` is only required when the specs are assigned to a variable before #### Use the plugin's schema -Parse your data with the plugin's schema instead of the base schema, for example in a new file under `src/api/`. The custom field is validated and fully typed: +Parse your data with the plugin's schema instead of the base schema, for example in a new file under `src/`. The custom field is validated and fully typed: ```typescript -import { OpportunitySchema, type Opportunity } from "./schemas/plugin"; +import { OpportunitySchema, type Opportunity } from "./data/plugin.js"; const opportunity: Opportunity = OpportunitySchema.parse({ id: "573525f2-8e15-4405-83fb-e6523511d893", @@ -191,13 +194,14 @@ const opportunity: Opportunity = OpportunitySchema.parse({ opportunity.customFields?.legacyId?.value; // number ``` -The template's services still import their own `Opportunity` type from `src/api/schemas`. Replace those imports with the plugin's schema and type as you wire in real data, so the two don't collide. +To serve the custom field from the API, set `OpportunitySchema` in `src/data/schema.ts` to the plugin's schema. Response validation, the repository's types, and the OpenAPI document all pick it up. A plugin's custom filters don't come with it: the template's search route ignores them and reports them in `filterInfo.errors`. To support them, update the search route and your repository. For a one-off extension without a plugin, use `withCustomFields()` from the same module. See the [extensions guide](https://github.com/HHS/simpler-grants-protocol/blob/main/lib/ts-sdk/src/extensions/README.md) for custom filters, transforms, and publishing plugins. ## Additional resources - [CommonGrants TypeScript SDK](https://github.com/HHS/simpler-grants-protocol/tree/main/lib/ts-sdk#readme) +- [Hono API template](https://github.com/common-grants/cg-api-template-hono) - [TypeSpec](https://typespec.io/docs/) - [Zod](https://zod.dev/) -- [Express.js](https://expressjs.com/) +- [Hono](https://hono.dev/) From 72bf97da248947a82d1196cdd007b7c3faa9fd4e Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:23:43 -0700 Subject: [PATCH 5/7] docs(website): reframe the SDK section as upgrading from an earlier SDK The Hono template already ships SDK 0.9. Keep the module table and the 0.9 changes, and drop the pinned TypeSpec installs and tsconfig step that only applied to cg init projects. --- .../content/docs/guides/using-typescript.mdx | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/website/src/content/docs/guides/using-typescript.mdx b/website/src/content/docs/guides/using-typescript.mdx index 17b9d93a9..d72f753dd 100644 --- a/website/src/content/docs/guides/using-typescript.mdx +++ b/website/src/content/docs/guides/using-typescript.mdx @@ -74,17 +74,19 @@ The template includes the following files and directories: ## Next steps -Once the template is running, add the SDK and connect the API to your data. +Once the template is running, connect the API to your data. If your project started from an earlier version of the template, upgrade its SDK first. -### Add the SDK +### Upgrading from an earlier SDK -The [`@common-grants/sdk`](https://www.npmjs.com/package/@common-grants/sdk) package provides Zod schemas, TypeScript types, and an extension framework that match the current protocol version. Install it, the matching TypeSpec library, and the CommonGrants CLI: +The [`@common-grants/sdk`](https://www.npmjs.com/package/@common-grants/sdk) package provides Zod schemas, TypeScript types, and an extension framework that match the current protocol version. The template depends on SDK 0.9. If your project uses an earlier version, upgrade it and run the checks: ```bash -pnpm add --save-exact @common-grants/sdk@0.9.0 @common-grants/core@0.5.0 -pnpm add --save-exact -D @common-grants/cli@0.5.0 @typespec/compiler@1.17.0 @typespec/http@1.17.0 @typespec/openapi@1.17.0 @typespec/openapi3@1.17.0 @typespec/json-schema@1.17.0 @typespec/rest@0.87.0 @typespec/versioning@0.87.0 +pnpm add @common-grants/sdk@^0.9.0 +pnpm run ci ``` +SDK 0.9 updates the schemas to protocol v0.5 and is published as ESM only. The template is already an ES module project, so it needs no configuration changes. + The SDK is split into modules, each with its own import path: | Import path | Description | @@ -95,12 +97,7 @@ The SDK is split into modules, each with its own import path: | `@common-grants/sdk/extensions` | Custom field and plugin framework | | `@common-grants/sdk/client` | HTTP client with auth, pagination, and search | -The SDK exposes its modules through `package.json` `exports`, which the template's `"moduleResolution": "node"` setting ignores. In `tsconfig.json`, replace `"module": "CommonJS"` and `"moduleResolution": "node"` with: - -```json title=tsconfig.json - "module": "NodeNext", - "moduleResolution": "NodeNext", -``` +The SDK exposes these modules through `package.json` `exports` and has no root entrypoint, so import from the paths above. ### Validate data with the SDK From 03f2adad595cc7e4d5b26ca93aef2d48c1e152b6 Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:23:43 -0700 Subject: [PATCH 6/7] docs(website): retitle the TypeScript guide Using TypeScript --- website/src/content/docs/guides/using-typescript.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/content/docs/guides/using-typescript.mdx b/website/src/content/docs/guides/using-typescript.mdx index d72f753dd..9cccb9e1c 100644 --- a/website/src/content/docs/guides/using-typescript.mdx +++ b/website/src/content/docs/guides/using-typescript.mdx @@ -1,5 +1,5 @@ --- -title: Building a TypeScript API +title: Using TypeScript description: Learn how to implement a CommonGrants API in TypeScript. sidebar: order: 1 From 518dfb813a68b9e3471dbeb9cf5e55b89d638fb9 Mon Sep 17 00:00:00 2001 From: Bryan Thompson Date: Mon, 26 Oct 2026 11:53:18 -0700 Subject: [PATCH 7/7] docs(website): raise zod floor in SDK upgrade step and tidy guide Upgrade zod alongside the SDK and dedupe so the SDK no longer keeps zod 4.6.5 (GHSA-mrhx-c7xc-m3w5). List .nvmrc and PORTING.md in the template tree and drop the orphaned TypeSpec link. --- website/src/content/docs/guides/using-typescript.mdx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/website/src/content/docs/guides/using-typescript.mdx b/website/src/content/docs/guides/using-typescript.mdx index 9cccb9e1c..5e7303d4a 100644 --- a/website/src/content/docs/guides/using-typescript.mdx +++ b/website/src/content/docs/guides/using-typescript.mdx @@ -57,6 +57,8 @@ The template includes the following files and directories: +- .nvmrc Pins Node.js 24 +- PORTING.md How to connect your own data - src/ - index.ts Node entrypoint — the only file that knows about Node - app.ts `createApp({ repository })` builds the app @@ -78,10 +80,11 @@ Once the template is running, connect the API to your data. If your project star ### Upgrading from an earlier SDK -The [`@common-grants/sdk`](https://www.npmjs.com/package/@common-grants/sdk) package provides Zod schemas, TypeScript types, and an extension framework that match the current protocol version. The template depends on SDK 0.9. If your project uses an earlier version, upgrade it and run the checks: +The [`@common-grants/sdk`](https://www.npmjs.com/package/@common-grants/sdk) package provides Zod schemas, TypeScript types, and an extension framework that match the current protocol version. The template depends on SDK 0.9. If your project uses an earlier version, upgrade the SDK and Zod, deduplicate Zod so the SDK shares your project's copy, and run the checks: ```bash -pnpm add @common-grants/sdk@^0.9.0 +pnpm add @common-grants/sdk@^0.9.0 zod@^4.6.6 +pnpm dedupe pnpm run ci ``` @@ -199,6 +202,5 @@ For a one-off extension without a plugin, use `withCustomFields()` from the same - [CommonGrants TypeScript SDK](https://github.com/HHS/simpler-grants-protocol/tree/main/lib/ts-sdk#readme) - [Hono API template](https://github.com/common-grants/cg-api-template-hono) -- [TypeSpec](https://typespec.io/docs/) - [Zod](https://zod.dev/) - [Hono](https://hono.dev/)