@contractspec/lib.contracts-runtime-server-rest 3.0.0 → 3.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 +21 -0
- package/README.md +164 -1
- package/package.json +6 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Chaman Ventures, SASU
|
|
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
CHANGED
|
@@ -1,3 +1,166 @@
|
|
|
1
1
|
# @contractspec/lib.contracts-runtime-server-rest
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
REST runtime adapters for exposing ContractSpec operations over HTTP.
|
|
4
|
+
|
|
5
|
+
Website: https://contractspec.io/
|
|
6
|
+
|
|
7
|
+
## Why this package exists
|
|
8
|
+
|
|
9
|
+
This package is the REST adapter layer extracted from `@contractspec/lib.contracts`.
|
|
10
|
+
|
|
11
|
+
It lets you take an `OperationSpecRegistry` and expose it in multiple server environments with consistent behavior:
|
|
12
|
+
|
|
13
|
+
- framework-agnostic fetch handler
|
|
14
|
+
- Express adapter
|
|
15
|
+
- Elysia adapter
|
|
16
|
+
- Next.js App Router adapter
|
|
17
|
+
- Next.js Pages Router adapter
|
|
18
|
+
|
|
19
|
+
## Package boundary (important)
|
|
20
|
+
|
|
21
|
+
Use this package for:
|
|
22
|
+
|
|
23
|
+
- HTTP transport projection of operation specs.
|
|
24
|
+
- Shared REST concerns (route derivation, input parsing, CORS, error mapping).
|
|
25
|
+
- Helper utilities reused by GraphQL runtime (`contracts-adapter-input`, `contracts-adapter-hydration`).
|
|
26
|
+
|
|
27
|
+
Do not use this package for:
|
|
28
|
+
|
|
29
|
+
- Defining operation contracts (use `@contractspec/lib.contracts-spec`).
|
|
30
|
+
- Building GraphQL schema (use `@contractspec/lib.contracts-runtime-server-graphql`).
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install @contractspec/lib.contracts-runtime-server-rest @contractspec/lib.contracts-spec
|
|
36
|
+
# or
|
|
37
|
+
bun add @contractspec/lib.contracts-runtime-server-rest @contractspec/lib.contracts-spec
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Install whichever peer framework you use (`express`, `elysia`, `next`).
|
|
41
|
+
|
|
42
|
+
## Export map
|
|
43
|
+
|
|
44
|
+
- Generic handler:
|
|
45
|
+
- `createFetchHandler`
|
|
46
|
+
- `RestOptions`
|
|
47
|
+
- Framework adapters:
|
|
48
|
+
- `expressRouter`
|
|
49
|
+
- `elysiaPlugin`
|
|
50
|
+
- `makeNextAppHandler`
|
|
51
|
+
- `makeNextPagesHandler`
|
|
52
|
+
- Shared adapter internals:
|
|
53
|
+
- `createInputTypeBuilder`
|
|
54
|
+
- `hydrateResourceIfNeeded`
|
|
55
|
+
- `parseReturns`
|
|
56
|
+
|
|
57
|
+
## Quick start (framework-agnostic)
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { createFetchHandler } from "@contractspec/lib.contracts-runtime-server-rest";
|
|
61
|
+
import type { HandlerCtx } from "@contractspec/lib.contracts-spec";
|
|
62
|
+
import type { OperationSpecRegistry } from "@contractspec/lib.contracts-spec/operations/registry";
|
|
63
|
+
|
|
64
|
+
declare const operations: OperationSpecRegistry;
|
|
65
|
+
|
|
66
|
+
const handler = createFetchHandler(
|
|
67
|
+
operations,
|
|
68
|
+
(request): HandlerCtx => ({
|
|
69
|
+
actor: "user",
|
|
70
|
+
channel: "web",
|
|
71
|
+
traceId: request.headers.get("x-trace-id") ?? undefined,
|
|
72
|
+
}),
|
|
73
|
+
{
|
|
74
|
+
basePath: "/api/contracts",
|
|
75
|
+
cors: true,
|
|
76
|
+
prettyJson: 2,
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const response = await handler(
|
|
81
|
+
new Request("https://example.com/api/contracts/workspace/get/v1.0.0")
|
|
82
|
+
);
|
|
83
|
+
console.log(response.status);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Framework examples
|
|
87
|
+
|
|
88
|
+
### Express
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import express from "express";
|
|
92
|
+
import { expressRouter } from "@contractspec/lib.contracts-runtime-server-rest/rest-express";
|
|
93
|
+
|
|
94
|
+
declare const operations: import("@contractspec/lib.contracts-spec/operations/registry").OperationSpecRegistry;
|
|
95
|
+
|
|
96
|
+
const app = express();
|
|
97
|
+
app.use(express.json());
|
|
98
|
+
|
|
99
|
+
app.use(
|
|
100
|
+
expressRouter(
|
|
101
|
+
express,
|
|
102
|
+
operations,
|
|
103
|
+
() => ({ actor: "user", channel: "web" }),
|
|
104
|
+
{ basePath: "/api/contracts", cors: true }
|
|
105
|
+
)
|
|
106
|
+
);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Next.js App Router
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import { makeNextAppHandler } from "@contractspec/lib.contracts-runtime-server-rest/rest-next-app";
|
|
113
|
+
|
|
114
|
+
declare const operations: import("@contractspec/lib.contracts-spec/operations/registry").OperationSpecRegistry;
|
|
115
|
+
|
|
116
|
+
const handler = makeNextAppHandler(
|
|
117
|
+
operations,
|
|
118
|
+
() => ({ actor: "user", channel: "web" }),
|
|
119
|
+
{ basePath: "/api/contracts" }
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
export const GET = handler;
|
|
123
|
+
export const POST = handler;
|
|
124
|
+
export const OPTIONS = handler;
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Runtime behavior details
|
|
128
|
+
|
|
129
|
+
- Default method mapping:
|
|
130
|
+
- query -> `GET`
|
|
131
|
+
- command -> `POST`
|
|
132
|
+
- Default path mapping:
|
|
133
|
+
- `/<operation.key with dots replaced by slashes>/v<version>`
|
|
134
|
+
- GET input parsing:
|
|
135
|
+
- if `input` query param exists, parse it as JSON
|
|
136
|
+
- otherwise use query params as flat object
|
|
137
|
+
- POST input parsing:
|
|
138
|
+
- supports `application/json` and `application/x-www-form-urlencoded`
|
|
139
|
+
- Error mapping defaults:
|
|
140
|
+
- validation errors -> `400`
|
|
141
|
+
- `PolicyDenied*` errors -> `403`
|
|
142
|
+
- unknown errors -> `500`
|
|
143
|
+
- unsupported content type -> `415`
|
|
144
|
+
|
|
145
|
+
You can override error serialization using `RestOptions.onError`.
|
|
146
|
+
|
|
147
|
+
## AI assistant guidance
|
|
148
|
+
|
|
149
|
+
When generating code:
|
|
150
|
+
|
|
151
|
+
- Define operation specs first in `@contractspec/lib.contracts-spec`.
|
|
152
|
+
- Start with `createFetchHandler` for deterministic behavior, then choose framework wrappers.
|
|
153
|
+
- Keep `basePath` explicit to avoid route ambiguity in generated handlers.
|
|
154
|
+
|
|
155
|
+
When debugging:
|
|
156
|
+
|
|
157
|
+
- If a route is 404, verify operation key/version and derived path.
|
|
158
|
+
- If input parsing fails on GET, check whether caller sends `input=` JSON vs plain query params.
|
|
159
|
+
|
|
160
|
+
## Split migration from deprecated monolith
|
|
161
|
+
|
|
162
|
+
- `@contractspec/lib.contracts/server/rest-generic` -> `@contractspec/lib.contracts-runtime-server-rest/rest-generic`
|
|
163
|
+
- `@contractspec/lib.contracts/server/rest-express` -> `@contractspec/lib.contracts-runtime-server-rest/rest-express`
|
|
164
|
+
- `@contractspec/lib.contracts/server/rest-elysia` -> `@contractspec/lib.contracts-runtime-server-rest/rest-elysia`
|
|
165
|
+
- `@contractspec/lib.contracts/server/rest-next-app` -> `@contractspec/lib.contracts-runtime-server-rest/rest-next-app`
|
|
166
|
+
- `@contractspec/lib.contracts/server/rest-next-pages` -> `@contractspec/lib.contracts-runtime-server-rest/rest-next-pages`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contractspec/lib.contracts-runtime-server-rest",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "REST server runtime adapters for ContractSpec contracts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -19,19 +19,19 @@
|
|
|
19
19
|
"dev": "contractspec-bun-build dev"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@contractspec/lib.contracts-spec": "3.
|
|
23
|
-
"@contractspec/lib.schema": "3.
|
|
22
|
+
"@contractspec/lib.contracts-spec": "3.2.0",
|
|
23
|
+
"@contractspec/lib.schema": "3.2.0"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"@pothos/core": "^4.9.1",
|
|
27
|
-
"elysia": "^1.4.
|
|
27
|
+
"elysia": "^1.4.27",
|
|
28
28
|
"express": "^5.2.1",
|
|
29
29
|
"next": "16.1.6"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@contractspec/tool.typescript": "3.
|
|
32
|
+
"@contractspec/tool.typescript": "3.2.0",
|
|
33
33
|
"typescript": "^5.9.3",
|
|
34
|
-
"@contractspec/tool.bun": "3.
|
|
34
|
+
"@contractspec/tool.bun": "3.2.0"
|
|
35
35
|
},
|
|
36
36
|
"files": [
|
|
37
37
|
"dist",
|