@rtorcato/api-ts-rest-express 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Richard Torcato
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # @rtorcato/api-ts-rest-express
2
+
3
+ Mount a [ts-rest](https://ts-rest.com) contract on Express and serve its OpenAPI docs (Scalar) in one call — wires `@ts-rest/express` + `@ts-rest/open-api` into the api-common docs stack.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add @rtorcato/api-ts-rest-express @ts-rest/core @ts-rest/express @ts-rest/open-api express zod
9
+ ```
10
+
11
+ > **Version note:** requires `@ts-rest/*` **≥ 3.53.0-rc.1**, which is the first release supporting **zod 4** and **express 5** (stable 3.52 pins zod 3 / express 4). Bump the peer range to `^3.53` once 3.53 ships stable.
12
+
13
+ `@ts-rest/*`, `express`, and `zod` are peer dependencies.
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import express from 'express'
19
+ import { z } from 'zod'
20
+ import { initContract, initServer, mountTsRest } from '@rtorcato/api-ts-rest-express'
21
+
22
+ const c = initContract()
23
+ const contract = c.router({
24
+ getUser: {
25
+ method: 'GET',
26
+ path: '/users/:id',
27
+ pathParams: z.object({ id: z.string() }),
28
+ responses: { 200: z.object({ id: z.string(), name: z.string() }) },
29
+ },
30
+ })
31
+
32
+ const s = initServer()
33
+ const router = s.router(contract, {
34
+ getUser: async ({ params: { id } }) => ({ status: 200, body: { id, name: 'Ada' } }),
35
+ })
36
+
37
+ const app = express()
38
+ app.use(express.json())
39
+
40
+ mountTsRest(app, {
41
+ contract,
42
+ router,
43
+ openapi: { info: { title: 'Users API', version: '1.0.0' } },
44
+ })
45
+ // → contract routes are live
46
+ // → GET /openapi.json (OpenAPI 3.1 generated from the contract)
47
+ // → GET /docs (Scalar API reference UI)
48
+ ```
49
+
50
+ `initContract` / `initServer` are re-exported so you can build the contract and server from one import.
51
+
52
+ ## Options
53
+
54
+ `mountTsRest(app, options)`:
55
+
56
+ - `contract` / `router` — the ts-rest contract and its `s.router(...)` implementation.
57
+ - `endpointOptions?` — forwarded to `createExpressEndpoints` (`globalMiddleware`, `responseValidation`, …).
58
+ - `openapi?` — `{ info, options?, mount? }`. When present, generates an OpenAPI 3.1 doc from the contract and serves it via [`@rtorcato/api-openapi-express`](https://github.com/rtorcato/api-common/tree/main/packages/api-openapi-express). `options` goes to `generateOpenApi` (e.g. `{ setOperationId: true }`); `mount` overrides the docs paths/UI/theme.
59
+
60
+ ## Related
61
+
62
+ - [`@rtorcato/api-openapi-express`](https://github.com/rtorcato/api-common/tree/main/packages/api-openapi-express) — the Scalar/Swagger mount this builds on
63
+ - [`@rtorcato/api-openapi-hono`](https://github.com/rtorcato/api-common/tree/main/packages/api-openapi-hono) — schema-first OpenAPI for Hono
64
+
65
+ Source: https://github.com/rtorcato/api-common/tree/main/packages/api-ts-rest-express
@@ -0,0 +1,47 @@
1
+ import { type MountOpenAPIOptions } from '@rtorcato/api-openapi-express';
2
+ import type { AppRouter } from '@ts-rest/core';
3
+ import { createExpressEndpoints } from '@ts-rest/express';
4
+ import { generateOpenApi } from '@ts-rest/open-api';
5
+ import type { IRouter } from 'express';
6
+ type EndpointOptions = Parameters<typeof createExpressEndpoints>[3];
7
+ export interface OpenApiConfig {
8
+ info: {
9
+ title: string;
10
+ version: string;
11
+ description?: string;
12
+ };
13
+ /** Extra `generateOpenApi` options, e.g. `{ setOperationId: true }`. */
14
+ options?: Parameters<typeof generateOpenApi>[2];
15
+ /** Overrides for the docs mount (paths, UI, theme) — `doc` is supplied for you. */
16
+ mount?: Omit<MountOpenAPIOptions, 'doc'>;
17
+ }
18
+ export interface MountTsRestOptions {
19
+ contract: AppRouter;
20
+ /**
21
+ * The implemented router from `initServer().router(contract, handlers)` — that
22
+ * call already type-checks the handlers against the contract, so it's accepted
23
+ * loosely here.
24
+ */
25
+ router: unknown;
26
+ /** Passed to `createExpressEndpoints` (globalMiddleware, responseValidation, …). */
27
+ endpointOptions?: EndpointOptions;
28
+ /** Generate an OpenAPI 3.1 doc from the contract and serve it (Scalar by default). */
29
+ openapi?: OpenApiConfig;
30
+ }
31
+ /**
32
+ * Mount a ts-rest contract on an Express app/router and, optionally, serve its
33
+ * OpenAPI docs — one call wiring `@ts-rest/express` + `@ts-rest/open-api` into the
34
+ * api-common Scalar docs (`@rtorcato/api-openapi-express`).
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const s = initServer()
39
+ * const router = s.router(contract, { getUser: async ({ params }) => ({ status: 200, body: … }) })
40
+ * mountTsRest(app, { contract, router, openapi: { info: { title: 'My API', version: '1.0.0' } } })
41
+ * // → contract routes are live; GET /openapi.json + GET /docs (Scalar)
42
+ * ```
43
+ */
44
+ export declare function mountTsRest(app: IRouter, options: MountTsRestOptions): void;
45
+ export { initContract } from '@ts-rest/core';
46
+ export { initServer } from '@ts-rest/express';
47
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,mBAAmB,EAAE,MAAM,+BAA+B,CAAA;AACtF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAC9C,OAAO,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAKtC,KAAK,eAAe,GAAG,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC,CAAC,CAAC,CAAA;AAEnE,MAAM,WAAW,aAAa;IAC7B,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;IAC9D,wEAAwE;IACxE,OAAO,CAAC,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,CAAA;IAC/C,mFAAmF;IACnF,KAAK,CAAC,EAAE,IAAI,CAAC,mBAAmB,EAAE,KAAK,CAAC,CAAA;CACxC;AAED,MAAM,WAAW,kBAAkB;IAClC,QAAQ,EAAE,SAAS,CAAA;IACnB;;;;OAIG;IACH,MAAM,EAAE,OAAO,CAAA;IACf,oFAAoF;IACpF,eAAe,CAAC,EAAE,eAAe,CAAA;IACjC,sFAAsF;IACtF,OAAO,CAAC,EAAE,aAAa,CAAA;CACvB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,kBAAkB,GAAG,IAAI,CAoB3E;AAGD,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,31 @@
1
+ import { mountOpenAPI } from '@rtorcato/api-openapi-express';
2
+ import { createExpressEndpoints } from '@ts-rest/express';
3
+ import { generateOpenApi } from '@ts-rest/open-api';
4
+ /**
5
+ * Mount a ts-rest contract on an Express app/router and, optionally, serve its
6
+ * OpenAPI docs — one call wiring `@ts-rest/express` + `@ts-rest/open-api` into the
7
+ * api-common Scalar docs (`@rtorcato/api-openapi-express`).
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const s = initServer()
12
+ * const router = s.router(contract, { getUser: async ({ params }) => ({ status: 200, body: … }) })
13
+ * mountTsRest(app, { contract, router, openapi: { info: { title: 'My API', version: '1.0.0' } } })
14
+ * // → contract routes are live; GET /openapi.json + GET /docs (Scalar)
15
+ * ```
16
+ */
17
+ export function mountTsRest(app, options) {
18
+ createExpressEndpoints(options.contract, options.router, app, options.endpointOptions);
19
+ if (options.openapi) {
20
+ const doc = generateOpenApi(options.contract, { openapi: '3.1.0', info: options.openapi.info }, options.openapi.options);
21
+ // mountOpenAPI accepts any target with Express-style `.get(path, handler)`.
22
+ mountOpenAPI(app, {
23
+ doc,
24
+ ...options.openapi.mount,
25
+ });
26
+ }
27
+ }
28
+ // Re-export the ts-rest entry points so consumers build contracts + servers from one import.
29
+ export { initContract } from '@ts-rest/core';
30
+ export { initServer } from '@ts-rest/express';
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA4B,MAAM,+BAA+B,CAAA;AAEtF,OAAO,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AA8BnD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW,CAAC,GAAY,EAAE,OAA2B;IACpE,sBAAsB,CACrB,OAAO,CAAC,QAAQ,EAChB,OAAO,CAAC,MAAsB,EAC9B,GAAG,EACH,OAAO,CAAC,eAAe,CACvB,CAAA;IAED,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;QACrB,MAAM,GAAG,GAAG,eAAe,CAC1B,OAAO,CAAC,QAAQ,EAChB,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,EAChD,OAAO,CAAC,OAAO,CAAC,OAAO,CACvB,CAAA;QACD,4EAA4E;QAC5E,YAAY,CAAC,GAAoD,EAAE;YAClE,GAAG;YACH,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK;SACxB,CAAC,CAAA;IACH,CAAC;AACF,CAAC;AAED,6FAA6F;AAC7F,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA"}
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@rtorcato/api-ts-rest-express",
3
+ "version": "0.2.0",
4
+ "description": "Mount a ts-rest contract on Express and serve its OpenAPI docs (Scalar) — wires @ts-rest/express + @ts-rest/open-api into api-common.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "Richard Torcato",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "engines": {
20
+ "node": ">=22"
21
+ },
22
+ "dependencies": {
23
+ "@rtorcato/api-openapi-express": "0.2.0"
24
+ },
25
+ "peerDependencies": {
26
+ "@ts-rest/core": ">=3.53.0-rc.1 <4",
27
+ "@ts-rest/express": ">=3.53.0-rc.1 <4",
28
+ "@ts-rest/open-api": ">=3.53.0-rc.1 <4",
29
+ "express": "^5.0.0",
30
+ "zod": "^4.0.0"
31
+ },
32
+ "devDependencies": {
33
+ "@ts-rest/core": "3.53.0-rc.1",
34
+ "@ts-rest/express": "3.53.0-rc.1",
35
+ "@ts-rest/open-api": "3.53.0-rc.1",
36
+ "@types/express": "^5.0.0",
37
+ "@types/supertest": "^7.2.0",
38
+ "express": "^5.1.0",
39
+ "supertest": "^7.1.0",
40
+ "typescript": "~6.0.3",
41
+ "zod": "^4.4.3"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "keywords": [
47
+ "api",
48
+ "nodejs",
49
+ "typescript",
50
+ "express",
51
+ "ts-rest",
52
+ "openapi",
53
+ "contract"
54
+ ],
55
+ "repository": {
56
+ "type": "git",
57
+ "url": "git+https://github.com/rtorcato/api-common.git",
58
+ "directory": "packages/api-ts-rest-express"
59
+ },
60
+ "homepage": "https://github.com/rtorcato/api-common/tree/main/packages/api-ts-rest-express#readme",
61
+ "bugs": {
62
+ "url": "https://github.com/rtorcato/api-common/issues"
63
+ },
64
+ "scripts": {
65
+ "build": "tsc -p tsconfig.build.json",
66
+ "typecheck": "tsc --noEmit",
67
+ "test": "vitest run"
68
+ }
69
+ }