Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions infrastructure/evault-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"vitest": "^3.0.9"
},
"dependencies": {
"@fastify/formbody": "^8.0.2",
"@fastify/swagger": "^8.14.0",
"@fastify/swagger-ui": "^3.0.0",
"@testcontainers/neo4j": "^10.24.2",
Expand Down
117 changes: 59 additions & 58 deletions infrastructure/evault-core/src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,85 @@
import { JSONSchema7 } from "json-schema";

export type SchemaType = {
schema: JSONSchema7;
deserialize: (value: any) => any;
schema: JSONSchema7;
deserialize: (value: any) => any;
};

export const SchemaTypes: Record<string, SchemaType> = {
date: {
schema: {
type: "string",
format: "date-time",
date: {
schema: {
type: "string",
format: "date-time",
},
deserialize: (value: string) => new Date(value),
},
deserialize: (value: string) => new Date(value),
},
number: {
schema: {
type: "number",
number: {
schema: {
type: "number",
},

deserialize: (value: number) => value,
},
deserialize: (value: number) => value,
},
string: {
schema: {
type: "string",
string: {
schema: {
type: "string",
},
deserialize: (value: string) => value,
},
deserialize: (value: string) => value,
},
boolean: {
schema: {
type: "boolean",
boolean: {
schema: {
type: "boolean",
},
deserialize: (value: boolean) => value,
},
deserialize: (value: boolean) => value,
},
array: {
schema: {
type: "array",
array: {
schema: {
type: "array",
},
deserialize: (value: any[]) => value,
},
deserialize: (value: any[]) => value,
},
object: {
schema: {
type: "object",
object: {
schema: {
type: "object",
},
deserialize: (value: Record<string, any>) => value,
},
deserialize: (value: Record<string, any>) => value,
},
};

export function getSchemaType(value: any): SchemaType {
if (value instanceof Date) return SchemaTypes.date;
if (Array.isArray(value)) return SchemaTypes.array;
if (typeof value === "object" && value !== null) return SchemaTypes.object;
if (typeof value === "number") return SchemaTypes.number;
if (typeof value === "boolean") return SchemaTypes.boolean;
return SchemaTypes.string;
if (value instanceof Date) return SchemaTypes.date;
if (Array.isArray(value)) return SchemaTypes.array;
if (typeof value === "object" && value !== null) return SchemaTypes.object;
if (typeof value === "number") return SchemaTypes.number;
if (typeof value === "boolean") return SchemaTypes.boolean;
return SchemaTypes.string;
}

export function serializeValue(value: any): { value: any; type: string } {
const type = getSchemaType(value);
let serializedValue = value;
const type = getSchemaType(value);
let serializedValue = value;

if (type === SchemaTypes.date) {
serializedValue = value.toISOString();
} else if (type === SchemaTypes.object) {
serializedValue = JSON.stringify(value);
}
if (type === SchemaTypes.date) {
serializedValue = value.toISOString();
} else if (type === SchemaTypes.object) {
serializedValue = JSON.stringify(value);
}

return {
value: serializedValue,
type:
Object.keys(SchemaTypes).find((key) => SchemaTypes[key] === type) ||
"string",
};
return {
value: serializedValue,
type:
Object.keys(SchemaTypes).find((key) => SchemaTypes[key] === type) ||
"string",
};
}

export function deserializeValue(value: any, type: string): any {
const schemaType = SchemaTypes[type];
if (!schemaType) return value;
const schemaType = SchemaTypes[type];
if (!schemaType) return value;

if (type === "object") {
return JSON.parse(value);
}
if (type === "object") {
return JSON.parse(value);
}

return schemaType.deserialize(value);
return schemaType.deserialize(value);
}
29 changes: 8 additions & 21 deletions infrastructure/evault-core/src/evault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,17 @@ class EVault {
password: process.env.ENCRYPTION_PASSWORD,
});

const yoga = createYoga({
schema: this.graphqlServer.getSchema(),
graphiql: true,
});
// change
const yoga = this.graphqlServer.init();

this.server.route({
url: "/graphql",
// Bind to the Yoga's endpoint to avoid rendering on any path
url: yoga.graphqlEndpoint,
method: ["GET", "POST", "OPTIONS"],
handler: async (req: FastifyRequest, reply: FastifyReply) => {
const response = await yoga.fetch(req.url, {
method: req.method,
headers: req.headers,
body: req.method === "POST" ? req.body : undefined,
});
reply.status(response.status);
const headers: Record<string, string> = {};
response.headers.forEach((value, key) => {
headers[key] = value;
});
reply.headers(headers);
reply.send(response.body);
return reply;
},
handler: (req, reply) =>
yoga.handleNodeRequestAndResponse(req, reply, {
req,
reply,
}),
});

// Mount Voyager endpoint
Expand Down
Loading
Loading