Skip to content

Commit

Permalink
restructure utils
Browse files Browse the repository at this point in the history
  • Loading branch information
gladwindos committed Feb 16, 2024
1 parent c0da611 commit e56f901
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
44 changes: 44 additions & 0 deletions src/utils/config/config-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { JSONSchemaType } from "ajv";
import { Config } from "../../types/config";
import { KeyValue, Destination, Endpoint } from "../../types/endpoints";

const keyValueSchema: JSONSchemaType<KeyValue> = {
type: "object",
properties: {
key: { type: "string" },
value: { type: "string" },
},
required: ["key", "value"],
};

const destinationSchema: JSONSchemaType<Destination> = {
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<Endpoint> = {
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<Config> = {
type: "object",
properties: {
endpoints: {
type: "array",
items: endpointSchema,
},
},
required: ["endpoints"],
};
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<KeyValue> = {
type: "object",
properties: {
key: { type: "string" },
value: { type: "string" },
},
required: ["key", "value"],
};

const destinationSchema: JSONSchemaType<Destination> = {
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<Endpoint> = {
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<Config> = {
type: "object",
properties: {
endpoints: {
type: "array",
items: endpointSchema,
},
},
required: ["endpoints"],
};

const validateEndpointUniqueness = (endpoints: Endpoint[]): string[] => {
const endpointNames = new Set<string>();
const endpointPaths = new Set<string>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Destination } from "../types/endpoints";
import { Destination } from "../../types/endpoints";

interface Headers {
[key: string]: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Endpoint } from "../types/endpoints";
import { Endpoint } from "../../types/endpoints";

export const createQueryParams = (
params: Endpoint["destination"]["params"],
Expand Down

0 comments on commit e56f901

Please sign in to comment.