Skip to content

Commit

Permalink
create cors policy schema and middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
gladwindos committed Feb 17, 2024
1 parent f87d586 commit 4b2967d
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 5 deletions.
67 changes: 64 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.ts",
"scripts": {
"dev": "turbo dev",
"dev": "turbo build && turbo dev",
"build": "turbo build",
"test": "turbo test",
"test:watch": "turbo run test:watch",
Expand Down
2 changes: 2 additions & 0 deletions packages/policies/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jest.config.js
dist/
4 changes: 4 additions & 0 deletions packages/policies/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["@endpointly/eslint-config/base.json"],
"root": true
}
5 changes: 5 additions & 0 deletions packages/policies/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
25 changes: 25 additions & 0 deletions packages/policies/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@endpointly/policies",
"version": "0.0.0",
"description": "",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
"build": "tsc -d",
"test": "jest",
"test:watch": "jest --watch",
"lint": "eslint .",
"clean": "rimraf ./dist"
},
"author": "",
"license": "ISC",
"dependencies": {
"ajv": "^8.12.0",
"cors": "^2.8.5"
},
"devDependencies": {
"@endpointly/eslint-config": "*",
"@endpointly/tsconfig": "*",
"@types/cors": "^2.8.17"
}
}
2 changes: 2 additions & 0 deletions packages/policies/src/cors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./schema";
export * from "./middleware";
3 changes: 3 additions & 0 deletions packages/policies/src/cors/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import cors from "cors";

export { cors as corsMiddleware };
46 changes: 46 additions & 0 deletions packages/policies/src/cors/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { JSONSchemaType } from "ajv";

export interface CorsPolicy {
origin?: boolean | string | string[];
methods?: string | string[];
allowedHeaders?: string | string[];
exposedHeaders?: string | string[];
credentials?: boolean;
maxAge?: number;
optionsSuccessStatus?: number;
}

export const corsSchema: JSONSchemaType<CorsPolicy> = {
type: "object",
properties: {
origin: {
type: ["boolean", "string", "array"],
oneOf: [
{ type: "boolean" },
{ type: "string" },
{ type: "array", items: { type: "string" } },
],
default: "*",
nullable: true,
},
methods: {
type: ["string", "array"],
oneOf: [{ type: "string" }, { type: "array", items: { type: "string" } }],
default: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"],
nullable: true,
},
allowedHeaders: {
type: ["string", "array"],
oneOf: [{ type: "string" }, { type: "array", items: { type: "string" } }],
nullable: true,
},
exposedHeaders: {
type: ["string", "array"],
oneOf: [{ type: "string" }, { type: "array", items: { type: "string" } }],
nullable: true,
},
credentials: { type: "boolean", nullable: true },
maxAge: { type: "number", nullable: true },
optionsSuccessStatus: { type: "number", default: 204, nullable: true },
},
};
2 changes: 2 additions & 0 deletions packages/policies/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./schema";
export * from "./cors";
16 changes: 16 additions & 0 deletions packages/policies/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { JSONSchemaType } from "ajv";
import { CorsPolicy, corsSchema } from "./cors";

export interface Policies {
cors?: CorsPolicy;
}

export const policiesSchema: JSONSchemaType<Policies> = {
type: "object",
properties: {
cors: {
...corsSchema,
nullable: true,
},
},
};
7 changes: 7 additions & 0 deletions packages/policies/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@endpointly/tsconfig/base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "."
}
}
3 changes: 2 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@endpointly/server",
"version": "0.0.0",
"description": "",
"main": "index.js",
"main": "dist/src/index.js",
"scripts": {
"dev": "nodemon ./src/index.ts",
"build": "tsc",
Expand All @@ -22,6 +22,7 @@
"devDependencies": {
"@endpointly/tsconfig": "*",
"@endpointly/eslint-config": "*",
"@endpointly/policies": "*",
"@types/express": "^4.17.21",
"http-proxy-middleware": "^2.0.6",
"nodemon": "^3.0.3"
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from "express";
import { corsMiddleware } from "@endpointly/policies";
import { parseConfig } from "./utils/config/parse-config";
import { createProxies } from "./utils/proxy/create-proxies";

Expand All @@ -12,6 +13,8 @@ if (process.env.NODE_ENV === "development") {
});
}

router.use(corsMiddleware());

createProxies(router, config.endpoints);

export { router };
2 changes: 2 additions & 0 deletions packages/server/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Policies } from "@endpointly/policies";
import { Endpoint } from "./endpoints";

export interface Config {
endpoints: Endpoint[];
policies?: Policies;
}
5 changes: 5 additions & 0 deletions packages/server/src/utils/config/config-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JSONSchemaType } from "ajv";
import { policiesSchema } from "@endpointly/policies";
import { Config } from "../../types/config";
import { KeyValue, Destination, Endpoint } from "../../types/endpoints";

Expand Down Expand Up @@ -39,6 +40,10 @@ export const configSchema: JSONSchemaType<Config> = {
type: "array",
items: endpointSchema,
},
policies: {
...policiesSchema,
nullable: true,
},
},
required: ["endpoints"],
};

0 comments on commit 4b2967d

Please sign in to comment.