diff --git a/src/router.ts b/src/router.ts index 6635956..e6a0d91 100644 --- a/src/router.ts +++ b/src/router.ts @@ -1,6 +1,6 @@ import express from "express"; -import { parseConfig } from "./utils/parse-config"; -import { createProxies } from "./utils/create-proxies"; +import { parseConfig } from "./utils/config/parse-config"; +import { createProxies } from "./utils/proxy/create-proxies"; // TODO: prevent the app from crashing if the config is invalid const config = parseConfig("config.yml"); diff --git a/src/utils/config/config-schema.ts b/src/utils/config/config-schema.ts new file mode 100644 index 0000000..d5354b2 --- /dev/null +++ b/src/utils/config/config-schema.ts @@ -0,0 +1,44 @@ +import { JSONSchemaType } from "ajv"; +import { Config } from "../../types/config"; +import { KeyValue, Destination, Endpoint } from "../../types/endpoints"; + +const keyValueSchema: JSONSchemaType = { + type: "object", + properties: { + key: { type: "string" }, + value: { type: "string" }, + }, + required: ["key", "value"], +}; + +const destinationSchema: JSONSchemaType = { + type: "object", + properties: { + url: { type: "string" }, + headers: { type: "array", items: keyValueSchema, nullable: true }, + params: { type: "array", items: keyValueSchema, nullable: true }, + }, + required: ["url"], +}; + +const endpointSchema: JSONSchemaType = { + type: "object", + properties: { + name: { type: "string" }, + path: { type: "string" }, + method: { type: "string", enum: ["GET", "POST", "PUT", "DELETE"] }, + destination: destinationSchema, + }, + required: ["name", "path", "method", "destination"], +}; + +export const configSchema: JSONSchemaType = { + type: "object", + properties: { + endpoints: { + type: "array", + items: endpointSchema, + }, + }, + required: ["endpoints"], +}; diff --git a/src/utils/parse-config.ts b/src/utils/config/parse-config.ts similarity index 89% rename from src/utils/parse-config.ts rename to src/utils/config/parse-config.ts index 47830cc..4488800 100644 --- a/src/utils/parse-config.ts +++ b/src/utils/config/parse-config.ts @@ -1,6 +1,6 @@ import fs from "fs"; import YAML from "yaml"; -import { Config } from "../types/config"; +import { Config } from "../../types/config"; import { validateConfig } from "./validate-config"; export const parseConfig = (filePath: string): Config => { diff --git a/src/utils/validate-config.ts b/src/utils/config/validate-config.ts similarity index 58% rename from src/utils/validate-config.ts rename to src/utils/config/validate-config.ts index 3a1912c..c1f3746 100644 --- a/src/utils/validate-config.ts +++ b/src/utils/config/validate-config.ts @@ -1,50 +1,10 @@ -import Ajv, { JSONSchemaType } from "ajv"; -import { Destination, Endpoint, KeyValue } from "../types/endpoints"; -import { Config } from "../types/config"; +import Ajv from "ajv"; +import { Endpoint } from "../../types/endpoints"; +import { Config } from "../../types/config"; +import { configSchema } from "./config-schema"; const ajv = new Ajv(); -const keyValueSchema: JSONSchemaType = { - type: "object", - properties: { - key: { type: "string" }, - value: { type: "string" }, - }, - required: ["key", "value"], -}; - -const destinationSchema: JSONSchemaType = { - type: "object", - properties: { - url: { type: "string" }, - headers: { type: "array", items: keyValueSchema, nullable: true }, - params: { type: "array", items: keyValueSchema, nullable: true }, - }, - required: ["url"], -}; - -const endpointSchema: JSONSchemaType = { - type: "object", - properties: { - name: { type: "string" }, - path: { type: "string" }, - method: { type: "string", enum: ["GET", "POST", "PUT", "DELETE"] }, - destination: destinationSchema, - }, - required: ["name", "path", "method", "destination"], -}; - -const configSchema: JSONSchemaType = { - type: "object", - properties: { - endpoints: { - type: "array", - items: endpointSchema, - }, - }, - required: ["endpoints"], -}; - const validateEndpointUniqueness = (endpoints: Endpoint[]): string[] => { const endpointNames = new Set(); const endpointPaths = new Set(); diff --git a/src/utils/create-headers.ts b/src/utils/proxy/create-headers.ts similarity index 84% rename from src/utils/create-headers.ts rename to src/utils/proxy/create-headers.ts index 7dcd91d..61aeee5 100644 --- a/src/utils/create-headers.ts +++ b/src/utils/proxy/create-headers.ts @@ -1,4 +1,4 @@ -import { Destination } from "../types/endpoints"; +import { Destination } from "../../types/endpoints"; interface Headers { [key: string]: string; diff --git a/src/utils/create-proxies.ts b/src/utils/proxy/create-proxies.ts similarity index 94% rename from src/utils/create-proxies.ts rename to src/utils/proxy/create-proxies.ts index 9800a64..10c848e 100644 --- a/src/utils/create-proxies.ts +++ b/src/utils/proxy/create-proxies.ts @@ -1,6 +1,6 @@ import { Router, Request } from "express"; import { createProxyMiddleware } from "http-proxy-middleware"; -import { Endpoint } from "../types/endpoints"; +import { Endpoint } from "../../types/endpoints"; import { createHeaders } from "./create-headers"; import { createQueryParams } from "./create-query-params"; diff --git a/src/utils/create-query-params.ts b/src/utils/proxy/create-query-params.ts similarity index 80% rename from src/utils/create-query-params.ts rename to src/utils/proxy/create-query-params.ts index 4248b41..092266b 100644 --- a/src/utils/create-query-params.ts +++ b/src/utils/proxy/create-query-params.ts @@ -1,4 +1,4 @@ -import { Endpoint } from "../types/endpoints"; +import { Endpoint } from "../../types/endpoints"; export const createQueryParams = ( params: Endpoint["destination"]["params"],