From 4d65900d131dd2e027483933f67b0ea4bae13c8d Mon Sep 17 00:00:00 2001 From: Netfloex Date: Mon, 10 Jan 2022 12:23:31 +0100 Subject: [PATCH] Added 'raw' option --- src/lib/createConfig.ts | 6 ++++++ src/lib/validateConfig.ts | 17 +++++++++++++---- src/models/nginx-config-parser.d.ts | 1 + src/tests/__snapshots__/create.test.ts.snap | 1 + src/tests/__snapshots__/validate.test.ts.snap | 7 ++++++- src/tests/configs/full-error-config.json | 10 +++++++++- src/tests/configs/full-valid-config.json | 3 +++ src/tests/configs/full-valid-server.json | 5 ++++- 8 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/lib/createConfig.ts b/src/lib/createConfig.ts index 6fc2ca1..63858d6 100644 --- a/src/lib/createConfig.ts +++ b/src/lib/createConfig.ts @@ -105,6 +105,12 @@ const createLocation = async (location: Server): Promise => { block.auth_basic_user_file = filename; } + if (location.raw) { + Object.entries(location.raw).forEach(([key, value]) => { + block[key] = value; + }); + } + return block; }; diff --git a/src/lib/validateConfig.ts b/src/lib/validateConfig.ts index 8d10013..f415cab 100644 --- a/src/lib/validateConfig.ts +++ b/src/lib/validateConfig.ts @@ -16,9 +16,14 @@ const returnKeys = [ "redirect", "rewrite", "html", - "static" + "static", + "raw" ]; +const optionalReturnKeys = ["raw"]; + +export const jsUnion = z.union([z.string(), z.number(), z.boolean()]); + export const returnKeysFromOption = ( options: Record ): string[] => Object.keys(options).filter((key) => returnKeys.includes(key)); @@ -81,7 +86,10 @@ const oneReturnRefinement = }); } } - if (keys.length > 1) { + + if ( + keys.filter((key) => !optionalReturnKeys.includes(key)).length > 1 + ) { addIssue({ message: chalk`Too many "return" types, found: {yellow ${keys.join( ", " @@ -98,7 +106,7 @@ export const locationSchema = z custom_css: urlsOrUrlSchema, custom_js: urlsOrUrlSchema, return: z.string().or(z.number()), - headers: z.record(z.union([z.string(), z.number(), z.boolean()])), + headers: z.record(jsUnion), cors: z .boolean() .transform((bool) => (bool ? "*" : false)) @@ -125,7 +133,8 @@ export const locationSchema = z message: chalk`Static path does not exist: {dim ${path}}` }); } - }) + }), + raw: z.record(jsUnion) }) .partial() .strict(); diff --git a/src/models/nginx-config-parser.d.ts b/src/models/nginx-config-parser.d.ts index 9d85bf6..942375f 100644 --- a/src/models/nginx-config-parser.d.ts +++ b/src/models/nginx-config-parser.d.ts @@ -36,6 +36,7 @@ declare module "@webantic/nginx-config-parser" { include?: string[]; alias?: string; + [T: string]: string[] | string | number | boolean; } class Parser { public toJSON: (conf: string) => Config; diff --git a/src/tests/__snapshots__/create.test.ts.snap b/src/tests/__snapshots__/create.test.ts.snap index 9a0ca42..411d7e7 100644 --- a/src/tests/__snapshots__/create.test.ts.snap +++ b/src/tests/__snapshots__/create.test.ts.snap @@ -37,6 +37,7 @@ exports[`Create config Full Server 1`] = ` add_header Second Value; auth_basic 2d77215cd91113df4d3442ee904fd740; auth_basic_user_file /current/data/auth/2d77215cd91113df4d3442ee904fd740; + custom option; } location /custom_assets { diff --git a/src/tests/__snapshots__/validate.test.ts.snap b/src/tests/__snapshots__/validate.test.ts.snap index cec5763..762b5ad 100644 --- a/src/tests/__snapshots__/validate.test.ts.snap +++ b/src/tests/__snapshots__/validate.test.ts.snap @@ -35,6 +35,9 @@ exports[`Validate Config It accepts a valid config 1`] = ` "proxy_pass": "http://example.com", }, ], + "raw": { + "custom": "option", + }, "subdomains": { "no_return_allowed": { "locations": [ @@ -108,6 +111,8 @@ exports[`Validate Config It checks for an invalid config 1`] = ` "[ERROR] Error: Username should not be empty", "[ERROR] Path: servers.example.com.locations.extra.auth.password", "[ERROR] Error: Required", + "[ERROR] Path: servers.example.com.subdomains.too_many", + "[ERROR] Error: Too many \\"return\\" types, found: redirect, html, raw, please use only one of them.", "[ERROR] Path: servers.dns_error", "[ERROR] Error: DNS lookup failed for: not-existing", ] @@ -117,7 +122,7 @@ exports[`Validate Config It tests for invalid number of return types 1`] = ` [ "[ERROR] There was an issue with your config, the errors are listed below.", "[ERROR] Path: servers.no returns", - "[ERROR] Error: Please specify at least one \\"return\\" type: proxy_pass, return, redirect, rewrite, html, static", + "[ERROR] Error: Please specify at least one \\"return\\" type: proxy_pass, return, redirect, rewrite, html, static, raw", "[ERROR] Path: servers.example.com", "[ERROR] Error: Too many \\"return\\" types, found: proxy_pass, redirect, rewrite, html, return, please use only one of them.", ] diff --git a/src/tests/configs/full-error-config.json b/src/tests/configs/full-error-config.json index 080fd52..0597feb 100644 --- a/src/tests/configs/full-error-config.json +++ b/src/tests/configs/full-error-config.json @@ -27,7 +27,15 @@ "redirect": [], "return": [], "rewrite": [], - "subdomains": {} + "subdomains": { + "too_many": { + "raw": { + "three": "return keys" + }, + "redirect": "/", + "html": "The raw option is an optional return key." + } + } } }, "unknown": "" diff --git a/src/tests/configs/full-valid-config.json b/src/tests/configs/full-valid-config.json index dedba74..74d7afd 100644 --- a/src/tests/configs/full-valid-config.json +++ b/src/tests/configs/full-valid-config.json @@ -22,6 +22,9 @@ "proxy_pass": "http://example.com" } }, + "raw": { + "custom": "option" + }, "subdomains": { "www": "http://example.com", diff --git a/src/tests/configs/full-valid-server.json b/src/tests/configs/full-valid-server.json index ed80fd0..193bea3 100644 --- a/src/tests/configs/full-valid-server.json +++ b/src/tests/configs/full-valid-server.json @@ -22,5 +22,8 @@ "redirect": "/redirect", "return": "200 return directive", "rewrite": "^ https://rewrite", - "websocket": true + "websocket": true, + "raw": { + "custom": "option" + } }