Skip to content

Commit 112dbcb

Browse files
authored
feat: web3 adapter (#115)
1 parent e41be12 commit 112dbcb

File tree

12 files changed

+1392
-546
lines changed

12 files changed

+1392
-546
lines changed

infrastructure/evault-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"vitest": "^3.0.9"
2525
},
2626
"dependencies": {
27+
"@fastify/formbody": "^8.0.2",
2728
"@fastify/swagger": "^8.14.0",
2829
"@fastify/swagger-ui": "^3.0.0",
2930
"@testcontainers/neo4j": "^10.24.2",
Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,85 @@
11
import { JSONSchema7 } from "json-schema";
22

33
export type SchemaType = {
4-
schema: JSONSchema7;
5-
deserialize: (value: any) => any;
4+
schema: JSONSchema7;
5+
deserialize: (value: any) => any;
66
};
77

88
export const SchemaTypes: Record<string, SchemaType> = {
9-
date: {
10-
schema: {
11-
type: "string",
12-
format: "date-time",
9+
date: {
10+
schema: {
11+
type: "string",
12+
format: "date-time",
13+
},
14+
deserialize: (value: string) => new Date(value),
1315
},
14-
deserialize: (value: string) => new Date(value),
15-
},
16-
number: {
17-
schema: {
18-
type: "number",
16+
number: {
17+
schema: {
18+
type: "number",
19+
},
20+
21+
deserialize: (value: number) => value,
1922
},
20-
deserialize: (value: number) => value,
21-
},
22-
string: {
23-
schema: {
24-
type: "string",
23+
string: {
24+
schema: {
25+
type: "string",
26+
},
27+
deserialize: (value: string) => value,
2528
},
26-
deserialize: (value: string) => value,
27-
},
28-
boolean: {
29-
schema: {
30-
type: "boolean",
29+
boolean: {
30+
schema: {
31+
type: "boolean",
32+
},
33+
deserialize: (value: boolean) => value,
3134
},
32-
deserialize: (value: boolean) => value,
33-
},
34-
array: {
35-
schema: {
36-
type: "array",
35+
array: {
36+
schema: {
37+
type: "array",
38+
},
39+
deserialize: (value: any[]) => value,
3740
},
38-
deserialize: (value: any[]) => value,
39-
},
40-
object: {
41-
schema: {
42-
type: "object",
41+
object: {
42+
schema: {
43+
type: "object",
44+
},
45+
deserialize: (value: Record<string, any>) => value,
4346
},
44-
deserialize: (value: Record<string, any>) => value,
45-
},
4647
};
4748

4849
export function getSchemaType(value: any): SchemaType {
49-
if (value instanceof Date) return SchemaTypes.date;
50-
if (Array.isArray(value)) return SchemaTypes.array;
51-
if (typeof value === "object" && value !== null) return SchemaTypes.object;
52-
if (typeof value === "number") return SchemaTypes.number;
53-
if (typeof value === "boolean") return SchemaTypes.boolean;
54-
return SchemaTypes.string;
50+
if (value instanceof Date) return SchemaTypes.date;
51+
if (Array.isArray(value)) return SchemaTypes.array;
52+
if (typeof value === "object" && value !== null) return SchemaTypes.object;
53+
if (typeof value === "number") return SchemaTypes.number;
54+
if (typeof value === "boolean") return SchemaTypes.boolean;
55+
return SchemaTypes.string;
5556
}
5657

5758
export function serializeValue(value: any): { value: any; type: string } {
58-
const type = getSchemaType(value);
59-
let serializedValue = value;
59+
const type = getSchemaType(value);
60+
let serializedValue = value;
6061

61-
if (type === SchemaTypes.date) {
62-
serializedValue = value.toISOString();
63-
} else if (type === SchemaTypes.object) {
64-
serializedValue = JSON.stringify(value);
65-
}
62+
if (type === SchemaTypes.date) {
63+
serializedValue = value.toISOString();
64+
} else if (type === SchemaTypes.object) {
65+
serializedValue = JSON.stringify(value);
66+
}
6667

67-
return {
68-
value: serializedValue,
69-
type:
70-
Object.keys(SchemaTypes).find((key) => SchemaTypes[key] === type) ||
71-
"string",
72-
};
68+
return {
69+
value: serializedValue,
70+
type:
71+
Object.keys(SchemaTypes).find((key) => SchemaTypes[key] === type) ||
72+
"string",
73+
};
7374
}
7475

7576
export function deserializeValue(value: any, type: string): any {
76-
const schemaType = SchemaTypes[type];
77-
if (!schemaType) return value;
77+
const schemaType = SchemaTypes[type];
78+
if (!schemaType) return value;
7879

79-
if (type === "object") {
80-
return JSON.parse(value);
81-
}
80+
if (type === "object") {
81+
return JSON.parse(value);
82+
}
8283

83-
return schemaType.deserialize(value);
84+
return schemaType.deserialize(value);
8485
}

infrastructure/evault-core/src/evault.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,17 @@ class EVault {
5757
password: process.env.ENCRYPTION_PASSWORD,
5858
});
5959

60-
const yoga = createYoga({
61-
schema: this.graphqlServer.getSchema(),
62-
graphiql: true,
63-
});
64-
// change
60+
const yoga = this.graphqlServer.init();
6561

6662
this.server.route({
67-
url: "/graphql",
63+
// Bind to the Yoga's endpoint to avoid rendering on any path
64+
url: yoga.graphqlEndpoint,
6865
method: ["GET", "POST", "OPTIONS"],
69-
handler: async (req: FastifyRequest, reply: FastifyReply) => {
70-
const response = await yoga.fetch(req.url, {
71-
method: req.method,
72-
headers: req.headers,
73-
body: req.method === "POST" ? req.body : undefined,
74-
});
75-
reply.status(response.status);
76-
const headers: Record<string, string> = {};
77-
response.headers.forEach((value, key) => {
78-
headers[key] = value;
79-
});
80-
reply.headers(headers);
81-
reply.send(response.body);
82-
return reply;
83-
},
66+
handler: (req, reply) =>
67+
yoga.handleNodeRequestAndResponse(req, reply, {
68+
req,
69+
reply,
70+
}),
8471
});
8572

8673
// Mount Voyager endpoint

0 commit comments

Comments
 (0)