@projected/http 0.0.0-stage → 0.1.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/dist/.tsbuildinfo +1 -0
- package/dist/client.d.ts +89 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +176 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/node.d.ts +20 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +64 -0
- package/dist/node.js.map +1 -0
- package/dist/openapi.d.ts +43 -0
- package/dist/openapi.d.ts.map +1 -0
- package/dist/openapi.js +296 -0
- package/dist/openapi.js.map +1 -0
- package/dist/protocol.d.ts +68 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +98 -0
- package/dist/protocol.js.map +1 -0
- package/dist/server.d.ts +46 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +200 -0
- package/dist/server.js.map +1 -0
- package/package.json +41 -3
- package/src/client.ts +293 -0
- package/src/index.ts +32 -0
- package/src/node.ts +81 -0
- package/src/openapi.ts +344 -0
- package/src/protocol.ts +118 -0
- package/src/server.ts +263 -0
- package/README.md +0 -3
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { AnyContracts, Application, Authenticator, OperationResult } from "@projected/application";
|
|
2
|
+
import { type OpenApiOptions } from "./openapi.js";
|
|
3
|
+
import { type Problem } from "./protocol.js";
|
|
4
|
+
/** A web-standard request handler: runs on Node (via {@link serve}), Bun, Deno and edge runtimes. */
|
|
5
|
+
export type FetchHandler = (request: Request) => Promise<Response>;
|
|
6
|
+
export interface HttpHandlerOptions {
|
|
7
|
+
/** Mount point, e.g. `/api`. Defaults to the root. */
|
|
8
|
+
readonly basePath?: string;
|
|
9
|
+
/** Largest accepted batch. Defaults to 100. */
|
|
10
|
+
readonly maxBatchSize?: number;
|
|
11
|
+
/** Serve the OpenAPI document at `GET /openapi.json`. Defaults to true. */
|
|
12
|
+
readonly openApi?: boolean | OpenApiOptions;
|
|
13
|
+
/** Generates message ids for requests without an `X-Message-Id` header. */
|
|
14
|
+
readonly messageIds?: () => string;
|
|
15
|
+
/**
|
|
16
|
+
* Establishes the principal of each request (e.g. `betterAuthAuthenticator(auth)`). Without
|
|
17
|
+
* one, every request is anonymous and only `access: "public"` operations can run.
|
|
18
|
+
*/
|
|
19
|
+
readonly authenticator?: Authenticator;
|
|
20
|
+
/** `WWW-Authenticate` value sent with 401 responses. Defaults to `Bearer`. */
|
|
21
|
+
readonly authenticationChallenge?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Other handlers to serve under path prefixes, checked first — e.g. an auth provider's own
|
|
24
|
+
* sign-in routes: `{ "/api/auth": auth.handler }`.
|
|
25
|
+
*/
|
|
26
|
+
readonly routes?: Readonly<Record<string, FetchHandler>>;
|
|
27
|
+
}
|
|
28
|
+
/** One entry of a batch. A failed entry did not reach a business outcome. */
|
|
29
|
+
export type BatchItemResult = OperationResult | {
|
|
30
|
+
readonly status: "failed";
|
|
31
|
+
readonly problem: Problem;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Serve an application over HTTP. Every route is derived from its contracts:
|
|
35
|
+
*
|
|
36
|
+
* - `POST /commands/{type}/version/{version}` — run one command; the body is its input
|
|
37
|
+
* - `POST /queries/{type}/version/{version}` — run one query
|
|
38
|
+
* - `POST /commands`, `POST /queries` — run a batch: `{ "commands": [{ type, version, data }] }`
|
|
39
|
+
* - `GET /openapi.json` — the OpenAPI 3.1 document
|
|
40
|
+
* - `GET /health/live` (and `GET /health`) — liveness: the process answers
|
|
41
|
+
* - `GET /health/ready` — readiness: the application's {@link Application.health} report, 503
|
|
42
|
+
* while a dependency is down or the service is draining
|
|
43
|
+
* - `POST /operate/{task}` — run an operator task, for principals with `projected:operate`
|
|
44
|
+
*/
|
|
45
|
+
export declare function httpHandler<C extends AnyContracts>(app: Application<C>, options?: HttpHandlerOptions): FetchHandler;
|
|
46
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACX,aAAa,EAGb,eAAe,EAEhB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAmB,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EAA4F,KAAK,OAAO,EAAE,MAAM,eAAe,CAAC;AAEvI,qGAAqG;AACrG,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEnE,MAAM,WAAW,kBAAkB;IACjC,sDAAsD;IACtD,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,+CAA+C;IAC/C,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,2EAA2E;IAC3E,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,cAAc,CAAC;IAC5C,2EAA2E;IAC3E,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,MAAM,CAAC;IACnC;;;OAGG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,aAAa,CAAC;IACvC,8EAA8E;IAC9E,QAAQ,CAAC,uBAAuB,CAAC,EAAE,MAAM,CAAC;IAC1C;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;CAC1D;AAED,6EAA6E;AAC7E,MAAM,MAAM,eAAe,GAAG,eAAe,GAAG;IAAE,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;AAazG;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,OAAO,GAAE,kBAAuB,GAAG,YAAY,CAkMvH"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { openApiDocument } from "./openapi.js";
|
|
2
|
+
import { HEADERS, PLURAL, PROBLEM_TYPES, problem, problemForDispatchError, problemForOperateError } from "./protocol.js";
|
|
3
|
+
const json = (body, status, headers = {}) => new Response(JSON.stringify(body), {
|
|
4
|
+
status,
|
|
5
|
+
headers: { "content-type": status >= 400 ? "application/problem+json" : "application/json", ...headers },
|
|
6
|
+
});
|
|
7
|
+
const ROUTE = /^\/(commands|queries)\/([a-z0-9-]+)\/version\/([1-9][0-9]*)$/;
|
|
8
|
+
const BATCH = /^\/(commands|queries)$/;
|
|
9
|
+
const TASK = /^\/operate\/([a-z0-9-]+)$/;
|
|
10
|
+
const KIND = { commands: "command", queries: "query" };
|
|
11
|
+
/**
|
|
12
|
+
* Serve an application over HTTP. Every route is derived from its contracts:
|
|
13
|
+
*
|
|
14
|
+
* - `POST /commands/{type}/version/{version}` — run one command; the body is its input
|
|
15
|
+
* - `POST /queries/{type}/version/{version}` — run one query
|
|
16
|
+
* - `POST /commands`, `POST /queries` — run a batch: `{ "commands": [{ type, version, data }] }`
|
|
17
|
+
* - `GET /openapi.json` — the OpenAPI 3.1 document
|
|
18
|
+
* - `GET /health/live` (and `GET /health`) — liveness: the process answers
|
|
19
|
+
* - `GET /health/ready` — readiness: the application's {@link Application.health} report, 503
|
|
20
|
+
* while a dependency is down or the service is draining
|
|
21
|
+
* - `POST /operate/{task}` — run an operator task, for principals with `projected:operate`
|
|
22
|
+
*/
|
|
23
|
+
export function httpHandler(app, options = {}) {
|
|
24
|
+
const basePath = (options.basePath ?? "").replace(/\/+$/, "");
|
|
25
|
+
const maxBatchSize = options.maxBatchSize ?? 100;
|
|
26
|
+
const newId = options.messageIds ?? (() => crypto.randomUUID());
|
|
27
|
+
const openApiOptions = typeof options.openApi === "object" ? options.openApi : {};
|
|
28
|
+
const document = options.openApi === false ? undefined : openApiDocument(app.contracts, openApiOptions);
|
|
29
|
+
const telemetry = app.telemetry;
|
|
30
|
+
const mounts = Object.entries(options.routes ?? {})
|
|
31
|
+
.map(([prefix, handler]) => [prefix.replace(/\/+$/, ""), handler])
|
|
32
|
+
.sort(([a], [b]) => b.length - a.length);
|
|
33
|
+
const metadataFrom = (request, principal) => {
|
|
34
|
+
const header = (name) => request.headers.get(name) ?? undefined;
|
|
35
|
+
const messageId = header(HEADERS.messageId) ?? newId();
|
|
36
|
+
return {
|
|
37
|
+
messageId,
|
|
38
|
+
correlationId: header(HEADERS.correlationId) ?? messageId,
|
|
39
|
+
causationId: header(HEADERS.causationId),
|
|
40
|
+
idempotencyKey: header(HEADERS.idempotencyKey),
|
|
41
|
+
...(principal === undefined ? {} : { principal }),
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
const traceHeaders = (metadata) => ({
|
|
45
|
+
[HEADERS.messageId]: metadata.messageId,
|
|
46
|
+
[HEADERS.correlationId]: metadata.correlationId,
|
|
47
|
+
});
|
|
48
|
+
const readJson = async (request) => {
|
|
49
|
+
const contentType = request.headers.get("content-type") ?? "";
|
|
50
|
+
const text = await request.text();
|
|
51
|
+
if (text.trim() === "")
|
|
52
|
+
return { ok: true, body: {} };
|
|
53
|
+
if (!/^application\/([a-z.+-]*\+)?json\b/i.test(contentType)) {
|
|
54
|
+
return {
|
|
55
|
+
ok: false,
|
|
56
|
+
problem: problem(PROBLEM_TYPES.unsupportedMediaType, "Unsupported media type", 415, "Send application/json"),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
return { ok: true, body: JSON.parse(text) };
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return { ok: false, problem: problem(PROBLEM_TYPES.malformedRequest, "Malformed request", 400, "Body is not valid JSON") };
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const runOne = async (kind, type, version, input, metadata) => app.dispatch({ kind, type, version, input, metadata });
|
|
67
|
+
const challenge = options.authenticationChallenge ?? "Bearer";
|
|
68
|
+
const respondToProblem = (response, headers) => {
|
|
69
|
+
const extra = response.status === 503 ? { "retry-after": "1" } : response.status === 401 ? { "www-authenticate": challenge } : {};
|
|
70
|
+
return json(response, response.status, { ...headers, ...extra });
|
|
71
|
+
};
|
|
72
|
+
/** The request's principal, or the response when it cannot be established. */
|
|
73
|
+
const authenticate = async (request) => {
|
|
74
|
+
if (options.authenticator === undefined)
|
|
75
|
+
return { ok: true, principal: undefined };
|
|
76
|
+
const headers = Object.fromEntries([...request.headers].map(([name, value]) => [name.toLowerCase(), value]));
|
|
77
|
+
const authenticated = await options.authenticator.authenticate({ headers });
|
|
78
|
+
return authenticated.isErr()
|
|
79
|
+
? { ok: false, response: respondToProblem(problem(PROBLEM_TYPES.dependencyUnavailable, "Service temporarily unavailable", 503), {}) }
|
|
80
|
+
: { ok: true, principal: authenticated.value };
|
|
81
|
+
};
|
|
82
|
+
const handleTask = async (request, task) => {
|
|
83
|
+
const authenticated = await authenticate(request);
|
|
84
|
+
if (!authenticated.ok)
|
|
85
|
+
return authenticated.response;
|
|
86
|
+
const metadata = metadataFrom(request, authenticated.principal);
|
|
87
|
+
const body = await readJson(request);
|
|
88
|
+
if (!body.ok)
|
|
89
|
+
return json(body.problem, body.problem.status, traceHeaders(metadata));
|
|
90
|
+
const outcome = await app.operate(task, body.body, metadata);
|
|
91
|
+
return outcome.match({
|
|
92
|
+
ok: (value) => json({ status: "ok", value }, 200, traceHeaders(metadata)),
|
|
93
|
+
err: (error) => respondToProblem(problemForOperateError(error), traceHeaders(metadata)),
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
const handleOperation = async (request, single, batch) => {
|
|
97
|
+
const authenticated = await authenticate(request);
|
|
98
|
+
if (!authenticated.ok)
|
|
99
|
+
return authenticated.response;
|
|
100
|
+
const principal = authenticated.principal;
|
|
101
|
+
const metadata = metadataFrom(request, principal);
|
|
102
|
+
const body = await readJson(request);
|
|
103
|
+
if (!body.ok)
|
|
104
|
+
return json(body.problem, body.problem.status, traceHeaders(metadata));
|
|
105
|
+
if (single !== null) {
|
|
106
|
+
const [, plural = "", type = "", version = "0"] = single;
|
|
107
|
+
const outcome = await runOne(KIND[plural], type, Number(version), body.body, metadata);
|
|
108
|
+
return outcome.match({
|
|
109
|
+
ok: (result) => json(result, 200, traceHeaders(metadata)),
|
|
110
|
+
err: (error) => respondToProblem(problemForDispatchError(error), traceHeaders(metadata)),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
const [, plural = ""] = batch ?? [];
|
|
114
|
+
const kind = KIND[plural];
|
|
115
|
+
const items = body.body?.[PLURAL[kind]];
|
|
116
|
+
if (!Array.isArray(items)) {
|
|
117
|
+
return json(problem(PROBLEM_TYPES.malformedRequest, "Malformed request", 400, `Expected { "${PLURAL[kind]}": [...] }`), 400, traceHeaders(metadata));
|
|
118
|
+
}
|
|
119
|
+
if (items.length > maxBatchSize) {
|
|
120
|
+
return json(problem(PROBLEM_TYPES.payloadTooLarge, "Batch too large", 413, `At most ${maxBatchSize} items per batch`), 413, traceHeaders(metadata));
|
|
121
|
+
}
|
|
122
|
+
const results = [];
|
|
123
|
+
for (const [index, item] of items.entries()) {
|
|
124
|
+
const { type, version, data } = (item ?? {});
|
|
125
|
+
if (typeof type !== "string" || typeof version !== "number") {
|
|
126
|
+
results.push({
|
|
127
|
+
status: "failed",
|
|
128
|
+
problem: problem(PROBLEM_TYPES.malformedRequest, "Malformed batch item", 400, `Item ${index} needs type and version`),
|
|
129
|
+
});
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
// A batch's Idempotency-Key covers every item: retrying the batch replays each item's outcome.
|
|
133
|
+
const itemMetadata = {
|
|
134
|
+
...metadata,
|
|
135
|
+
messageId: `${metadata.messageId}/${index}`,
|
|
136
|
+
idempotencyKey: metadata.idempotencyKey === undefined ? undefined : `${metadata.idempotencyKey}/${index}`,
|
|
137
|
+
};
|
|
138
|
+
const outcome = await runOne(kind, type, version, data ?? {}, itemMetadata);
|
|
139
|
+
results.push(outcome.match({
|
|
140
|
+
ok: (result) => result,
|
|
141
|
+
err: (error) => ({ status: "failed", problem: problemForDispatchError(error) }),
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
return json({ results }, 200, traceHeaders(metadata));
|
|
145
|
+
};
|
|
146
|
+
return async (request) => {
|
|
147
|
+
const url = new URL(request.url);
|
|
148
|
+
for (const [prefix, handler] of mounts) {
|
|
149
|
+
if (url.pathname === prefix || url.pathname.startsWith(`${prefix}/`))
|
|
150
|
+
return handler(request);
|
|
151
|
+
}
|
|
152
|
+
if (basePath !== "" && url.pathname !== basePath && !url.pathname.startsWith(`${basePath}/`)) {
|
|
153
|
+
return json(problem(PROBLEM_TYPES.notFound, "Not found", 404), 404);
|
|
154
|
+
}
|
|
155
|
+
const path = url.pathname.slice(basePath.length) || "/";
|
|
156
|
+
if (path === "/health" || path === "/health/live") {
|
|
157
|
+
return request.method === "GET" ? json({ status: "ok" }, 200) : methodNotAllowed("GET");
|
|
158
|
+
}
|
|
159
|
+
if (path === "/health/ready") {
|
|
160
|
+
if (request.method !== "GET")
|
|
161
|
+
return methodNotAllowed("GET");
|
|
162
|
+
const report = await app.health();
|
|
163
|
+
// Not a problem document: probes and operators read the report either way.
|
|
164
|
+
return new Response(JSON.stringify(report), {
|
|
165
|
+
status: report.status === "ready" ? 200 : 503,
|
|
166
|
+
headers: { "content-type": "application/json", "cache-control": "no-store" },
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
if (path === "/openapi.json" && document !== undefined) {
|
|
170
|
+
return request.method === "GET" ? json(document, 200) : methodNotAllowed("GET");
|
|
171
|
+
}
|
|
172
|
+
const single = ROUTE.exec(path);
|
|
173
|
+
const batch = single === null ? BATCH.exec(path) : null;
|
|
174
|
+
const task = single === null && batch === null ? TASK.exec(path) : null;
|
|
175
|
+
if (single === null && batch === null && task === null)
|
|
176
|
+
return json(problem(PROBLEM_TYPES.notFound, "Not found", 404), 404);
|
|
177
|
+
if (request.method !== "POST")
|
|
178
|
+
return methodNotAllowed("POST");
|
|
179
|
+
const carrier = Object.fromEntries([...request.headers].map(([name, value]) => [name.toLowerCase(), value]));
|
|
180
|
+
const route = task !== null ? "/operate/{task}" : single === null ? `/${batch?.[1] ?? ""}` : `/${single[1] ?? ""}/{type}/version/{version}`;
|
|
181
|
+
return telemetry.continueFrom(carrier, () => telemetry.span(`POST ${route}`, { kind: "server", attributes: { "http.request.method": "POST", "http.route": route, "url.path": url.pathname } }, async (span) => {
|
|
182
|
+
const started = performance.now();
|
|
183
|
+
const response = task !== null ? await handleTask(request, task[1] ?? "") : await handleOperation(request, single, batch);
|
|
184
|
+
span.setAttributes({ "http.response.status_code": response.status });
|
|
185
|
+
if (response.status >= 500)
|
|
186
|
+
span.fail(`HTTP ${response.status}`);
|
|
187
|
+
// OpenTelemetry's semantic convention: seconds, by method, route and status.
|
|
188
|
+
telemetry.record("http.server.request.duration", (performance.now() - started) / 1000, {
|
|
189
|
+
"http.request.method": "POST",
|
|
190
|
+
"http.route": route,
|
|
191
|
+
"http.response.status_code": response.status,
|
|
192
|
+
});
|
|
193
|
+
return response;
|
|
194
|
+
}));
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function methodNotAllowed(allow) {
|
|
198
|
+
return json(problem(PROBLEM_TYPES.methodNotAllowed, "Method not allowed", 405), 405, { allow });
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,eAAe,EAAuB,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,OAA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Ae,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;YAC1H,IAAI,CAAC,aAAa,CAAC,EAAE,2BAA2B,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;YACrE,IAAI,QAAQ,CAAC,MAAM,IAAI,GAAG;gBAAE,IAAI,CAAC,IAAI,CAAC,QAAQ,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;YACjE,6EAA6E;YAC7E,SAAS,CAAC,MAAM,CAAC,8BAA8B,EAAE,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,GAAG,IAAI,EAAE;gBACrF,qBAAqB,EAAE,MAAM;gBAC7B,YAAY,EAAE,KAAK;gBACnB,2BAA2B,EAAE,QAAQ,CAAC,MAAM;aAC7C,CAAC,CAAC;YACH,OAAO,QAAQ,CAAC;QAClB,CAAC,CACF,CACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAa;IACrC,OAAO,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,gBAAgB,EAAE,oBAAoB,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;AAClG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@projected/http",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "HTTP adapters for projected: business-operation endpoints and OpenAPI derived from contracts, typed clients, and a Result-based HTTP client.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/martinzokov/projected.git",
|
|
9
|
+
"directory": "typescript/packages/http"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/martinzokov/projected#readme",
|
|
12
|
+
"bugs": "https://github.com/martinzokov/projected/issues",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./node": {
|
|
21
|
+
"types": "./dist/node.d.ts",
|
|
22
|
+
"default": "./dist/node.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"better-result": "^3.0.1",
|
|
31
|
+
"zod": "^4.6.7",
|
|
32
|
+
"@projected/application": "0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@seriousme/openapi-schema-validator": "^2.11.0",
|
|
36
|
+
"ajv": "^8.20.0",
|
|
37
|
+
"ajv-formats": "^3.0.1",
|
|
38
|
+
"@projected/store-memory": "0.1.0",
|
|
39
|
+
"@projected/testing": "0.1.0"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
}
|
|
6
44
|
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DependencyFailure,
|
|
3
|
+
DependencyUnavailable,
|
|
4
|
+
Result,
|
|
5
|
+
TaggedError,
|
|
6
|
+
decodeWireError,
|
|
7
|
+
propagate,
|
|
8
|
+
type AnyContracts,
|
|
9
|
+
type AnyOperationContract,
|
|
10
|
+
type ErrorOf,
|
|
11
|
+
type ExecutionContext,
|
|
12
|
+
type InfrastructureError,
|
|
13
|
+
type OutputOf,
|
|
14
|
+
type RemoteService,
|
|
15
|
+
type Telemetry,
|
|
16
|
+
} from "@projected/application";
|
|
17
|
+
import type { z } from "zod";
|
|
18
|
+
import { HEADERS, operationPath } from "./protocol.js";
|
|
19
|
+
|
|
20
|
+
type Fetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
21
|
+
|
|
22
|
+
export interface HttpServiceOptions {
|
|
23
|
+
/** Base URL of the remote service, including any base path: `https://school.internal/api`. */
|
|
24
|
+
readonly baseUrl: string;
|
|
25
|
+
/** Defaults to the global `fetch`. Pass a handler-backed function to test without a network. */
|
|
26
|
+
readonly fetch?: Fetch;
|
|
27
|
+
/** Extra headers for every request, e.g. authorization. */
|
|
28
|
+
readonly headers?: () => Record<string, string>;
|
|
29
|
+
/** Per-request timeout. Defaults to 10 seconds. */
|
|
30
|
+
readonly timeoutMs?: number;
|
|
31
|
+
/** Records a client span per call and sends the trace context (`traceparent`) along. */
|
|
32
|
+
readonly telemetry?: Telemetry;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A {@link RemoteService} over HTTP: call another projected service's commands and queries with
|
|
37
|
+
* types taken from its contracts. Business errors come back as instances of the contract's error
|
|
38
|
+
* classes; every transport problem becomes an infrastructure error.
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* const school = httpService(schoolContracts, { baseUrl: "http://school:3000" });
|
|
42
|
+
* const enlisted = await school.execute(EnlistStudent, { firstname: "Bruce", ... }, context);
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function httpService<C extends AnyContracts>(contracts: C, options: HttpServiceOptions): RemoteService<C> {
|
|
46
|
+
const dependency = contracts.name;
|
|
47
|
+
const doFetch: Fetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
48
|
+
const baseUrl = options.baseUrl.replace(/\/+$/, "");
|
|
49
|
+
const timeoutMs = options.timeoutMs ?? 10_000;
|
|
50
|
+
|
|
51
|
+
const unavailable = (message: string, cause?: unknown) =>
|
|
52
|
+
new DependencyUnavailable({ dependency, message: `${dependency}: ${message}`, cause });
|
|
53
|
+
const failure = (message: string, cause?: unknown) =>
|
|
54
|
+
new DependencyFailure({ dependency, message: `${dependency}: ${message}`, cause });
|
|
55
|
+
|
|
56
|
+
const execute = <Op extends AnyOperationContract>(
|
|
57
|
+
contract: Op,
|
|
58
|
+
input: unknown,
|
|
59
|
+
context?: ExecutionContext,
|
|
60
|
+
): Promise<Result<OutputOf<Op>, ErrorOf<Op> | InfrastructureError>> => {
|
|
61
|
+
const telemetry = options.telemetry;
|
|
62
|
+
if (telemetry === undefined) return send(contract, input, context, {});
|
|
63
|
+
return telemetry.span(
|
|
64
|
+
`${contract.kind} ${dependency}/${contract.key}`,
|
|
65
|
+
{ kind: "client", attributes: { "projected.remote_service": dependency, "projected.operation.type": contract.type } },
|
|
66
|
+
async (span) => {
|
|
67
|
+
const carrier: Record<string, string> = {};
|
|
68
|
+
telemetry.inject(carrier);
|
|
69
|
+
const outcome = await send(contract, input, context, carrier);
|
|
70
|
+
if (outcome.isErr() && (DependencyFailure.is(outcome.error) || DependencyUnavailable.is(outcome.error))) {
|
|
71
|
+
span.fail(outcome.error.message);
|
|
72
|
+
}
|
|
73
|
+
return outcome;
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const send = async <Op extends AnyOperationContract>(
|
|
79
|
+
contract: Op,
|
|
80
|
+
input: unknown,
|
|
81
|
+
context: ExecutionContext | undefined,
|
|
82
|
+
trace: Record<string, string>,
|
|
83
|
+
): Promise<Result<OutputOf<Op>, ErrorOf<Op> | InfrastructureError>> => {
|
|
84
|
+
const metadata = propagate(context);
|
|
85
|
+
const headers: Record<string, string> = {
|
|
86
|
+
"content-type": "application/json",
|
|
87
|
+
accept: "application/json",
|
|
88
|
+
...options.headers?.(),
|
|
89
|
+
...trace,
|
|
90
|
+
...(metadata.correlationId === undefined ? {} : { [HEADERS.correlationId]: metadata.correlationId }),
|
|
91
|
+
...(metadata.causationId === undefined ? {} : { [HEADERS.causationId]: metadata.causationId }),
|
|
92
|
+
};
|
|
93
|
+
const sent = await Result.tryPromise({
|
|
94
|
+
try: () =>
|
|
95
|
+
doFetch(`${baseUrl}${operationPath(contract.kind, contract.type, contract.version)}`, {
|
|
96
|
+
method: "POST",
|
|
97
|
+
headers,
|
|
98
|
+
body: JSON.stringify(input),
|
|
99
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
100
|
+
}),
|
|
101
|
+
catch: (cause) => unavailable("request failed", cause),
|
|
102
|
+
});
|
|
103
|
+
if (sent.isErr()) return Result.err(sent.error);
|
|
104
|
+
const response = sent.value;
|
|
105
|
+
|
|
106
|
+
if (response.status !== 200) {
|
|
107
|
+
// 409 means the remote raced another change or is still running this request; 429/503/504
|
|
108
|
+
// mean "later". All retryable. Everything else (401/403 included) needs a fix on our side.
|
|
109
|
+
if ([409, 429, 503, 504].includes(response.status)) return Result.err(unavailable(`HTTP ${response.status}`));
|
|
110
|
+
return Result.err(failure(`HTTP ${response.status}: ${await response.text().catch(() => "")}`));
|
|
111
|
+
}
|
|
112
|
+
const body = await Result.tryPromise({
|
|
113
|
+
try: () => response.json() as Promise<unknown>,
|
|
114
|
+
catch: (cause) => failure("response is not JSON", cause),
|
|
115
|
+
});
|
|
116
|
+
if (body.isErr()) return Result.err(body.error);
|
|
117
|
+
const result = body.value as { status?: unknown; value?: unknown; error?: unknown };
|
|
118
|
+
|
|
119
|
+
if (result.status === "ok") {
|
|
120
|
+
const output = contract.output.safeParse(result.value);
|
|
121
|
+
return output.success
|
|
122
|
+
? Result.ok(output.data as OutputOf<Op>)
|
|
123
|
+
: Result.err(failure("output does not match the contract", output.error));
|
|
124
|
+
}
|
|
125
|
+
if (result.status === "error") {
|
|
126
|
+
const error = decodeWireError(contract, result.error);
|
|
127
|
+
return error === undefined ? Result.err(failure("returned an undeclared error", result.error)) : Result.err(error);
|
|
128
|
+
}
|
|
129
|
+
return Result.err(failure("response is not an operation result"));
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
return { contracts, execute } as RemoteService<C>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A third-party API answered with a status the caller asked to treat as data. */
|
|
136
|
+
export class HttpStatusError extends TaggedError("HttpStatusError")<{
|
|
137
|
+
status: number;
|
|
138
|
+
body: unknown;
|
|
139
|
+
message: string;
|
|
140
|
+
}> {}
|
|
141
|
+
|
|
142
|
+
/** Statuses a request asks to receive as {@link HttpStatusError}, or `undefined` for none. */
|
|
143
|
+
export type ExpectedStatuses = readonly number[] | undefined;
|
|
144
|
+
|
|
145
|
+
export interface HttpRequest<S extends z.ZodType, X extends ExpectedStatuses = ExpectedStatuses> {
|
|
146
|
+
readonly method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
147
|
+
/** Path relative to the client's base URL. */
|
|
148
|
+
readonly path: string;
|
|
149
|
+
readonly query?: Readonly<Record<string, string | number | boolean | undefined>>;
|
|
150
|
+
/** Serialized as JSON. */
|
|
151
|
+
readonly body?: unknown;
|
|
152
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
153
|
+
/** Schema the success body must match. Mismatches are a {@link DependencyFailure}. */
|
|
154
|
+
readonly response: S;
|
|
155
|
+
/** Statuses returned as {@link HttpStatusError} instead of a failure — e.g. `[404]` to model "not found". */
|
|
156
|
+
readonly expectedErrors?: X | undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* What a request can fail with. {@link HttpStatusError} is only possible — and only in the type —
|
|
161
|
+
* when the request lists `expectedErrors`, so a plain call composes as
|
|
162
|
+
* `Result<T, DependencyUnavailable | DependencyFailure>` with nothing to map away.
|
|
163
|
+
*/
|
|
164
|
+
export type HttpRequestError<X extends ExpectedStatuses> =
|
|
165
|
+
| (X extends readonly number[] ? HttpStatusError : never)
|
|
166
|
+
| DependencyUnavailable
|
|
167
|
+
| DependencyFailure;
|
|
168
|
+
|
|
169
|
+
export interface HttpResponse<T> {
|
|
170
|
+
readonly status: number;
|
|
171
|
+
readonly headers: Headers;
|
|
172
|
+
readonly body: T;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Result-based HTTP client for driven adapters that wrap third-party APIs. */
|
|
176
|
+
export interface HttpClient {
|
|
177
|
+
request<S extends z.ZodType, const X extends ExpectedStatuses = undefined>(
|
|
178
|
+
request: HttpRequest<S, X>,
|
|
179
|
+
): Promise<Result<HttpResponse<z.output<S>>, HttpRequestError<X>>>;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface HttpClientOptions {
|
|
183
|
+
/** Name used in errors and logs: `"payments"`. */
|
|
184
|
+
readonly dependency: string;
|
|
185
|
+
readonly baseUrl: string;
|
|
186
|
+
readonly fetch?: Fetch;
|
|
187
|
+
readonly headers?: () => Record<string, string>;
|
|
188
|
+
readonly timeoutMs?: number;
|
|
189
|
+
/** Records a client span per request and sends the trace context along. */
|
|
190
|
+
readonly telemetry?: Telemetry;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Create an {@link HttpClient}. Use it inside a driven adapter that implements one of your own
|
|
195
|
+
* ports, so the application never sees HTTP:
|
|
196
|
+
*
|
|
197
|
+
* ```ts
|
|
198
|
+
* export const stripePayments = (http: HttpClient): PaymentGateway => ({
|
|
199
|
+
* charge: async (amount) =>
|
|
200
|
+
* (await http.request({ method: "POST", path: "/charges", body: amount, response: Charge }))
|
|
201
|
+
* .map(({ body }) => ({ chargeId: body.id })),
|
|
202
|
+
* });
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
export function httpClient(options: HttpClientOptions): HttpClient {
|
|
206
|
+
const doFetch: Fetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
207
|
+
const baseUrl = options.baseUrl.replace(/\/+$/, "");
|
|
208
|
+
const timeoutMs = options.timeoutMs ?? 10_000;
|
|
209
|
+
const { dependency } = options;
|
|
210
|
+
|
|
211
|
+
type AnyFailure = HttpStatusError | DependencyUnavailable | DependencyFailure;
|
|
212
|
+
const traced = <S extends z.ZodType>(request: HttpRequest<S>): Promise<Result<HttpResponse<z.output<S>>, AnyFailure>> => {
|
|
213
|
+
const telemetry = options.telemetry;
|
|
214
|
+
if (telemetry === undefined) return send(request, {});
|
|
215
|
+
return telemetry.span(
|
|
216
|
+
`${request.method} ${dependency}`,
|
|
217
|
+
{ kind: "client", attributes: { "http.request.method": request.method, "projected.dependency": dependency } },
|
|
218
|
+
async (span) => {
|
|
219
|
+
const carrier: Record<string, string> = {};
|
|
220
|
+
telemetry.inject(carrier);
|
|
221
|
+
const outcome = await send(request, carrier);
|
|
222
|
+
if (outcome.isOk()) span.setAttributes({ "http.response.status_code": outcome.value.status });
|
|
223
|
+
else if (!HttpStatusError.is(outcome.error)) span.fail(outcome.error.message);
|
|
224
|
+
return outcome;
|
|
225
|
+
},
|
|
226
|
+
);
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const send = async <S extends z.ZodType>(
|
|
230
|
+
request: HttpRequest<S>,
|
|
231
|
+
trace: Record<string, string>,
|
|
232
|
+
): Promise<Result<HttpResponse<z.output<S>>, HttpStatusError | DependencyUnavailable | DependencyFailure>> => {
|
|
233
|
+
{
|
|
234
|
+
const url = new URL(`${baseUrl}/${request.path.replace(/^\/+/, "")}`);
|
|
235
|
+
for (const [key, value] of Object.entries(request.query ?? {})) {
|
|
236
|
+
if (value !== undefined) url.searchParams.set(key, String(value));
|
|
237
|
+
}
|
|
238
|
+
const sent = await Result.tryPromise({
|
|
239
|
+
try: () =>
|
|
240
|
+
doFetch(url, {
|
|
241
|
+
method: request.method,
|
|
242
|
+
headers: {
|
|
243
|
+
accept: "application/json",
|
|
244
|
+
...(request.body === undefined ? {} : { "content-type": "application/json" }),
|
|
245
|
+
...options.headers?.(),
|
|
246
|
+
...trace,
|
|
247
|
+
...request.headers,
|
|
248
|
+
},
|
|
249
|
+
...(request.body === undefined ? {} : { body: JSON.stringify(request.body) }),
|
|
250
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
251
|
+
}),
|
|
252
|
+
catch: (cause) => new DependencyUnavailable({ dependency, message: `${dependency}: request failed`, cause }),
|
|
253
|
+
});
|
|
254
|
+
if (sent.isErr()) return Result.err(sent.error);
|
|
255
|
+
const response = sent.value;
|
|
256
|
+
const text = await response.text().catch(() => "");
|
|
257
|
+
const parsed = (() => {
|
|
258
|
+
if (text === "") return undefined;
|
|
259
|
+
try {
|
|
260
|
+
return JSON.parse(text) as unknown;
|
|
261
|
+
} catch {
|
|
262
|
+
return text;
|
|
263
|
+
}
|
|
264
|
+
})();
|
|
265
|
+
|
|
266
|
+
if (!response.ok) {
|
|
267
|
+
if (request.expectedErrors?.includes(response.status)) {
|
|
268
|
+
return Result.err(
|
|
269
|
+
new HttpStatusError({ status: response.status, body: parsed, message: `${dependency}: HTTP ${response.status}` }),
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
const retryable = [408, 429, 502, 503, 504].includes(response.status);
|
|
273
|
+
const message = `${dependency}: HTTP ${response.status}`;
|
|
274
|
+
return Result.err(
|
|
275
|
+
retryable
|
|
276
|
+
? new DependencyUnavailable({ dependency, message })
|
|
277
|
+
: new DependencyFailure({ dependency, message, cause: parsed }),
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
const body = request.response.safeParse(parsed);
|
|
281
|
+
if (!body.success) {
|
|
282
|
+
return Result.err(
|
|
283
|
+
new DependencyFailure({ dependency, message: `${dependency}: unexpected response body`, cause: body.error }),
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
return Result.ok({ status: response.status, headers: response.headers, body: body.data });
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// `send` only produces HttpStatusError for statuses in `expectedErrors`, which is what
|
|
291
|
+
// HttpRequestError<X> says; the compiler cannot follow that through the runtime check.
|
|
292
|
+
return { request: traced as HttpClient["request"] };
|
|
293
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @projected/http — the HTTP driving and driven adapters. Import it only from
|
|
3
|
+
* `src/infrastructure/**`: the architecture rules reject it anywhere else.
|
|
4
|
+
*
|
|
5
|
+
* Everything here is web-standard (Request, Response, fetch) and runs on Node, Bun, Deno and
|
|
6
|
+
* edge runtimes. The Node server lives in `@projected/http/node`.
|
|
7
|
+
*/
|
|
8
|
+
export { httpHandler, type BatchItemResult, type FetchHandler, type HttpHandlerOptions } from "./server.js";
|
|
9
|
+
export { openApiDocument, type OpenApiDocument, type OpenApiOptions } from "./openapi.js";
|
|
10
|
+
export {
|
|
11
|
+
HttpStatusError,
|
|
12
|
+
httpClient,
|
|
13
|
+
httpService,
|
|
14
|
+
type ExpectedStatuses,
|
|
15
|
+
type HttpClient,
|
|
16
|
+
type HttpClientOptions,
|
|
17
|
+
type HttpRequest,
|
|
18
|
+
type HttpRequestError,
|
|
19
|
+
type HttpResponse,
|
|
20
|
+
type HttpServiceOptions,
|
|
21
|
+
} from "./client.js";
|
|
22
|
+
export {
|
|
23
|
+
CORS_HEADERS,
|
|
24
|
+
HEADERS,
|
|
25
|
+
PROBLEM_TYPES,
|
|
26
|
+
operationId,
|
|
27
|
+
operationPath,
|
|
28
|
+
payloadTooLarge,
|
|
29
|
+
problemForDispatchError,
|
|
30
|
+
problemForOperateError,
|
|
31
|
+
type Problem,
|
|
32
|
+
} from "./protocol.js";
|