@lodestar/api 1.20.0-dev.f6d3bce802 → 1.20.0-dev.ff253a7e4b
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/README.md +1 -1
- package/lib/beacon/client/beacon.d.ts +4 -3
- package/lib/beacon/client/beacon.js +3 -36
- package/lib/beacon/client/beacon.js.map +1 -1
- package/lib/beacon/client/config.d.ts +4 -3
- package/lib/beacon/client/config.js +3 -6
- package/lib/beacon/client/config.js.map +1 -1
- package/lib/beacon/client/debug.d.ts +4 -3
- package/lib/beacon/client/debug.js +4 -45
- package/lib/beacon/client/debug.js.map +1 -1
- package/lib/beacon/client/events.d.ts +5 -2
- package/lib/beacon/client/events.js +32 -35
- package/lib/beacon/client/events.js.map +1 -1
- package/lib/beacon/client/index.d.ts +6 -3
- package/lib/beacon/client/index.js +2 -2
- package/lib/beacon/client/index.js.map +1 -1
- package/lib/beacon/client/lightclient.d.ts +4 -3
- package/lib/beacon/client/lightclient.js +4 -7
- package/lib/beacon/client/lightclient.js.map +1 -1
- package/lib/beacon/client/lodestar.d.ts +4 -3
- package/lib/beacon/client/lodestar.js +4 -7
- package/lib/beacon/client/lodestar.js.map +1 -1
- package/lib/beacon/client/node.d.ts +4 -3
- package/lib/beacon/client/node.js +4 -7
- package/lib/beacon/client/node.js.map +1 -1
- package/lib/beacon/client/proof.d.ts +5 -4
- package/lib/beacon/client/proof.js +5 -61
- package/lib/beacon/client/proof.js.map +1 -1
- package/lib/beacon/client/validator.d.ts +4 -3
- package/lib/beacon/client/validator.js +4 -7
- package/lib/beacon/client/validator.js.map +1 -1
- package/lib/beacon/index.d.ts +5 -5
- package/lib/beacon/index.js.map +1 -1
- package/lib/beacon/routes/beacon/block.d.ts +282 -164
- package/lib/beacon/routes/beacon/block.js +324 -116
- package/lib/beacon/routes/beacon/block.js.map +1 -1
- package/lib/beacon/routes/beacon/index.d.ts +6 -588
- package/lib/beacon/routes/beacon/index.js +16 -26
- package/lib/beacon/routes/beacon/index.js.map +1 -1
- package/lib/beacon/routes/beacon/pool.d.ts +155 -117
- package/lib/beacon/routes/beacon/pool.js +146 -42
- package/lib/beacon/routes/beacon/pool.js.map +1 -1
- package/lib/beacon/routes/beacon/rewards.d.ts +113 -101
- package/lib/beacon/routes/beacon/rewards.js +98 -63
- package/lib/beacon/routes/beacon/rewards.js.map +1 -1
- package/lib/beacon/routes/beacon/state.d.ts +172 -226
- package/lib/beacon/routes/beacon/state.js +204 -126
- package/lib/beacon/routes/beacon/state.js.map +1 -1
- package/lib/beacon/routes/config.d.ts +21 -36
- package/lib/beacon/routes/config.js +45 -24
- package/lib/beacon/routes/config.js.map +1 -1
- package/lib/beacon/routes/debug.d.ts +32 -95
- package/lib/beacon/routes/debug.js +87 -45
- package/lib/beacon/routes/debug.js.map +1 -1
- package/lib/beacon/routes/events.d.ts +20 -16
- package/lib/beacon/routes/events.js +39 -17
- package/lib/beacon/routes/events.js.map +1 -1
- package/lib/beacon/routes/index.d.ts +19 -19
- package/lib/beacon/routes/index.js +1 -0
- package/lib/beacon/routes/index.js.map +1 -1
- package/lib/beacon/routes/lightclient.d.ts +33 -55
- package/lib/beacon/routes/lightclient.js +107 -47
- package/lib/beacon/routes/lightclient.js.map +1 -1
- package/lib/beacon/routes/lodestar.d.ts +74 -148
- package/lib/beacon/routes/lodestar.js +126 -77
- package/lib/beacon/routes/lodestar.js.map +1 -1
- package/lib/beacon/routes/node.d.ts +48 -82
- package/lib/beacon/routes/node.js +90 -57
- package/lib/beacon/routes/node.js.map +1 -1
- package/lib/beacon/routes/proof.d.ts +25 -31
- package/lib/beacon/routes/proof.js +44 -24
- package/lib/beacon/routes/proof.js.map +1 -1
- package/lib/beacon/routes/validator.d.ts +547 -320
- package/lib/beacon/routes/validator.js +494 -213
- package/lib/beacon/routes/validator.js.map +1 -1
- package/lib/beacon/server/beacon.d.ts +3 -4
- package/lib/beacon/server/beacon.js +4 -57
- package/lib/beacon/server/beacon.js.map +1 -1
- package/lib/beacon/server/config.d.ts +3 -4
- package/lib/beacon/server/config.js +4 -5
- package/lib/beacon/server/config.js.map +1 -1
- package/lib/beacon/server/debug.d.ts +3 -4
- package/lib/beacon/server/debug.js +4 -54
- package/lib/beacon/server/debug.js.map +1 -1
- package/lib/beacon/server/events.d.ts +4 -4
- package/lib/beacon/server/events.js +17 -23
- package/lib/beacon/server/events.js.map +1 -1
- package/lib/beacon/server/index.d.ts +7 -8
- package/lib/beacon/server/index.js +13 -14
- package/lib/beacon/server/index.js.map +1 -1
- package/lib/beacon/server/lightclient.d.ts +3 -4
- package/lib/beacon/server/lightclient.js +4 -5
- package/lib/beacon/server/lightclient.js.map +1 -1
- package/lib/beacon/server/lodestar.d.ts +3 -4
- package/lib/beacon/server/lodestar.js +4 -5
- package/lib/beacon/server/lodestar.js.map +1 -1
- package/lib/beacon/server/node.d.ts +3 -4
- package/lib/beacon/server/node.js +4 -18
- package/lib/beacon/server/node.js.map +1 -1
- package/lib/beacon/server/proof.d.ts +3 -4
- package/lib/beacon/server/proof.js +4 -36
- package/lib/beacon/server/proof.js.map +1 -1
- package/lib/beacon/server/validator.d.ts +3 -4
- package/lib/beacon/server/validator.js +4 -21
- package/lib/beacon/server/validator.js.map +1 -1
- package/lib/builder/client.d.ts +4 -6
- package/lib/builder/client.js +3 -9
- package/lib/builder/client.js.map +1 -1
- package/lib/builder/index.d.ts +8 -5
- package/lib/builder/index.js +4 -1
- package/lib/builder/index.js.map +1 -1
- package/lib/builder/routes.d.ts +24 -38
- package/lib/builder/routes.js +77 -33
- package/lib/builder/routes.js.map +1 -1
- package/lib/builder/server/index.d.ts +6 -6
- package/lib/builder/server/index.js +7 -8
- package/lib/builder/server/index.js.map +1 -1
- package/lib/index.d.ts +8 -6
- package/lib/index.js +4 -4
- package/lib/index.js.map +1 -1
- package/lib/keymanager/client.d.ts +4 -3
- package/lib/keymanager/client.js +4 -7
- package/lib/keymanager/client.js.map +1 -1
- package/lib/keymanager/index.d.ts +5 -4
- package/lib/keymanager/index.js +1 -1
- package/lib/keymanager/index.js.map +1 -1
- package/lib/keymanager/routes.d.ts +155 -177
- package/lib/keymanager/routes.js +236 -123
- package/lib/keymanager/routes.js.map +1 -1
- package/lib/keymanager/server/index.d.ts +6 -6
- package/lib/keymanager/server/index.js +7 -8
- package/lib/keymanager/server/index.js.map +1 -1
- package/lib/server/index.d.ts +2 -0
- package/lib/server/index.js +3 -0
- package/lib/server/index.js.map +1 -0
- package/lib/utils/client/error.d.ts +6 -0
- package/lib/utils/client/error.js +8 -0
- package/lib/utils/client/error.js.map +1 -0
- package/lib/utils/client/httpClient.d.ts +35 -75
- package/lib/utils/client/httpClient.js +201 -153
- package/lib/utils/client/httpClient.js.map +1 -1
- package/lib/utils/client/index.d.ts +6 -2
- package/lib/utils/client/index.js +6 -2
- package/lib/utils/client/index.js.map +1 -1
- package/lib/utils/client/method.d.ts +11 -0
- package/lib/utils/client/method.js +27 -0
- package/lib/utils/client/method.js.map +1 -0
- package/lib/utils/client/request.d.ts +33 -0
- package/lib/utils/client/request.js +66 -0
- package/lib/utils/client/request.js.map +1 -0
- package/lib/utils/client/response.d.ts +35 -0
- package/lib/utils/client/response.js +166 -0
- package/lib/utils/client/response.js.map +1 -0
- package/lib/utils/codecs.d.ts +28 -0
- package/lib/utils/codecs.js +105 -0
- package/lib/utils/codecs.js.map +1 -0
- package/lib/utils/fork.d.ts +8 -0
- package/lib/utils/fork.js +35 -0
- package/lib/utils/fork.js.map +1 -0
- package/lib/utils/headers.d.ts +37 -0
- package/lib/utils/headers.js +135 -0
- package/lib/utils/headers.js.map +1 -0
- package/lib/utils/httpStatusCode.js.map +1 -0
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +1 -1
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/metadata.d.ts +88 -0
- package/lib/utils/metadata.js +126 -0
- package/lib/utils/metadata.js.map +1 -0
- package/lib/utils/schema.d.ts +31 -17
- package/lib/utils/schema.js +18 -6
- package/lib/utils/schema.js.map +1 -1
- package/lib/utils/serdes.d.ts +3 -0
- package/lib/utils/serdes.js +13 -0
- package/lib/utils/serdes.js.map +1 -1
- package/lib/utils/server/{errors.d.ts → error.d.ts} +2 -2
- package/lib/utils/server/{errors.js → error.js} +1 -1
- package/lib/utils/server/error.js.map +1 -0
- package/lib/utils/server/handler.d.ts +11 -0
- package/lib/utils/server/handler.js +109 -0
- package/lib/utils/server/handler.js.map +1 -0
- package/lib/utils/server/index.d.ts +5 -4
- package/lib/utils/server/index.js +5 -4
- package/lib/utils/server/index.js.map +1 -1
- package/lib/utils/server/method.d.ts +35 -0
- package/lib/utils/server/method.js +2 -0
- package/lib/utils/server/method.js.map +1 -0
- package/lib/utils/server/parser.d.ts +3 -0
- package/lib/utils/server/parser.js +20 -0
- package/lib/utils/server/parser.js.map +1 -0
- package/lib/utils/server/route.d.ts +20 -0
- package/lib/utils/server/route.js +19 -0
- package/lib/utils/server/route.js.map +1 -0
- package/lib/utils/types.d.ts +116 -104
- package/lib/utils/types.js +2 -155
- package/lib/utils/types.js.map +1 -1
- package/lib/utils/urlFormat.d.ts +2 -2
- package/lib/utils/urlFormat.js +3 -3
- package/lib/utils/urlFormat.js.map +1 -1
- package/lib/utils/wireFormat.d.ts +8 -0
- package/lib/utils/wireFormat.js +23 -0
- package/lib/utils/wireFormat.js.map +1 -0
- package/package.json +9 -6
- package/lib/interfaces.d.ts +0 -44
- package/lib/interfaces.js +0 -2
- package/lib/interfaces.js.map +0 -1
- package/lib/utils/acceptHeader.d.ts +0 -9
- package/lib/utils/acceptHeader.js +0 -62
- package/lib/utils/acceptHeader.js.map +0 -1
- package/lib/utils/client/client.d.ts +0 -29
- package/lib/utils/client/client.js +0 -88
- package/lib/utils/client/client.js.map +0 -1
- package/lib/utils/client/httpStatusCode.js.map +0 -1
- package/lib/utils/routes.d.ts +0 -6
- package/lib/utils/routes.js +0 -31
- package/lib/utils/routes.js.map +0 -1
- package/lib/utils/server/errors.js.map +0 -1
- package/lib/utils/server/genericJsonServer.d.ts +0 -13
- package/lib/utils/server/genericJsonServer.js +0 -32
- package/lib/utils/server/genericJsonServer.js.map +0 -1
- package/lib/utils/server/registerRoute.d.ts +0 -3
- package/lib/utils/server/registerRoute.js +0 -13
- package/lib/utils/server/registerRoute.js.map +0 -1
- package/lib/utils/server/types.d.ts +0 -22
- package/lib/utils/server/types.js +0 -2
- package/lib/utils/server/types.js.map +0 -1
- /package/lib/utils/{client/httpStatusCode.d.ts → httpStatusCode.d.ts} +0 -0
- /package/lib/utils/{client/httpStatusCode.js → httpStatusCode.js} +0 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { HttpHeader, MediaType, SUPPORTED_MEDIA_TYPES, parseAcceptHeader, parseContentTypeHeader } from "../headers.js";
|
|
2
|
+
import { isRequestWithoutBody, } from "../types.js";
|
|
3
|
+
import { WireFormat, fromWireFormat, getWireFormat } from "../wireFormat.js";
|
|
4
|
+
import { ApiError } from "./error.js";
|
|
5
|
+
export function createFastifyHandler(definition, method, _operationId) {
|
|
6
|
+
return async (req, resp) => {
|
|
7
|
+
// Determine response wire format first to inform application method
|
|
8
|
+
// about the preferable return type to avoid unnecessary serialization
|
|
9
|
+
let responseMediaType;
|
|
10
|
+
const acceptHeader = req.headers.accept;
|
|
11
|
+
if (definition.resp.isEmpty) {
|
|
12
|
+
// Ignore Accept header, the response will be sent without body
|
|
13
|
+
responseMediaType = null;
|
|
14
|
+
}
|
|
15
|
+
else if (acceptHeader === undefined || acceptHeader === "*/*") {
|
|
16
|
+
// Default to json to not force user to set header, e.g. when using curl
|
|
17
|
+
responseMediaType = MediaType.json;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
const { onlySupport } = definition.resp;
|
|
21
|
+
const supportedMediaTypes = onlySupport !== undefined ? [fromWireFormat(onlySupport)] : SUPPORTED_MEDIA_TYPES;
|
|
22
|
+
responseMediaType = parseAcceptHeader(acceptHeader, supportedMediaTypes);
|
|
23
|
+
if (responseMediaType === null) {
|
|
24
|
+
throw new ApiError(406, `Accepted media types not supported: ${acceptHeader}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const responseWireFormat = responseMediaType !== null ? getWireFormat(responseMediaType) : null;
|
|
28
|
+
let response;
|
|
29
|
+
try {
|
|
30
|
+
if (isRequestWithoutBody(definition)) {
|
|
31
|
+
response = await method(definition.req.parseReq(req), {
|
|
32
|
+
sszBytes: null,
|
|
33
|
+
returnBytes: responseWireFormat === WireFormat.ssz,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const contentType = req.headers[HttpHeader.ContentType];
|
|
38
|
+
if (contentType === undefined) {
|
|
39
|
+
throw new ApiError(400, "Content-Type header is required");
|
|
40
|
+
}
|
|
41
|
+
const requestMediaType = parseContentTypeHeader(contentType);
|
|
42
|
+
if (requestMediaType === null) {
|
|
43
|
+
throw new ApiError(415, `Unsupported media type: ${contentType.split(";", 1)[0]}`);
|
|
44
|
+
}
|
|
45
|
+
const requestWireFormat = getWireFormat(requestMediaType);
|
|
46
|
+
const { onlySupport } = definition.req;
|
|
47
|
+
if (onlySupport !== undefined && onlySupport !== requestWireFormat) {
|
|
48
|
+
throw new ApiError(415, `Endpoint only supports ${onlySupport.toUpperCase()} requests`);
|
|
49
|
+
}
|
|
50
|
+
switch (requestWireFormat) {
|
|
51
|
+
case WireFormat.json:
|
|
52
|
+
response = await method(definition.req.parseReqJson(req), {
|
|
53
|
+
sszBytes: null,
|
|
54
|
+
returnBytes: responseWireFormat === WireFormat.ssz,
|
|
55
|
+
});
|
|
56
|
+
break;
|
|
57
|
+
case WireFormat.ssz:
|
|
58
|
+
response = await method(definition.req.parseReqSsz(req), {
|
|
59
|
+
sszBytes: req.body,
|
|
60
|
+
returnBytes: responseWireFormat === WireFormat.ssz,
|
|
61
|
+
});
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
if (e instanceof ApiError)
|
|
68
|
+
throw e;
|
|
69
|
+
// Errors related to parsing should return 400 status code
|
|
70
|
+
throw new ApiError(400, e.message);
|
|
71
|
+
}
|
|
72
|
+
if (response?.status !== undefined) {
|
|
73
|
+
resp.statusCode = response.status;
|
|
74
|
+
}
|
|
75
|
+
switch (responseWireFormat) {
|
|
76
|
+
case WireFormat.json: {
|
|
77
|
+
const metaHeaders = definition.resp.meta.toHeadersObject(response?.meta);
|
|
78
|
+
metaHeaders[HttpHeader.ContentType] = MediaType.json;
|
|
79
|
+
void resp.headers(metaHeaders);
|
|
80
|
+
const data = response?.data instanceof Uint8Array
|
|
81
|
+
? definition.resp.data.toJson(definition.resp.data.deserialize(response.data, response.meta), response.meta)
|
|
82
|
+
: definition.resp.data.toJson(response?.data, response?.meta);
|
|
83
|
+
const metaJson = definition.resp.meta.toJson(response?.meta);
|
|
84
|
+
if (definition.resp.transform) {
|
|
85
|
+
return definition.resp.transform.toResponse(data, metaJson);
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
data,
|
|
89
|
+
...metaJson,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
case WireFormat.ssz: {
|
|
93
|
+
const metaHeaders = definition.resp.meta.toHeadersObject(response?.meta);
|
|
94
|
+
metaHeaders[HttpHeader.ContentType] = MediaType.ssz;
|
|
95
|
+
void resp.headers(metaHeaders);
|
|
96
|
+
const data = response?.data instanceof Uint8Array
|
|
97
|
+
? response.data
|
|
98
|
+
: definition.resp.data.serialize(response?.data, response?.meta);
|
|
99
|
+
// Fastify supports returning `Uint8Array` from handler and will efficiently
|
|
100
|
+
// convert it to a `Buffer` internally without copying the underlying `ArrayBuffer`
|
|
101
|
+
return data;
|
|
102
|
+
}
|
|
103
|
+
case null:
|
|
104
|
+
// Send response without body
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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ACP,6BAA6B;gBAC7B,OAAO;QACX,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./
|
|
4
|
-
export * from "./
|
|
1
|
+
export * from "./error.js";
|
|
2
|
+
export * from "./handler.js";
|
|
3
|
+
export * from "./method.js";
|
|
4
|
+
export * from "./parser.js";
|
|
5
|
+
export * from "./route.js";
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./
|
|
4
|
-
export * from "./
|
|
1
|
+
export * from "./error.js";
|
|
2
|
+
export * from "./handler.js";
|
|
3
|
+
export * from "./method.js";
|
|
4
|
+
export * from "./parser.js";
|
|
5
|
+
export * from "./route.js";
|
|
5
6
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EmptyMeta, EmptyResponseData } from "../codecs.js";
|
|
2
|
+
import { HttpSuccessCodes } from "../httpStatusCode.js";
|
|
3
|
+
import { Endpoint, HasOnlyOptionalProps } from "../types.js";
|
|
4
|
+
type ApplicationResponseObject<E extends Endpoint> = {
|
|
5
|
+
/**
|
|
6
|
+
* Set non-200 success status code
|
|
7
|
+
*/
|
|
8
|
+
status?: HttpSuccessCodes;
|
|
9
|
+
} & (E["return"] extends EmptyResponseData ? {
|
|
10
|
+
data?: never;
|
|
11
|
+
} : {
|
|
12
|
+
data: E["return"] | (E["return"] extends undefined ? undefined : Uint8Array);
|
|
13
|
+
}) & (E["meta"] extends EmptyMeta ? {
|
|
14
|
+
meta?: never;
|
|
15
|
+
} : {
|
|
16
|
+
meta: E["meta"];
|
|
17
|
+
});
|
|
18
|
+
export type ApplicationResponse<E extends Endpoint> = HasOnlyOptionalProps<ApplicationResponseObject<E>> extends true ? ApplicationResponseObject<E> | void : ApplicationResponseObject<E>;
|
|
19
|
+
export type ApiContext = {
|
|
20
|
+
/**
|
|
21
|
+
* Raw ssz bytes from request payload, only available for ssz requests
|
|
22
|
+
*/
|
|
23
|
+
sszBytes?: Uint8Array | null;
|
|
24
|
+
/**
|
|
25
|
+
* Informs application method about preferable return type to avoid unnecessary serialization
|
|
26
|
+
*/
|
|
27
|
+
returnBytes?: boolean;
|
|
28
|
+
};
|
|
29
|
+
type GenericOptions = Record<string, unknown>;
|
|
30
|
+
export type ApplicationMethod<E extends Endpoint> = (args: E["args"], context?: ApiContext, opts?: GenericOptions) => Promise<ApplicationResponse<E>>;
|
|
31
|
+
export type ApplicationMethods<Es extends Record<string, Endpoint>> = {
|
|
32
|
+
[K in keyof Es]: ApplicationMethod<Es[K]>;
|
|
33
|
+
};
|
|
34
|
+
export {};
|
|
35
|
+
//# sourceMappingURL=method.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"method.js","sourceRoot":"","sources":["../../../src/utils/server/method.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MediaType } from "../headers.js";
|
|
2
|
+
export function addSszContentTypeParser(server) {
|
|
3
|
+
// Cache body schema symbol, does not change per request
|
|
4
|
+
let bodySchemaSymbol;
|
|
5
|
+
server.addContentTypeParser(MediaType.ssz, { parseAs: "buffer" }, async (request, payload) => {
|
|
6
|
+
if (bodySchemaSymbol === undefined) {
|
|
7
|
+
// Get body schema symbol to be able to access validation function
|
|
8
|
+
// https://github.com/fastify/fastify/blob/af2ccb5ff681c1d0ac22eb7314c6fa803f73c873/lib/symbols.js#L25
|
|
9
|
+
bodySchemaSymbol = Object.getOwnPropertySymbols(request.context).find((s) => s.description === "body-schema");
|
|
10
|
+
}
|
|
11
|
+
// JSON schema validation will be applied to `Buffer` object, it is required to override validation function
|
|
12
|
+
// See https://github.com/fastify/help/issues/1012, it is not possible right now to define a schema per content type
|
|
13
|
+
request.context[bodySchemaSymbol] = () => true;
|
|
14
|
+
// We could just return the `Buffer` here which is a subclass of `Uint8Array` but downstream code does not require it
|
|
15
|
+
// and it's better to convert it here to avoid unexpected behavior such as `Buffer.prototype.slice` not copying memory
|
|
16
|
+
// See https://github.com/nodejs/node/issues/41588#issuecomment-1016269584
|
|
17
|
+
return new Uint8Array(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../../../src/utils/server/parser.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,SAAS,EAAC,MAAM,eAAe,CAAC;AAExC,MAAM,UAAU,uBAAuB,CAAC,MAA+B;IACrE,wDAAwD;IACxD,IAAI,gBAAoC,CAAC;IAEzC,MAAM,CAAC,oBAAoB,CACzB,SAAS,CAAC,GAAG,EACb,EAAC,OAAO,EAAE,QAAQ,EAAC,EACnB,KAAK,EAAE,OAA+B,EAAE,OAAe,EAAE,EAAE;QACzD,IAAI,gBAAgB,KAAK,SAAS,EAAE,CAAC;YACnC,kEAAkE;YAClE,sGAAsG;YACtG,gBAAgB,GAAG,MAAM,CAAC,qBAAqB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,KAAK,aAAa,CAAC,CAAC;QAChH,CAAC;QACD,4GAA4G;QAC5G,oHAAoH;QACnH,OAAO,CAAC,OAA8C,CAAC,gBAA0B,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC;QAEjG,qHAAqH;QACrH,sHAAsH;QACtH,0EAA0E;QAC1E,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IAChF,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type * as fastify from "fastify";
|
|
2
|
+
import { Endpoint, RouteDefinition, RouteDefinitions } from "../types.js";
|
|
3
|
+
import { FastifyHandler } from "./handler.js";
|
|
4
|
+
import { ApplicationMethod, ApplicationMethods } from "./method.js";
|
|
5
|
+
export type FastifySchema = fastify.FastifySchema & {
|
|
6
|
+
operationId: string;
|
|
7
|
+
tags?: string[];
|
|
8
|
+
};
|
|
9
|
+
export type FastifyRoute<E extends Endpoint> = {
|
|
10
|
+
url: string;
|
|
11
|
+
method: fastify.HTTPMethods;
|
|
12
|
+
handler: FastifyHandler<E>;
|
|
13
|
+
schema: FastifySchema;
|
|
14
|
+
};
|
|
15
|
+
export type FastifyRoutes<Es extends Record<string, Endpoint>> = {
|
|
16
|
+
[K in keyof Es]: FastifyRoute<Es[K]>;
|
|
17
|
+
};
|
|
18
|
+
export declare function createFastifyRoute<E extends Endpoint>(definition: RouteDefinition<E>, method: ApplicationMethod<E>, operationId: string): FastifyRoute<E>;
|
|
19
|
+
export declare function createFastifyRoutes<Es extends Record<string, Endpoint>>(definitions: RouteDefinitions<Es>, methods: ApplicationMethods<Es>): FastifyRoutes<Es>;
|
|
20
|
+
//# sourceMappingURL=route.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { mapValues } from "@lodestar/utils";
|
|
2
|
+
import { getFastifySchema } from "../schema.js";
|
|
3
|
+
import { toColonNotationPath } from "../urlFormat.js";
|
|
4
|
+
import { createFastifyHandler } from "./handler.js";
|
|
5
|
+
export function createFastifyRoute(definition, method, operationId) {
|
|
6
|
+
return {
|
|
7
|
+
url: toColonNotationPath(definition.url),
|
|
8
|
+
method: definition.method,
|
|
9
|
+
handler: createFastifyHandler(definition, method, operationId),
|
|
10
|
+
schema: {
|
|
11
|
+
...getFastifySchema(definition.req.schema),
|
|
12
|
+
operationId,
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function createFastifyRoutes(definitions, methods) {
|
|
17
|
+
return mapValues(definitions, (definition, operationId) => createFastifyRoute(definition, methods?.[operationId]?.bind(methods), operationId));
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route.js","sourceRoot":"","sources":["../../../src/utils/server/route.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,SAAS,EAAC,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAC,mBAAmB,EAAC,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAiB,oBAAoB,EAAC,MAAM,cAAc,CAAC;AAgBlE,MAAM,UAAU,kBAAkB,CAChC,UAA8B,EAC9B,MAA4B,EAC5B,WAAmB;IAEnB,OAAO;QACL,GAAG,EAAE,mBAAmB,CAAC,UAAU,CAAC,GAAG,CAAC;QACxC,MAAM,EAAE,UAAU,CAAC,MAAM;QACzB,OAAO,EAAE,oBAAoB,CAAC,UAAU,EAAE,MAAM,EAAE,WAAW,CAAC;QAC9D,MAAM,EAAE;YACN,GAAG,gBAAgB,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC;YAC1C,WAAW;SACZ;KACF,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,mBAAmB,CACjC,WAAiC,EACjC,OAA+B;IAE/B,OAAO,SAAS,CAAC,WAAW,EAAE,CAAC,UAAU,EAAE,WAAW,EAAE,EAAE,CACxD,kBAAkB,CAAC,UAAU,EAAE,OAAO,EAAE,CAAC,WAAW,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,WAAqB,CAAC,CAC7F,CAAC;AACJ,CAAC"}
|
package/lib/utils/types.d.ts
CHANGED
|
@@ -1,117 +1,129 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
export type
|
|
7
|
-
[K in keyof
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import { ExtraRequestInit } from "./client/request.js";
|
|
2
|
+
import { EmptyMeta } from "./codecs.js";
|
|
3
|
+
import { HeadersExtra } from "./headers.js";
|
|
4
|
+
import { SchemaDefinition } from "./schema.js";
|
|
5
|
+
import { WireFormat } from "./wireFormat.js";
|
|
6
|
+
export type HasOnlyOptionalProps<T> = {
|
|
7
|
+
[K in keyof T]-?: object extends Pick<T, K> ? never : K;
|
|
8
|
+
} extends {
|
|
9
|
+
[_ in keyof T]: never;
|
|
10
|
+
} ? true : false;
|
|
11
|
+
export type PathParams = Record<string, string | number>;
|
|
12
|
+
export type QueryParams = Record<string, string | number | boolean | (string | number)[]>;
|
|
13
|
+
export type HeaderParams = Record<string, string>;
|
|
14
|
+
export type RequestData<P extends PathParams = PathParams, Q extends QueryParams = QueryParams, H extends HeaderParams = HeaderParams> = {
|
|
15
|
+
params?: P;
|
|
16
|
+
query?: Q;
|
|
17
|
+
headers?: H;
|
|
12
18
|
};
|
|
13
|
-
export type
|
|
14
|
-
|
|
15
|
-
method: "GET" | "POST" | "DELETE";
|
|
16
|
-
statusOk?: number;
|
|
19
|
+
export type JsonRequestData<B = unknown, P extends PathParams = PathParams, Q extends QueryParams = QueryParams, H extends HeaderParams = HeaderParams> = RequestData<P, Q, H> & {
|
|
20
|
+
body?: B;
|
|
17
21
|
};
|
|
18
|
-
export type
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
body
|
|
22
|
-
|
|
22
|
+
export type SszRequestData<P extends JsonRequestData> = Omit<P, "body"> & ("body" extends keyof P ? (P["body"] extends void ? {
|
|
23
|
+
body?: never;
|
|
24
|
+
} : {
|
|
25
|
+
body: Uint8Array;
|
|
26
|
+
}) : {
|
|
27
|
+
body?: never;
|
|
28
|
+
});
|
|
29
|
+
export type HttpMethod = "GET" | "POST" | "DELETE";
|
|
30
|
+
/**
|
|
31
|
+
* This type describes the general shape of a route
|
|
32
|
+
*
|
|
33
|
+
* This includes both http and application-level shape
|
|
34
|
+
* - The http method
|
|
35
|
+
* - Used to more strictly enforce the shape of the request
|
|
36
|
+
* - The application-level parameters
|
|
37
|
+
* - this enforces the shape of the input data passed by the client and to the route handler
|
|
38
|
+
* - The http request
|
|
39
|
+
* - this enforces the shape of the querystring, url params, request body
|
|
40
|
+
* - The application-level return data
|
|
41
|
+
* - this enforces the shape of the output data passed back to the client and returned by the route handler
|
|
42
|
+
* - The application-level return metadata
|
|
43
|
+
* - this enforces the shape of the returned metadata, used informationally and to help decode the return data
|
|
44
|
+
*/
|
|
45
|
+
export type Endpoint<Method extends HttpMethod = HttpMethod, ArgsType = unknown, RequestType extends Method extends "GET" ? RequestData : JsonRequestData = JsonRequestData, ReturnType = unknown, Meta = unknown> = {
|
|
46
|
+
method: Method;
|
|
47
|
+
/** The parameters the client passes / server app code ingests */
|
|
48
|
+
args: ArgsType;
|
|
49
|
+
/** The parameters in the http request */
|
|
50
|
+
request: RequestType;
|
|
51
|
+
/** The return data */
|
|
52
|
+
return: ReturnType;
|
|
53
|
+
/** The return metadata */
|
|
54
|
+
meta: Meta;
|
|
23
55
|
};
|
|
24
|
-
|
|
25
|
-
export type
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
56
|
+
/** Encode / decode requests to & from function params, as well as schema definitions */
|
|
57
|
+
export type RequestWithoutBodyCodec<E extends Endpoint> = {
|
|
58
|
+
writeReq: (p: E["args"]) => E["request"];
|
|
59
|
+
parseReq: (r: E["request"]) => E["args"];
|
|
60
|
+
schema: SchemaDefinition<E["request"]>;
|
|
29
61
|
};
|
|
30
|
-
export type
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
schema?: SchemaDefinition<ReqType>;
|
|
62
|
+
export type JsonRequestMethods<E extends Endpoint> = {
|
|
63
|
+
writeReqJson: (p: E["args"]) => E["request"];
|
|
64
|
+
parseReqJson: (r: E["request"]) => E["args"];
|
|
34
65
|
};
|
|
35
|
-
export type
|
|
36
|
-
[
|
|
37
|
-
|
|
38
|
-
|
|
66
|
+
export type SszRequestMethods<E extends Endpoint> = {
|
|
67
|
+
writeReqSsz: (p: E["args"]) => SszRequestData<E["request"]>;
|
|
68
|
+
parseReqSsz: (r: SszRequestData<E["request"]>) => E["args"];
|
|
69
|
+
};
|
|
70
|
+
export type RequestWithBodyCodec<E extends Endpoint> = JsonRequestMethods<E> & SszRequestMethods<E> & {
|
|
71
|
+
schema: SchemaDefinition<E["request"]>;
|
|
72
|
+
/** Support ssz-only or json-only requests */
|
|
73
|
+
onlySupport?: WireFormat;
|
|
39
74
|
};
|
|
40
|
-
/** Curried definition to infer only one of the two generic types */
|
|
41
|
-
export type ReqGenArg<Fn extends (...args: any) => any, ReqType extends ReqGeneric> = ReqSerializer<Fn, ReqType>;
|
|
42
|
-
/** Shortcut for routes that have no params, query nor body */
|
|
43
|
-
export declare const reqEmpty: ReqSerializer<() => void, ReqEmpty>;
|
|
44
|
-
/** Shortcut for routes that have only body */
|
|
45
|
-
export declare const reqOnlyBody: <T>(type: TypeJson<T>, bodySchema: Schema) => ReqGenArg<(arg: T) => Promise<void>, {
|
|
46
|
-
body: unknown;
|
|
47
|
-
}>;
|
|
48
|
-
/** SSZ factory helper + typed. limit = 1e6 as a big enough random number */
|
|
49
|
-
export declare function ArrayOf<T>(elementType: Type<T>): ArrayType<Type<T>, unknown, unknown>;
|
|
50
|
-
/**
|
|
51
|
-
* SSZ factory helper + typed to return responses of type
|
|
52
|
-
* ```
|
|
53
|
-
* data: T
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
export declare function ContainerData<T>(dataType: TypeJson<T>): TypeJson<{
|
|
57
|
-
data: T;
|
|
58
|
-
}>;
|
|
59
|
-
/**
|
|
60
|
-
* SSZ factory helper + typed to return responses of type `{data: T; executionOptimistic: boolean}`
|
|
61
|
-
*/
|
|
62
|
-
export declare function ContainerDataExecutionOptimistic<T>(dataType: TypeJson<T>): TypeJson<{
|
|
63
|
-
data: T;
|
|
64
|
-
executionOptimistic: boolean;
|
|
65
|
-
}>;
|
|
66
|
-
/**
|
|
67
|
-
* SSZ factory helper + typed to return responses of type
|
|
68
|
-
* ```
|
|
69
|
-
* data: T
|
|
70
|
-
* version: ForkName
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
|
-
export declare function WithVersion<T>(getType: (fork: ForkName) => TypeJson<T>): TypeJson<{
|
|
74
|
-
data: T;
|
|
75
|
-
version: ForkName;
|
|
76
|
-
}>;
|
|
77
|
-
/**
|
|
78
|
-
* SSZ factory helper to wrap an existing type with `{executionOptimistic: boolean}`
|
|
79
|
-
*/
|
|
80
|
-
export declare function WithExecutionOptimistic<T extends {
|
|
81
|
-
data: unknown;
|
|
82
|
-
}>(type: TypeJson<T>): TypeJson<T & {
|
|
83
|
-
executionOptimistic: boolean;
|
|
84
|
-
}>;
|
|
85
75
|
/**
|
|
86
|
-
*
|
|
76
|
+
* Handles translation between `Endpoint["args"]` and `Endpoint["request"]`
|
|
87
77
|
*/
|
|
88
|
-
export
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
78
|
+
export type RequestCodec<E extends Endpoint> = E["method"] extends "GET" ? RequestWithoutBodyCodec<E> : "body" extends keyof E["request"] ? RequestWithBodyCodec<E> : RequestWithoutBodyCodec<E>;
|
|
79
|
+
export declare function isRequestWithoutBody<E extends Endpoint>(definition: RouteDefinition<E>): definition is RouteDefinition<E> & {
|
|
80
|
+
req: RequestWithoutBodyCodec<E>;
|
|
81
|
+
};
|
|
82
|
+
export type ResponseDataCodec<T, M> = {
|
|
83
|
+
toJson: (data: T, meta: M) => unknown;
|
|
84
|
+
fromJson: (data: unknown, meta: M) => T;
|
|
85
|
+
serialize: (data: T, meta: M) => Uint8Array;
|
|
86
|
+
deserialize: (data: Uint8Array, meta: M) => T;
|
|
87
|
+
};
|
|
88
|
+
export type ResponseMetadataCodec<T> = {
|
|
89
|
+
toJson: (val: T) => unknown;
|
|
90
|
+
fromJson: (val: unknown) => T;
|
|
91
|
+
toHeadersObject: (val: T) => Record<string, string>;
|
|
92
|
+
fromHeaders: (headers: HeadersExtra) => T;
|
|
93
|
+
};
|
|
94
|
+
export type ResponseCodec<E extends Endpoint> = {
|
|
95
|
+
data: ResponseDataCodec<E["return"], E["meta"]>;
|
|
96
|
+
meta: ResponseMetadataCodec<E["meta"]>;
|
|
97
|
+
/** Occasionally, json responses require an extra transformation to separate the data from metadata */
|
|
98
|
+
transform?: {
|
|
99
|
+
toResponse: (data: unknown, meta: unknown) => unknown;
|
|
100
|
+
fromResponse: (resp: unknown) => {
|
|
101
|
+
data: E["return"];
|
|
102
|
+
} & (E["meta"] extends EmptyMeta ? {
|
|
103
|
+
meta?: never;
|
|
104
|
+
} : {
|
|
105
|
+
meta: E["meta"];
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
/** Support ssz-only or json-only responses */
|
|
109
|
+
onlySupport?: WireFormat;
|
|
110
|
+
/** Indicator used to handle empty responses */
|
|
111
|
+
isEmpty?: true;
|
|
112
|
+
};
|
|
93
113
|
/**
|
|
94
|
-
*
|
|
114
|
+
* Top-level definition of a route used by both the client and server
|
|
115
|
+
* - url and method
|
|
116
|
+
* - request and response codec
|
|
117
|
+
* - request json schema
|
|
95
118
|
*/
|
|
96
|
-
export
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
type JsonCase = "snake" | "constant" | "camel" | "param" | "header" | "pascal" | "dot" | "notransform";
|
|
103
|
-
/** Helper to only translate casing */
|
|
104
|
-
export declare function jsonType<T extends Record<string, unknown> | Record<string, unknown>[] | unknown[]>(jsonCase: JsonCase): TypeJson<T>;
|
|
105
|
-
/** Helper to not do any transformation with the type */
|
|
106
|
-
export declare function sameType<T>(): TypeJson<T>;
|
|
107
|
-
export type KeysOfNonVoidResolveValues<Api extends Record<string, APIClientHandler>> = {
|
|
108
|
-
[K in keyof Api]: ApiClientResponseData<Resolves<Api[K]>> extends void ? never : K;
|
|
109
|
-
}[keyof Api];
|
|
110
|
-
export type ReturnTypes<Api extends Record<string, APIClientHandler>> = {
|
|
111
|
-
[K in keyof Pick<Api, KeysOfNonVoidResolveValues<Api>>]: TypeJson<ApiClientResponseData<Resolves<Api[K]>>>;
|
|
119
|
+
export type RouteDefinition<E extends Endpoint> = {
|
|
120
|
+
url: string;
|
|
121
|
+
method: E["method"];
|
|
122
|
+
req: RequestCodec<E>;
|
|
123
|
+
resp: ResponseCodec<E>;
|
|
124
|
+
init?: ExtraRequestInit;
|
|
112
125
|
};
|
|
113
|
-
export type
|
|
114
|
-
[K in keyof
|
|
126
|
+
export type RouteDefinitions<Es extends Record<string, Endpoint>> = {
|
|
127
|
+
[K in keyof Es]: RouteDefinition<Es[K]>;
|
|
115
128
|
};
|
|
116
|
-
export {};
|
|
117
129
|
//# sourceMappingURL=types.d.ts.map
|